blob: aebe4923824e2da23dd1cbe97b2469cce23d307d [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 Lundblade4c532ca2021-02-18 21:31:49 -07003 Copyright (c) 2018-2021, 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 Lundblade2ded3d92018-10-09 21:36:11 +08002334 0xc0, // tag for string date
2335 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002336
Laurence Lundbladec7114722020-08-13 05:11:40 -07002337 0xc0, // tag for string date
2338 0x00, // Wrong type for a string date
2339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002340 0xc1, // tag for epoch date
2341 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2342
Laurence Lundbladec7114722020-08-13 05:11:40 -07002343 0xc1,
2344 0x62, 'h', 'i', // wrong type tagged
2345
Laurence Lundblade99615302020-11-29 11:19:47 -08002346 // CBOR_TAG_ENC_AS_B64
2347 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002348 0x1a, 0x53, 0x72, 0x4E, 0x01,
2349
2350 0xc1, // tag for epoch date
2351 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002352
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002353 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002354 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002356 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002357 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002358
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002359 0xc1, // tag for epoch date
2360 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2361 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2362
2363 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002364 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2365
2366 0xc1, // tag for epoch date
2367 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2368
2369 0xc1,
2370 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2371
2372 0xc1, // tag for epoch date
2373 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002374};
2375
2376
Laurence Lundbladec7114722020-08-13 05:11:40 -07002377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002378// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002379#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002380static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002381
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002382 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002383
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002384 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002385
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002386 return diff > 0.0000001;
2387}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002388#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002389
2390
Laurence Lundblade99615302020-11-29 11:19:47 -08002391
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002392int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002393{
2394 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002395 QCBORItem Item;
2396 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002397
Laurence Lundbladeee851742020-01-08 08:37:05 -08002398 QCBORDecode_Init(&DCtx,
2399 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2400 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002401
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002402 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002403 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002404 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002405 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002406 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002407 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002408 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002409 }
2410
Laurence Lundbladec7114722020-08-13 05:11:40 -07002411 // Wrong type for a string date
2412 uError = QCBORDecode_GetNext(&DCtx, &Item);
2413 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002414 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002415 }
2416
2417 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2418 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2419 return -4;
2420 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002421 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002422 Item.val.epochDate.nSeconds != 1400000000
2423#ifndef USEFULBUF_DISABLE_ALL_FLOAT
2424 || Item.val.epochDate.fSecondsFraction != 0
2425#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
2426 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002427 return -5;
2428 }
2429
2430 // Wrong type for an epoch date
2431 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2432 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002433 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002434
Laurence Lundblade99615302020-11-29 11:19:47 -08002435 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2436 // The date is decoded and the two tags are returned. This is to
2437 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002438 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2439 return -7;
2440 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002441 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2442 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002443#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002444 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002445#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08002446 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002447 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002448 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002449
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002450 // Epoch date that is too large for our representation
2451 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002452 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002453 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002454
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002455#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade9682a532020-06-06 18:33:04 -07002456#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002457 // Epoch date in float format with fractional seconds
2458 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2459 return -10;
2460 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002461 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002462 Item.val.epochDate.nSeconds != 1
2463#ifndef USEFULBUF_DISABLE_ALL_FLOAT
2464 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )
2465#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
2466 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002467 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002469
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002470 // Epoch date float that is too large for our representation
2471 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002472 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002473 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002474
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002475 // Epoch date double that is just slightly too large
2476 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002477 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002478 }
2479
2480 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002481 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2482 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2483 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2484 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002485 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002486 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002487
2488 // Nan
2489 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2490 return -15;
2491 }
2492
2493 // +Inifinity double-precision
2494 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2495 return -16;
2496 }
2497
2498#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2499 // -Inifinity half-precision
2500 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2501 return -17;
2502 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002503#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002504 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2505 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002506 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002507#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002508
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002509#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002510 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2511 return -19;
2512 }
2513 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2514 return -20;
2515 }
2516 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2517 return -21;
2518 }
2519 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2520 return -22;
2521 }
2522 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2523 return -23;
2524 }
2525 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2526 return -24;
2527 }
2528#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2529 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2530 return -25;
2531 }
2532#else
2533 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2534 return -26;
2535 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002536#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002537
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002538#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002539#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002540
2541 return 0;
2542}
2543
Laurence Lundblade4b270642020-08-14 12:53:07 -07002544/*
2545 Test cases covered here. Some items cover more than one of these.
2546 positive integer (zero counts as a positive integer)
2547 negative integer
2548 half-precision float
2549 single-precision float
2550 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002551
Laurence Lundblade4b270642020-08-14 12:53:07 -07002552 float Overflow error
2553 Wrong type error for epoch
2554 Wrong type error for date string
2555 float disabled error
2556 half-precision disabled error
2557 -Infinity
2558 Slightly too large integer
2559 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002560
Laurence Lundblade4b270642020-08-14 12:53:07 -07002561 Get epoch by int
2562 Get string by int
2563 Get epoch by string
2564 Get string by string
2565 Fail to get epoch by wrong int label
2566 Fail to get string by wrong string label
2567 Fail to get epoch by string because it is invalid
2568 Fail to get epoch by int because it is invalid
2569
2570 Untagged values
2571 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002572static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002573 0x86,
2574
2575 0xc1,
2576 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2577
Laurence Lundbladec7114722020-08-13 05:11:40 -07002578 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002579 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2580
2581 0xc1, // tag for epoch date
2582 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2583
2584 0xc1, // tag for epoch date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002585 0x80, // Erroneous empty array as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002586
2587 0xc0, // tag for string date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002588 0xa0, // Erroneous empty map as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002589
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002590 0xad, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002591
2592 0x00,
2593 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002594 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002595
2596 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002597 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002598 0xc1, // tag for epoch date
2599 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2600
2601 // Untagged integer 0
2602 0x08,
2603 0x00,
2604
2605 // Utagged date string with string label y
2606 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002607 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002608
2609 // Untagged -1000 with label z
2610 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002611 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002612 0x39, 0x03, 0xe7,
2613
Laurence Lundbladec7114722020-08-13 05:11:40 -07002614 0x07,
2615 0xc1, // tag for epoch date
2616 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2617
Laurence Lundblade4b270642020-08-14 12:53:07 -07002618 0x05,
2619 0xc1,
2620 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2621
Laurence Lundbladec7114722020-08-13 05:11:40 -07002622 // Untagged single-precision float with value 3.14 with string label x
2623 0x61, 0x78,
2624 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2625
Laurence Lundbladec7114722020-08-13 05:11:40 -07002626 // Untagged half-precision float with value -2
2627 0x09,
2628 0xF9, 0xC0, 0x00,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002629
2630 /* Tagged date-only date string */
2631 0x63, 0x53, 0x44, 0x53,
2632 0xD9, 0x03, 0xEC,
2633 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2634
2635 /* Untagged date-only date string */
2636 0x18, 0x63,
2637 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2638
2639 /* Tagged days-count epoch date */
2640 0x63, 0x53, 0x44, 0x45,
2641 0xD8, 0x64, /* tag(100) */
2642 0x39, 0x29, 0xB3, /* -10676 */
2643
2644 /* Untagged days-count epoch date */
2645 0x11,
2646 0x19, 0x0F, 0x9A /* 3994 */
2647
Laurence Lundbladec7114722020-08-13 05:11:40 -07002648};
2649
2650int32_t SpiffyDateDecodeTest()
2651{
2652 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002653 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002654 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2655 nEpochDate4, nEpochDate6, nEpochDateFail,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002656 nEpochDate1400000000, nEpochDays1, nEpochDays2;
2657 UsefulBufC StringDate1, StringDate2, StringDays1, StringDays2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002658 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002659
2660 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002661 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002662 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002663 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002664
Laurence Lundblade9b334962020-08-27 10:55:53 -07002665 // Too-negative float, -9.2233720368547748E+18
2666 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002667 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002668#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002669 if(uError != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002670 return 1111;
2671 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002672#else
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002673 if(uError != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_FLOAT_DATE_DISABLED)) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002674 return 1112;
2675 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002676#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade4b270642020-08-14 12:53:07 -07002677
2678 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002679 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002680 uError = QCBORDecode_GetAndResetError(&DC);
2681 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002682 return 1;
2683 }
2684
Laurence Lundblade4b270642020-08-14 12:53:07 -07002685 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002686 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002687 uError = QCBORDecode_GetAndResetError(&DC);
2688#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2689#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002690 const QCBORError uExpectedforHalfMinusInfinity = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_DATE_OVERFLOW);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002691#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002692 const QCBORError uExpectedforHalfMinusInfinity = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_FLOAT_DATE_DISABLED);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002693#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2694#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002695 const QCBORError uExpectedforHalfMinusInfinity = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_HALF_PRECISION_DISABLED);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002696#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2697 if(uError != uExpectedforHalfMinusInfinity) {
2698 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002699 }
2700
Laurence Lundblade4b270642020-08-14 12:53:07 -07002701 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002702 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002703 uError = QCBORDecode_GetAndResetError(&DC);
2704 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2705 return 3;
2706 }
2707
2708 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002709 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002710 uError = QCBORDecode_GetAndResetError(&DC);
2711 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2712 return 4;
2713 }
2714
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002715 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002716
2717 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002718 QCBORDecode_GetEpochDateInMapN(&DC,
2719 5,
2720 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2721 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2722 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002723#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2724 if(nEpochDate2 != -9223372036854773760LL) {
2725 return 101;
2726 }
2727#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2728 uError = QCBORDecode_GetAndResetError(&DC);
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002729 if(uError != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_FLOAT_DATE_DISABLED)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002730 return 102;
2731 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002732#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002733
Laurence Lundblade4b270642020-08-14 12:53:07 -07002734 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002735 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2736 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002737#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2738 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002739 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002740 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002741#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2742 uError = QCBORDecode_GetAndResetError(&DC);
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002743 if(uError != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_FLOAT_DATE_DISABLED)) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002744 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002745 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002746#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2747
2748 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002749 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2750 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002751#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2752 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002753 return 103;
2754 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002755#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2756 uError = QCBORDecode_GetAndResetError(&DC);
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002757 if(uError != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_FLOAT_DATE_DISABLED)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002758 return 104;
2759 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002760#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2761
Laurence Lundblade9b334962020-08-27 10:55:53 -07002762 // A half-precision date with value -2 FFF
2763 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2764 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002765#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2766 if(nEpochDate4 != -2) {
2767 return 105;
2768 }
2769#else
2770 uError = QCBORDecode_GetAndResetError(&DC);
2771 if(uError == QCBOR_SUCCESS) {
2772 return 106;
2773 }
2774#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002775
Laurence Lundblade4b270642020-08-14 12:53:07 -07002776
2777 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002778 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2779 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2780 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002781 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002782 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002783 return 107;
2784 }
2785
2786 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002787 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2788 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002789 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002790 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002791 return 108;
2792 }
2793
2794 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002795 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2796 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2797 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002798 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002799 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002800 return 109;
2801 }
2802
2803 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002804 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2805 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002806 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002807 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002808 return 110;
2809 }
2810
2811 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002812
Laurence Lundblade4b270642020-08-14 12:53:07 -07002813 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002814 QCBORDecode_GetEpochDateInMapN(&DC,
2815 1,
2816 QCBOR_TAG_REQUIREMENT_TAG |
2817 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2818 &nEpochDate1400000000);
2819 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002820 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002821 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2822 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002823 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002824 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2825 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002826 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002827 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2828 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002829 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002830 QCBORDecode_GetEpochDateInMapSZ(&DC,
2831 "z",
2832 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2833 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2834 &nEpochDate6);
2835 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002836
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002837 /* The days format is much simpler than the date format
2838 * because it can't be a floating point value. The test
2839 * of the spiffy decode functions sufficiently covers
2840 * the test of the non-spiffy decode days date decoding.
2841 * There is no full fan out of the error conditions
2842 * and decode options as that is implemented by code
2843 * that is tested well by the date testing above.
2844 */
2845 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
2846 &StringDays1);
2847
2848 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2849 &StringDays2);
2850
2851 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
2852 &nEpochDays1);
2853
2854 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2855 &nEpochDays2);
2856
Laurence Lundblade4b270642020-08-14 12:53:07 -07002857 QCBORDecode_ExitMap(&DC);
2858 QCBORDecode_ExitArray(&DC);
2859 uError = QCBORDecode_Finish(&DC);
2860 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002861 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002862 }
2863
Laurence Lundblade9b334962020-08-27 10:55:53 -07002864 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002865 return 200;
2866 }
2867
Laurence Lundblade9b334962020-08-27 10:55:53 -07002868 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002869 return 201;
2870 }
2871
Laurence Lundblade9b334962020-08-27 10:55:53 -07002872 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002873 return 202;
2874 }
2875
Laurence Lundblade9b334962020-08-27 10:55:53 -07002876 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002877 return 203;
2878 }
2879
Laurence Lundblade9b334962020-08-27 10:55:53 -07002880 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002881 return 204;
2882 }
2883
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002884 if(nEpochDays1 != -10676) {
2885 return 205;
2886 }
2887
2888 if(nEpochDays2 != 3994) {
2889 return 206;
2890 }
2891
Laurence Lundblade9b334962020-08-27 10:55:53 -07002892 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2893 return 205;
2894 }
2895
2896 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2897 return 206;
2898 }
2899
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002900 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
2901 return 207;
2902 }
2903
2904 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
2905 return 208;
2906 }
2907
Laurence Lundbladec7114722020-08-13 05:11:40 -07002908 return 0;
2909}
2910
2911
2912
Laurence Lundblade9b334962020-08-27 10:55:53 -07002913// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002914static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002915 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002916 0x81, // Array of one
2917 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2918 0x82, // Array of two that is the faction 1/3
2919 0x01,
2920 0x03,
2921
2922 /*
2923 More than 4 tags on an item 225(226(227(228(229([])))))
2924 */
2925 0xd8, 0xe1,
2926 0xd8, 0xe2,
2927 0xd8, 0xe3,
2928 0xd8, 0xe4,
2929 0xd8, 0xe5,
2930 0x80,
2931
2932 /* tag 10489608748473423768(
2933 2442302356(
2934 21590(
2935 240(
2936 []))))
2937 */
2938 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2939 0xda, 0x91, 0x92, 0x93, 0x94,
2940 0xd9, 0x54, 0x56,
2941 0xd8, 0xf0,
2942 0x80,
2943
2944 /* tag 21590(
2945 10489608748473423768(
2946 2442302357(
2947 65534(
2948 []))))
2949 */
2950 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2951 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2952 0xda, 0x91, 0x92, 0x93, 0x95,
2953 0xd9, 0xff, 0xfe,
2954 0x80,
2955
2956 /* Make sure to blow past the limit of tags that must be mapped.
2957 works in conjuntion with entries above.
2958 269488144(269488145(269488146(269488147([]))))
2959 */
2960 0xda, 0x10, 0x10, 0x10, 0x10,
2961 0xda, 0x10, 0x10, 0x10, 0x11,
2962 0xda, 0x10, 0x10, 0x10, 0x12,
2963 0xda, 0x10, 0x10, 0x10, 0x13,
2964 0x80,
2965
2966 /* An invalid decimal fraction with an additional tag */
2967 0xd9, 0xff, 0xfa,
2968 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2969 0x00, // the integer 0; should be a byte string
2970};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002971
Laurence Lundblade59289e52019-12-30 13:44:37 -08002972/*
2973 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002974 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002975 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002976static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002977 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002978
Laurence Lundblade59289e52019-12-30 13:44:37 -08002979/*
2980DB 9192939495969798 # tag(10489608748473423768)
2981 D8 88 # tag(136)
2982 C6 # tag(6)
2983 C7 # tag(7)
2984 80 # array(0)
2985*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002986static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002987 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002988
2989/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002990 55799(55799(55799({
2991 6(7(-23)): 5859837686836516696(7({
2992 7(-20): 11({
2993 17(-18): 17(17(17("Organization"))),
2994 9(-17): 773("SSG"),
2995 -15: 16(17(6(7("Confusion")))),
2996 17(-16): 17("San Diego"),
2997 17(-14): 17("US")
2998 }),
2999 23(-19): 19({
3000 -11: 9({
3001 -9: -7
3002 }),
3003 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3004 })
3005 })),
3006 16(-22): 23({
3007 11(8(7(-5))): 8(-3)
3008 })
3009 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003010 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003011static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003012 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3013 0xc6, 0xc7, 0x36,
3014 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3015 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3016 0xcb, 0xa5,
3017 0xd1, 0x31,
3018 0xd1, 0xd1, 0xd1, 0x6c,
3019 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3020 0xc9, 0x30,
3021 0xd9, 0x03, 0x05, 0x63,
3022 0x53, 0x53, 0x47,
3023 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003024 0xd0, 0xd1, 0xc6, 0xc7,
3025 0x69,
3026 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003027 0xd1, 0x2f,
3028 0xd1, 0x69,
3029 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3030 0xd1, 0x2d,
3031 0xd1, 0x62,
3032 0x55, 0x53,
3033 0xd7, 0x32,
3034 0xd3, 0xa2,
3035 0x2a,
3036 0xc9, 0xa1,
3037 0x28,
3038 0x26,
3039 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3040 0xcc, 0x4a,
3041 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3042 0xd0, 0x35,
3043 0xd7, 0xa1,
3044 0xcb, 0xc8, 0xc7, 0x24,
3045 0xc8, 0x22};
3046
Laurence Lundblade9b334962020-08-27 10:55:53 -07003047
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003048static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003049 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003050
3051 0xc0, // tag for string date
3052 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3053
3054 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3055
3056 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3057
3058 0xd8, 0x23, // tag for regex
3059 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3060
3061 0xc0, // tag for string date
3062 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundblade9b334962020-08-27 10:55:53 -07003063};
3064
3065
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003066static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003067
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003068
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003069int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003070{
3071 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003072 QCBORItem Item;
3073 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003074
Laurence Lundbladeee851742020-01-08 08:37:05 -08003075 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003076 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003077 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003078
Laurence Lundblade9b334962020-08-27 10:55:53 -07003079 /*
3080 This test matches the magic number tag and the fraction tag
3081 55799([...])
3082 */
3083 uError = QCBORDecode_GetNext(&DCtx, &Item);
3084 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003085 return -2;
3086 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003087 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003088 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3089 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003091
Laurence Lundblade9b334962020-08-27 10:55:53 -07003092 /*
3093 4([1,3])
3094 */
3095 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003096#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003097 if(uError != QCBOR_SUCCESS ||
3098 Item.uDataType != QCBOR_TYPE_ARRAY ||
3099 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3100 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3101 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3102 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3103 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3104 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3105 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003106 return -4;
3107 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003108 // consume the items in the array
3109 uError = QCBORDecode_GetNext(&DCtx, &Item);
3110 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003111
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003112#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003113 if(uError != QCBOR_SUCCESS ||
3114 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3115 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3116 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3117 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3118 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3119 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3120 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003121 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003122#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003123
Laurence Lundblade9b334962020-08-27 10:55:53 -07003124 /*
3125 More than 4 tags on an item 225(226(227(228(229([])))))
3126 */
3127 uError = QCBORDecode_GetNext(&DCtx, &Item);
3128 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003129 return -6;
3130 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003131
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003132 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3133 return -106;
3134 }
3135
3136
Laurence Lundblade9b334962020-08-27 10:55:53 -07003137 /* tag 10489608748473423768(
3138 2442302356(
3139 21590(
3140 240(
3141 []))))
3142 */
3143 uError = QCBORDecode_GetNext(&DCtx, &Item);
3144 if(uError != QCBOR_SUCCESS ||
3145 Item.uDataType != QCBOR_TYPE_ARRAY ||
3146 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3147 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3148 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3149 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003150 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003151 }
3152
3153 /* tag 21590(
3154 10489608748473423768(
3155 2442302357(
3156 21591(
3157 []))))
3158 */
3159 uError = QCBORDecode_GetNext(&DCtx, &Item);
3160 if(uError != QCBOR_SUCCESS ||
3161 Item.uDataType != QCBOR_TYPE_ARRAY ||
3162 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3163 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3164 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3165 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3166 return -8;
3167 }
3168
3169 /* Make sure to blow past the limit of tags that must be mapped.
3170 works in conjuntion with entries above.
3171 269488144(269488145(269488146(269488147([]))))
3172 */
3173 uError = QCBORDecode_GetNext(&DCtx, &Item);
3174 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3175 return -9;
3176 }
3177
3178 uError = QCBORDecode_GetNext(&DCtx, &Item);
3179 if(uError == QCBOR_SUCCESS) {
3180 return -10;
3181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003182
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003183 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003184 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003185 // tage and then matches it. Caller-config lists are no longer
3186 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003187 QCBORDecode_Init(&DCtx,
3188 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3189 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003190 const uint64_t puList[] = {0x9192939495969798, 257};
3191 const QCBORTagListIn TL = {2, puList};
3192 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003193
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003194 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3195 return -8;
3196 }
3197 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3198 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3199 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3200 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3201 Item.val.uCount != 0) {
3202 return -9;
3203 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003204
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003205 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003206 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003207 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003208 const uint64_t puLongList[17] = {1,2,1};
3209 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003210 QCBORDecode_Init(&DCtx,
3211 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3212 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003213 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3214 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3215 return -11;
3216 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003217
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003218 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003219 QCBORTagListOut Out = {0, 4, puTags};
3220
3221
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003222 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003223 QCBORDecode_Init(&DCtx,
3224 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3225 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003226 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3227 return -12;
3228 }
3229 if(puTags[0] != 0x9192939495969798 ||
3230 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003231 puTags[2] != 0x06 ||
3232 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003233 return -13;
3234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003235
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003236 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003237 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003238 QCBORDecode_Init(&DCtx,
3239 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3240 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003241 QCBORTagListOut OutSmall = {0, 3, puTags};
3242 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3243 return -14;
3244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003245
Laurence Lundblade9b334962020-08-27 10:55:53 -07003246
3247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003248 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003249 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3250 // It is a bit of a messy test and maybe could be improved, but
3251 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003252 QCBORDecode_Init(&DCtx,
3253 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3254 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003255 int n = CheckCSRMaps(&DCtx);
3256 if(n) {
3257 return n-2000;
3258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003259
Laurence Lundblade59289e52019-12-30 13:44:37 -08003260 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003261 QCBORDecode_Init(&DCtx,
3262 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3263 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003264
Laurence Lundblade9b334962020-08-27 10:55:53 -07003265 /* With the spiffy decode revision, this tag list is not used.
3266 It doesn't matter if a tag is in this list or not so some
3267 tests that couldn't process a tag because it isn't in this list
3268 now can process these unlisted tags. The tests have been
3269 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003270 const uint64_t puTagList[] = {773, 1, 90599561};
3271 const QCBORTagListIn TagList = {3, puTagList};
3272 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003273
3274
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003275 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3276 return -100;
3277 }
3278 if(Item.uDataType != QCBOR_TYPE_MAP ||
3279 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3280 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3281 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3282 Item.val.uCount != 2 ||
3283 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3284 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3285 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3286 Out.uNumUsed != 3) {
3287 return -101;
3288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003289
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003290 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3291 return -102;
3292 }
3293 if(Item.uDataType != QCBOR_TYPE_MAP ||
3294 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3295 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003296 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003297 Item.val.uCount != 2 ||
3298 puTags[0] != 5859837686836516696 ||
3299 puTags[1] != 7 ||
3300 Out.uNumUsed != 2) {
3301 return -103;
3302 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003303
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003304 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3305 return -104;
3306 }
3307 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003308 Item.val.uCount != 5 ||
3309 puTags[0] != 0x0b ||
3310 Out.uNumUsed != 1) {
3311 return -105;
3312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003313
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003314 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3315 return -106;
3316 }
3317 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3318 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3319 Item.val.string.len != 12 ||
3320 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3321 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3322 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3323 Out.uNumUsed != 3) {
3324 return -105;
3325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003326
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003327 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3328 return -107;
3329 }
3330 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3331 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3332 Item.val.string.len != 3 ||
3333 puTags[0] != 773 ||
3334 Out.uNumUsed != 1) {
3335 return -108;
3336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003337
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003338 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3339 return -109;
3340 }
3341 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003342 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003343 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003344 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003345 puTags[3] != 7 ||
3346 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003347 return -110;
3348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003349
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003350 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3351 return -111;
3352 }
3353 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3354 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3355 Item.val.string.len != 9 ||
3356 puTags[0] != 17 ||
3357 Out.uNumUsed != 1) {
3358 return -112;
3359 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003360
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003361 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3362 return -111;
3363 }
3364 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3365 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3366 Item.val.string.len != 2 ||
3367 puTags[0] != 17 ||
3368 Out.uNumUsed != 1) {
3369 return -112;
3370 }
3371
3372 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3373 return -113;
3374 }
3375 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003376 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003377 Item.val.uCount != 2 ||
3378 puTags[0] != 19 ||
3379 Out.uNumUsed != 1) {
3380 return -114;
3381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003382
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003383 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3384 return -115;
3385 }
3386 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003387 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003388 Item.val.uCount != 1 ||
3389 puTags[0] != 9 ||
3390 Out.uNumUsed != 1) {
3391 return -116;
3392 }
3393
3394 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3395 return -116;
3396 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003397 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003398 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003399 Out.uNumUsed != 0) {
3400 return -117;
3401 }
3402
3403 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3404 return -118;
3405 }
3406 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3407 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003408 puTags[0] != 12 ||
3409 Out.uNumUsed != 1) {
3410 return -119;
3411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003412
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003413 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3414 return -120;
3415 }
3416 if(Item.uDataType != QCBOR_TYPE_MAP ||
3417 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3418 Item.val.uCount != 1 ||
3419 puTags[0] != 0x17 ||
3420 Out.uNumUsed != 1) {
3421 return -121;
3422 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003423
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003424 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3425 return -122;
3426 }
3427 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003428 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003429 Item.val.int64 != -3 ||
3430 puTags[0] != 8 ||
3431 Out.uNumUsed != 1) {
3432 return -123;
3433 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003434
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003435 if(QCBORDecode_Finish(&DCtx)) {
3436 return -124;
3437 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003438
3439 UsefulBufC DateString;
3440 QCBORDecode_Init(&DCtx,
3441 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3442 QCBOR_DECODE_MODE_NORMAL);
3443
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003444 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003445 // tagged date string
3446 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3447 // untagged date string
3448 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3449 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3450 return 100;
3451 }
3452 // untagged byte string
3453 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3454 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3455 return 101;
3456 }
3457 // tagged regex
3458 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3459 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3460 return 102;
3461 }
3462 // tagged date string with a byte string
3463 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3464 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3465 return 103;
3466 }
3467 QCBORDecode_ExitArray(&DCtx);
3468 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3469 return 104;
3470 }
3471
3472
3473 QCBORDecode_Init(&DCtx,
3474 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3475 QCBOR_DECODE_MODE_NORMAL);
3476
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003477 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003478 // tagged date string
3479 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3480 // untagged date string
3481 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3482 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3483 return 200;
3484 }
3485 // untagged byte string
3486 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3487 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3488 return 201;
3489 }
3490 // tagged regex
3491 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3492 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3493 return 202;
3494 }
3495 // tagged date string with a byte string
3496 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3497 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3498 return 203;
3499 }
3500 QCBORDecode_ExitArray(&DCtx);
3501 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3502 return 204;
3503 }
3504
3505 QCBORDecode_Init(&DCtx,
3506 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3507 QCBOR_DECODE_MODE_NORMAL);
3508
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003509 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003510 // tagged date string
3511 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3512 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3513 return 300;
3514 }
3515 // untagged date string
3516 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3517 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3518 return 301;
3519 }
3520 // untagged byte string
3521 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3522 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3523 return 302;
3524 }
3525 // tagged regex
3526 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3527 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3528 return 303;
3529 }
3530 // tagged date string with a byte string
3531 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3532 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3533 return 304;
3534 }
3535 QCBORDecode_ExitArray(&DCtx);
3536 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3537 return 305;
3538 }
3539
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003540 return 0;
3541}
3542
3543
3544
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003545
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003546static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003547 0x83,
3548 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3549 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3550 0xA4,
3551 0x63, 0x42, 0x4E, 0x2B,
3552 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3553 0x18, 0x40,
3554 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3555 0x63, 0x42, 0x4E, 0x2D,
3556 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3557 0x38, 0x3F,
3558 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3559
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003560/* The expected big num */
3561static const uint8_t spBigNum[] = {
3562 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3563 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003564
3565
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003566int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003567{
3568 QCBORDecodeContext DCtx;
3569 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003570 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003571
Laurence Lundbladeee851742020-01-08 08:37:05 -08003572 QCBORDecode_Init(&DCtx,
3573 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3574 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003575
3576
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003577 //
3578 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3579 return -1;
3580 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003581 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003582 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003583
3584 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003585 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003586 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003587 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003588 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003589 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003590 }
3591
3592 //
3593 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003594 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003595 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003596 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003597 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003598 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003599
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003600 //
3601 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003602 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003603 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003604 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003605 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003607 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003608 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003609 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3610 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003611 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003612 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003613 }
3614
3615 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003616 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003617 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3618 Item.uLabelType != QCBOR_TYPE_INT64 ||
3619 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003620 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003621 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003622 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003624 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003625 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003626 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3627 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003628 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003629 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003630 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003631
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003632 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003633 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003634 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3635 Item.uLabelType != QCBOR_TYPE_INT64 ||
3636 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003637 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003638 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003639 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003640
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003641 return 0;
3642}
3643
3644
3645
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003646static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003647 uint8_t uDataType,
3648 uint8_t uNestingLevel,
3649 uint8_t uNextNest,
3650 int64_t nLabel,
3651 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003652{
3653 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003654 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003655
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003656 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3657 if(Item.uDataType != uDataType) return -1;
3658 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003659 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3660 Item.uLabelType != QCBOR_TYPE_UINT64) {
3661 return -1;
3662 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003663 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3664 if(Item.label.int64 != nLabel) return -1;
3665 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003666 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003667 }
3668 }
3669 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303670 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003671
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003672 if(pItem) {
3673 *pItem = Item;
3674 }
3675 return 0;
3676}
3677
3678
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003679// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003680static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003681{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303682 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003683
Laurence Lundblade9b334962020-08-27 10:55:53 -07003684 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003685
Laurence Lundblade9b334962020-08-27 10:55:53 -07003686 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003687
Laurence Lundblade9b334962020-08-27 10:55:53 -07003688 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3689 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3690 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3691 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3692 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003693
Laurence Lundblade9b334962020-08-27 10:55:53 -07003694 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3695 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003696
Laurence Lundblade9b334962020-08-27 10:55:53 -07003697 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3698 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003699
Laurence Lundblade9b334962020-08-27 10:55:53 -07003700 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3701 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003702
Laurence Lundblade9b334962020-08-27 10:55:53 -07003703 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003704
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003705 return 0;
3706}
3707
3708
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003709/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003710{
3711 -23: {
3712 -20: {
3713 -18: "Organization",
3714 -17: "SSG",
3715 -15: "Confusion",
3716 -16: "San Diego",
3717 -14: "US"
3718 },
3719 -19: {
3720 -11: {
3721 -9: -7
3722 },
3723 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3724 }
3725 },
3726 -22: {
3727 -5: -3
3728 }
3729}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003730*/
3731static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003732 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3733 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3734 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3735 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3736 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3737 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3738 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3739 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3740 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3741
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003742// Same map as above, but using indefinite lengths
3743static const uint8_t spCSRInputIndefLen[] = {
3744 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3745 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3746 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3747 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3748 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3749 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3750 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3751 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3752 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3753 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3754
3755
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003756int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003757{
3758 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003759
Laurence Lundbladeee851742020-01-08 08:37:05 -08003760 QCBORDecode_Init(&DCtx,
3761 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3762 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003763
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003764 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003765}
3766
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003767
3768
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003769int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003770{
3771 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003772
Laurence Lundbladeee851742020-01-08 08:37:05 -08003773 QCBORDecode_Init(&DCtx,
3774 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3775 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003776
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003777 QCBORItem Item;
3778 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003779
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003780 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3781 return -1;
3782 }
3783 if(Item.uDataType != QCBOR_TYPE_MAP) {
3784 return -2;
3785 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003786
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003787 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3788 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3789 return -3;
3790 }
3791
3792 return 0;
3793}
3794
3795
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003796
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003797int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003798{
3799 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003800
Laurence Lundbladeee851742020-01-08 08:37:05 -08003801 QCBORDecode_Init(&DCtx,
3802 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3803 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003804
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003805 return CheckCSRMaps(&DCtx);
3806}
3807
3808
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003809
Laurence Lundblade17ede402018-10-13 11:43:07 +08003810static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3811{
3812 UsefulOutBuf UOB;
3813 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003814
Laurence Lundblade17ede402018-10-13 11:43:07 +08003815 int i;
3816 for(i = 0; i < n; i++) {
3817 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3818 }
3819
3820 for(i = 0; i < n; i++) {
3821 UsefulOutBuf_AppendByte(&UOB, 0xff);
3822 }
3823 return UsefulOutBuf_OutUBuf(&UOB);
3824}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003825
3826
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003827static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003828{
3829 QCBORDecodeContext DC;
3830 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003831
Laurence Lundblade17ede402018-10-13 11:43:07 +08003832 int j;
3833 for(j = 0; j < nNestLevel; j++) {
3834 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003835 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003836 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3837 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003838 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003839 return -4;
3840 } else {
3841 return 0; // Decoding doesn't recover after an error
3842 }
3843 } else {
3844 // Should be no error
3845 if(nReturn) {
3846 return -9; // Should not have got an error
3847 }
3848 }
3849 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3850 return -7;
3851 }
3852 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003853 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003854 if(nReturn) {
3855 return -3;
3856 }
3857 return 0;
3858}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003859
3860
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003861int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003862{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303863 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003864 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003865 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003866 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003867 int nReturn = parse_indeflen_nested(Nested, i);
3868 if(nReturn) {
3869 return nReturn;
3870 }
3871 }
3872 return 0;
3873}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003874
Laurence Lundbladeee851742020-01-08 08:37:05 -08003875// [1, [2, 3]]
3876static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3877// No closing break
3878static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3879// Not enough closing breaks
3880static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3881// Too many closing breaks
3882static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3883// Unclosed indeflen inside def len
3884static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3885// confused tag
3886static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003887
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003888int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003889{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003890 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003891 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003892 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003893
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003894 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003895 QCBORDecodeContext DC;
3896 QCBORItem Item;
3897 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003898
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003899 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303900
3901 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3902 Item.uNestingLevel != 0 ||
3903 Item.uNextNestLevel != 1) {
3904 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003906
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003907 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303908 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3909 Item.uNestingLevel != 1 ||
3910 Item.uNextNestLevel != 1) {
3911 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003912 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003913
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003914 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303915 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3916 Item.uNestingLevel != 1 ||
3917 Item.uNextNestLevel != 2) {
3918 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003920
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003921 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003922 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303923 Item.uNestingLevel != 2 ||
3924 Item.uNextNestLevel != 2) {
3925 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003926 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003927
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003928 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003929 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303930 Item.uNestingLevel != 2 ||
3931 Item.uNextNestLevel != 0) {
3932 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003933 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003934
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003935 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303936 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003938
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003939 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003940 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003941
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003942 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003943
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003944 nResult = QCBORDecode_GetNext(&DC, &Item);
3945 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303946 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003947 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003948
Laurence Lundblade570fab52018-10-13 18:28:27 +08003949 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003950 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303951 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003952 }
3953
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003954
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003955 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003956 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003957
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003958 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003959
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003960 nResult = QCBORDecode_GetNext(&DC, &Item);
3961 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303962 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003963 }
3964
3965 nResult = QCBORDecode_GetNext(&DC, &Item);
3966 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303967 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003968 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003969
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003970 nResult = QCBORDecode_GetNext(&DC, &Item);
3971 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303972 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003973 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003974
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003975 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003976 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303977 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003978 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003979
3980
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003981 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003982 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003983
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003984 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003985
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003986 nResult = QCBORDecode_GetNext(&DC, &Item);
3987 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303988 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003989 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003991 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003992 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303993 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003994 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303995
Laurence Lundblade642282a2020-06-23 12:00:33 -07003996 nResult = QCBORDecode_GetNext(&DC, &Item);
3997 if(nResult != QCBOR_ERR_BAD_BREAK) {
3998 return -140;
3999 }
4000
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004001
Laurence Lundblade570fab52018-10-13 18:28:27 +08004002 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004003 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004004
Laurence Lundblade570fab52018-10-13 18:28:27 +08004005 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004006
Laurence Lundblade570fab52018-10-13 18:28:27 +08004007 nResult = QCBORDecode_GetNext(&DC, &Item);
4008 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304009 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004010 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004011
Laurence Lundblade570fab52018-10-13 18:28:27 +08004012 nResult = QCBORDecode_GetNext(&DC, &Item);
4013 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304014 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004015 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004016
Laurence Lundblade570fab52018-10-13 18:28:27 +08004017 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004018 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304019 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004020 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004021
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304022 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004023 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004024
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304025 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004026
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304027 nResult = QCBORDecode_GetNext(&DC, &Item);
4028 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304029 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304030 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004031
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304032 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304033 if(nResult != QCBOR_ERR_BAD_BREAK) {
4034 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304035 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004036
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004037 return 0;
4038}
4039
Laurence Lundblade17ede402018-10-13 11:43:07 +08004040
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004041#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4042
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004043static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004044 0x81, // Array of length one
4045 0x7f, // text string marked with indefinite length
4046 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4047 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4048 0xff // ending break
4049};
4050
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004051static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304052 0x81, // Array of length one
4053 0x7f, // text string marked with indefinite length
4054 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4055 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4056 0xff // ending break
4057};
4058
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004059static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304060 0x81, // Array of length one
4061 0x7f, // text string marked with indefinite length
4062 0x01, 0x02, // Not a string
4063 0xff // ending break
4064};
4065
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004066static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304067 0x81, // Array of length one
4068 0x7f, // text string marked with indefinite length
4069 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4070 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4071 // missing end of string
4072};
4073
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004074static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304075 0xa1, // Array of length one
4076 0x7f, // text string marked with indefinite length
4077 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4078 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4079 0xff, // ending break
4080 0x01 // integer being labeled.
4081};
4082
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004083/**
4084 Make an indefinite length string
4085
4086 @param Storage Storage for string, must be 144 bytes in size
4087 @return The indefinite length string
4088
4089 This makes an array with one indefinite length string that has 7 chunks
4090 from size of 1 byte up to 64 bytes.
4091 */
4092static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304093{
4094 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004095
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304096 UsefulOutBuf_Init(&UOB, Storage);
4097 UsefulOutBuf_AppendByte(&UOB, 0x81);
4098 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004099
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004100 uint8_t uStringByte = 0;
4101 // Use of type int is intentional
4102 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4103 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304104 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004105 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4106 for(int j = 0; j < uChunkSize; j++) {
4107 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4108 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304109 }
4110 }
4111 UsefulOutBuf_AppendByte(&UOB, 0xff);
4112
4113 return UsefulOutBuf_OutUBuf(&UOB);
4114}
4115
4116static int CheckBigString(UsefulBufC BigString)
4117{
4118 if(BigString.len != 255) {
4119 return 1;
4120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004121
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304122 for(uint8_t i = 0; i < 255; i++){
4123 if(((const uint8_t *)BigString.ptr)[i] != i) {
4124 return 1;
4125 }
4126 }
4127 return 0;
4128}
4129
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304130
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004131int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304132{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304133 QCBORDecodeContext DC;
4134 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304135 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004136 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004137
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304138 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004139 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304140 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004141
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304142 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304143 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004145
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304146 if(QCBORDecode_GetNext(&DC, &Item)) {
4147 return -2;
4148 }
4149 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4150 return -3;
4151 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004152
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304153 if(QCBORDecode_GetNext(&DC, &Item)) {
4154 return -4;
4155 }
4156 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4157 return -5;
4158 }
4159 if(QCBORDecode_Finish(&DC)) {
4160 return -6;
4161 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304162
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304163 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004164 QCBORDecode_Init(&DC,
4165 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4166 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004167
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304168 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4169 return -7;
4170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004171
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304172 if(QCBORDecode_GetNext(&DC, &Item)) {
4173 return -8;
4174 }
4175 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4176 return -9;
4177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004178
Laurence Lundblade30816f22018-11-10 13:40:22 +07004179 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304180 return -10;
4181 }
4182
4183 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004184 QCBORDecode_Init(&DC,
4185 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4186 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004187
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304188 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4189 return -11;
4190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004191
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304192 if(QCBORDecode_GetNext(&DC, &Item)) {
4193 return -12;
4194 }
4195 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4196 return -13;
4197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004198
Laurence Lundblade30816f22018-11-10 13:40:22 +07004199 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304200 return -14;
4201 }
4202
4203 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004204 QCBORDecode_Init(&DC,
4205 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4206 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004207
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304208 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4209 return -15;
4210 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004211
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304212 if(QCBORDecode_GetNext(&DC, &Item)) {
4213 return -16;
4214 }
4215 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4216 return -17;
4217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004218
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304219 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4220 return -18;
4221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004222
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304223 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304224 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
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 -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004230
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304231 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304232 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004234
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304235 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004236 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304237
4238 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4239 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304240 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004242
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304243 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304244 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004245 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004246
Laurence Lundbladeee851742020-01-08 08:37:05 -08004247 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4248 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004249
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304250 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304251 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304252 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004254
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304255 QCBORDecode_GetNext(&DC, &Item);
4256 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304257 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304258 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004259 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304260 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004262
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304263 // ---- big bstr -----
4264 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004265
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304266 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4267 return -25;
4268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004269
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304270 if(QCBORDecode_GetNext(&DC, &Item)) {
4271 return -26;
4272 }
4273 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304274 return -26;
4275 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004276
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304277 if(QCBORDecode_GetNext(&DC, &Item)) {
4278 return -27;
4279 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304280 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304281 return -28;
4282 }
4283 if(CheckBigString(Item.val.string)) {
4284 return -3;
4285 }
4286 if(QCBORDecode_Finish(&DC)) {
4287 return -29;
4288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004289
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304290 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004291 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004292
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304293 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4294 return -30;
4295 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004296
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304297 QCBORDecode_GetNext(&DC, &Item);
4298 if(Item.uDataType != QCBOR_TYPE_MAP) {
4299 return -31;
4300 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004301
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304302 if(QCBORDecode_GetNext(&DC, &Item)){
4303 return -32;
4304 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004305 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4306 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304307 Item.uDataAlloc || !Item.uLabelAlloc ||
4308 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4309 return -33;
4310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004311
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304312 if(QCBORDecode_Finish(&DC)) {
4313 return -34;
4314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004315
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004316 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004317}
4318
4319
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004320int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304321{
4322 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004323 QCBORError nCBORError;
4324
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004325
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304326 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004327 QCBORDecode_Init(&DC,
4328 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4329 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004330
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004331 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004332
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004333 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4334 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304335 return -1;
4336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004337
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004338 if(CheckCSRMaps(&DC)) {
4339 return -2;
4340 }
4341
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004342 // Next parse, save pointers to a few strings, destroy original and
4343 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004344 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004345 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004346
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304347 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004348 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304349 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004350
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304351 QCBORItem Item1, Item2, Item3, Item4;
4352 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004353 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304354 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4355 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004356 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304357 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004358 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304359 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004360 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304361 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004362 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304363 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004364 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004365
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304366 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004367
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304368 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304369 Item1.uDataType != QCBOR_TYPE_INT64 ||
4370 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004371 Item1.uDataAlloc != 0 ||
4372 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004373 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004374 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004376
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304377
4378 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004379 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304380 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004381 Item2.uDataAlloc != 0 ||
4382 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304383 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004384 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004385
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304386 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004387 Item3.uDataAlloc == 0 ||
4388 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004389 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004390 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004392
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304393 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004394 Item4.uDataAlloc == 0 ||
4395 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004396 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004397 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004399
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304400 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004401 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004402 QCBORDecode_Init(&DC,
4403 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4404 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304405 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4406 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004407 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304408 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004409 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004410 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004411 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304412 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4413 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4414 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4415 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4416 }
4417 }
4418 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004419 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004420 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304421 }
4422
4423 return 0;
4424}
4425
Laurence Lundbladef6531662018-12-04 10:42:22 +09004426
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004427int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004428{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004429 // Set up the decoder with a tiny bit of CBOR to parse because
4430 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004431 QCBORDecodeContext DC;
4432 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4433 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004434
Laurence Lundbladef6531662018-12-04 10:42:22 +09004435 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004436 // Then fish into the internals of the decode context
4437 // to get the allocator function so it can be called directly.
4438 // Also figure out how much pool is available for use
4439 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004440 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004441 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4442 if(nError) {
4443 return -9;
4444 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004445 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4446 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4447 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004448
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004449 // First test -- ask for one more byte than available and see failure
4450 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004451 if(!UsefulBuf_IsNULL(Allocated)) {
4452 return -1;
4453 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004454
Laurence Lundbladef6531662018-12-04 10:42:22 +09004455 // Re do the set up for the next test that will do a successful alloc,
4456 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004457 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004458 pAlloc = DC.StringAllocator.pfAllocator;
4459 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4460 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004461
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004462 // Allocate one byte less than available and see success
4463 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004464 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4465 return -2;
4466 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004467 // Ask for some more and see failure
4468 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004469 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4470 return -3;
4471 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004472 // Free the first allocate, retry the second and see success
4473 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4474 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004475 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4476 return -4;
4477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004478
Laurence Lundbladef6531662018-12-04 10:42:22 +09004479 // Re do set up for next test that involves a successful alloc,
4480 // and a successful realloc and a failed realloc
4481 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004482 pAlloc = DC.StringAllocator.pfAllocator;
4483 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004484
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004485 // Allocate half the pool and see success
4486 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004487 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4488 return -5;
4489 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004490 // Reallocate to take up the whole pool and see success
4491 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004492 if(UsefulBuf_IsNULL(Allocated2)) {
4493 return -6;
4494 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004495 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004496 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4497 return -7;
4498 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004499 // Try to allocate more to be sure there is failure after a realloc
4500 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4501 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004502 return -8;
4503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004504
Laurence Lundbladef6531662018-12-04 10:42:22 +09004505 return 0;
4506}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004507
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004508
4509/* Just enough of an allocator to test configuration of one */
4510static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4511{
4512 (void)pOldMem; // unused variable
4513
4514 if(uNewSize) {
4515 // Assumes the context pointer is the buffer and
4516 // nothing too big will ever be asked for.
4517 // This is only good for this basic test!
4518 return (UsefulBuf) {pCtx, uNewSize};
4519 } else {
4520 return NULLUsefulBuf;
4521 }
4522}
4523
4524
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004525int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004526{
4527 // Set up the decoder with a tiny bit of CBOR to parse because
4528 // nothing can be done with it unless that is set up.
4529 QCBORDecodeContext DC;
4530 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4531 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4532
4533 uint8_t pAllocatorBuffer[50];
4534
4535 // This is really just to test that this call works.
4536 // The full functionality of string allocators is tested
4537 // elsewhere with the MemPool internal allocator.
4538 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4539
4540 QCBORItem Item;
4541 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4542 return -1;
4543 }
4544
4545 if(Item.uDataAlloc == 0 ||
4546 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4547 Item.val.string.ptr != pAllocatorBuffer) {
4548 return -2;
4549 }
4550
4551 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4552 return -3;
4553 }
4554
4555 return 0;
4556}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004557#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4558
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004559
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004560#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004561
Laurence Lundbladea826c502020-05-10 21:07:00 -07004562/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004563 [
4564 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004565 4([-20, 4759477275222530853136]),
4566 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004567 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004568 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004569 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004570 5([ 9223372036854775806, -4759477275222530853137])
4571 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004572 ]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004573 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004574static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004575 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004576 0xC4, 0x82, 0x20,
4577 0x03,
4578 0xC4, 0x82, 0x33,
4579 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4580 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4581 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4582 0xC5, 0x82, 0x19, 0x01, 0x2C,
4583 0x18, 0x64,
4584 0xC5, 0x82, 0x33,
4585 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4586 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4587 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004588 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4589 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004590 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4591 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4592};
4593
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004594
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004595int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004596{
4597 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004598 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004599 QCBORItem item;
4600
Laurence Lundblade17af4902020-01-07 19:11:55 -08004601 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4602 0x06, 0x07, 0x08, 0x09, 0x010};
4603 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004604
4605
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004606 QCBORDecode_Init(&DC,
4607 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4608 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004609
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004610 uErr = QCBORDecode_GetNext(&DC, &item);
4611 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004612 return 1;
4613 }
4614
4615 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4616 return 2;
4617 }
4618
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004619 uErr = QCBORDecode_GetNext(&DC, &item);
4620 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004621 return 3;
4622 }
4623
4624 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4625 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4626 item.val.expAndMantissa.nExponent != -1) {
4627 return 4;
4628 }
4629
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004630 uErr = QCBORDecode_GetNext(&DC, &item);
4631 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004632 return 5;
4633 }
4634
4635 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4636 item.val.expAndMantissa.nExponent != -20 ||
4637 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4638 return 6;
4639 }
4640
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004641 uErr = QCBORDecode_GetNext(&DC, &item);
4642 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004643 return 7;
4644 }
4645
4646 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4647 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4648 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4649 return 8;
4650 }
4651
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004652 uErr = QCBORDecode_GetNext(&DC, &item);
4653 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004654 return 9;
4655 }
4656
4657 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4658 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4659 item.val.expAndMantissa.nExponent != 300) {
4660 return 10;
4661 }
4662
Laurence Lundbladea826c502020-05-10 21:07:00 -07004663 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004664 uErr = QCBORDecode_GetNext(&DC, &item);
4665 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004666 return 11;
4667 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004668 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4669 item.val.expAndMantissa.nExponent != -20 ||
4670 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4671 return 12;
4672 }
4673
Laurence Lundbladea826c502020-05-10 21:07:00 -07004674 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004675 uErr = QCBORDecode_GetNext(&DC, &item);
4676 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004677 return 13;
4678 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004679 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4680 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4681 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4682 return 14;
4683 }
4684
Laurence Lundbladea826c502020-05-10 21:07:00 -07004685 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004686 uErr = QCBORDecode_GetNext(&DC, &item);
4687 if(uErr != QCBOR_SUCCESS) {
4688 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004689 }
4690 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4691 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4692 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004693 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004694 }
4695
Laurence Lundbladea826c502020-05-10 21:07:00 -07004696 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004697 uErr = QCBORDecode_GetNext(&DC, &item);
4698 if(uErr != QCBOR_SUCCESS) {
4699 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004700 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004701 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4702 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4703 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004704 return 18;
4705 }
4706
4707 uErr = QCBORDecode_Finish(&DC);
4708 if(uErr != QCBOR_SUCCESS) {
4709 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004710 }
4711
4712 /* Now encode some stuff and then decode it */
4713 uint8_t pBuf[40];
4714 QCBOREncodeContext EC;
4715 UsefulBufC Encoded;
4716
4717 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4718 QCBOREncode_OpenArray(&EC);
4719 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4720 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4721 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4722 QCBOREncode_CloseArray(&EC);
4723 QCBOREncode_Finish(&EC, &Encoded);
4724
4725
4726 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004727 uErr = QCBORDecode_GetNext(&DC, &item);
4728 if(uErr != QCBOR_SUCCESS) {
4729 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004730 }
4731
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004732 uErr = QCBORDecode_GetNext(&DC, &item);
4733 if(uErr != QCBOR_SUCCESS) {
4734 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004735 }
4736
4737 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4738 item.val.expAndMantissa.nExponent != 1000 ||
4739 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004740 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004741 }
4742
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004743 uErr = QCBORDecode_GetNext(&DC, &item);
4744 if(uErr != QCBOR_SUCCESS) {
4745 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004746 }
4747
4748 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4749 item.val.expAndMantissa.nExponent != INT32_MIN ||
4750 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004751 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004752 }
4753
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004754 uErr = QCBORDecode_GetNext(&DC, &item);
4755 if(uErr != QCBOR_SUCCESS) {
4756 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004757 }
4758
4759 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4760 item.val.expAndMantissa.nExponent != INT32_MAX ||
4761 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004762 return 106;
4763 }
4764
4765
4766 int64_t nExp, nMant;
4767 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4768 UsefulBufC Mant;
4769 bool bIsNeg;
4770
4771 QCBORDecode_Init(&DC,
4772 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4773 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004774 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004775
4776 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004777 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004778
4779 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004780 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4781 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004782
4783 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004784 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4785 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004786
4787 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004788 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004789
4790 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004791 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4792 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004793
4794 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004795 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4796 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004797
4798 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004799 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4800 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004801
4802 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004803 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4804 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004805
4806 QCBORDecode_ExitArray(&DC);
4807
4808 uErr = QCBORDecode_Finish(&DC);
4809 if(uErr != QCBOR_SUCCESS) {
4810 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004811 }
4812
4813 return 0;
4814}
4815
4816
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004817static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004818 // Exponent > INT64_MAX
4819 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4820 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4821 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4822 // Mantissa > INT64_MAX
4823 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4824 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4825 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4826 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004827 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004828 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004829 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004830 // bad content for big num
4831 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4832 // bad content for big num
4833 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4834 // Bad integer for exponent
4835 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4836 // Bad integer for mantissa
4837 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4838 // 3 items in array
4839 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004840#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004841 // unterminated indefinite length array
4842 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004843#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4844 // unterminated indefinite length array
4845 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4846#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004847 // Empty array
4848 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4849 // Second is not an integer
4850 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4851 // First is not an integer
4852 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4853 // Not an array
4854 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4855};
4856
4857
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004858int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004859{
4860 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004861 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4862 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004863}
4864
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004865#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004866
4867
4868
4869/*
4870 Some basic CBOR with map and array used in a lot of tests.
4871 The map labels are all strings
4872
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004873 {
4874 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004875 "an array of two strings": [
4876 "string1", "string2"
4877 ],
4878 "map in a map": {
4879 "bytes 1": h'78787878',
4880 "bytes 2": h'79797979',
4881 "another int": 98,
4882 "text 2": "lies, damn lies and statistics"
4883 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004884 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004885 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004886
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004887int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004888{
4889 QCBORItem Item1, Item2, Item3;
4890 int64_t nDecodedInt1, nDecodedInt2;
4891 UsefulBufC B1, B2, S1, S2, S3;
4892
4893 QCBORDecodeContext DCtx;
4894 QCBORError nCBORError;
4895
4896 QCBORDecode_Init(&DCtx, input, 0);
4897
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004898 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004899
4900 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4901
4902 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4903 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004904 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4905 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4906 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004907 QCBORDecode_ExitMap(&DCtx);
4908
4909 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4910 QCBORDecode_GetNext(&DCtx, &Item1);
4911 QCBORDecode_GetNext(&DCtx, &Item2);
4912 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4913 return -400;
4914 }
4915 QCBORDecode_ExitArray(&DCtx);
4916
4917 // Parse the same array again using GetText() instead of GetItem()
4918 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004919 QCBORDecode_GetTextString(&DCtx, &S2);
4920 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004921 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4922 return 5000;
4923 }
4924 /* QCBORDecode_GetText(&DCtx, &S3);
4925 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4926 return 5001;
4927 } */
4928
4929 QCBORDecode_ExitArray(&DCtx);
4930
4931 QCBORDecode_ExitMap(&DCtx);
4932
4933 nCBORError = QCBORDecode_Finish(&DCtx);
4934
4935 if(nCBORError) {
4936 return (int32_t)nCBORError;
4937 }
4938
4939 if(nDecodedInt1 != 42) {
4940 return 1001;
4941 }
4942
4943 if(nDecodedInt2 != 98) {
4944 return 1002;
4945 }
4946
4947 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004948 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004949 return 1003;
4950 }
4951
4952 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004953 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004954 return 1004;
4955 }
4956
Laurence Lundblade9b334962020-08-27 10:55:53 -07004957 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004958 return 1005;
4959 }
4960
4961 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4962 return 1006;
4963 }
4964
4965 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4966 return 1007;
4967 }
4968
4969 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4970 return 1008;
4971 }
4972
4973 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4974 return 1009;
4975 }
4976
4977 return 0;
4978}
4979
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004980/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004981 {
4982 -75008: h'05083399',
4983 88: [],
4984 100100: {
4985 "sub1": {
4986 10: [
4987 0
4988 ],
4989 -75009: h'A46823990001',
4990 100100: {
4991 "json": "{ \"ueid\", \"xyz\"}",
4992 "subsub": {
4993 100002: h'141813191001'
4994 }
4995 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004996 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004997 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004998 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004999 */
5000
5001static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005002 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
5003 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
5004 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
5005 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
5006 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
5007 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
5008 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
5009 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
5010 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
5011 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
5012 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
5013 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005014};
5015
5016/* Get item in multi-level nesting in spNestedCBOR */
5017static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
5018{
5019 UsefulBufC String;
5020
5021 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
5022 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
5023
5024 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5025 QCBORDecode_EnterMap(pDCtx, NULL);
5026 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5027 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
5028 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
5029 if(QCBORDecode_GetError(pDCtx)) {
5030 return 4001;
5031 }
5032 if(UsefulBuf_Compare(String, test_oemid)) {
5033 return 4002;
5034 }
5035 QCBORDecode_ExitMap(pDCtx);
5036 QCBORDecode_ExitMap(pDCtx);
5037 QCBORDecode_ExitMap(pDCtx);
5038 QCBORDecode_ExitMap(pDCtx);
5039
5040 return 0;
5041}
5042
5043/* Iterations on the zero-length array in spNestedCBOR */
5044static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
5045{
5046 QCBORItem Item;
5047 QCBORError uErr;
5048
5049 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
5050 for(int x = 0; x < 20; x++) {
5051 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5052 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5053 return 4100;
5054
5055 }
5056 }
5057 QCBORDecode_ExitArray(pDCtx);
5058 if(QCBORDecode_GetError(pDCtx)) {
5059 return 4101;
5060 }
5061
5062 return 0;
5063}
5064
5065/* Various iterations on the array that contains a zero in spNestedCBOR */
5066static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5067{
5068 QCBORError uErr;
5069
5070 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5071 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5072 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5073 int64_t nInt = 99;
5074 QCBORDecode_GetInt64(pDCtx, &nInt);
5075 if(nInt != 0) {
5076 return 4200;
5077 }
5078 for(int x = 0; x < 20; x++) {
5079 QCBORItem Item;
5080 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5081 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5082 return 4201;
5083
5084 }
5085 }
5086 QCBORDecode_ExitArray(pDCtx);
5087 if(QCBORDecode_GetAndResetError(pDCtx)) {
5088 return 4202;
5089 }
5090 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5091 UsefulBufC dD;
5092 QCBORDecode_GetByteString(pDCtx, &dD);
5093 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5094 return 4203;
5095 }
5096 for(int x = 0; x < 20; x++) {
5097 QCBORDecode_GetByteString(pDCtx, &dD);
5098 uErr = QCBORDecode_GetAndResetError(pDCtx);
5099 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5100 return 4204;
5101 }
5102 }
5103 QCBORDecode_ExitArray(pDCtx);
5104 QCBORDecode_ExitMap(pDCtx);
5105 QCBORDecode_ExitMap(pDCtx);
5106
5107 return 0;
5108}
5109
5110/* Repeatedly enter and exit maps and arrays, go off the end of maps
5111 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005112static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005113{
5114 QCBORDecodeContext DCtx;
5115 int32_t nReturn;
5116 QCBORError uErr;
5117
5118 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5119 QCBORDecode_EnterMap(&DCtx, NULL);
5120
5121 for(int j = 0; j < 5; j++) {
5122 for(int i = 0; i < 20; i++) {
5123 nReturn = DecodeNestedGetSubSub(&DCtx);
5124 if(nReturn) {
5125 return nReturn;
5126 }
5127 }
5128
5129 for(int i = 0; i < 20; i++) {
5130 nReturn = DecodeNestedGetEmpty(&DCtx);
5131 if(nReturn ) {
5132 return nReturn;
5133 }
5134 }
5135
5136 for(int i = 0; i < 20; i++) {
5137 nReturn = DecodeNestedGetZero(&DCtx);
5138 if(nReturn ) {
5139 return nReturn;
5140 }
5141 }
5142 }
5143
5144 QCBORDecode_ExitMap(&DCtx);
5145 uErr = QCBORDecode_Finish(&DCtx);
5146 if(uErr) {
5147 return (int32_t)uErr + 4100;
5148 }
5149
5150 return 0;
5151}
5152
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005153
5154/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005155 [
5156 23,
5157 6000,
5158 h'67616C6163746963',
5159 h'686176656E20746F6B656E'
5160 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005161 */
5162static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005163 0x84,
5164 0x17,
5165 0x19, 0x17, 0x70,
5166 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5167 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005168
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005169/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005170static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5171
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005172/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005173static const uint8_t spEmptyMap[] = {0xa0};
5174
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005175#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005176/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005177static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005178
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005179
Laurence Lundbladef0499502020-08-01 11:55:57 -07005180/*
5181 {
5182 0: [],
5183 9: [
5184 [],
5185 []
5186 ],
5187 8: {
5188 1: [],
5189 2: {},
5190 3: []
5191 },
5192 4: {},
5193 5: [],
5194 6: [
5195 [],
5196 []
5197 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005198 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005199 */
5200static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005201 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5202 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5203 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5204 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005205
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005206#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5207
5208
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005209/*
5210 Too many tags
5211 Invalid tag content
5212 Duplicate label
5213 Integer overflow
5214 Date overflow
5215
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005216 {
5217 1: 224(225(226(227(4(0))))),
5218 2: 1(h''),
5219 3: -18446744073709551616,
5220 4: 1(1.0e+300),
5221 5: 0, 8: 8
5222 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005223 */
5224static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005225 0xa7,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005226 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5227 0x02, 0xc1, 0x40,
5228 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5229 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5230 0x05, 0x00,
5231 0x05, 0x00,
5232 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005233};
5234
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005235/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005236static const uint8_t spUnRecoverableMapError1[] = {
5237 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5238};
5239
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005240#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005241/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005242static const uint8_t spUnRecoverableMapError2[] = {
5243 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5244};
5245
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005246/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005247static const uint8_t spUnRecoverableMapError3[] = {
5248 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5249};
5250
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005251/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005252static const uint8_t spUnRecoverableMapError4[] = {
5253 0xbf,
5254 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5255 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5256 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5257 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5258 0xff
5259};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005260#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005261
Laurence Lundblade63926052021-03-29 16:05:51 -07005262const unsigned char not_well_formed_submod_section[] = {
5263 0xa1, 0x14, 0x1f,
5264};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005265
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005266int32_t EnterMapTest()
5267{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005268 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005269 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005270 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005271 int32_t nReturn;
5272 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005273
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005274#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005275 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005276 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005277
Laurence Lundbladef0499502020-08-01 11:55:57 -07005278
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005279 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005280 QCBORDecode_ExitArray(&DCtx);
5281
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005282 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5283 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005284 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005285 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005286 QCBORDecode_ExitArray(&DCtx);
5287 QCBORDecode_ExitArray(&DCtx);
5288
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005289 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5290 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005291 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005292 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005293 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005294 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005295 QCBORDecode_ExitArray(&DCtx);
5296 QCBORDecode_ExitMap(&DCtx);
5297
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005298 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005299 QCBORDecode_ExitMap(&DCtx);
5300
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005301 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005302 QCBORDecode_ExitArray(&DCtx);
5303
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005304 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5305 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005306 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005307 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005308 QCBORDecode_ExitArray(&DCtx);
5309 QCBORDecode_ExitArray(&DCtx);
5310
5311 QCBORDecode_ExitMap(&DCtx);
5312
5313 uErr = QCBORDecode_Finish(&DCtx);
5314 if(uErr != QCBOR_SUCCESS){
5315 return 3011;
5316 }
5317
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005318 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005319 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005320 if(nReturn) {
5321 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005322 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005323#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5324
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005325
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005326 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005327 if(nReturn) {
5328 return nReturn;
5329 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005330
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005331
Laurence Lundblade937ea812020-05-08 11:38:23 -07005332
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005333 // These tests confirm the cursor is at the right place after entering
5334 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005335 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005336
5337 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005338 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005339 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005340 QCBORDecode_GetNext(&DCtx, &Item1);
5341 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5342 return 2001;
5343 }
5344
5345
Laurence Lundblade9b334962020-08-27 10:55:53 -07005346 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005347 QCBORDecode_VGetNext(&DCtx, &Item1);
5348 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005349 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5350 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5351 UsefulBuf_Compare(ArrayItem.label.string,
5352 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5353 return 2051;
5354 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005355 QCBORDecode_GetNext(&DCtx, &Item1);
5356 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5357 return 2002;
5358 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005359 QCBORDecode_ExitArray(&DCtx);
5360 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5361 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5362 UsefulBuf_Compare(ArrayItem.label.string,
5363 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5364 return 2052;
5365 }
5366
Laurence Lundblade937ea812020-05-08 11:38:23 -07005367
Laurence Lundblade9b334962020-08-27 10:55:53 -07005368 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005369 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005370 QCBORDecode_GetNext(&DCtx, &Item1);
5371 QCBORDecode_GetNext(&DCtx, &Item1);
5372 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005373 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5374 QCBORDecode_GetNext(&DCtx, &Item1);
5375 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5376 return 2003;
5377 }
5378
Laurence Lundblade9b334962020-08-27 10:55:53 -07005379 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005380 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005381 QCBORDecode_GetNext(&DCtx, &Item1);
5382 QCBORDecode_GetNext(&DCtx, &Item1);
5383 QCBORDecode_GetNext(&DCtx, &Item1);
5384 QCBORDecode_GetNext(&DCtx, &Item1);
5385 QCBORDecode_GetNext(&DCtx, &Item1);
5386 QCBORDecode_GetNext(&DCtx, &Item1);
5387 QCBORDecode_GetNext(&DCtx, &Item1);
5388 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5389 QCBORDecode_GetNext(&DCtx, &Item1);
5390 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005391 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005392 }
5393
Laurence Lundblade9b334962020-08-27 10:55:53 -07005394 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005395 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005396 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5397 QCBORDecode_ExitArray(&DCtx);
5398 QCBORDecode_GetNext(&DCtx, &Item1);
5399 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5400 return 2006;
5401 }
5402 QCBORDecode_ExitMap(&DCtx);
5403 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5404 return 2007;
5405 }
5406
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005407 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005408 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005409 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005410 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5411 uErr = QCBORDecode_GetAndResetError(&DCtx);
5412 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005413 return 2008;
5414 }
5415 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005416 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005417 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005418 return 2009;
5419 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005420
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005421
5422 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005423 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005424 // This will fail because the map is empty.
5425 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5426 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005427 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005428 return 2010;
5429 }
5430 QCBORDecode_ExitMap(&DCtx);
5431 uErr = QCBORDecode_Finish(&DCtx);
5432 if(uErr != QCBOR_SUCCESS){
5433 return 2011;
5434 }
5435
5436
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005437#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005438 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005439 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005440 // This will fail because the map is empty.
5441 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5442 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005443 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005444 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005445 }
5446 QCBORDecode_ExitMap(&DCtx);
5447 uErr = QCBORDecode_Finish(&DCtx);
5448 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005449 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005450 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005451#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005452
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005453
5454 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005455 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005456 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005457 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005458 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005459 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005460 QCBORDecode_ExitArray(&DCtx);
5461 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5462 QCBORDecode_ExitArray(&DCtx);
5463 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005464 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005465 return 2014;
5466 }
5467
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005468 int64_t nInt;
5469 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005470 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005471 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005472 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005473 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5474 return 2021;
5475 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005476 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5477 return 2121;
5478 }
5479 (void)QCBORDecode_GetAndResetError(&DCtx);
5480
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005481
5482 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5483 uErr = QCBORDecode_GetAndResetError(&DCtx);
5484 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5485 return 2022;
5486 }
5487
5488 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5489 uErr = QCBORDecode_GetAndResetError(&DCtx);
5490 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5491 return 2023;
5492 }
5493
5494 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5495 uErr = QCBORDecode_GetAndResetError(&DCtx);
5496#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005497 if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005498 return 2024;
5499 }
5500#else
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005501 if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_FLOAT_DATE_DISABLED)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005502 return 2027;
5503 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005504#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005505
5506 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5507 uErr = QCBORDecode_GetAndResetError(&DCtx);
5508 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5509 return 2025;
5510 }
5511
5512 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5513
5514 QCBORDecode_ExitMap(&DCtx);
5515 uErr = QCBORDecode_Finish(&DCtx);
5516 if(uErr != QCBOR_SUCCESS) {
5517 return 2026;
5518 }
5519
5520 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005521 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005522 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5523 uErr = QCBORDecode_GetAndResetError(&DCtx);
5524 if(uErr != QCBOR_ERR_BAD_BREAK) {
5525 return 2030;
5526 }
5527
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005528#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005529 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005530 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005531 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5532 uErr = QCBORDecode_GetAndResetError(&DCtx);
5533 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5534 return 2031;
5535 }
5536
5537 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005538 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005539 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5540 uErr = QCBORDecode_GetAndResetError(&DCtx);
5541 if(uErr != QCBOR_ERR_HIT_END) {
5542 return 2032;
5543 }
5544
5545 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005546 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005547 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5548 uErr = QCBORDecode_GetAndResetError(&DCtx);
5549 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5550 return 2033;
5551 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005552#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5553
Laurence Lundblade732e52d2021-02-22 20:11:01 -07005554 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5555 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5556 if(Item1.uDataType != QCBOR_TYPE_MAP) {
5557 return 2401;
5558 }
5559 if(QCBORDecode_GetError(&DCtx)) {
5560 return 2402;
5561 }
5562
5563 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5564 QCBORDecode_VGetNext(&DCtx, &Item1);
5565 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5566 Item1.val.uCount != 3 ||
5567 Item1.uNextNestLevel != 1) {
5568 return 2403;
5569 }
5570 if(QCBORDecode_GetError(&DCtx)) {
5571 return 2404;
5572 }
5573 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5574 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
5575 Item1.uNextNestLevel != 1 ||
5576 Item1.val.int64 != 42) {
5577 return 2405;
5578 }
5579 if(QCBORDecode_GetError(&DCtx)) {
5580 return 2406;
5581 }
5582 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5583 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
5584 Item1.uNestingLevel != 1 ||
5585 Item1.uNextNestLevel != 1 ||
5586 Item1.val.uCount != 2) {
5587 return 2407;
5588 }
5589 if(QCBORDecode_GetError(&DCtx)) {
5590 return 2408;
5591 }
5592 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5593 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5594 Item1.uNestingLevel != 1 ||
5595 Item1.uNextNestLevel != 0 ||
5596 Item1.val.uCount != 4) {
5597 return 2409;
5598 }
5599 if(QCBORDecode_GetError(&DCtx)) {
5600 return 2410;
5601 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005602
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005603 nReturn = DecodeNestedIterate();
5604
Laurence Lundblade63926052021-03-29 16:05:51 -07005605
5606 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
5607 QCBORDecode_EnterMap(&DCtx, NULL);
5608 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
5609 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
5610 return 2500;
5611 }
5612
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005613 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005614}
5615
5616
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005617struct NumberConversion {
5618 char *szDescription;
5619 UsefulBufC CBOR;
5620 int64_t nConvertedToInt64;
5621 QCBORError uErrorInt64;
5622 uint64_t uConvertToUInt64;
5623 QCBORError uErrorUint64;
5624 double dConvertToDouble;
5625 QCBORError uErrorDouble;
5626};
5627
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005628static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005629 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005630 "too large to fit into int64_t",
5631 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5632 0,
5633 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5634 0,
5635 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5636 ((double)INT64_MIN) + 1 ,
5637 QCBOR_SUCCESS
5638 },
5639 {
5640 "largest negative int that fits in int64_t",
5641 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5642 INT64_MIN,
5643 QCBOR_SUCCESS,
5644 0,
5645 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5646 (double)INT64_MIN,
5647 QCBOR_SUCCESS
5648 },
5649 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005650 "negative bignum -1",
5651 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5652 -1,
5653 QCBOR_SUCCESS,
5654 0,
5655 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5656 -1.0,
5657 QCBOR_SUCCESS
5658 },
5659 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005660 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005661 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5662 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005663#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005664 257000,
5665 QCBOR_SUCCESS,
5666 257000,
5667 QCBOR_SUCCESS,
5668 257000.0,
5669 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005670#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005671 0,
5672 QCBOR_ERR_UNEXPECTED_TYPE,
5673 0,
5674 QCBOR_ERR_UNEXPECTED_TYPE,
5675 0.0,
5676 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005677#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005678 },
5679 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005680 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005681 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5682 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005683#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005684 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005685 QCBOR_SUCCESS,
5686 0,
5687 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005688 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005689 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005690#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005691 0,
5692 QCBOR_ERR_UNEXPECTED_TYPE,
5693 0,
5694 QCBOR_ERR_UNEXPECTED_TYPE,
5695 0.0,
5696 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005697#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005698 },
5699 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005700 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005701 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5702 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005703#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005704 2056,
5705 QCBOR_SUCCESS,
5706 2056,
5707 QCBOR_SUCCESS,
5708 2056.0,
5709 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005710#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005711 0,
5712 QCBOR_ERR_UNEXPECTED_TYPE,
5713 0,
5714 QCBOR_ERR_UNEXPECTED_TYPE,
5715 0.0,
5716 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005717#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005718 },
5719 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005720 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005721 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5722 0,
5723 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5724 0,
5725 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5726 -18446744073709551617.0,
5727 QCBOR_SUCCESS
5728 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005729#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005730 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005731 "Positive bignum 0x01020304 indefinite length string",
5732 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5733 0x01020304,
5734 QCBOR_SUCCESS,
5735 0x01020304,
5736 QCBOR_SUCCESS,
5737 16909060.0,
5738 QCBOR_SUCCESS
5739 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005740#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005741 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005742 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005743 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5744 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005745#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005746 0,
5747 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5748 0,
5749 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5750 -INFINITY,
5751 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005752#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005753 0,
5754 QCBOR_ERR_UNEXPECTED_TYPE,
5755 0,
5756 QCBOR_ERR_UNEXPECTED_TYPE,
5757 0.0,
5758 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005759#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005760 },
5761 {
5762 "big float [9223372036854775806, 9223372036854775806]",
5763 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5764 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005765#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005766 0,
5767 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5768 0,
5769 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5770 INFINITY,
5771 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005772#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005773 0,
5774 QCBOR_ERR_UNEXPECTED_TYPE,
5775 0,
5776 QCBOR_ERR_UNEXPECTED_TYPE,
5777 0.0,
5778 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005779#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005780 },
5781 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005782 "Big float 3 * 2^^2",
5783 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005784#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005785 12,
5786 QCBOR_SUCCESS,
5787 12,
5788 QCBOR_SUCCESS,
5789 12.0,
5790 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005791#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005792 0,
5793 QCBOR_ERR_UNEXPECTED_TYPE,
5794 0,
5795 QCBOR_ERR_UNEXPECTED_TYPE,
5796 0.0,
5797 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005798#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005799 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005800 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005801 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005802 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5803 0,
5804 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5805 18446744073709551615ULL,
5806 QCBOR_SUCCESS,
5807 18446744073709551615.0,
5808 QCBOR_SUCCESS
5809 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005810 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005811 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005812 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5813 65536-1,
5814 QCBOR_SUCCESS,
5815 0xffff,
5816 QCBOR_SUCCESS,
5817 65535.0,
5818 QCBOR_SUCCESS
5819 },
5820 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005821 "Postive integer 0",
5822 {(uint8_t[]){0x0}, 1},
5823 0LL,
5824 QCBOR_SUCCESS,
5825 0ULL,
5826 QCBOR_SUCCESS,
5827 0.0,
5828 QCBOR_SUCCESS
5829 },
5830 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005831 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005832 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5833 -9223372036854775807-1, // INT64_MIN
5834 QCBOR_SUCCESS,
5835 0ULL,
5836 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5837 -9223372036854775808.0,
5838 QCBOR_SUCCESS
5839 },
5840 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005841 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005842 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005843#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005844 100L,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005845 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005846 100ULL,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005847 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005848#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5849 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005850 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_HW_FLOAT_DISABLED),
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005851 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005852 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_ERR_HW_FLOAT_DISABLED),
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005853#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005854 100.3,
5855 QCBOR_SUCCESS
5856 },
5857 {
5858 "Floating point value NaN 0xfa7fc00000",
5859 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
5860 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005861 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005862 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005863 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005864 NAN,
5865 QCBOR_SUCCESS
5866 },
5867 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005868 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005869 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005870#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005871 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005872 -4,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005873 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005874 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005875 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005876 -4.0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005877 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)
5878#else /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005879 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005880 QCBOR_ERR_ALL_FLOAT_DISABLED,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005881 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005882 QCBOR_ERR_ALL_FLOAT_DISABLED,
5883 0,
5884 QCBOR_ERR_ALL_FLOAT_DISABLED,
5885#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005886 },
5887 {
5888 "Decimal fraction 3/10",
5889 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005890#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005891 0,
5892 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5893 0,
5894 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5895 0.30000000000000004,
5896 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005897#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005898 0,
5899 QCBOR_ERR_UNEXPECTED_TYPE,
5900 0,
5901 QCBOR_ERR_UNEXPECTED_TYPE,
5902 0.0,
5903 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005904#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005905 },
5906 {
5907 "+inifinity",
5908 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005909 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005910 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005911 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005912 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005913 INFINITY,
5914 QCBOR_SUCCESS
5915 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005916
5917 {
5918 "extreme pos bignum",
5919 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5920 // 50 rows of 8 is 400 digits.
5921 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5922 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5923 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5924 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5925 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5926 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5927 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5928 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5929 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5930 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5931 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5932 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5933 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5934 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5935 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5936 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5937 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5938 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5939 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5940 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5941 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5942 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5943 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5944 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5945 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5946 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5947 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5948 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5949 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5950 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5951 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5952 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5953 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5954 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5955 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5956 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5957 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5958 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5959 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5960 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5961 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5962 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5963 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5964 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5965 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5966 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5967 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5968 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5969 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5970 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5971 404},
5972 0,
5973 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5974 0,
5975 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5976#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5977 INFINITY,
5978 QCBOR_SUCCESS
5979#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5980 0,
5981 QCBOR_ERR_HW_FLOAT_DISABLED,
5982#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5983 },
5984
5985 {
5986 "extreme neg bignum",
5987 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5988 // 50 rows of 8 is 400 digits.
5989 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5990 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5991 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5992 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5993 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5994 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5995 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5996 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5997 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5998 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5999 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6000 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6001 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6002 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6003 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6004 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6005 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6006 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6007 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6008 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6009 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6010 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6011 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6012 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6013 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6014 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6015 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6016 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6017 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6018 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6019 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6020 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6021 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6022 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6023 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6024 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6025 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6026 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6027 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6028 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6029 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6030 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6031 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6032 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6033 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6034 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6035 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6036 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6037 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6038 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
6039 404},
6040 0,
6041 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6042 0,
6043 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6044#ifndef QCBOR_DISABLE_FLOAT_HW_USE
6045 -INFINITY,
6046 QCBOR_SUCCESS
6047#else /* QCBOR_DISABLE_FLOAT_HW_USE */
6048 0,
6049 QCBOR_ERR_HW_FLOAT_DISABLED,
6050#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
6051 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006052
6053 {
6054 "big float underflow [9223372036854775806, -9223372036854775806]",
6055 {(uint8_t[]){
6056 0xC5, 0x82,
6057 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6058 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006059#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006060 0,
6061 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6062 0,
6063 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6064 0,
6065 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006066#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006067 0,
6068 QCBOR_ERR_UNEXPECTED_TYPE,
6069 0,
6070 QCBOR_ERR_UNEXPECTED_TYPE,
6071 0.0,
6072 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006073#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006074 },
6075
6076 {
6077 "bigfloat that evaluates to -INFINITY",
6078 {(uint8_t[]){
6079 0xC5, 0x82,
6080 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6081 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006082#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006083 0,
6084 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6085 0,
6086 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6087 -INFINITY,
6088 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006089#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006090 0,
6091 QCBOR_ERR_UNEXPECTED_TYPE,
6092 0,
6093 QCBOR_ERR_UNEXPECTED_TYPE,
6094 0.0,
6095 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006096#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006097 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006098};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006099
6100
6101
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006102
6103static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6104{
6105 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6106#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6107 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6108 return 1;
6109 }
6110#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6111 (void)Pool;
6112#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6113 return 0;
6114}
6115
6116
Laurence Lundblade313b2862020-05-16 01:23:06 -07006117int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006118{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006119 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6120 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006121
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006122 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6123 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006124
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006125 // Set up the decoding context including a memory pool so that
6126 // indefinite length items can be checked
6127 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006128 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006129
6130 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006131 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6132 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006133 }
6134
6135 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006136 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006137 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006138 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006139 }
6140 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006141 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006142 }
6143
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006144 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006145 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6146 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006147 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006148
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006149 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006150 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006151 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006152 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006153 }
6154 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006155 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006156 }
6157
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006158 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006159 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6160 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006161 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006162#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006163 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006164 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006165 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006166 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006167 }
6168 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6169 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006170 // NaN's can't be compared for equality. A NaN is
6171 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006172 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006173 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006174 }
6175 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006176 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006177 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006178 }
6179 }
6180 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006181#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006182 }
6183
6184 return 0;
6185}
6186
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006187
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006188
6189
Laurence Lundbladee3553422020-05-02 11:11:17 -07006190int32_t CBORSequenceDecodeTests(void)
6191{
6192 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006193 QCBORItem Item;
6194 QCBORError uCBORError;
6195 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006196
6197 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006198
Laurence Lundbladee3553422020-05-02 11:11:17 -07006199 // The input for the date test happens to be a sequence so it
6200 // is reused. It is a sequence because it doesn't start as
6201 // an array or map.
6202 QCBORDecode_Init(&DCtx,
6203 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
6204 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006205
Laurence Lundbladee3553422020-05-02 11:11:17 -07006206 // Get the first item
6207 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6208 if(uCBORError != QCBOR_SUCCESS) {
6209 return 1;
6210 }
6211 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
6212 return 2;
6213 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006214
Laurence Lundblade87495732021-02-26 10:05:55 -07006215 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6216 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6217 uConsumed != 12) {
6218 return 102;
6219 }
6220
Laurence Lundbladee3553422020-05-02 11:11:17 -07006221 // Get a second item
6222 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07006223 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
6224 return 66;
6225 }
6226
Laurence Lundblade87495732021-02-26 10:05:55 -07006227 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6228 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6229 uConsumed != 14) {
6230 return 102;
6231 }
6232
Laurence Lundbladec7114722020-08-13 05:11:40 -07006233 // Get a third item
6234 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006235 if(uCBORError != QCBOR_SUCCESS) {
6236 return 2;
6237 }
6238 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6239 return 3;
6240 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006241
Laurence Lundbladee3553422020-05-02 11:11:17 -07006242 // A sequence can have stuff at the end that may
6243 // or may not be valid CBOR. The protocol decoder knows
6244 // when to stop by definition of the protocol, not
6245 // when the top-level map or array is ended.
6246 // Finish still has to be called to know that
6247 // maps and arrays (if there were any) were closed
6248 // off correctly. When called like this it
6249 // must return the error QCBOR_ERR_EXTRA_BYTES.
6250 uCBORError = QCBORDecode_Finish(&DCtx);
6251 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6252 return 4;
6253 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006254
6255
Laurence Lundbladee3553422020-05-02 11:11:17 -07006256 // --- Test an empty input ----
6257 uint8_t empty[1];
6258 UsefulBufC Empty = {empty, 0};
6259 QCBORDecode_Init(&DCtx,
6260 Empty,
6261 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006262
Laurence Lundbladee3553422020-05-02 11:11:17 -07006263 uCBORError = QCBORDecode_Finish(&DCtx);
6264 if(uCBORError != QCBOR_SUCCESS) {
6265 return 5;
6266 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006267
6268
Laurence Lundbladee3553422020-05-02 11:11:17 -07006269 // --- Sequence with unclosed indefinite length array ---
6270 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006271
Laurence Lundbladee3553422020-05-02 11:11:17 -07006272 QCBORDecode_Init(&DCtx,
6273 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6274 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006275
Laurence Lundbladee3553422020-05-02 11:11:17 -07006276 // Get the first item
6277 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6278 if(uCBORError != QCBOR_SUCCESS) {
6279 return 7;
6280 }
6281 if(Item.uDataType != QCBOR_TYPE_INT64) {
6282 return 8;
6283 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006284
Laurence Lundbladee3553422020-05-02 11:11:17 -07006285 // Get a second item
6286 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006287#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006288 if(uCBORError != QCBOR_SUCCESS) {
6289 return 9;
6290 }
6291 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6292 return 10;
6293 }
6294
6295 // Try to finish before consuming all bytes to confirm
6296 // that the still-open error is returned.
6297 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006298 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006299 return 11;
6300 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006301#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6302 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6303 return 20;
6304 }
6305#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006306
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006307
Laurence Lundbladee3553422020-05-02 11:11:17 -07006308 // --- Sequence with a closed indefinite length array ---
6309 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006310
Laurence Lundbladee3553422020-05-02 11:11:17 -07006311 QCBORDecode_Init(&DCtx,
6312 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6313 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006314
Laurence Lundbladee3553422020-05-02 11:11:17 -07006315 // Get the first item
6316 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6317 if(uCBORError != QCBOR_SUCCESS) {
6318 return 12;
6319 }
6320 if(Item.uDataType != QCBOR_TYPE_INT64) {
6321 return 13;
6322 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006323
Laurence Lundbladee3553422020-05-02 11:11:17 -07006324 // Get a second item
6325 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006326#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6327
Laurence Lundbladee3553422020-05-02 11:11:17 -07006328 if(uCBORError != QCBOR_SUCCESS) {
6329 return 14;
6330 }
6331 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6332 return 15;
6333 }
6334
6335 // Try to finish before consuming all bytes to confirm
6336 // that the still-open error is returned.
6337 uCBORError = QCBORDecode_Finish(&DCtx);
6338 if(uCBORError != QCBOR_SUCCESS) {
6339 return 16;
6340 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006341#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6342 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6343 return 20;
6344 }
6345#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006346
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006347
Laurence Lundbladee3553422020-05-02 11:11:17 -07006348 return 0;
6349}
6350
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006351
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006352
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006353int32_t IntToTests()
6354{
6355 int nErrCode;
6356 int32_t n32;
6357 int16_t n16;
6358 int8_t n8;
6359 uint32_t u32;
6360 uint16_t u16;
6361 uint8_t u8;
6362 uint64_t u64;
6363
6364 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6365 if(nErrCode == -1 || n32 != 1) {
6366 return 1;
6367 }
6368
6369 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6370 if(nErrCode == -1 || n32 != INT32_MAX) {
6371 return 2;
6372 }
6373
6374 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6375 if(nErrCode == -1 || n32 != INT32_MIN) {
6376 return 3;
6377 }
6378
6379 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6380 if(nErrCode != -1) {
6381 return 4;
6382 }
6383
6384 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6385 if(nErrCode != -1) {
6386 return 5;
6387 }
6388
6389
6390 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6391 if(nErrCode == -1 || n16 != INT16_MAX) {
6392 return 6;
6393 }
6394
6395 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6396 if(nErrCode == -1 || n16 != INT16_MIN) {
6397 return 7;
6398 }
6399
6400 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6401 if(nErrCode == -1 || n16 != 1) {
6402 return 8;
6403 }
6404
6405 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6406 if(nErrCode != -1) {
6407 return 9;
6408 }
6409
6410 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6411 if(nErrCode != -1) {
6412 return 10;
6413 }
6414
6415
6416 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6417 if(nErrCode == -1 || n8 != 1) {
6418 return 11;
6419 }
6420
6421 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6422 if(nErrCode == -1 || n8 != INT8_MAX) {
6423 return 12;
6424 }
6425
6426 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6427 if(nErrCode == -1 || n8 != INT8_MIN) {
6428 return 13;
6429 }
6430
6431 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6432 if(nErrCode != -1) {
6433 return 14;
6434 }
6435
6436 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6437 if(nErrCode != -1) {
6438 return 15;
6439 }
6440
6441
6442 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6443 if(nErrCode == -1 || u32 != 1) {
6444 return 16;
6445 }
6446
6447 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6448 if(nErrCode == -1 || u32 != UINT32_MAX) {
6449 return 17;
6450 }
6451
6452 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6453 if(nErrCode == -1 || u32 != 0) {
6454 return 18;
6455 }
6456
6457 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6458 if(nErrCode != -1) {
6459 return 19;
6460 }
6461
6462 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6463 if(nErrCode != -1) {
6464 return 20;
6465 }
6466
6467
6468 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6469 if(nErrCode == -1 || u16 != UINT16_MAX) {
6470 return 21;
6471 }
6472
6473 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6474 if(nErrCode == -1 || u16 != 0) {
6475 return 22;
6476 }
6477
6478 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6479 if(nErrCode == -1 || u16 != 1) {
6480 return 23;
6481 }
6482
6483 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6484 if(nErrCode != -1) {
6485 return 24;
6486 }
6487
6488 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6489 if(nErrCode != -1) {
6490 return 25;
6491 }
6492
6493
6494 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6495 if(nErrCode == -1 || u8 != UINT8_MAX) {
6496 return 26;
6497 }
6498
6499 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6500 if(nErrCode == -1 || u8 != 0) {
6501 return 27;
6502 }
6503
6504 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6505 if(nErrCode == -1 || u8 != 1) {
6506 return 28;
6507 }
6508
6509 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6510 if(nErrCode != -1) {
6511 return 29;
6512 }
6513
6514 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6515 if(nErrCode != -1) {
6516 return 30;
6517 }
6518
6519
6520 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6521 if(nErrCode == -1 || u64 != 1) {
6522 return 31;
6523 }
6524
6525 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6526 if(nErrCode == -1 || u64 != INT64_MAX) {
6527 return 32;
6528 }
6529
6530 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6531 if(nErrCode == -1 || u64 != 0) {
6532 return 33;
6533 }
6534
6535 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6536 if(nErrCode != -1) {
6537 return 34;
6538 }
6539
6540 return 0;
6541}
6542
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006543
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006544
6545
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006546/*
6547A sequence with
6548 A wrapping bstr
6549 containing a map
6550 1
6551 2
6552 A wrapping bstr
6553 containing an array
6554 3
6555 wrapping bstr
6556 4
6557 5
6558 6
6559 array
6560 7
6561 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006562 */
6563
Laurence Lundblade55013642020-09-23 05:39:22 -07006564static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006565{
Laurence Lundblade55013642020-09-23 05:39:22 -07006566 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006567 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006568 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006569
Laurence Lundblade55013642020-09-23 05:39:22 -07006570 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006571
6572 QCBOREncode_BstrWrap(&EC);
6573 QCBOREncode_OpenMap(&EC);
6574 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6575 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6576 QCBOREncode_CloseMap(&EC);
6577 QCBOREncode_BstrWrap(&EC);
6578 QCBOREncode_OpenArray(&EC);
6579 QCBOREncode_AddInt64(&EC, 3);
6580 QCBOREncode_BstrWrap(&EC);
6581 QCBOREncode_AddInt64(&EC, 4);
6582 QCBOREncode_CloseBstrWrap(&EC, NULL);
6583 QCBOREncode_AddInt64(&EC, 5);
6584 QCBOREncode_CloseArray(&EC);
6585 QCBOREncode_CloseBstrWrap(&EC, NULL);
6586 QCBOREncode_AddInt64(&EC, 6);
6587 QCBOREncode_CloseBstrWrap(&EC, NULL);
6588 QCBOREncode_OpenArray(&EC);
6589 QCBOREncode_AddInt64(&EC, 7);
6590 QCBOREncode_AddInt64(&EC, 8);
6591 QCBOREncode_CloseArray(&EC);
6592
6593 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006594 if(uErr) {
6595 Encoded = NULLUsefulBufC;
6596 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006597
6598 return Encoded;
6599}
6600
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006601/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006602static const uint8_t spBreakInByteString[] = {
6603 0x41, 0xff
6604};
6605
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006606
6607int32_t EnterBstrTest()
6608{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006609 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006610
6611 QCBORDecodeContext DC;
6612
Laurence Lundblade55013642020-09-23 05:39:22 -07006613 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006614
Laurence Lundblade55013642020-09-23 05:39:22 -07006615 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006616
6617
Laurence Lundblade9b334962020-08-27 10:55:53 -07006618 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006619 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006620 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6621 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006622 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006623 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006624 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006625 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006626 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006627 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006628 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006629 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006630 QCBORDecode_ExitArray(&DC);
6631 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006632 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006633 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006634 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006635 QCBORDecode_GetInt64(&DC, &n7);
6636 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006637 QCBORDecode_ExitArray(&DC);
6638
6639 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006640 if(uErr) {
6641 return (int32_t)uErr;
6642 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006643
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006644
6645 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6646 * Successful because no items are fetched from byte string.
6647 */
6648 QCBORDecode_Init(&DC,
6649 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6650 0);
6651 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6652 uErr = QCBORDecode_GetError(&DC);
6653 if(uErr) {
6654 return 100 + (int32_t)uErr;
6655 }
6656
6657 QCBORDecode_ExitBstrWrapped(&DC);
6658 uErr = QCBORDecode_GetError(&DC);
6659 if(uErr) {
6660 return 200 + (int32_t)uErr;
6661 }
6662
6663 /* Try to get item that is a break out of a byte string wrapped CBOR.
6664 * It fails because there should be no break.
6665 */
6666 QCBORDecode_Init(&DC,
6667 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6668 0);
6669 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6670 QCBORItem Item;
6671 uErr = QCBORDecode_GetNext(&DC, &Item);
6672 if(uErr != QCBOR_ERR_BAD_BREAK) {
6673 return 300 + (int32_t)uErr;
6674 }
6675
6676 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006677}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006678
6679
6680
6681
6682static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006683 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006684
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006685 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006686 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006687 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6688 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6689 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006690
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006691 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006692 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6693 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6694 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006695
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006696 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006697 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006698 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6699 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006700
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006701 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006702 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6703 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006704
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006705 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006706 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006707 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6708 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006709
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006710 21,
6711 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6712 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6713
6714 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006715 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006716 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6717 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006718
6719 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006720 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6721 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006722
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006723 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006724 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006725 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6726 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006727
6728 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006729 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6730 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006731
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006732 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006733 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006734 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6735 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006736
6737 0x18, 0x33,
6738 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6739
6740 // MIME
6741 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006742 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6743 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6744 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006745
6746 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006747 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6748 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006749
6750 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006751 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6752 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6753 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006754
6755 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006756 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6757 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006758
6759 // UUID
6760 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006761 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6762 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006763
6764 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006765 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6766 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006767};
6768
6769int32_t DecodeTaggedTypeTests()
6770{
6771 QCBORDecodeContext DC;
6772 QCBORError uErr;
6773
6774 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6775
6776 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006777 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006778
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006779 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006780 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006781 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006782 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6783 return 1;
6784 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006785 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006786 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6787 return 2;
6788 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006789 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006790 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6791 return 3;
6792 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006793 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006794 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006795 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6796 return 4;
6797 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006798 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006799 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006800 return 5;
6801 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006802
Laurence Lundblade9b334962020-08-27 10:55:53 -07006803 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006804 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6805 bNeg != false) {
6806 return 10;
6807 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006808 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006809 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6810 bNeg != true) {
6811 return 11;
6812 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006813 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006814 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6815 return 12;
6816 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006817 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_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 13;
6820 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006821 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006822 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006823 return 14;
6824 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006825
Laurence Lundblade9b334962020-08-27 10:55:53 -07006826 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006827 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6828 return 20;
6829 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006830 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006831 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6832 return 21;
6833 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006834 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006835 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006836 return 22;
6837 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006838 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006839 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006840 return 23;
6841 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006842
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006843#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006844 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006845 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6846 return 30;
6847 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006848#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006849 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006850 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6851 return 31;
6852 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006853 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006854 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006855 return 32;
6856 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006857 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006858 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006859 return 33;
6860 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006861
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006862#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006863 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006864 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6865 return 40;
6866 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006867#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006868 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006869 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6870 return 41;
6871 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006872 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006873 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006874 return 42;
6875 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006876 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006877 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006878 return 43;
6879 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006880
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006881#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006882 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006883 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6884 return 50;
6885 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006886#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006887 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006888 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6889 return 51;
6890 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006891 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006892 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006893 return 52;
6894 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006895 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006896 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006897 return 53;
6898 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006899
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006900#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006901 // MIME
6902 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006903 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006904 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6905 bIsNot7Bit == true) {
6906 return 60;
6907 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006908 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006909 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6910 bIsNot7Bit == true) {
6911 return 61;
6912 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006913 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006914 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6915 bIsNot7Bit == false) {
6916 return 62;
6917 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006918 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006919 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6920 bIsNot7Bit == false) {
6921 return 63;
6922 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006923 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006924 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006925 return 64;
6926 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006927 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006928 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006929 return 65;
6930 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006931
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006932
Laurence Lundblade9b334962020-08-27 10:55:53 -07006933 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006934 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6935 return 70;
6936 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006937#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
6938
Laurence Lundblade9b334962020-08-27 10:55:53 -07006939 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006940 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6941 return 71;
6942 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006943 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006944 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006945 return 72;
6946 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006947 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006948 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006949 return 73;
6950 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006951
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006952 // Improvement: add some more error test cases
6953
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006954 QCBORDecode_ExitMap(&DC);
6955
6956 uErr = QCBORDecode_Finish(&DC);
6957 if(uErr != QCBOR_SUCCESS) {
6958 return 100;
6959 }
6960
6961 return 0;
6962}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006963
6964
6965
6966
6967/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006968 [
6969 "aaaaaaaaaa",
6970 {}
6971 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006972 */
6973static const uint8_t spTooLarge1[] = {
6974 0x9f,
6975 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6976 0xa0,
6977 0xff
6978};
6979
6980/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006981 [
6982 {
6983 0: "aaaaaaaaaa"
6984 }
6985 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006986 */
6987static const uint8_t spTooLarge2[] = {
6988 0x9f,
6989 0xa1,
6990 0x00,
6991 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6992 0xff
6993};
6994
6995/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006996 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006997
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006998 {
6999 0: "aaaaaaaaaa"
7000 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007001 */
7002static const uint8_t spTooLarge3[] = {
7003 0x4d,
7004 0xa1,
7005 0x00,
7006 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7007};
7008
7009int32_t TooLargeInputTest(void)
7010{
7011 QCBORDecodeContext DC;
7012 QCBORError uErr;
7013 UsefulBufC String;
7014
7015 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7016 // to 10 There's not really any way to test this error
7017 // condition. The error condition is not complex, so setting
7018 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7019
7020 // The input CBOR is only too large because the
7021 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7022 //
7023 // This test is disabled for the normal test runs because of the
7024 // special build requirement.
7025
7026
7027 // Tests the start of a map being too large
7028 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007029 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007030 QCBORDecode_GetTextString(&DC, &String);
7031 uErr = QCBORDecode_GetError(&DC);
7032 if(uErr != QCBOR_SUCCESS) {
7033 return 1;
7034 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007035 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007036 uErr = QCBORDecode_GetError(&DC);
7037 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7038 return 2;
7039 }
7040
7041 // Tests the end of a map being too large
7042 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007043 QCBORDecode_EnterArray(&DC, NULL);
7044 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007045 uErr = QCBORDecode_GetError(&DC);
7046 if(uErr != QCBOR_SUCCESS) {
7047 return 3;
7048 }
7049 QCBORDecode_ExitMap(&DC);
7050 uErr = QCBORDecode_GetError(&DC);
7051 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7052 return 4;
7053 }
7054
7055 // Tests the entire input CBOR being too large when processing bstr wrapping
7056 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7057 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7058 uErr = QCBORDecode_GetError(&DC);
7059 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7060 return 5;
7061 }
7062
7063 return 0;
7064}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007065
7066
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007067#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7068
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007069static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007070 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007071 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7072 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7073 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7074 0x03,
7075 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7076 0xc3,
7077 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007078};
7079
7080int32_t SpiffyIndefiniteLengthStringsTests()
7081{
7082 QCBORDecodeContext DCtx;
7083
7084 QCBORDecode_Init(&DCtx,
7085 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7086 QCBOR_DECODE_MODE_NORMAL);
7087
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007088 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007089 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7090
7091 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007092 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007093 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
7094 if(QCBORDecode_GetAndResetError(&DCtx)) {
7095 return 1;
7096 }
7097
7098 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7099 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7100 return 2;
7101 }
7102
7103 uint64_t uInt;
7104 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7105 if(QCBORDecode_GetAndResetError(&DCtx)) {
7106 return 3;
7107 }
7108 if(uInt != 3) {
7109 return 4;
7110 }
7111
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007112#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007113 double uDouble;
7114 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7115 "label2",
7116 0xff,
7117 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007118#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007119 if(QCBORDecode_GetAndResetError(&DCtx)) {
7120 return 5;
7121 }
7122 if(uDouble != -16777474) {
7123 return 6;
7124 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007125#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007126 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7127 return 7;
7128 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007129#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007130#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007131
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007132
7133 QCBORDecode_ExitMap(&DCtx);
7134
7135 if(QCBORDecode_Finish(&DCtx)) {
7136 return 99;
7137 }
7138
7139 return 0;
7140}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007141#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007142
7143
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007144/*
7145 * An array of an integer and an array. The second array contains
7146 * a bstr-wrapped map.
7147 *
7148 * [7, [h'A36D6669... (see next lines) 73']]
7149 *
7150 * {"first integer": 42,
7151 * "an array of two strings": ["string1", "string2"],
7152 * "map in a map":
7153 * { "bytes 1": h'78787878',
7154 * "bytes 2": h'79797979',
7155 * "another int": 98,
7156 * "text 2": "lies, damn lies and statistics"
7157 * }
7158 * }
7159 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007160
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007161static const uint8_t pValidWrappedMapEncoded[] = {
7162 0x82, 0x07, 0x81, 0x58, 0x97,
7163 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7164 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7165 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7166 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7167 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7168 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7169 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7170 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7171 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7172 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7173 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7174 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7175 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7176 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7177 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7178 0x73
7179};
7180
7181#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7182
7183/* As above, but the arrays are indefinite length */
7184static const uint8_t pValidIndefWrappedMapEncoded[] = {
7185 0x9f, 0x07, 0x9f, 0x58, 0x97,
7186 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7187 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7188 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7189 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7190 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7191 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7192 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7193 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7194 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7195 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7196 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7197 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7198 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7199 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7200 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7201 0x73,
7202 0xff, 0xff
7203};
7204#endif
7205
7206
7207static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7208
7209#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7210static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7211#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7212
7213#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7214static const uint8_t pWrappedByIndefiniteLength[] = {
7215 0x81,
7216 0xd8, 0x18,
7217 0x5f,
7218 0x41, 0x83,
7219 0x41, 0x18,
7220 0x43, 0x2A, 0x18, 0x2B,
7221 0x42, 0x18, 0x2C,
7222 0xff
7223};
7224#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7225
7226
7227int32_t PeekAndRewindTest()
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007228{
7229 QCBORItem Item;
7230 QCBORError nCBORError;
7231 QCBORDecodeContext DCtx;
7232
7233 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7234
7235 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7236 return 100+(int32_t)nCBORError;
7237 }
7238 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7239 return 200;
7240 }
7241
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007242 QCBORDecode_VPeekNext(&DCtx, &Item);
7243 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
7244 return 150+(int32_t)nCBORError;
7245 }
7246 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7247 return 250;
7248 }
7249
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007250 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7251 return (int32_t)nCBORError;
7252 }
7253 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7254 return 300;
7255 }
7256
7257 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7258 return 400 + (int32_t)nCBORError;
7259 }
7260 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7261 return 500;
7262 }
7263
7264 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7265 return (int32_t)nCBORError;
7266 }
7267 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7268 return 600;
7269 }
7270
7271 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7272 return 900 + (int32_t)nCBORError;
7273 }
7274 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7275 Item.uDataType != QCBOR_TYPE_INT64 ||
7276 Item.val.int64 != 42 ||
7277 Item.uDataAlloc ||
7278 Item.uLabelAlloc ||
7279 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7280 return 1000;
7281 }
7282
7283 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7284 return 1100 + (int32_t)nCBORError;
7285 }
7286
7287 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7288 Item.uDataType != QCBOR_TYPE_INT64 ||
7289 Item.val.int64 != 42 ||
7290 Item.uDataAlloc ||
7291 Item.uLabelAlloc ||
7292 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7293 return 1200;
7294 }
7295
7296
7297 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7298 return 1300 + (int32_t)nCBORError;
7299 }
7300 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7301 Item.uDataAlloc ||
7302 Item.uLabelAlloc ||
7303 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7304 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007305 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007306 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007307 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007308
7309 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7310 return 1500 + (int32_t)nCBORError;
7311 }
7312 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7313 Item.uDataAlloc ||
7314 Item.uLabelAlloc ||
7315 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7316 return 1600;
7317 }
7318
7319 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7320 return 1700 + (int32_t)nCBORError;
7321 }
7322 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7323 Item.uDataAlloc ||
7324 Item.uLabelAlloc ||
7325 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7326 return 1800;
7327 }
7328
7329 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7330 return (int32_t)nCBORError;
7331 }
7332 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7333 Item.uDataAlloc ||
7334 Item.uLabelAlloc ||
7335 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7336 return 1900;
7337 }
7338
7339 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7340 return (int32_t)nCBORError;
7341 }
7342 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7343 Item.uDataAlloc ||
7344 Item.uLabelAlloc ||
7345 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7346 return 2000;
7347 }
7348
7349
7350 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7351 return 2100 + (int32_t)nCBORError;
7352 }
7353 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7354 Item.uDataAlloc ||
7355 Item.uLabelAlloc ||
7356 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7357 Item.uDataType != QCBOR_TYPE_MAP ||
7358 Item.val.uCount != 4) {
7359 return 2100;
7360 }
7361
7362 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7363 return 2200 + (int32_t)nCBORError;
7364 }
7365 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7366 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7367 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7368 Item.uDataAlloc ||
7369 Item.uLabelAlloc ||
7370 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7371 return 2300;
7372 }
7373
7374 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7375 return 2400 + (int32_t)nCBORError;
7376 }
7377 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7378 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7379 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7380 Item.uDataAlloc ||
7381 Item.uLabelAlloc ||
7382 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7383 return 2500;
7384 }
7385
7386 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7387 return 2600 + (int32_t)nCBORError;
7388 }
7389 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7390 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7391 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7392 Item.uDataAlloc ||
7393 Item.uLabelAlloc ||
7394 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7395 return 2700;
7396 }
7397
7398 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7399 return 2800 + (int32_t)nCBORError;
7400 }
7401 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7402 Item.uDataAlloc ||
7403 Item.uLabelAlloc ||
7404 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7405 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007406 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007407 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007408 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007409
7410 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7411 return 3000 + (int32_t)nCBORError;
7412 }
7413 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7414 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7415 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7416 Item.uDataAlloc ||
7417 Item.uLabelAlloc ||
7418 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7419 return 3100;
7420 }
7421
7422 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7423 return 3200 + (int32_t)nCBORError;
7424 }
7425 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7426 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7427 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7428 Item.uDataAlloc ||
7429 Item.uLabelAlloc ||
7430 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7431 return 3300;
7432 }
7433
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007434 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
7435 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7436 return 3300 + (int32_t)nCBORError;
7437 }
7438
7439 QCBORDecode_VPeekNext(&DCtx, &Item);
7440 nCBORError = QCBORDecode_GetError(&DCtx);
7441 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7442 return 3400 + (int32_t)nCBORError;
7443 }
7444
7445 QCBORDecode_VPeekNext(&DCtx, &Item);
7446 nCBORError = QCBORDecode_GetError(&DCtx);
7447 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7448 return 3500 + (int32_t)nCBORError;
7449 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007450
7451
7452 // Rewind to top level after entering several maps
7453 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7454
7455 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7456 return (int32_t)nCBORError;
7457 }
7458 if(Item.uDataType != QCBOR_TYPE_MAP ||
7459 Item.val.uCount != 3) {
7460 return 400;
7461 }
7462
7463 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7464 return 4000+(int32_t)nCBORError;
7465 }
7466
7467 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7468 Item.uDataType != QCBOR_TYPE_INT64 ||
7469 Item.val.int64 != 42 ||
7470 Item.uDataAlloc ||
7471 Item.uLabelAlloc ||
7472 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7473 return 4100;
7474 }
7475
7476 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7477 return 4100+(int32_t)nCBORError;
7478 }
7479 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7480 Item.uDataAlloc ||
7481 Item.uLabelAlloc ||
7482 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7483 Item.uDataType != QCBOR_TYPE_ARRAY ||
7484 Item.val.uCount != 2) {
7485 return 4200;
7486 }
7487
7488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7489 return 4200+(int32_t)nCBORError;
7490 }
7491 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7492 Item.uDataAlloc ||
7493 Item.uLabelAlloc ||
7494 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7495 return 4300;
7496 }
7497
7498 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7499 return 4300+(int32_t)nCBORError;
7500 }
7501 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7502 Item.uDataAlloc ||
7503 Item.uLabelAlloc ||
7504 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7505 return 4400;
7506 }
7507
7508 QCBORDecode_Rewind(&DCtx);
7509
7510 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7511 return 4400+(int32_t)nCBORError;
7512 }
7513 if(Item.uDataType != QCBOR_TYPE_MAP ||
7514 Item.val.uCount != 3) {
7515 return 4500;
7516 }
7517
7518 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7519 return (int32_t)nCBORError;
7520 }
7521
7522 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7523 Item.uDataType != QCBOR_TYPE_INT64 ||
7524 Item.val.int64 != 42 ||
7525 Item.uDataAlloc ||
7526 Item.uLabelAlloc ||
7527 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7528 return 4600;
7529 }
7530
7531 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7532 return (int32_t)nCBORError;
7533 }
7534 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7535 Item.uDataAlloc ||
7536 Item.uLabelAlloc ||
7537 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7538 Item.uDataType != QCBOR_TYPE_ARRAY ||
7539 Item.val.uCount != 2) {
7540 return 4700;
7541 }
7542
7543 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7544 return (int32_t)nCBORError;
7545 }
7546 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7547 Item.uDataAlloc ||
7548 Item.uLabelAlloc ||
7549 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7550 return 4800;
7551 }
7552
7553 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7554 return 4900+(int32_t)nCBORError;
7555 }
7556 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7557 Item.uDataAlloc ||
7558 Item.uLabelAlloc ||
7559 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7560 return 5000;
7561 }
7562
7563
7564 // Rewind an entered map
7565 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7566
7567 QCBORDecode_EnterMap(&DCtx, NULL);
7568
7569 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7570 return 5100+(int32_t)nCBORError;
7571 }
7572
7573 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7574 Item.uDataType != QCBOR_TYPE_INT64 ||
7575 Item.val.int64 != 42 ||
7576 Item.uDataAlloc ||
7577 Item.uLabelAlloc ||
7578 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7579 return 5200;
7580 }
7581
7582 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7583 return 5200+(int32_t)nCBORError;
7584 }
7585 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7586 Item.uDataAlloc ||
7587 Item.uLabelAlloc ||
7588 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7589 Item.uDataType != QCBOR_TYPE_ARRAY ||
7590 Item.val.uCount != 2) {
7591 return -5300;
7592 }
7593
7594 QCBORDecode_Rewind(&DCtx);
7595
7596 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7597 return 5300+(int32_t)nCBORError;
7598 }
7599
7600 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7601 Item.uDataType != QCBOR_TYPE_INT64 ||
7602 Item.val.int64 != 42 ||
7603 Item.uDataAlloc ||
7604 Item.uLabelAlloc ||
7605 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7606 return 5400;
7607 }
7608
7609 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7610 return 5400+(int32_t)nCBORError;
7611 }
7612 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7613 Item.uDataAlloc ||
7614 Item.uLabelAlloc ||
7615 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7616 Item.uDataType != QCBOR_TYPE_ARRAY ||
7617 Item.val.uCount != 2) {
7618 return 5500;
7619 }
7620
7621
7622 // Rewind and entered array inside an entered map
7623 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7624
7625 QCBORDecode_EnterMap(&DCtx, NULL);
7626
7627 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7628
7629 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7630 return 5600+(int32_t)nCBORError;
7631 }
7632 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7633 Item.uDataAlloc ||
7634 Item.uLabelAlloc ||
7635 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7636 return 5700;
7637 }
7638
7639 QCBORDecode_Rewind(&DCtx);
7640
7641 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7642 return 5700+(int32_t)nCBORError;
7643 }
7644 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7645 Item.uDataAlloc ||
7646 Item.uLabelAlloc ||
7647 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7648 return 5800;
7649 }
7650
7651 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7652 return (int32_t)nCBORError;
7653 }
7654 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7655 Item.uDataAlloc ||
7656 Item.uLabelAlloc ||
7657 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7658 return 5900;
7659 }
7660
7661 QCBORDecode_Rewind(&DCtx);
7662
7663 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7664 return 5900+(int32_t)nCBORError;
7665 }
7666 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7667 Item.uDataAlloc ||
7668 Item.uLabelAlloc ||
7669 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7670 return 6000;
7671 }
7672
7673
7674 // Rewind a byte string inside an array inside an array
7675 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
7676
7677 QCBORDecode_EnterArray(&DCtx, NULL);
7678
7679 uint64_t i;
7680 QCBORDecode_GetUInt64(&DCtx, &i);
7681
7682 QCBORDecode_EnterArray(&DCtx, NULL);
7683
7684 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7685 if(QCBORDecode_GetError(&DCtx)) {
7686 return 6100;
7687 }
7688
7689 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7690 return (int32_t)nCBORError;
7691 }
7692 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7693 return 6200;
7694 }
7695
7696 QCBORDecode_Rewind(&DCtx);
7697
7698 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7699 return 6300+(int32_t)nCBORError;
7700 }
7701 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7702 return 6400;
7703 }
7704
7705#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7706 // Rewind a byte string inside an indefinite-length array inside
7707 // indefinite-length array
7708
7709 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
7710
7711 QCBORDecode_EnterArray(&DCtx, NULL);
7712
7713 QCBORDecode_GetUInt64(&DCtx, &i);
7714
7715 QCBORDecode_EnterArray(&DCtx, NULL);
7716
7717 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7718 if(QCBORDecode_GetError(&DCtx)) {
7719 return 6500;
7720 }
7721
7722 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7723 return 6600+(int32_t)nCBORError;
7724 }
7725 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7726 return 6700;
7727 }
7728
7729 QCBORDecode_Rewind(&DCtx);
7730
7731 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7732 return 6800+(int32_t)nCBORError;
7733 }
7734 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7735 return 6900;
7736 }
7737#endif
7738
7739 // Rewind an empty map
7740 // [100, {}]
7741 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
7742 QCBORDecode_EnterArray(&DCtx, NULL);
7743 QCBORDecode_GetUInt64(&DCtx, &i);
7744 if(i != 100) {
7745 return 7010;
7746 }
7747 QCBORDecode_EnterMap(&DCtx, NULL);
7748
7749 /* Do it 5 times to be sure multiple rewinds work */
7750 for(int n = 0; n < 5; n++) {
7751 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7752 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7753 return 7000 + n;
7754 }
7755 QCBORDecode_Rewind(&DCtx);
7756 }
7757 QCBORDecode_ExitMap(&DCtx);
7758 QCBORDecode_Rewind(&DCtx);
7759 QCBORDecode_GetUInt64(&DCtx, &i);
7760 if(i != 100) {
7761 return 7010;
7762 }
7763 QCBORDecode_ExitArray(&DCtx);
7764 QCBORDecode_Rewind(&DCtx);
7765 QCBORDecode_EnterArray(&DCtx, NULL);
7766 i = 9;
7767 QCBORDecode_GetUInt64(&DCtx, &i);
7768 if(i != 100) {
7769 return 7020;
7770 }
7771 if(QCBORDecode_GetError(&DCtx)){
7772 return 7030;
7773 }
7774
7775 // Rewind an empty indefinite length map
7776#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7777 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
7778 QCBORDecode_EnterArray(&DCtx, NULL);
7779 QCBORDecode_GetUInt64(&DCtx, &i);
7780 if(i != 100) {
7781 return 7810;
7782 }
7783 QCBORDecode_EnterMap(&DCtx, NULL);
7784
7785 /* Do it 5 times to be sure multiple rewinds work */
7786 for(int n = 0; n < 5; n++) {
7787 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7788 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7789 return 7800 + n;
7790 }
7791 QCBORDecode_Rewind(&DCtx);
7792 }
7793 QCBORDecode_ExitMap(&DCtx);
7794 QCBORDecode_Rewind(&DCtx);
7795 QCBORDecode_GetUInt64(&DCtx, &i);
7796 if(i != 100) {
7797 return 7810;
7798 }
7799 QCBORDecode_ExitArray(&DCtx);
7800 QCBORDecode_Rewind(&DCtx);
7801 QCBORDecode_EnterArray(&DCtx, NULL);
7802 i = 9;
7803 QCBORDecode_GetUInt64(&DCtx, &i);
7804 if(i != 100) {
7805 return 7820;
7806 }
7807 if(QCBORDecode_GetError(&DCtx)){
7808 return 7830;
7809 }
7810#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7811
7812 // Rewind an indefnite length byte-string wrapped sequence
7813#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7814 QCBORDecode_Init(&DCtx,
7815 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
7816 0);
7817 UsefulBuf_MAKE_STACK_UB(Pool, 100);
7818 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
7819
7820 QCBORDecode_EnterArray(&DCtx, NULL);
7821 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
7822 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007823 /* this is what happens when trying to enter
7824 indefinite-length byte string
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007825 wrapped CBOR. Tolerate for now. Eventually it needs
7826 to be fixed so this works, but that is not simple. */
7827 return 7300;
7828 }
7829
7830 /*
7831 QCBORDecode_GetUInt64(&DCtx, &i);
7832 if(i != 42) {
7833 return 7110;
7834 }
7835 QCBORDecode_Rewind(&DCtx);
7836 QCBORDecode_GetUInt64(&DCtx, &i);
7837 if(i != 42) {
7838 return 7220;
7839 }*/
7840#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7841
7842
7843 // Rewind an indefnite length byte-string wrapped sequence
7844
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007845 return 0;
7846}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007847
7848
7849
7850
7851static const uint8_t spBooleansInMap[] =
7852{
7853 0xa1, 0x08, 0xf5
7854};
7855
7856static const uint8_t spBooleansInMapWrongType[] =
7857{
7858 0xa1, 0x08, 0xf6
7859};
7860
7861static const uint8_t spBooleansInMapNWF[] =
7862{
7863 0xa1, 0x08, 0x1a
7864};
7865
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007866static const uint8_t spNullInMap[] =
7867{
7868 0xa1, 0x08, 0xf6
7869};
7870
7871static const uint8_t spUndefinedInMap[] =
7872{
7873 0xa1, 0x08, 0xf7
7874};
7875
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007876
7877int32_t BoolTest(void)
7878{
7879 QCBORDecodeContext DCtx;
7880 bool b;
7881
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007882 QCBORDecode_Init(&DCtx,
7883 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7884 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007885 QCBORDecode_EnterMap(&DCtx, NULL);
7886 QCBORDecode_GetBool(&DCtx, &b);
7887 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7888 return 1;
7889 }
7890
7891 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
7892 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7893 return 2;
7894 }
7895
7896 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
7897 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7898 return 3;
7899 }
7900
7901
7902 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
7903 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7904 return 4;
7905 }
7906
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007907 QCBORDecode_Init(&DCtx,
7908 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
7909 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007910 QCBORDecode_EnterMap(&DCtx, NULL);
7911 QCBORDecode_GetBool(&DCtx, &b);
7912 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7913 return 5;
7914 }
7915
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007916 QCBORDecode_Init(&DCtx,
7917 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7918 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007919 QCBORDecode_EnterMap(&DCtx, NULL);
7920 QCBORDecode_GetBool(&DCtx, &b);
7921 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7922 return 6;
7923 }
7924
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007925
7926 QCBORDecode_Init(&DCtx,
7927 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7928 0);
7929 QCBORDecode_EnterMap(&DCtx, NULL);
7930 QCBORDecode_GetNull(&DCtx);
7931 if(QCBORDecode_GetAndResetError(&DCtx)) {
7932 return 7;
7933 }
7934
7935 QCBORDecode_Init(&DCtx,
7936 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7937 0);
7938 QCBORDecode_EnterMap(&DCtx, NULL);
7939 QCBORDecode_GetNull(&DCtx);
7940 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7941 return 8;
7942 }
7943
7944 QCBORDecode_Init(&DCtx,
7945 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7946 0);
7947 QCBORDecode_EnterMap(&DCtx, NULL);
7948 QCBORDecode_GetNullInMapN(&DCtx, 8);
7949 if(QCBORDecode_GetAndResetError(&DCtx)) {
7950 return 9;
7951 }
7952
7953 QCBORDecode_Init(&DCtx,
7954 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7955 0);
7956 QCBORDecode_EnterMap(&DCtx, NULL);
7957 QCBORDecode_GetNullInMapN(&DCtx, 8);
7958 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7959 return 10;
7960 }
7961
7962 QCBORDecode_Init(&DCtx,
7963 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7964 0);
7965 QCBORDecode_EnterMap(&DCtx, NULL);
7966 QCBORDecode_GetUndefined(&DCtx);
7967 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7968 return 11;
7969 }
7970
7971 QCBORDecode_Init(&DCtx,
7972 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7973 0);
7974 QCBORDecode_EnterMap(&DCtx, NULL);
7975 QCBORDecode_GetUndefined(&DCtx);
7976 if(QCBORDecode_GetAndResetError(&DCtx)) {
7977 return 12;
7978 }
7979
7980 QCBORDecode_Init(&DCtx,
7981 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7982 0);
7983 QCBORDecode_EnterMap(&DCtx, NULL);
7984 QCBORDecode_GetUndefined(&DCtx);
7985 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7986 return 13;
7987 }
7988
7989 QCBORDecode_Init(&DCtx,
7990 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7991 0);
7992 QCBORDecode_EnterMap(&DCtx, NULL);
7993 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
7994 if(QCBORDecode_GetAndResetError(&DCtx)) {
7995 return 14;
7996 }
7997
7998 QCBORDecode_Init(&DCtx,
7999 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8000 0);
8001 QCBORDecode_EnterMap(&DCtx, NULL);
8002 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8003 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8004 return 15;
8005 }
8006
8007 QCBORDecode_Init(&DCtx,
8008 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8009 0);
8010 QCBORDecode_EnterMap(&DCtx, NULL);
8011 QCBORDecode_GetUndefined(&DCtx);
8012 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8013 return 15;
8014 }
8015
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008016 return 0;
8017}