blob: 236f59c89f65bdd040c6aedf617c77d29595e0ff [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.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070036#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080037#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080038#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070039#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080040
Laurence Lundblade9b334962020-08-27 10:55:53 -070041// Handy macro to compare a UsefulBuf to a C string
42#define UsefulBufCompareToSZ(x, y) \
43 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044
Laurence Lundbladea2e29072018-12-30 09:20:06 -080045#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080046#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080047
48static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080049{
50 if(szLabel) {
51 printf("%s ", szLabel);
52 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080053
Laurence Lundblade570fab52018-10-13 18:28:27 +080054 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080055 for(i = 0; i < Buf.len; i++) {
56 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 printf("%02x ", Z);
58 }
59 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080060
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 fflush(stdout);
62}
Laurence Lundbladee2c893c2020-12-26 17:41:53 -080063#endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064
Laurence Lundbladecc7da412020-12-27 00:09:07 -080065/*
66 [
67 -9223372036854775808,
68 -4294967297,
69 -4294967296,
70 -4294967295,
71 -4294967294,
72 -2147483648,
73 -2147483647,
74 -65538,
75 -65537,
76 -65536,
77 -65535,
78 -65534,
79 -257,
80 -256,
81 -255,
82 -254,
83 -25,
84 -24,
85 -23,
86 -1,
87 0,
88 0,
89 1,
90 22,
91 23,
92 24,
93 25,
94 26,
95 254,
96 255,
97 256,
98 257,
99 65534,
100 65535,
101 65536,
102 65537,
103 65538,
104 2147483647,
105 2147483647,
106 2147483648,
107 2147483649,
108 4294967294,
109 4294967295,
110 4294967296,
111 4294967297,
112 9223372036854775807,
113 18446744073709551615
114 ]
115 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700117static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800118 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
119 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
120 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
121 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
122 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
123 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
124 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
125 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
126 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
127 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
128 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
129 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
130 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
131 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
132 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
133 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
134 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
135 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
136 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
137 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
138 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
139 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
140 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
141 0xff, 0xff};
142
143
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144// return CBOR error or -1 if type of value doesn't match
145
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800146static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700148 QCBORItem Item;
149 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800150
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800151 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700152 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 if(Item.uDataType != QCBOR_TYPE_ARRAY)
154 return -1;
155
156 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700157 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800158 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800159 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800160 return -1;
161
162 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700163 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800165 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800167
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800168 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700169 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800171 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800172 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800173
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800174 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700175 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800177 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800178 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800179
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800180 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700181 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800182 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800183 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800184 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800185
186
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800187 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700188 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if(Item.uDataType != QCBOR_TYPE_INT64 ||
190 Item.val.int64 != -2147483648)
191 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800192
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800193 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700194 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(Item.uDataType != QCBOR_TYPE_INT64 ||
196 Item.val.int64 != -2147483647)
197 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800198
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700200 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(Item.uDataType != QCBOR_TYPE_INT64 ||
202 Item.val.int64 != -65538)
203 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800204
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800205 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700206 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800207 if(Item.uDataType != QCBOR_TYPE_INT64 ||
208 Item.val.int64 != -65537)
209 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800210
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800211 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700212 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800213 if(Item.uDataType != QCBOR_TYPE_INT64 ||
214 Item.val.int64 != -65536)
215 return -1;
216
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800217
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700219 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if(Item.uDataType != QCBOR_TYPE_INT64 ||
221 Item.val.int64 != -65535)
222 return -1;
223
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800224
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800225 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700226 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800227 if(Item.uDataType != QCBOR_TYPE_INT64 ||
228 Item.val.int64 != -65534)
229 return -1;
230
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800231
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800232 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700233 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800234 if(Item.uDataType != QCBOR_TYPE_INT64 ||
235 Item.val.int64 != -257)
236 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800237
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700239 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800240 if(Item.uDataType != QCBOR_TYPE_INT64 ||
241 Item.val.int64 != -256)
242 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800243
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800244 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700245 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800246 if(Item.uDataType != QCBOR_TYPE_INT64 ||
247 Item.val.int64 != -255)
248 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800250 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700251 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800252 if(Item.uDataType != QCBOR_TYPE_INT64 ||
253 Item.val.int64 != -254)
254 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800255
256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700258 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800259 if(Item.uDataType != QCBOR_TYPE_INT64 ||
260 Item.val.int64 != -25)
261 return -1;
262
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800263
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800264 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700265 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800266 if(Item.uDataType != QCBOR_TYPE_INT64 ||
267 Item.val.int64 != -24)
268 return -1;
269
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800270
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800271 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700272 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800273 if(Item.uDataType != QCBOR_TYPE_INT64 ||
274 Item.val.int64 != -23)
275 return -1;
276
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800277
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800278 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700279 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800280 if(Item.uDataType != QCBOR_TYPE_INT64 ||
281 Item.val.int64 != -1)
282 return -1;
283
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800284
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800285 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700286 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800287 if(Item.uDataType != QCBOR_TYPE_INT64 ||
288 Item.val.int64 != 0)
289 return -1;
290
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800291
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800292 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700293 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800294 if(Item.uDataType != QCBOR_TYPE_INT64 ||
295 Item.val.int64 != 0)
296 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800297
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800298 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700299 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800300 if(Item.uDataType != QCBOR_TYPE_INT64 ||
301 Item.val.int64 != 1)
302 return -1;
303
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800304
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800305 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700306 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800307 if(Item.uDataType != QCBOR_TYPE_INT64 ||
308 Item.val.int64 != 22)
309 return -1;
310
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800311
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800312 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700313 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800314 if(Item.uDataType != QCBOR_TYPE_INT64 ||
315 Item.val.int64 != 23)
316 return -1;
317
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800318
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800319 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700320 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800321 if(Item.uDataType != QCBOR_TYPE_INT64 ||
322 Item.val.int64 != 24)
323 return -1;
324
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800325
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800326 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700327 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800328 if(Item.uDataType != QCBOR_TYPE_INT64 ||
329 Item.val.int64 != 25)
330 return -1;
331
332 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700333 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800334 if(Item.uDataType != QCBOR_TYPE_INT64 ||
335 Item.val.int64 != 26)
336 return -1;
337
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800338
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800339 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700340 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800341 if(Item.uDataType != QCBOR_TYPE_INT64 ||
342 Item.val.int64 != 254)
343 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800344
345
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800346 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700347 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800348 if(Item.uDataType != QCBOR_TYPE_INT64 ||
349 Item.val.int64 != 255)
350 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800351
352
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800353 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700354 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 if(Item.uDataType != QCBOR_TYPE_INT64 ||
356 Item.val.int64 != 256)
357 return -1;
358
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800359
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800360 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700361 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800362 if(Item.uDataType != QCBOR_TYPE_INT64 ||
363 Item.val.int64 != 257)
364 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800365
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800366 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700367 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800368 if(Item.uDataType != QCBOR_TYPE_INT64 ||
369 Item.val.int64 != 65534)
370 return -1;
371
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700374 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800375 if(Item.uDataType != QCBOR_TYPE_INT64 ||
376 Item.val.int64 != 65535)
377 return -1;
378
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800379
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700381 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800382 if(Item.uDataType != QCBOR_TYPE_INT64 ||
383 Item.val.int64 != 65536)
384 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800385
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800386 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700387 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800388 if(Item.uDataType != QCBOR_TYPE_INT64 ||
389 Item.val.int64 != 65537)
390 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800391
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800392 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700393 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800394 if(Item.uDataType != QCBOR_TYPE_INT64 ||
395 Item.val.int64 != 65538)
396 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800397
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800398 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700399 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800400 if(Item.uDataType != QCBOR_TYPE_INT64 ||
401 Item.val.int64 != 2147483647)
402 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800403
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800404 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700405 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800406 if(Item.uDataType != QCBOR_TYPE_INT64 ||
407 Item.val.int64 != 2147483647)
408 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800410 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700411 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800412 if(Item.uDataType != QCBOR_TYPE_INT64 ||
413 Item.val.int64 != 2147483648)
414 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800415
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800416 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700417 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800418 if(Item.uDataType != QCBOR_TYPE_INT64 ||
419 Item.val.int64 != 2147483649)
420 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800421
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800422 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700423 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800424 if(Item.uDataType != QCBOR_TYPE_INT64 ||
425 Item.val.int64 != 4294967294)
426 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800427
428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700430 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800431 if(Item.uDataType != QCBOR_TYPE_INT64 ||
432 Item.val.int64 != 4294967295)
433 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
435
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800436 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700437 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800438 if(Item.uDataType != QCBOR_TYPE_INT64 ||
439 Item.val.int64 != 4294967296)
440 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800441
442
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800443 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700444 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800445 if(Item.uDataType != QCBOR_TYPE_INT64 ||
446 Item.val.int64 != 4294967297)
447 return -1;
448
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800449
450
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800451 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700452 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800453 if(Item.uDataType != QCBOR_TYPE_INT64 ||
454 Item.val.int64 != 9223372036854775807LL)
455 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800456
457
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800458 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700459 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800460 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
461 Item.val.uint64 != 18446744073709551615ULL)
462 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800463
464
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800465 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
466 return -1;
467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800468
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800469 return 0;
470}
471
472
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800473/* One less than the smallest negative integer allowed in C. Decoding
474 this should fail.
475 -9223372036854775809
476 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800477static const uint8_t spTooSmallNegative[] = {
478 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000479};
480
481
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800482/*
483 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800484 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800485 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800486int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000488 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800490
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000491 QCBORDecode_Init(&DCtx,
492 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
493 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800494
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000495 // The really big test of all successes
496 nReturn = IntegerValuesParseTestInternal(&DCtx);
497 if(nReturn) {
498 return nReturn;
499 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800500
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000501 // The one large negative integer that can be parsed
502 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800503 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000504 QCBOR_DECODE_MODE_NORMAL);
505
506 QCBORItem item;
507 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
508 nReturn = -4000;
509 }
510
511 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800512}
513
514
515/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800516 Creates a simple CBOR array and returns it in *pEncoded. The array is
517 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800518
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800519 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800520
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521 */
522
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800523static uint8_t spSimpleArrayBuffer[50];
524
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800525static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800526{
527 QCBOREncodeContext ECtx;
528 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800530 *pEncoded = NULL;
531 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800532
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800533 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800534 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800535 // and last with the buffer to do the actual encoding
536 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700537 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800538 QCBOREncode_OpenArray(&ECtx);
539 QCBOREncode_AddInt64(&ECtx, nInt1);
540 QCBOREncode_AddInt64(&ECtx, nInt2);
541 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
542 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
543 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800544
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800545 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800546 goto Done;
547
548 if(*pEncoded != NULL) {
549 nReturn = 0;
550 goto Done;
551 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800552
553 // Use static buffer to avoid dependency on malloc()
554 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800555 goto Done;
556 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800557 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800558
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800560
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800561Done:
562 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800563}
564
565
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800567 Some basic CBOR with map and array used in a lot of tests.
568 The map labels are all strings
569
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800570 {
571 "first integer": 42,
572 "an array of two strings": [
573 "string1", "string2"
574 ],
575 "map in a map": {
576 "bytes 1": h'78787878',
577 "bytes 2": h'79797979',
578 "another int": 98,
579 "text 2": "lies, damn lies and statistics"
580 }
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900581 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800582 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800583static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800584 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
585 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
586 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
587 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
588 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
589 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
590 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
591 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69,
592 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4,
593 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
594 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79,
595 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79,
596 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68,
597 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62,
598 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
599 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64,
600 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
601 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61,
602 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800603
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800604
605#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700606// Same as above, but with indefinite lengths.
607static const uint8_t pValidMapIndefEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800608 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
609 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
610 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
611 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
612 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
613 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
614 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
615 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20,
616 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70,
617 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
618 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
619 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
620 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
621 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
622 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
623 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
624 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
625 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
626 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
627 0xff, 0xff};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800628#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700629
630
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800631static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700632 size_t nLen,
633 int64_t *pInt1,
634 int64_t *pInt2,
635 const uint8_t **pBuf3,
636 size_t *pBuf3Len,
637 const uint8_t **pBuf4,
638 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800639{
640 QCBORDecodeContext DCtx;
641 QCBORItem Item;
642 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800643
Laurence Lundbladeee851742020-01-08 08:37:05 -0800644 QCBORDecode_Init(&DCtx,
645 (UsefulBufC){pEncoded, nLen},
646 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800648 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700649 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
650 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800651 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700652 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800653
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700655 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
656 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800657 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700658 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800659 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800661 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700662 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
663 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800664 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700665 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800666 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800667
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800668 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700669 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
670 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800671 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700672 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800673 *pBuf3 = Item.val.string.ptr;
674 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800675
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800676 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700677 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
678 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800679 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700680 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800681 *pBuf4 = Item.val.string.ptr;
682 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800683
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800684 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800685
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800686Done:
687 return(nReturn);
688}
689
690
691
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800692
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800693int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800694{
695 uint8_t *pEncoded;
696 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800697
Laurence Lundblade5e390822019-01-06 12:35:01 -0800698 int64_t i1=0, i2=0;
699 size_t i3=0, i4=0;
700 const uint8_t *s3= (uint8_t *)"";
701 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800702
703
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
705 return(-1);
706 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800707
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800708 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800709
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800710 if(i1 != 23 ||
711 i2 != 6000 ||
712 i3 != 8 ||
713 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530714 memcmp("galactic", s3, 8) !=0 ||
715 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800716 return(-1);
717 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800718
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800719 return(0);
720}
721
722
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700723/*
724 [
725 0,
726 [],
727 [
728 [],
729 [
730 0
731 ],
732 {},
733 {
734 1: {},
735 2: {},
736 3: []
737 }
738 ]
739 ]
740 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800741static uint8_t sEmpties[] = {
742 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
743 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700744
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800745#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700746/* Same as above, but with indefinte lengths */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800747static const uint8_t sEmptiesIndef[] = {
Laurence Lundblade02625d42020-06-25 14:41:41 -07007480x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700749 0x00,
750 0x9F,
751 0xFF,
752 0x9F,
753 0x9F,
754 0xFF,
755 0x9F,
756 0x00,
757 0xFF,
758 0xBF,
759 0xFF,
760 0xBF,
761 0x01,
762 0xBF,
763 0xFF,
764 0x02,
765 0xBF,
766 0xFF,
767 0x03,
768 0x9F,
769 0xFF,
770 0xFF,
771 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700772 0xFF};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800773#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade02625d42020-06-25 14:41:41 -0700774
775
776static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700777{
778 QCBORDecodeContext DCtx;
779 QCBORItem Item;
780
Laurence Lundbladeee851742020-01-08 08:37:05 -0800781 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700782 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800783 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700784
785 // Array with 3 items
786 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
787 Item.uDataType != QCBOR_TYPE_ARRAY ||
788 Item.uNestingLevel != 0 ||
789 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700790 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700791 return -1;
792 }
793
794 // An integer 0
795 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
796 Item.uDataType != QCBOR_TYPE_INT64 ||
797 Item.uNestingLevel != 1 ||
798 Item.uNextNestLevel != 1 ||
799 Item.val.uint64 != 0) {
800 return -2;
801 }
802
803 // An empty array
804 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
805 Item.uDataType != QCBOR_TYPE_ARRAY ||
806 Item.uNestingLevel != 1 ||
807 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700808 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700809 return -3;
810 }
811
812 // An array with 4 items
813 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
814 Item.uDataType != QCBOR_TYPE_ARRAY ||
815 Item.uNestingLevel != 1 ||
816 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700817 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700818 return -4;
819 }
820
821 // An empty array
822 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
823 Item.uDataType != QCBOR_TYPE_ARRAY ||
824 Item.uNestingLevel != 2 ||
825 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700826 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700827 return -5;
828 }
829
830 // An array with 1 item
831 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
832 Item.uDataType != QCBOR_TYPE_ARRAY ||
833 Item.uNestingLevel != 2 ||
834 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700835 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700836 return -6;
837 }
838
839 // An integer 0
840 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
841 Item.uDataType != QCBOR_TYPE_INT64 ||
842 Item.uNestingLevel != 3 ||
843 Item.uNextNestLevel != 2 ||
844 Item.val.uint64 != 0) {
845 return -7;
846 }
847
848 // An empty map
849 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
850 Item.uDataType != QCBOR_TYPE_MAP ||
851 Item.uNestingLevel != 2 ||
852 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700853 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700854 return -8;
855 }
856
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700857 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700858 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
859 Item.uDataType != QCBOR_TYPE_MAP ||
860 Item.uNestingLevel != 2 ||
861 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700862 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700863 return -9;
864 }
865
866 // An empty map
867 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
868 Item.uDataType != QCBOR_TYPE_MAP ||
869 Item.uNestingLevel != 3 ||
870 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700871 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700872 return -10;
873 }
874
875 // An empty map
876 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
877 Item.uDataType != QCBOR_TYPE_MAP ||
878 Item.uNestingLevel != 3 ||
879 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700880 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700881 return -11;
882 }
883
884 // An empty array
885 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
886 Item.uDataType != QCBOR_TYPE_ARRAY ||
887 Item.uNestingLevel != 3 ||
888 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700889 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700890 return -12;
891 }
892
893 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
894 return -13;
895 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700896 return 0;
897}
898
899
900int32_t EmptyMapsAndArraysTest()
901{
902 int nResult;
903 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
904 true);
905 if(nResult) {
906 return nResult;
907 }
908
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800909#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700910 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
911 false);
912
913 if(nResult) {
914 return nResult -100;
915 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800916#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700917
918 return 0;
919}
920
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700921
922static const uint8_t sEmptyMap[] = {
Michael Richardson87de9af2021-02-18 23:13:31 -0500923 0xA1, //# map(1)
924 0x02, //# unsigned(2)
925 0xA0, //# map(0)
926};
927
928int32_t ParseEmptyMapInMapTest(void)
929{
930 QCBORDecodeContext DCtx;
931 QCBORItem Item;
932 int nReturn = 0;
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700933 QCBORError uErr;
Michael Richardson87de9af2021-02-18 23:13:31 -0500934
935 QCBORDecode_Init(&DCtx,
936 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap),
937 QCBOR_DECODE_MODE_NORMAL);
938
939 /* now open the first Map */
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700940 uErr = QCBORDecode_GetNext(&DCtx, &Item);
941 if(uErr != QCBOR_SUCCESS ||
Michael Richardson87de9af2021-02-18 23:13:31 -0500942 Item.uDataType != QCBOR_TYPE_MAP) {
943 nReturn = -3;
944 goto done;
945 }
946
947 if(QCBORDecode_GetNext(&DCtx, &Item) != 0) {
948 nReturn = -1;
949 goto done;
950 }
951 if(Item.uDataType != QCBOR_TYPE_MAP ||
952 Item.uNestingLevel != 1 ||
953 Item.label.int64 != 2) {
954 nReturn = -2;
955 goto done;
956 }
957
958 done:
959 return(nReturn);
960}
961
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700962
Michael Richardson87de9af2021-02-18 23:13:31 -0500963/* [[[[[[[[[[]]]]]]]]]] */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800964static const uint8_t spDeepArrays[] = {
965 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
966 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800967
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800968int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800969{
970 QCBORDecodeContext DCtx;
971 int nReturn = 0;
972 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800973
Laurence Lundbladeee851742020-01-08 08:37:05 -0800974 QCBORDecode_Init(&DCtx,
975 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
976 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800977
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800978 for(i = 0; i < 10; i++) {
979 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800980
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800981 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
982 Item.uDataType != QCBOR_TYPE_ARRAY ||
983 Item.uNestingLevel != i) {
984 nReturn = -1;
985 break;
986 }
987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800988
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800989 return(nReturn);
990}
991
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800992/* Big enough to test nesting to the depth of 24
993 [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]
994 */
995static const uint8_t spTooDeepArrays[] = {
996 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
997 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
998 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
999 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001000
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001001int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002{
1003 QCBORDecodeContext DCtx;
1004 int nReturn = 0;
1005 int i;
1006 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001007
1008
Laurence Lundbladeee851742020-01-08 08:37:05 -08001009 QCBORDecode_Init(&DCtx,
1010 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
1011 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001012
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001013 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001014
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001015 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
1016 Item.uDataType != QCBOR_TYPE_ARRAY ||
1017 Item.uNestingLevel != i) {
1018 nReturn = -1;
1019 break;
1020 }
1021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001022
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001023 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001024 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001025
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001026 return(nReturn);
1027}
1028
1029
1030
1031
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001032int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001033{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001034 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001035
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001036 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001037 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001038
Laurence Lundbladeee851742020-01-08 08:37:05 -08001039 QCBORDecode_Init(&DCtx,
1040 (UsefulBufC){spExpectedEncodedInts, nNum},
1041 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001042
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001043 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001044
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001045 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001046 nResult = -1;
1047 goto Done;
1048 }
1049 }
1050Done:
1051 return nResult;
1052}
1053
1054
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001055
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001056int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001057{
1058 uint8_t *pEncoded;
1059 int nReturn;
1060 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001061
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001062 int64_t i1, i2;
1063 size_t i3, i4;
1064 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001065
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001066 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001067
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001068 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
1069 return(-1);
1070 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001071
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001072 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07001073 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
1074 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001075 if(nResult == 0) {
1076 nReturn = -1;
1077 }
1078 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001079
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001080 return(nReturn);
1081}
1082
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301083/*
1084 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001085 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1086 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301087 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001088static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089{
1090 QCBORDecodeContext DCtx;
1091 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001092 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001093
Laurence Lundbladeee851742020-01-08 08:37:05 -08001094 QCBORDecode_Init(&DCtx,
1095 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1096 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001097
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001099 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001100 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001101 if(Item.uDataType != QCBOR_TYPE_MAP ||
1102 Item.val.uCount != 3)
1103 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001104
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001105 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001106 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001107 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001108
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001109 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001110 Item.uDataType != QCBOR_TYPE_INT64 ||
1111 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301112 Item.uDataAlloc ||
1113 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001114 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001115 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001116 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001117
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001119 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001120 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001121 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301122 Item.uDataAlloc ||
1123 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001124 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001125 Item.uDataType != QCBOR_TYPE_ARRAY ||
1126 Item.val.uCount != 2)
1127 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001128
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001129 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001130 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001131 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001132 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301133 Item.uDataAlloc ||
1134 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001135 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001136 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001138
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001139 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001140 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001141 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001142 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301143 Item.uDataAlloc ||
1144 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001145 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001146 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001147 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001148
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001149 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001150 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001151 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001152 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301153 Item.uDataAlloc ||
1154 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001155 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001156 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001158 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001160
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001162 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001164 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001166 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301167 Item.uDataAlloc ||
1168 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001169 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001170 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001172
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001173 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001174 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001176 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001177 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001178 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301179 Item.uDataAlloc ||
1180 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001181 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001184
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001185 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001186 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001187 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001188 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301189 Item.uDataAlloc ||
1190 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001191 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001192 Item.uDataType != QCBOR_TYPE_INT64 ||
1193 Item.val.int64 != 98)
1194 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001195
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001196 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001197 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001198 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001199 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001201 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301202 Item.uDataAlloc ||
1203 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001204 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001205 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001208 return 0;
1209}
1210
1211
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212/*
1213 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001214 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001215 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001216int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001217{
1218 QCBORDecodeContext DCtx;
1219 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001220 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001221
Laurence Lundbladeee851742020-01-08 08:37:05 -08001222 QCBORDecode_Init(&DCtx,
1223 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1224 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001225
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001227 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001228 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001229 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1230 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 return -1;
1232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001233
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001234 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001235 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001236 }
1237 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1238 Item.uDataAlloc ||
1239 Item.uLabelAlloc ||
1240 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001241 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001242 return -2;
1243 }
1244
1245 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001246 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001247 }
1248 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1249 Item.uDataType != QCBOR_TYPE_INT64 ||
1250 Item.val.int64 != 42 ||
1251 Item.uDataAlloc ||
1252 Item.uLabelAlloc) {
1253 return -3;
1254 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001255
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001256 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001257 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 }
1259 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1260 Item.uDataAlloc ||
1261 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001262 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001263 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1264 return -4;
1265 }
1266
1267 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001268 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001269 }
1270 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1271 Item.uDataAlloc ||
1272 Item.uLabelAlloc ||
1273 Item.uDataType != QCBOR_TYPE_ARRAY ||
1274 Item.val.uCount != 2) {
1275 return -5;
1276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001277
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001278 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001279 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001280 }
1281 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1282 Item.val.string.len != 7 ||
1283 Item.uDataAlloc ||
1284 Item.uLabelAlloc ||
1285 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1286 return -6;
1287 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001288
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001289 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001290 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001291 }
1292 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1293 Item.uDataAlloc ||
1294 Item.uLabelAlloc ||
1295 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1296 return -7;
1297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001298
1299
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001300 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001301 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001302 }
1303 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1304 Item.uDataAlloc ||
1305 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001306 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001307 return -8;
1308 }
1309
1310 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001311 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001312 }
1313 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1314 Item.uDataAlloc ||
1315 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001316 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1317 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001318 return -9;
1319 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001320
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001321 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001322 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001323 }
1324 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001325 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001326 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1327 Item.uDataAlloc ||
1328 Item.uLabelAlloc) {
1329 return -10;
1330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001331
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001332 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001333 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001334 }
1335 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1336 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1337 Item.uDataAlloc ||
1338 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001339 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001340 return -11;
1341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001342
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001343 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001344 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001345 }
1346 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001347 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001348 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1349 Item.uDataAlloc ||
1350 Item.uLabelAlloc) {
1351 return -12;
1352 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001353
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001354 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001355 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001356 }
1357 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1358 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1359 Item.uDataAlloc ||
1360 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001361 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001362 return -13;
1363 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001364
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001366 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001367 }
1368 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1369 Item.uDataAlloc ||
1370 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001371 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001372 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1373 return -14;
1374 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001375
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001376 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001377 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001378 }
1379 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1380 Item.uDataAlloc ||
1381 Item.uLabelAlloc ||
1382 Item.uDataType != QCBOR_TYPE_INT64 ||
1383 Item.val.int64 != 98) {
1384 return -15;
1385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001386
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001387 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001388 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001389 }
1390 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001391 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001392 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1393 Item.uDataAlloc ||
1394 Item.uLabelAlloc) {
1395 return -16;
1396 }
1397
1398 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001399 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 }
1401 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1402 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1403 Item.uDataAlloc ||
1404 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001405 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001406 return -17;
1407 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001408
1409
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001410 /*
1411 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1412 map that when interpreted as an array will be too many. Test
1413 data just has the start of the map, not all the items in the map.
1414 */
1415 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001416
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001417 QCBORDecode_Init(&DCtx,
1418 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1419 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001420
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001421 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001422 return -50;
1423 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001424
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001425 // TODO: test decoding of labels that are arrays or such
1426 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1427
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001428 return 0;
1429}
1430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001431
1432/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301433 Fully or partially decode pValidMapEncoded. When
1434 partially decoding check for the right error code.
1435 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001436
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301437 The partial decodes test error conditions of
1438 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001439
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301440 This could be combined with the above test
1441 and made prettier and maybe a little more
1442 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001443 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001444static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001445{
1446 QCBORDecodeContext DCtx;
1447 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001448 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001449
Laurence Lundbladeee851742020-01-08 08:37:05 -08001450 QCBORDecode_Init(&DCtx,
1451 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1452 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001453
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001454 if(nLevel < 1) {
1455 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1456 return -1;
1457 } else {
1458 return 0;
1459 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001460 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301461
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001462
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001463 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001464 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001465 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001466 if(Item.uDataType != QCBOR_TYPE_MAP ||
1467 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001468 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001469
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001470 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001471 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1472 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001473 } else {
1474 return 0;
1475 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001477
1478
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001479 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001480 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001481 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001482 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001483 Item.uDataType != QCBOR_TYPE_INT64 ||
1484 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001485 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001486 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001487 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001488
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001489 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001490 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1491 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001492 } else {
1493 return 0;
1494 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001495 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001496
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001497 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001498 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001499 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001500 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001501 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001502 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001503 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001504 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001505 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001506
1507
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001508 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001509 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1510 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001511 } else {
1512 return 0;
1513 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001515
1516
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001517 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001518 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001519 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001520 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001521 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001522 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001523 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001525 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001526 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1527 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001528 } else {
1529 return 0;
1530 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001531 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001532
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001533 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001534 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001535 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001536 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001537 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001538 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001539 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001540
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001541 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001542 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1543 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001544 } else {
1545 return 0;
1546 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001547 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001548
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001549 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001550 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001551 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001552 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001553 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554 Item.uDataType != QCBOR_TYPE_MAP ||
1555 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001556 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001557
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001558 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001559 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1560 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001561 } else {
1562 return 0;
1563 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001564 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001565
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001566 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001567 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001568 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001570 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001571 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001572 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001573 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001574 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001575
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001576 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001577 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1578 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001579 } else {
1580 return 0;
1581 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001583
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001584 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001585 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001586 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001588 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001590 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001591 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001592 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001593
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001594 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001595 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1596 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001597 } else {
1598 return 0;
1599 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001600 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001601
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001602 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001603 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001604 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001605 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001606 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001607 Item.uDataType != QCBOR_TYPE_INT64 ||
1608 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001609 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001610
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001611 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001612 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1613 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001614 } else {
1615 return 0;
1616 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001617 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001618
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001619 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001620 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001621 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001622 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001623 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001624 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001625 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001626 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001627 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001628
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301629 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001630 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001631 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001632
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001633 return 0;
1634}
1635
1636
1637
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001638
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001639int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001640{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001641 // Parse a moderatly complex map structure very thoroughly
1642 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1643 if(nResult) {
1644 return nResult;
1645 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001646
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001647 // Again, but in strings-only mode. It should succeed since the input
1648 // map has only string labels.
1649 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1650 if(nResult) {
1651 return nResult;
1652 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001653
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001654 // Again, but try to finish the decoding before the end of the
1655 // input at 10 different place and see that the right error code
1656 // is returned.
1657 for(int i = 0; i < 10; i++) {
1658 nResult = ExtraBytesTest(i);
1659 if(nResult) {
1660 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001661 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001662 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001663
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001664 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001665}
1666
1667
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001668/* The simple-values including some not well formed */
1669static const uint8_t spSimpleValues[] = {
1670 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
1671 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
1672 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001673
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001674int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001675{
1676 QCBORDecodeContext DCtx;
1677 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001678 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001679
1680
Laurence Lundbladeee851742020-01-08 08:37:05 -08001681 QCBORDecode_Init(&DCtx,
1682 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1683 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001684
1685
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001686 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001687 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001688 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1689 Item.val.uCount != 10)
1690 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001691
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001692 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001693 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001694 if(Item.uDataType != QCBOR_TYPE_FALSE)
1695 return -1;
1696
1697 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001698 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001699 if(Item.uDataType != QCBOR_TYPE_TRUE)
1700 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001701
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001702 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001703 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001704 if(Item.uDataType != QCBOR_TYPE_NULL)
1705 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001706
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001707 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001708 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001709 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1710 return -1;
1711
1712 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001713 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001714 return -1;
1715
1716 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001717 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001718 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1719 return -1;
1720
1721 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001722 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001723 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1724 return -1;
1725
Laurence Lundblade077475f2019-04-26 09:06:33 -07001726 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001727 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001728
Laurence Lundblade077475f2019-04-26 09:06:33 -07001729 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001730 return -1;
1731
Laurence Lundblade077475f2019-04-26 09:06:33 -07001732 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001733 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001734
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001735 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001736 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001737 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1738 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001739
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001740 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001741 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001742 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1743 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001744
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001745 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001746
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001747}
1748
1749
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001750int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001751{
1752 // Loop over all the not-well-formed instance of CBOR
1753 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001754 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
1755 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001756 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1757 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1758 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1759
Laurence Lundbladeee851742020-01-08 08:37:05 -08001760 // Set up decoder context. String allocator needed for indefinite
1761 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001762 QCBORDecodeContext DCtx;
1763 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001764#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1766 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001767#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001768
1769 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001770 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001771 do {
1772 QCBORItem Item;
1773
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001774 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1775 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001776
1777 // Every test vector must fail with
1778 // a not-well-formed error. If not
1779 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001780 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001781 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001782 /* Return index of failure and QCBOR error in the result */
1783 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001784 }
1785 }
1786 return 0;
1787}
1788
1789
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001790// 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 +08001791struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001792 UsefulBufC Input;
1793 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001794};
1795
Laurence Lundblade59289e52019-12-30 13:44:37 -08001796
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001797static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001798{
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001799 for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001800 QCBORDecodeContext DCtx;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001801 QCBORError uCBORError;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001802
Laurence Lundblade59289e52019-12-30 13:44:37 -08001803 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001804
1805#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001806 // Set up the decoding context including a memory pool so that
1807 // indefinite length items can be checked
Laurence Lundblade59289e52019-12-30 13:44:37 -08001808 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001809
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001810 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1811 if(uCBORError) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001812 return -9;
1813 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001814#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1815
Laurence Lundbladecf41c522021-02-20 10:19:07 -07001816
Laurence Lundblade59289e52019-12-30 13:44:37 -08001817 // Iterate until there is an error of some sort error
1818 QCBORItem Item;
1819 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001820 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001821 memset(&Item, 0x33, sizeof(Item));
1822
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001823 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1824 } while(uCBORError == QCBOR_SUCCESS);
1825
1826
Laurence Lundblade59289e52019-12-30 13:44:37 -08001827
1828 // Must get the expected error or the this test fails
1829 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001830 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001831 Item.uDataType != QCBOR_TYPE_NONE ||
1832 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001833 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001834 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001835 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001836 }
1837 }
1838
1839 return 0;
1840}
1841
1842
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001843static const struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001844 // Most of this is copied from not_well_formed.h. Here the error code
1845 // returned is also checked.
1846
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001847#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001848 // Indefinite length strings must be closed off
1849 // An indefinite length byte string not closed off
1850 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1851 // An indefinite length text string not closed off
1852 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1853
1854
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001855 // All the chunks in an indefinite length string must be of the type of
1856 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001857 // indefinite length byte string with text string chunk
1858 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1859 // indefinite length text string with a byte string chunk
1860 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1861 // indefinite length byte string with an positive integer chunk
1862 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1863 // indefinite length byte string with an negative integer chunk
1864 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1865 // indefinite length byte string with an array chunk
1866 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1867 // indefinite length byte string with an map chunk
1868 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1869 // indefinite length byte string with tagged integer chunk
1870 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1871 // indefinite length byte string with an simple type chunk
1872 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1873 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1874 // indefinite length text string with indefinite string inside
1875 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1876
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001877#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1878
1879 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1880 // An indefinite length text string not closed off
1881 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1882
1883
1884 // All the chunks in an indefinite length string must be of the type of
1885 // indefinite length string
1886 // indefinite length byte string with text string chunk
1887 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1888 // indefinite length text string with a byte string chunk
1889 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1890 // indefinite length byte string with an positive integer chunk
1891 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1892 // indefinite length byte string with an negative integer chunk
1893 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1894 // indefinite length byte string with an array chunk
1895 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1896 // indefinite length byte string with an map chunk
1897 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1898 // indefinite length byte string with tagged integer chunk
1899 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1900 // indefinite length byte string with an simple type chunk
1901 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1902 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1903 // indefinite length text string with indefinite string inside
1904 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1905#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1906
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001907
1908 // Definte length maps and arrays must be closed by having the right number of items
1909 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001910 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001911 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001912 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001913 // A definte length array that is supposed to have 511 items, but has only 1
1914 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1915 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001916 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001917 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001918 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001919
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001920#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001921 // Indefinte length maps and arrays must be ended by a break
1922 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001923 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001924 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001925 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001926 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001927 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001928 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001929 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001930
1931
1932 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001933 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001934 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001935 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001936 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001937 // Unclosed indefinite map containing a closed definite length array
1938 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1939 // Definite length map containing an unclosed indefinite length array
1940 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001941 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001942 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001943 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001944 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001945 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001946 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001947 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001948 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001949 // Unclosed indefinite length map in definite length maps
1950 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1951 QCBOR_ERR_NO_MORE_ITEMS},
1952 // Unclosed definite length map in indefinite length maps
1953 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1954 // Unclosed indefinite length array in definite length maps
1955 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1956 QCBOR_ERR_NO_MORE_ITEMS},
1957 // Unclosed definite length array in indefinite length maps
1958 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1959 // Unclosed indefinite length map in definite length arrays
1960 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1961 // Unclosed definite length map in indefinite length arrays
1962 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001963#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001964
1965 // The "argument" for the data item is incomplete
1966 // Positive integer missing 1 byte argument
1967 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1968 // Positive integer missing 2 byte argument
1969 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1970 // Positive integer missing 4 byte argument
1971 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1972 // Positive integer missing 8 byte argument
1973 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1974 // Positive integer missing 1 byte of 2 byte argument
1975 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1976 // Positive integer missing 2 bytes of 4 byte argument
1977 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1978 // Positive integer missing 1 bytes of 7 byte argument
1979 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1980 // Negative integer missing 1 byte argument
1981 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1982 // Binary string missing 1 byte argument
1983 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1984 // Text string missing 1 byte argument
1985 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1986 // Array missing 1 byte argument
1987 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1988 // Map missing 1 byte argument
1989 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1990 // Tag missing 1 byte argument
1991 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1992 // Simple missing 1 byte argument
1993 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001994 // half-precision with 1 byte argument
1995 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1996 // single-precision with 2 byte argument
1997 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1998 // double-precision with 3 byte argument
1999 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
2000
2001
2002 // Tag with no content
2003 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002004
2005
2006 // Breaks must not occur in definite length arrays and maps
2007 // Array of length 1 with sole member replaced by a break
2008 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2009 // Array of length 2 with 2nd member replaced by a break
2010 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2011 // Map of length 1 with sole member label replaced by a break
2012 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2013 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002014 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002015 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
2016 // Array of length 1 with 2nd member value replaced by a break
2017 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2018 // Map of length 2 with 2nd member replaced by a break
2019 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
2020
2021
2022 // Breaks must not occur on their own out of an indefinite length data item
2023 // A bare break is not well formed
2024 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
2025 // A bare break after a zero length definite length array
2026 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002027#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002028 // A bare break after a zero length indefinite length map
2029 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002030 // A break inside a definite length array inside an indefenite length array
2031 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2032 // Complicated mixed nesting with break outside indefinite length array
2033 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002034#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002035
2036
2037 // Forbidden two byte encodings of simple types
2038 // Must use 0xe0 instead
2039 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2040 // Should use 0xe1 instead
2041 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2042 // Should use 0xe2 instead
2043 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2044 // Should use 0xe3 instead
2045 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2046 // Should use 0xe4 instead
2047 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2048 // Should use 0xe5 instead
2049 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2050 // Should use 0xe6 instead
2051 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2052 // Should use 0xe7 instead
2053 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2054 // Should use 0xe8 instead
2055 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2056 // Should use 0xe9 instead
2057 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2058 // Should use 0xea instead
2059 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2060 // Should use 0xeb instead
2061 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2062 // Should use 0xec instead
2063 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2064 // Should use 0xed instead
2065 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2066 // Should use 0xee instead
2067 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2068 // Should use 0xef instead
2069 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2070 // Should use 0xf0 instead
2071 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2072 // Should use 0xf1 instead
2073 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2074 // Should use 0xf2 instead
2075 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2076 // Must use 0xf3 instead
2077 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2078 // Must use 0xf4 instead
2079 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2080 // Must use 0xf5 instead
2081 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2082 // Must use 0xf6 instead
2083 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2084 // Must use 0xf7 instead
2085 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2086 // Must use 0xf8 instead
2087 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002088 // Reserved
2089 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002090
2091 // Integers with additional info indefinite length
2092 // Positive integer with additional info indefinite length
2093 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
2094 // Negative integer with additional info indefinite length
2095 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
2096 // CBOR tag with "argument" an indefinite length
2097 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
2098 // CBOR tag with "argument" an indefinite length alternate vector
2099 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
2100
2101
2102 // Missing bytes from a deterministic length string
2103 // A byte string is of length 1 without the 1 byte
2104 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
2105 // A text string is of length 1 without the 1 byte
2106 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002107
2108#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08002109 // Byte string should have 2^32-15 bytes, but has one
2110 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2111 // Byte string should have 2^32-15 bytes, but has one
2112 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002113 // Byte string should have 2^64 bytes, but has 3
2114 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2115 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2116 // Text string should have 2^64 bytes, but has 3
2117 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2118 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002119#else
2120 // Byte string should have 2^32-15 bytes, but has one
2121 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2122 // Byte string should have 2^32-15 bytes, but has one
2123 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2124 // Byte string should have 2^16 bytes, but has 3
2125 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2126 // Text string should have 2^64 bytes, but has 3
2127 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2128#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002129
2130 // Use of unassigned additional information values
2131 // Major type positive integer with reserved value 28
2132 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2133 // Major type positive integer with reserved value 29
2134 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2135 // Major type positive integer with reserved value 30
2136 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2137 // Major type negative integer with reserved value 28
2138 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2139 // Major type negative integer with reserved value 29
2140 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2141 // Major type negative integer with reserved value 30
2142 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2143 // Major type byte string with reserved value 28 length
2144 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2145 // Major type byte string with reserved value 29 length
2146 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2147 // Major type byte string with reserved value 30 length
2148 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2149 // Major type text string with reserved value 28 length
2150 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2151 // Major type text string with reserved value 29 length
2152 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2153 // Major type text string with reserved value 30 length
2154 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2155 // Major type array with reserved value 28 length
2156 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2157 // Major type array with reserved value 29 length
2158 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2159 // Major type array with reserved value 30 length
2160 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2161 // Major type map with reserved value 28 length
2162 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2163 // Major type map with reserved value 29 length
2164 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2165 // Major type map with reserved value 30 length
2166 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2167 // Major type tag with reserved value 28 length
2168 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2169 // Major type tag with reserved value 29 length
2170 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2171 // Major type tag with reserved value 30 length
2172 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2173 // Major type simple with reserved value 28 length
2174 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2175 // Major type simple with reserved value 29 length
2176 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2177 // Major type simple with reserved value 30 length
2178 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2179
2180
2181 // Maps must have an even number of data items (key & value)
2182 // Map with 1 item when it should have 2
2183 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2184 // Map with 3 item when it should have 4
2185 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002186#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002187 // Map with 1 item when it should have 2
2188 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2189 // Map with 3 item when it should have 4
2190 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002191#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002192
2193
2194 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002195 // Text-based date, with an integer
2196 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2197 // Epoch date, with an byte string
2198 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2199 // tagged as both epoch and string dates
2200 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2201 // big num tagged an int, not a byte string
2202 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002203};
2204
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002205int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002206{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002207 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002208
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002209 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002210 if(nResult) {
2211 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002213
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002214 // Corrupt the UsefulInputBuf and see that
2215 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002216 QCBORDecodeContext DCtx;
2217 QCBORItem Item;
2218 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002219
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002220 QCBORDecode_Init(&DCtx,
2221 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2222 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002223
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002224 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2225 return (int32_t)uQCBORError;
2226 }
2227 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2228 // This wasn't supposed to happen
2229 return -1;
2230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002232 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002233
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002234 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2235 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2236 // Did not get back the error expected
2237 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002239
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002240
Laurence Lundblade98427e92020-09-28 21:33:23 -07002241 /*
2242 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2243 tests here can be performed to see that the max length
2244 error check works correctly. See DecodeBytes(). If the max
2245 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002246
Laurence Lundblade98427e92020-09-28 21:33:23 -07002247 This test will automatocally adapt the all CPU sizes
2248 through the use of SIZE_MAX.
2249 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002250
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002251 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002252 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002253
Laurence Lundblade98427e92020-09-28 21:33:23 -07002254 // This makes a CBOR head with a text string that is very long
2255 // but doesn't fill in the bytes of the text string as that is
2256 // not needed to test this part of QCBOR.
2257 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2258
2259 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2260
2261 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2262 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002263 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002264
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002265 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002266}
2267
2268
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002269/* Try all 256 values of the byte at nLen including recursing for
2270 each of the values to try values at nLen+1 ... up to nLenMax
2271 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002272static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002273{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002274 if(nLen >= nLenMax) {
2275 return;
2276 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002277
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002278 for(int inputByte = 0; inputByte < 256; inputByte++) {
2279 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002280 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002281 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002282
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002283 // Get ready to parse
2284 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002285 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002286
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002287 // Parse by getting the next item until an error occurs
2288 // Just about every possible decoder error can occur here
2289 // The goal of this test is not to check for the correct
2290 // error since that is not really possible. It is to
2291 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002292 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002293 QCBORItem Item;
2294 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002295 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002296 break;
2297 }
2298 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002299
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002300 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002301 }
2302}
2303
2304
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002305int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002306{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002307 // Size 2 tests 64K inputs and runs quickly
2308 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002310 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002311
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002312 return 0;
2313}
2314
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002315
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002316int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002317{
2318 // size 3 tests 16 million inputs and runs OK
2319 // in seconds on fast machines. Size 4 takes
2320 // 10+ minutes and 5 half a day on fast
2321 // machines. This test is kept separate from
2322 // the others so as to no slow down the use
2323 // of them as a very frequent regression.
2324 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002325
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002326 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002327
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002328 return 0;
2329}
2330
2331
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002332static const uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002333 0xc0, // tag for string date
2334 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002335
Laurence Lundbladec7114722020-08-13 05:11:40 -07002336 0xc0, // tag for string date
2337 0x00, // Wrong type for a string date
2338
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002339 0xc1, // tag for epoch date
2340 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2341
Laurence Lundbladec7114722020-08-13 05:11:40 -07002342 0xc1,
2343 0x62, 'h', 'i', // wrong type tagged
2344
Laurence Lundblade99615302020-11-29 11:19:47 -08002345 // CBOR_TAG_ENC_AS_B64
2346 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002347 0x1a, 0x53, 0x72, 0x4E, 0x01,
2348
2349 0xc1, // tag for epoch date
2350 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002351
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002352 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002353 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002355 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002356 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002357
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002358 0xc1, // tag for epoch date
2359 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2360 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2361
2362 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002363 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2364
2365 0xc1, // tag for epoch date
2366 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2367
2368 0xc1,
2369 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2370
2371 0xc1, // tag for epoch date
2372 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002373};
2374
2375
Laurence Lundbladec7114722020-08-13 05:11:40 -07002376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002377// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002378#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002379static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002381 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002382
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002383 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002384
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002385 return diff > 0.0000001;
2386}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002387#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002388
2389
Laurence Lundblade99615302020-11-29 11:19:47 -08002390
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002391int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002392{
2393 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002394 QCBORItem Item;
2395 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002396
Laurence Lundbladeee851742020-01-08 08:37:05 -08002397 QCBORDecode_Init(&DCtx,
2398 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2399 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002400
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002401 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002402 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002403 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002404 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002405 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002406 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002407 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002408 }
2409
Laurence Lundbladec7114722020-08-13 05:11:40 -07002410 // Wrong type for a string date
2411 uError = QCBORDecode_GetNext(&DCtx, &Item);
2412 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002413 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002414 }
2415
2416 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2417 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2418 return -4;
2419 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002420 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2421 Item.val.epochDate.nSeconds != 1400000000 ||
2422 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002423 return -5;
2424 }
2425
2426 // Wrong type for an epoch date
2427 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2428 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002429 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002430
Laurence Lundblade99615302020-11-29 11:19:47 -08002431 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2432 // The date is decoded and the two tags are returned. This is to
2433 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002434 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2435 return -7;
2436 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002437 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2438 Item.val.epochDate.nSeconds != 1400000001 ||
2439 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundblade99615302020-11-29 11:19:47 -08002440 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002441 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002443
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002444 // Epoch date that is too large for our representation
2445 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002446 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002447 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002448
Laurence Lundblade9682a532020-06-06 18:33:04 -07002449#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002450 // Epoch date in float format with fractional seconds
2451 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2452 return -10;
2453 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002454 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2455 Item.val.epochDate.nSeconds != 1 ||
2456 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002457 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002458 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002459
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002460 // Epoch date float that is too large for our representation
2461 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002462 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002464
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002465 // Epoch date double that is just slightly too large
2466 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002467 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002468 }
2469
2470 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002471 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2472 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2473 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2474 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002475 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002476 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002477
2478 // Nan
2479 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2480 return -15;
2481 }
2482
2483 // +Inifinity double-precision
2484 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2485 return -16;
2486 }
2487
2488#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2489 // -Inifinity half-precision
2490 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2491 return -17;
2492 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002493#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002494 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2495 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002496 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002497#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002498
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002499#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002500 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2501 return -19;
2502 }
2503 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2504 return -20;
2505 }
2506 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2507 return -21;
2508 }
2509 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2510 return -22;
2511 }
2512 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2513 return -23;
2514 }
2515 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2516 return -24;
2517 }
2518#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2519 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2520 return -25;
2521 }
2522#else
2523 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2524 return -26;
2525 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002526#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002527
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002528#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002529
2530 return 0;
2531}
2532
Laurence Lundblade4b270642020-08-14 12:53:07 -07002533/*
2534 Test cases covered here. Some items cover more than one of these.
2535 positive integer (zero counts as a positive integer)
2536 negative integer
2537 half-precision float
2538 single-precision float
2539 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002540
Laurence Lundblade4b270642020-08-14 12:53:07 -07002541 float Overflow error
2542 Wrong type error for epoch
2543 Wrong type error for date string
2544 float disabled error
2545 half-precision disabled error
2546 -Infinity
2547 Slightly too large integer
2548 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002549
Laurence Lundblade4b270642020-08-14 12:53:07 -07002550 Get epoch by int
2551 Get string by int
2552 Get epoch by string
2553 Get string by string
2554 Fail to get epoch by wrong int label
2555 Fail to get string by wrong string label
2556 Fail to get epoch by string because it is invalid
2557 Fail to get epoch by int because it is invalid
2558
2559 Untagged values
2560 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002561static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002562 0x86,
2563
2564 0xc1,
2565 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2566
Laurence Lundbladec7114722020-08-13 05:11:40 -07002567 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002568 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2569
2570 0xc1, // tag for epoch date
2571 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2572
2573 0xc1, // tag for epoch date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002574 0x80, // Erroneous empty array as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002575
2576 0xc0, // tag for string date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002577 0xa0, // Erroneous empty map as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002578
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002579 0xa9, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002580
2581 0x00,
2582 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002583 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002584
2585 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002586 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002587 0xc1, // tag for epoch date
2588 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2589
2590 // Untagged integer 0
2591 0x08,
2592 0x00,
2593
2594 // Utagged date string with string label y
2595 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002596 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002597
2598 // Untagged -1000 with label z
2599 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002600 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002601 0x39, 0x03, 0xe7,
2602
Laurence Lundbladec7114722020-08-13 05:11:40 -07002603 0x07,
2604 0xc1, // tag for epoch date
2605 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2606
Laurence Lundblade4b270642020-08-14 12:53:07 -07002607 0x05,
2608 0xc1,
2609 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2610
Laurence Lundbladec7114722020-08-13 05:11:40 -07002611 // Untagged single-precision float with value 3.14 with string label x
2612 0x61, 0x78,
2613 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2614
Laurence Lundbladec7114722020-08-13 05:11:40 -07002615 // Untagged half-precision float with value -2
2616 0x09,
2617 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002618};
2619
2620int32_t SpiffyDateDecodeTest()
2621{
2622 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002623 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002624 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2625 nEpochDate4, nEpochDate6, nEpochDateFail,
2626 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002627 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002628 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002629
2630 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002631 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002632 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002633 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002634
Laurence Lundblade9b334962020-08-27 10:55:53 -07002635 // Too-negative float, -9.2233720368547748E+18
2636 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002637 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002638#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002639 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2640 return 1111;
2641 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002642#else
2643 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2644 return 1112;
2645 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002646#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade4b270642020-08-14 12:53:07 -07002647
2648 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002649 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002650 uError = QCBORDecode_GetAndResetError(&DC);
2651 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002652 return 1;
2653 }
2654
Laurence Lundblade4b270642020-08-14 12:53:07 -07002655 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002656 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002657 uError = QCBORDecode_GetAndResetError(&DC);
2658#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2659#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2660 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2661#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2662 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2663#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2664#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2665 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2666#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2667 if(uError != uExpectedforHalfMinusInfinity) {
2668 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002669 }
2670
Laurence Lundblade4b270642020-08-14 12:53:07 -07002671 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002672 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002673 uError = QCBORDecode_GetAndResetError(&DC);
2674 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2675 return 3;
2676 }
2677
2678 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002679 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002680 uError = QCBORDecode_GetAndResetError(&DC);
2681 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2682 return 4;
2683 }
2684
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002685 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002686
2687 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002688 QCBORDecode_GetEpochDateInMapN(&DC,
2689 5,
2690 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2691 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2692 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002693#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2694 if(nEpochDate2 != -9223372036854773760LL) {
2695 return 101;
2696 }
2697#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2698 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002699 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002700 return 102;
2701 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002702#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002703
Laurence Lundblade4b270642020-08-14 12:53:07 -07002704 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002705 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2706 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002707#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2708 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002709 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002710 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002711#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2712 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002713 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002714 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002715 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002716#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2717
2718 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002719 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2720 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002721#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2722 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002723 return 103;
2724 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002725#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2726 uError = QCBORDecode_GetAndResetError(&DC);
2727 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2728 return 104;
2729 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002730#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2731
Laurence Lundblade9b334962020-08-27 10:55:53 -07002732 // A half-precision date with value -2 FFF
2733 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2734 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002735#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2736 if(nEpochDate4 != -2) {
2737 return 105;
2738 }
2739#else
2740 uError = QCBORDecode_GetAndResetError(&DC);
2741 if(uError == QCBOR_SUCCESS) {
2742 return 106;
2743 }
2744#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002745
Laurence Lundblade4b270642020-08-14 12:53:07 -07002746
2747 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002748 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2749 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2750 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002751 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002752 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002753 return 107;
2754 }
2755
2756 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002757 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2758 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002759 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002760 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002761 return 108;
2762 }
2763
2764 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002765 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2766 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2767 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002768 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002769 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002770 return 109;
2771 }
2772
2773 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002774 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2775 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002776 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002777 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002778 return 110;
2779 }
2780
2781 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002782
Laurence Lundblade4b270642020-08-14 12:53:07 -07002783 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002784 QCBORDecode_GetEpochDateInMapN(&DC,
2785 1,
2786 QCBOR_TAG_REQUIREMENT_TAG |
2787 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2788 &nEpochDate1400000000);
2789 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002790 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002791 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2792 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002793 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002794 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2795 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002796 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002797 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2798 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002799 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002800 QCBORDecode_GetEpochDateInMapSZ(&DC,
2801 "z",
2802 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2803 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2804 &nEpochDate6);
2805 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002806
2807 QCBORDecode_ExitMap(&DC);
2808 QCBORDecode_ExitArray(&DC);
2809 uError = QCBORDecode_Finish(&DC);
2810 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002811 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002812 }
2813
Laurence Lundblade9b334962020-08-27 10:55:53 -07002814 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002815 return 200;
2816 }
2817
Laurence Lundblade9b334962020-08-27 10:55:53 -07002818 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002819 return 201;
2820 }
2821
Laurence Lundblade9b334962020-08-27 10:55:53 -07002822 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002823 return 202;
2824 }
2825
Laurence Lundblade9b334962020-08-27 10:55:53 -07002826 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002827 return 203;
2828 }
2829
Laurence Lundblade9b334962020-08-27 10:55:53 -07002830 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002831 return 204;
2832 }
2833
Laurence Lundblade9b334962020-08-27 10:55:53 -07002834 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2835 return 205;
2836 }
2837
2838 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2839 return 206;
2840 }
2841
Laurence Lundbladec7114722020-08-13 05:11:40 -07002842 return 0;
2843}
2844
2845
2846
Laurence Lundblade9b334962020-08-27 10:55:53 -07002847// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002848static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002849 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002850 0x81, // Array of one
2851 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2852 0x82, // Array of two that is the faction 1/3
2853 0x01,
2854 0x03,
2855
2856 /*
2857 More than 4 tags on an item 225(226(227(228(229([])))))
2858 */
2859 0xd8, 0xe1,
2860 0xd8, 0xe2,
2861 0xd8, 0xe3,
2862 0xd8, 0xe4,
2863 0xd8, 0xe5,
2864 0x80,
2865
2866 /* tag 10489608748473423768(
2867 2442302356(
2868 21590(
2869 240(
2870 []))))
2871 */
2872 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2873 0xda, 0x91, 0x92, 0x93, 0x94,
2874 0xd9, 0x54, 0x56,
2875 0xd8, 0xf0,
2876 0x80,
2877
2878 /* tag 21590(
2879 10489608748473423768(
2880 2442302357(
2881 65534(
2882 []))))
2883 */
2884 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2885 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2886 0xda, 0x91, 0x92, 0x93, 0x95,
2887 0xd9, 0xff, 0xfe,
2888 0x80,
2889
2890 /* Make sure to blow past the limit of tags that must be mapped.
2891 works in conjuntion with entries above.
2892 269488144(269488145(269488146(269488147([]))))
2893 */
2894 0xda, 0x10, 0x10, 0x10, 0x10,
2895 0xda, 0x10, 0x10, 0x10, 0x11,
2896 0xda, 0x10, 0x10, 0x10, 0x12,
2897 0xda, 0x10, 0x10, 0x10, 0x13,
2898 0x80,
2899
2900 /* An invalid decimal fraction with an additional tag */
2901 0xd9, 0xff, 0xfa,
2902 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2903 0x00, // the integer 0; should be a byte string
2904};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002905
Laurence Lundblade59289e52019-12-30 13:44:37 -08002906/*
2907 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002908 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002909 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002910static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002911 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002912
Laurence Lundblade59289e52019-12-30 13:44:37 -08002913/*
2914DB 9192939495969798 # tag(10489608748473423768)
2915 D8 88 # tag(136)
2916 C6 # tag(6)
2917 C7 # tag(7)
2918 80 # array(0)
2919*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002920static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002921 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002922
2923/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002924 55799(55799(55799({
2925 6(7(-23)): 5859837686836516696(7({
2926 7(-20): 11({
2927 17(-18): 17(17(17("Organization"))),
2928 9(-17): 773("SSG"),
2929 -15: 16(17(6(7("Confusion")))),
2930 17(-16): 17("San Diego"),
2931 17(-14): 17("US")
2932 }),
2933 23(-19): 19({
2934 -11: 9({
2935 -9: -7
2936 }),
2937 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2938 })
2939 })),
2940 16(-22): 23({
2941 11(8(7(-5))): 8(-3)
2942 })
2943 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002944 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002945static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002946 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2947 0xc6, 0xc7, 0x36,
2948 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2949 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2950 0xcb, 0xa5,
2951 0xd1, 0x31,
2952 0xd1, 0xd1, 0xd1, 0x6c,
2953 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2954 0xc9, 0x30,
2955 0xd9, 0x03, 0x05, 0x63,
2956 0x53, 0x53, 0x47,
2957 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002958 0xd0, 0xd1, 0xc6, 0xc7,
2959 0x69,
2960 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002961 0xd1, 0x2f,
2962 0xd1, 0x69,
2963 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2964 0xd1, 0x2d,
2965 0xd1, 0x62,
2966 0x55, 0x53,
2967 0xd7, 0x32,
2968 0xd3, 0xa2,
2969 0x2a,
2970 0xc9, 0xa1,
2971 0x28,
2972 0x26,
2973 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2974 0xcc, 0x4a,
2975 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2976 0xd0, 0x35,
2977 0xd7, 0xa1,
2978 0xcb, 0xc8, 0xc7, 0x24,
2979 0xc8, 0x22};
2980
Laurence Lundblade9b334962020-08-27 10:55:53 -07002981
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002982static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002983 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07002984
2985 0xc0, // tag for string date
2986 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2987
2988 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2989
2990 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2991
2992 0xd8, 0x23, // tag for regex
2993 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2994
2995 0xc0, // tag for string date
2996 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002997};
2998
2999
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003000static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003001
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003002
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003003int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003004{
3005 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003006 QCBORItem Item;
3007 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003008
Laurence Lundbladeee851742020-01-08 08:37:05 -08003009 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003010 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003011 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003012
Laurence Lundblade9b334962020-08-27 10:55:53 -07003013 /*
3014 This test matches the magic number tag and the fraction tag
3015 55799([...])
3016 */
3017 uError = QCBORDecode_GetNext(&DCtx, &Item);
3018 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003019 return -2;
3020 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003021 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003022 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3023 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003025
Laurence Lundblade9b334962020-08-27 10:55:53 -07003026 /*
3027 4([1,3])
3028 */
3029 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003030#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003031 if(uError != QCBOR_SUCCESS ||
3032 Item.uDataType != QCBOR_TYPE_ARRAY ||
3033 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3034 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3035 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3036 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3037 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3038 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3039 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003040 return -4;
3041 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003042 // consume the items in the array
3043 uError = QCBORDecode_GetNext(&DCtx, &Item);
3044 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003045
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003046#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003047 if(uError != QCBOR_SUCCESS ||
3048 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3049 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3050 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3051 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3052 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3053 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3054 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003055 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003056#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003057
Laurence Lundblade9b334962020-08-27 10:55:53 -07003058 /*
3059 More than 4 tags on an item 225(226(227(228(229([])))))
3060 */
3061 uError = QCBORDecode_GetNext(&DCtx, &Item);
3062 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003063 return -6;
3064 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003065
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003066 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3067 return -106;
3068 }
3069
3070
Laurence Lundblade9b334962020-08-27 10:55:53 -07003071 /* tag 10489608748473423768(
3072 2442302356(
3073 21590(
3074 240(
3075 []))))
3076 */
3077 uError = QCBORDecode_GetNext(&DCtx, &Item);
3078 if(uError != QCBOR_SUCCESS ||
3079 Item.uDataType != QCBOR_TYPE_ARRAY ||
3080 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3081 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3082 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3083 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003084 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003085 }
3086
3087 /* tag 21590(
3088 10489608748473423768(
3089 2442302357(
3090 21591(
3091 []))))
3092 */
3093 uError = QCBORDecode_GetNext(&DCtx, &Item);
3094 if(uError != QCBOR_SUCCESS ||
3095 Item.uDataType != QCBOR_TYPE_ARRAY ||
3096 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3097 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3098 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3099 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3100 return -8;
3101 }
3102
3103 /* Make sure to blow past the limit of tags that must be mapped.
3104 works in conjuntion with entries above.
3105 269488144(269488145(269488146(269488147([]))))
3106 */
3107 uError = QCBORDecode_GetNext(&DCtx, &Item);
3108 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3109 return -9;
3110 }
3111
3112 uError = QCBORDecode_GetNext(&DCtx, &Item);
3113 if(uError == QCBOR_SUCCESS) {
3114 return -10;
3115 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003116
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003117 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003118 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003119 // tage and then matches it. Caller-config lists are no longer
3120 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003121 QCBORDecode_Init(&DCtx,
3122 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3123 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003124 const uint64_t puList[] = {0x9192939495969798, 257};
3125 const QCBORTagListIn TL = {2, puList};
3126 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003127
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003128 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3129 return -8;
3130 }
3131 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3132 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3133 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3134 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3135 Item.val.uCount != 0) {
3136 return -9;
3137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003138
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003139 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003140 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003141 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003142 const uint64_t puLongList[17] = {1,2,1};
3143 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003144 QCBORDecode_Init(&DCtx,
3145 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3146 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003147 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3148 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3149 return -11;
3150 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003151
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003152 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003153 QCBORTagListOut Out = {0, 4, puTags};
3154
3155
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003156 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003157 QCBORDecode_Init(&DCtx,
3158 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3159 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003160 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3161 return -12;
3162 }
3163 if(puTags[0] != 0x9192939495969798 ||
3164 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003165 puTags[2] != 0x06 ||
3166 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003167 return -13;
3168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003169
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003170 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003171 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003172 QCBORDecode_Init(&DCtx,
3173 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3174 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003175 QCBORTagListOut OutSmall = {0, 3, puTags};
3176 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3177 return -14;
3178 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003179
Laurence Lundblade9b334962020-08-27 10:55:53 -07003180
3181
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003182 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003183 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3184 // It is a bit of a messy test and maybe could be improved, but
3185 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003186 QCBORDecode_Init(&DCtx,
3187 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3188 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003189 int n = CheckCSRMaps(&DCtx);
3190 if(n) {
3191 return n-2000;
3192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003193
Laurence Lundblade59289e52019-12-30 13:44:37 -08003194 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003195 QCBORDecode_Init(&DCtx,
3196 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3197 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003198
Laurence Lundblade9b334962020-08-27 10:55:53 -07003199 /* With the spiffy decode revision, this tag list is not used.
3200 It doesn't matter if a tag is in this list or not so some
3201 tests that couldn't process a tag because it isn't in this list
3202 now can process these unlisted tags. The tests have been
3203 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003204 const uint64_t puTagList[] = {773, 1, 90599561};
3205 const QCBORTagListIn TagList = {3, puTagList};
3206 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003207
3208
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003209 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3210 return -100;
3211 }
3212 if(Item.uDataType != QCBOR_TYPE_MAP ||
3213 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3214 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3215 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3216 Item.val.uCount != 2 ||
3217 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3218 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3219 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3220 Out.uNumUsed != 3) {
3221 return -101;
3222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003223
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003224 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3225 return -102;
3226 }
3227 if(Item.uDataType != QCBOR_TYPE_MAP ||
3228 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3229 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003230 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003231 Item.val.uCount != 2 ||
3232 puTags[0] != 5859837686836516696 ||
3233 puTags[1] != 7 ||
3234 Out.uNumUsed != 2) {
3235 return -103;
3236 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003237
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003238 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3239 return -104;
3240 }
3241 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003242 Item.val.uCount != 5 ||
3243 puTags[0] != 0x0b ||
3244 Out.uNumUsed != 1) {
3245 return -105;
3246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003248 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3249 return -106;
3250 }
3251 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3252 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3253 Item.val.string.len != 12 ||
3254 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3255 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3256 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3257 Out.uNumUsed != 3) {
3258 return -105;
3259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003260
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003261 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3262 return -107;
3263 }
3264 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3265 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3266 Item.val.string.len != 3 ||
3267 puTags[0] != 773 ||
3268 Out.uNumUsed != 1) {
3269 return -108;
3270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003271
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003272 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3273 return -109;
3274 }
3275 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003276 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003277 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003278 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003279 puTags[3] != 7 ||
3280 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003281 return -110;
3282 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003283
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003284 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3285 return -111;
3286 }
3287 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3288 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3289 Item.val.string.len != 9 ||
3290 puTags[0] != 17 ||
3291 Out.uNumUsed != 1) {
3292 return -112;
3293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003294
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003295 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3296 return -111;
3297 }
3298 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3299 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3300 Item.val.string.len != 2 ||
3301 puTags[0] != 17 ||
3302 Out.uNumUsed != 1) {
3303 return -112;
3304 }
3305
3306 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3307 return -113;
3308 }
3309 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003310 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003311 Item.val.uCount != 2 ||
3312 puTags[0] != 19 ||
3313 Out.uNumUsed != 1) {
3314 return -114;
3315 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003316
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003317 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3318 return -115;
3319 }
3320 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003321 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003322 Item.val.uCount != 1 ||
3323 puTags[0] != 9 ||
3324 Out.uNumUsed != 1) {
3325 return -116;
3326 }
3327
3328 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3329 return -116;
3330 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003332 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003333 Out.uNumUsed != 0) {
3334 return -117;
3335 }
3336
3337 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3338 return -118;
3339 }
3340 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3341 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003342 puTags[0] != 12 ||
3343 Out.uNumUsed != 1) {
3344 return -119;
3345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003346
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003347 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3348 return -120;
3349 }
3350 if(Item.uDataType != QCBOR_TYPE_MAP ||
3351 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3352 Item.val.uCount != 1 ||
3353 puTags[0] != 0x17 ||
3354 Out.uNumUsed != 1) {
3355 return -121;
3356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003357
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003358 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3359 return -122;
3360 }
3361 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003362 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003363 Item.val.int64 != -3 ||
3364 puTags[0] != 8 ||
3365 Out.uNumUsed != 1) {
3366 return -123;
3367 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003368
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003369 if(QCBORDecode_Finish(&DCtx)) {
3370 return -124;
3371 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003372
3373 UsefulBufC DateString;
3374 QCBORDecode_Init(&DCtx,
3375 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3376 QCBOR_DECODE_MODE_NORMAL);
3377
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003378 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003379 // tagged date string
3380 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3381 // untagged date string
3382 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3383 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3384 return 100;
3385 }
3386 // untagged byte string
3387 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3388 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3389 return 101;
3390 }
3391 // tagged regex
3392 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3393 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3394 return 102;
3395 }
3396 // tagged date string with a byte string
3397 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3398 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3399 return 103;
3400 }
3401 QCBORDecode_ExitArray(&DCtx);
3402 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3403 return 104;
3404 }
3405
3406
3407 QCBORDecode_Init(&DCtx,
3408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3409 QCBOR_DECODE_MODE_NORMAL);
3410
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003411 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003412 // tagged date string
3413 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3414 // untagged date string
3415 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3416 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3417 return 200;
3418 }
3419 // untagged byte string
3420 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3421 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3422 return 201;
3423 }
3424 // tagged regex
3425 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3426 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3427 return 202;
3428 }
3429 // tagged date string with a byte string
3430 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3431 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3432 return 203;
3433 }
3434 QCBORDecode_ExitArray(&DCtx);
3435 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3436 return 204;
3437 }
3438
3439 QCBORDecode_Init(&DCtx,
3440 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3441 QCBOR_DECODE_MODE_NORMAL);
3442
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003443 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003444 // tagged date string
3445 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3446 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3447 return 300;
3448 }
3449 // untagged date string
3450 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3451 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3452 return 301;
3453 }
3454 // untagged byte string
3455 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3456 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3457 return 302;
3458 }
3459 // tagged regex
3460 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3461 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3462 return 303;
3463 }
3464 // tagged date string with a byte string
3465 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3466 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3467 return 304;
3468 }
3469 QCBORDecode_ExitArray(&DCtx);
3470 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3471 return 305;
3472 }
3473
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003474 return 0;
3475}
3476
3477
3478
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003479
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003480static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003481 0x83,
3482 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3483 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3484 0xA4,
3485 0x63, 0x42, 0x4E, 0x2B,
3486 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3487 0x18, 0x40,
3488 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3489 0x63, 0x42, 0x4E, 0x2D,
3490 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3491 0x38, 0x3F,
3492 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3493
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003494/* The expected big num */
3495static const uint8_t spBigNum[] = {
3496 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3497 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003498
3499
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003500int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003501{
3502 QCBORDecodeContext DCtx;
3503 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003504 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003505
Laurence Lundbladeee851742020-01-08 08:37:05 -08003506 QCBORDecode_Init(&DCtx,
3507 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3508 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003509
3510
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003511 //
3512 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3513 return -1;
3514 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003515 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003517
3518 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003519 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003520 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003521 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003522 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003523 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003524 }
3525
3526 //
3527 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003528 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003529 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003530 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003531 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003534 //
3535 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003536 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003537 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003538 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003539 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003540
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003541 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003542 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003543 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3544 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003545 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003546 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003547 }
3548
3549 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003550 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003551 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3552 Item.uLabelType != QCBOR_TYPE_INT64 ||
3553 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003554 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003555 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003556 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003558 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003559 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003560 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3561 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003562 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003563 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003564 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003565
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003566 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003567 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003568 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3569 Item.uLabelType != QCBOR_TYPE_INT64 ||
3570 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003571 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003572 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003573 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003574
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003575 return 0;
3576}
3577
3578
3579
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003580static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003581 uint8_t uDataType,
3582 uint8_t uNestingLevel,
3583 uint8_t uNextNest,
3584 int64_t nLabel,
3585 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003586{
3587 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003588 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003590 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3591 if(Item.uDataType != uDataType) return -1;
3592 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003593 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3594 Item.uLabelType != QCBOR_TYPE_UINT64) {
3595 return -1;
3596 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003597 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3598 if(Item.label.int64 != nLabel) return -1;
3599 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003600 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003601 }
3602 }
3603 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303604 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003606 if(pItem) {
3607 *pItem = Item;
3608 }
3609 return 0;
3610}
3611
3612
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003613// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003614static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003615{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303616 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003617
Laurence Lundblade9b334962020-08-27 10:55:53 -07003618 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003619
Laurence Lundblade9b334962020-08-27 10:55:53 -07003620 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003621
Laurence Lundblade9b334962020-08-27 10:55:53 -07003622 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3623 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3624 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3625 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3626 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003627
Laurence Lundblade9b334962020-08-27 10:55:53 -07003628 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3629 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003630
Laurence Lundblade9b334962020-08-27 10:55:53 -07003631 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3632 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003633
Laurence Lundblade9b334962020-08-27 10:55:53 -07003634 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3635 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003636
Laurence Lundblade9b334962020-08-27 10:55:53 -07003637 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003638
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003639 return 0;
3640}
3641
3642
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003643/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003644{
3645 -23: {
3646 -20: {
3647 -18: "Organization",
3648 -17: "SSG",
3649 -15: "Confusion",
3650 -16: "San Diego",
3651 -14: "US"
3652 },
3653 -19: {
3654 -11: {
3655 -9: -7
3656 },
3657 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3658 }
3659 },
3660 -22: {
3661 -5: -3
3662 }
3663}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003664*/
3665static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003666 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3667 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3668 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3669 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3670 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3671 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3672 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3673 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3674 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3675
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003676// Same map as above, but using indefinite lengths
3677static const uint8_t spCSRInputIndefLen[] = {
3678 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3679 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3680 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3681 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3682 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3683 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3684 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3685 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3686 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3687 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3688
3689
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003690int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003691{
3692 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003693
Laurence Lundbladeee851742020-01-08 08:37:05 -08003694 QCBORDecode_Init(&DCtx,
3695 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3696 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003697
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003698 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003699}
3700
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003701
3702
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003703int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003704{
3705 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003706
Laurence Lundbladeee851742020-01-08 08:37:05 -08003707 QCBORDecode_Init(&DCtx,
3708 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3709 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003710
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003711 QCBORItem Item;
3712 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003713
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003714 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3715 return -1;
3716 }
3717 if(Item.uDataType != QCBOR_TYPE_MAP) {
3718 return -2;
3719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003720
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003721 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3722 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3723 return -3;
3724 }
3725
3726 return 0;
3727}
3728
3729
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003730
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003731int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003732{
3733 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003734
Laurence Lundbladeee851742020-01-08 08:37:05 -08003735 QCBORDecode_Init(&DCtx,
3736 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3737 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003738
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003739 return CheckCSRMaps(&DCtx);
3740}
3741
3742
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003743
Laurence Lundblade17ede402018-10-13 11:43:07 +08003744static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3745{
3746 UsefulOutBuf UOB;
3747 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003748
Laurence Lundblade17ede402018-10-13 11:43:07 +08003749 int i;
3750 for(i = 0; i < n; i++) {
3751 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3752 }
3753
3754 for(i = 0; i < n; i++) {
3755 UsefulOutBuf_AppendByte(&UOB, 0xff);
3756 }
3757 return UsefulOutBuf_OutUBuf(&UOB);
3758}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003759
3760
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003761static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003762{
3763 QCBORDecodeContext DC;
3764 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003765
Laurence Lundblade17ede402018-10-13 11:43:07 +08003766 int j;
3767 for(j = 0; j < nNestLevel; j++) {
3768 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003769 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003770 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3771 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003772 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003773 return -4;
3774 } else {
3775 return 0; // Decoding doesn't recover after an error
3776 }
3777 } else {
3778 // Should be no error
3779 if(nReturn) {
3780 return -9; // Should not have got an error
3781 }
3782 }
3783 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3784 return -7;
3785 }
3786 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003787 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003788 if(nReturn) {
3789 return -3;
3790 }
3791 return 0;
3792}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003793
3794
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003795int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003796{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303797 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003798 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003799 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003800 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003801 int nReturn = parse_indeflen_nested(Nested, i);
3802 if(nReturn) {
3803 return nReturn;
3804 }
3805 }
3806 return 0;
3807}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003808
Laurence Lundbladeee851742020-01-08 08:37:05 -08003809// [1, [2, 3]]
3810static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3811// No closing break
3812static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3813// Not enough closing breaks
3814static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3815// Too many closing breaks
3816static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3817// Unclosed indeflen inside def len
3818static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3819// confused tag
3820static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003821
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003822int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003823{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003824 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003825 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003826 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003827
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003828 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003829 QCBORDecodeContext DC;
3830 QCBORItem Item;
3831 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003832
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003833 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303834
3835 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3836 Item.uNestingLevel != 0 ||
3837 Item.uNextNestLevel != 1) {
3838 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003839 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003840
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003841 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303842 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3843 Item.uNestingLevel != 1 ||
3844 Item.uNextNestLevel != 1) {
3845 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003846 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003847
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003848 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303849 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3850 Item.uNestingLevel != 1 ||
3851 Item.uNextNestLevel != 2) {
3852 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003853 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003854
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003855 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003856 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303857 Item.uNestingLevel != 2 ||
3858 Item.uNextNestLevel != 2) {
3859 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003860 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003861
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003862 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003863 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303864 Item.uNestingLevel != 2 ||
3865 Item.uNextNestLevel != 0) {
3866 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003867 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003868
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003869 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303870 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003871 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003872
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003873 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003874 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003875
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003876 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003877
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003878 nResult = QCBORDecode_GetNext(&DC, &Item);
3879 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303880 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003881 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003882
Laurence Lundblade570fab52018-10-13 18:28:27 +08003883 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003884 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303885 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003886 }
3887
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003888
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003889 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003890 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003891
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003892 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003893
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003894 nResult = QCBORDecode_GetNext(&DC, &Item);
3895 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303896 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003897 }
3898
3899 nResult = QCBORDecode_GetNext(&DC, &Item);
3900 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303901 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003902 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003903
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003904 nResult = QCBORDecode_GetNext(&DC, &Item);
3905 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303906 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003908
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003909 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003910 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303911 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003912 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003913
3914
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003915 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003916 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003917
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003918 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003919
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003920 nResult = QCBORDecode_GetNext(&DC, &Item);
3921 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303922 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003923 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003924
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003925 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003926 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303927 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003928 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303929
Laurence Lundblade642282a2020-06-23 12:00:33 -07003930 nResult = QCBORDecode_GetNext(&DC, &Item);
3931 if(nResult != QCBOR_ERR_BAD_BREAK) {
3932 return -140;
3933 }
3934
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003935
Laurence Lundblade570fab52018-10-13 18:28:27 +08003936 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003937 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003938
Laurence Lundblade570fab52018-10-13 18:28:27 +08003939 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003940
Laurence Lundblade570fab52018-10-13 18:28:27 +08003941 nResult = QCBORDecode_GetNext(&DC, &Item);
3942 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303943 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003944 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003945
Laurence Lundblade570fab52018-10-13 18:28:27 +08003946 nResult = QCBORDecode_GetNext(&DC, &Item);
3947 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303948 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003950
Laurence Lundblade570fab52018-10-13 18:28:27 +08003951 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003952 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303953 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003954 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003955
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303956 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003957 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303959 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003960
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303961 nResult = QCBORDecode_GetNext(&DC, &Item);
3962 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303963 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003965
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303966 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303967 if(nResult != QCBOR_ERR_BAD_BREAK) {
3968 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003970
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003971 return 0;
3972}
3973
Laurence Lundblade17ede402018-10-13 11:43:07 +08003974
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08003975#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
3976
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003977static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003978 0x81, // Array of length one
3979 0x7f, // text string marked with indefinite length
3980 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3981 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3982 0xff // ending break
3983};
3984
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003985static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303986 0x81, // Array of length one
3987 0x7f, // text string marked with indefinite length
3988 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3989 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3990 0xff // ending break
3991};
3992
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003993static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303994 0x81, // Array of length one
3995 0x7f, // text string marked with indefinite length
3996 0x01, 0x02, // Not a string
3997 0xff // ending break
3998};
3999
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004000static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304001 0x81, // Array of length one
4002 0x7f, // text string marked with indefinite length
4003 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4004 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4005 // missing end of string
4006};
4007
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004008static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304009 0xa1, // Array of length one
4010 0x7f, // text string marked with indefinite length
4011 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4012 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4013 0xff, // ending break
4014 0x01 // integer being labeled.
4015};
4016
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004017/**
4018 Make an indefinite length string
4019
4020 @param Storage Storage for string, must be 144 bytes in size
4021 @return The indefinite length string
4022
4023 This makes an array with one indefinite length string that has 7 chunks
4024 from size of 1 byte up to 64 bytes.
4025 */
4026static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304027{
4028 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004029
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304030 UsefulOutBuf_Init(&UOB, Storage);
4031 UsefulOutBuf_AppendByte(&UOB, 0x81);
4032 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004033
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004034 uint8_t uStringByte = 0;
4035 // Use of type int is intentional
4036 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4037 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304038 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004039 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4040 for(int j = 0; j < uChunkSize; j++) {
4041 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4042 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304043 }
4044 }
4045 UsefulOutBuf_AppendByte(&UOB, 0xff);
4046
4047 return UsefulOutBuf_OutUBuf(&UOB);
4048}
4049
4050static int CheckBigString(UsefulBufC BigString)
4051{
4052 if(BigString.len != 255) {
4053 return 1;
4054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004055
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304056 for(uint8_t i = 0; i < 255; i++){
4057 if(((const uint8_t *)BigString.ptr)[i] != i) {
4058 return 1;
4059 }
4060 }
4061 return 0;
4062}
4063
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304064
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004065int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304066{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304067 QCBORDecodeContext DC;
4068 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304069 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004070 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004071
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304072 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004073 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304074 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004075
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304076 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304077 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304078 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004079
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304080 if(QCBORDecode_GetNext(&DC, &Item)) {
4081 return -2;
4082 }
4083 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4084 return -3;
4085 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004086
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304087 if(QCBORDecode_GetNext(&DC, &Item)) {
4088 return -4;
4089 }
4090 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4091 return -5;
4092 }
4093 if(QCBORDecode_Finish(&DC)) {
4094 return -6;
4095 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304096
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304097 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004098 QCBORDecode_Init(&DC,
4099 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4100 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004101
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304102 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4103 return -7;
4104 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004105
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304106 if(QCBORDecode_GetNext(&DC, &Item)) {
4107 return -8;
4108 }
4109 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4110 return -9;
4111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004112
Laurence Lundblade30816f22018-11-10 13:40:22 +07004113 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304114 return -10;
4115 }
4116
4117 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004118 QCBORDecode_Init(&DC,
4119 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4120 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004121
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304122 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4123 return -11;
4124 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004125
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304126 if(QCBORDecode_GetNext(&DC, &Item)) {
4127 return -12;
4128 }
4129 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4130 return -13;
4131 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundblade30816f22018-11-10 13:40:22 +07004133 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304134 return -14;
4135 }
4136
4137 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004138 QCBORDecode_Init(&DC,
4139 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4140 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004141
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304142 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4143 return -15;
4144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004145
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304146 if(QCBORDecode_GetNext(&DC, &Item)) {
4147 return -16;
4148 }
4149 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4150 return -17;
4151 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004152
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304153 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4154 return -18;
4155 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004156
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304157 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304158 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004159
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304160 QCBORDecode_GetNext(&DC, &Item);
4161 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304162 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004164
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304165 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304166 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004168
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304169 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004170 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304171
4172 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4173 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304174 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004176
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304177 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304178 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004179 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004180
Laurence Lundbladeee851742020-01-08 08:37:05 -08004181 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4182 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004183
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304184 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304185 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304186 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004188
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304189 QCBORDecode_GetNext(&DC, &Item);
4190 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304191 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304192 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004193 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304194 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304195 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004196
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304197 // ---- big bstr -----
4198 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004199
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304200 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4201 return -25;
4202 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004203
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304204 if(QCBORDecode_GetNext(&DC, &Item)) {
4205 return -26;
4206 }
4207 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304208 return -26;
4209 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004210
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304211 if(QCBORDecode_GetNext(&DC, &Item)) {
4212 return -27;
4213 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304214 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304215 return -28;
4216 }
4217 if(CheckBigString(Item.val.string)) {
4218 return -3;
4219 }
4220 if(QCBORDecode_Finish(&DC)) {
4221 return -29;
4222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004223
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304224 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004225 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004226
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304227 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4228 return -30;
4229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004230
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304231 QCBORDecode_GetNext(&DC, &Item);
4232 if(Item.uDataType != QCBOR_TYPE_MAP) {
4233 return -31;
4234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004235
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304236 if(QCBORDecode_GetNext(&DC, &Item)){
4237 return -32;
4238 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004239 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4240 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304241 Item.uDataAlloc || !Item.uLabelAlloc ||
4242 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4243 return -33;
4244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004245
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304246 if(QCBORDecode_Finish(&DC)) {
4247 return -34;
4248 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004249
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004250 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004251}
4252
4253
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004254int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304255{
4256 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004257 QCBORError nCBORError;
4258
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004259
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304260 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004261 QCBORDecode_Init(&DC,
4262 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4263 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004264
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004265 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004266
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004267 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4268 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304269 return -1;
4270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004271
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004272 if(CheckCSRMaps(&DC)) {
4273 return -2;
4274 }
4275
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004276 // Next parse, save pointers to a few strings, destroy original and
4277 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004278 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004279 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004280
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304281 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004282 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304283 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004284
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304285 QCBORItem Item1, Item2, Item3, Item4;
4286 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004287 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304288 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4289 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004290 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304291 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004292 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304293 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004294 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304295 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004296 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304297 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004298 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004299
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304300 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004301
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304302 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304303 Item1.uDataType != QCBOR_TYPE_INT64 ||
4304 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004305 Item1.uDataAlloc != 0 ||
4306 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004307 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004308 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004310
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304311
4312 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004313 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304314 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004315 Item2.uDataAlloc != 0 ||
4316 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304317 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004318 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004319
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304320 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004321 Item3.uDataAlloc == 0 ||
4322 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004323 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004324 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004326
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304327 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004328 Item4.uDataAlloc == 0 ||
4329 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004330 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004331 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004333
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304334 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004335 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004336 QCBORDecode_Init(&DC,
4337 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4338 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304339 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4340 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004341 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304342 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004343 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004344 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004345 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304346 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4347 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4348 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4349 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4350 }
4351 }
4352 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004353 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004354 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304355 }
4356
4357 return 0;
4358}
4359
Laurence Lundbladef6531662018-12-04 10:42:22 +09004360
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004361int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004362{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004363 // Set up the decoder with a tiny bit of CBOR to parse because
4364 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004365 QCBORDecodeContext DC;
4366 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4367 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004368
Laurence Lundbladef6531662018-12-04 10:42:22 +09004369 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004370 // Then fish into the internals of the decode context
4371 // to get the allocator function so it can be called directly.
4372 // Also figure out how much pool is available for use
4373 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004374 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004375 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4376 if(nError) {
4377 return -9;
4378 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004379 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4380 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4381 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004382
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004383 // First test -- ask for one more byte than available and see failure
4384 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004385 if(!UsefulBuf_IsNULL(Allocated)) {
4386 return -1;
4387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004388
Laurence Lundbladef6531662018-12-04 10:42:22 +09004389 // Re do the set up for the next test that will do a successful alloc,
4390 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004391 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004392 pAlloc = DC.StringAllocator.pfAllocator;
4393 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4394 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004395
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004396 // Allocate one byte less than available and see success
4397 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004398 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4399 return -2;
4400 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004401 // Ask for some more and see failure
4402 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004403 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4404 return -3;
4405 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004406 // Free the first allocate, retry the second and see success
4407 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4408 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004409 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4410 return -4;
4411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004412
Laurence Lundbladef6531662018-12-04 10:42:22 +09004413 // Re do set up for next test that involves a successful alloc,
4414 // and a successful realloc and a failed realloc
4415 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004416 pAlloc = DC.StringAllocator.pfAllocator;
4417 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004418
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004419 // Allocate half the pool and see success
4420 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004421 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4422 return -5;
4423 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004424 // Reallocate to take up the whole pool and see success
4425 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004426 if(UsefulBuf_IsNULL(Allocated2)) {
4427 return -6;
4428 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004429 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004430 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4431 return -7;
4432 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004433 // Try to allocate more to be sure there is failure after a realloc
4434 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4435 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004436 return -8;
4437 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004438
Laurence Lundbladef6531662018-12-04 10:42:22 +09004439 return 0;
4440}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004441
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004442
4443/* Just enough of an allocator to test configuration of one */
4444static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4445{
4446 (void)pOldMem; // unused variable
4447
4448 if(uNewSize) {
4449 // Assumes the context pointer is the buffer and
4450 // nothing too big will ever be asked for.
4451 // This is only good for this basic test!
4452 return (UsefulBuf) {pCtx, uNewSize};
4453 } else {
4454 return NULLUsefulBuf;
4455 }
4456}
4457
4458
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004459int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004460{
4461 // Set up the decoder with a tiny bit of CBOR to parse because
4462 // nothing can be done with it unless that is set up.
4463 QCBORDecodeContext DC;
4464 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4465 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4466
4467 uint8_t pAllocatorBuffer[50];
4468
4469 // This is really just to test that this call works.
4470 // The full functionality of string allocators is tested
4471 // elsewhere with the MemPool internal allocator.
4472 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4473
4474 QCBORItem Item;
4475 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4476 return -1;
4477 }
4478
4479 if(Item.uDataAlloc == 0 ||
4480 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4481 Item.val.string.ptr != pAllocatorBuffer) {
4482 return -2;
4483 }
4484
4485 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4486 return -3;
4487 }
4488
4489 return 0;
4490}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004491#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4492
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004493
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004494#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004495
Laurence Lundbladea826c502020-05-10 21:07:00 -07004496/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004497 [
4498 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004499 4([-20, 4759477275222530853136]),
4500 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004501 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004502 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004503 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004504 5([ 9223372036854775806, -4759477275222530853137])
4505 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004506 ]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004507 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004508static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004509 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004510 0xC4, 0x82, 0x20,
4511 0x03,
4512 0xC4, 0x82, 0x33,
4513 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4514 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4515 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4516 0xC5, 0x82, 0x19, 0x01, 0x2C,
4517 0x18, 0x64,
4518 0xC5, 0x82, 0x33,
4519 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4520 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4521 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004522 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4523 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004524 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4525 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4526};
4527
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004528
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004529int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004530{
4531 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004532 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004533 QCBORItem item;
4534
Laurence Lundblade17af4902020-01-07 19:11:55 -08004535 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4536 0x06, 0x07, 0x08, 0x09, 0x010};
4537 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004538
4539
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004540 QCBORDecode_Init(&DC,
4541 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4542 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004543
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004544 uErr = QCBORDecode_GetNext(&DC, &item);
4545 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004546 return 1;
4547 }
4548
4549 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4550 return 2;
4551 }
4552
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004553 uErr = QCBORDecode_GetNext(&DC, &item);
4554 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004555 return 3;
4556 }
4557
4558 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4559 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4560 item.val.expAndMantissa.nExponent != -1) {
4561 return 4;
4562 }
4563
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004564 uErr = QCBORDecode_GetNext(&DC, &item);
4565 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004566 return 5;
4567 }
4568
4569 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4570 item.val.expAndMantissa.nExponent != -20 ||
4571 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4572 return 6;
4573 }
4574
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004575 uErr = QCBORDecode_GetNext(&DC, &item);
4576 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004577 return 7;
4578 }
4579
4580 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4581 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4582 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4583 return 8;
4584 }
4585
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004586 uErr = QCBORDecode_GetNext(&DC, &item);
4587 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004588 return 9;
4589 }
4590
4591 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4592 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4593 item.val.expAndMantissa.nExponent != 300) {
4594 return 10;
4595 }
4596
Laurence Lundbladea826c502020-05-10 21:07:00 -07004597 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004598 uErr = QCBORDecode_GetNext(&DC, &item);
4599 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004600 return 11;
4601 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004602 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4603 item.val.expAndMantissa.nExponent != -20 ||
4604 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4605 return 12;
4606 }
4607
Laurence Lundbladea826c502020-05-10 21:07:00 -07004608 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004609 uErr = QCBORDecode_GetNext(&DC, &item);
4610 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004611 return 13;
4612 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004613 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4614 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4615 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4616 return 14;
4617 }
4618
Laurence Lundbladea826c502020-05-10 21:07:00 -07004619 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004620 uErr = QCBORDecode_GetNext(&DC, &item);
4621 if(uErr != QCBOR_SUCCESS) {
4622 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004623 }
4624 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4625 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4626 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004627 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004628 }
4629
Laurence Lundbladea826c502020-05-10 21:07:00 -07004630 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004631 uErr = QCBORDecode_GetNext(&DC, &item);
4632 if(uErr != QCBOR_SUCCESS) {
4633 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004634 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004635 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4636 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4637 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004638 return 18;
4639 }
4640
4641 uErr = QCBORDecode_Finish(&DC);
4642 if(uErr != QCBOR_SUCCESS) {
4643 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004644 }
4645
4646 /* Now encode some stuff and then decode it */
4647 uint8_t pBuf[40];
4648 QCBOREncodeContext EC;
4649 UsefulBufC Encoded;
4650
4651 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4652 QCBOREncode_OpenArray(&EC);
4653 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4654 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4655 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4656 QCBOREncode_CloseArray(&EC);
4657 QCBOREncode_Finish(&EC, &Encoded);
4658
4659
4660 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004661 uErr = QCBORDecode_GetNext(&DC, &item);
4662 if(uErr != QCBOR_SUCCESS) {
4663 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004664 }
4665
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004666 uErr = QCBORDecode_GetNext(&DC, &item);
4667 if(uErr != QCBOR_SUCCESS) {
4668 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004669 }
4670
4671 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4672 item.val.expAndMantissa.nExponent != 1000 ||
4673 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004674 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004675 }
4676
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004677 uErr = QCBORDecode_GetNext(&DC, &item);
4678 if(uErr != QCBOR_SUCCESS) {
4679 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004680 }
4681
4682 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4683 item.val.expAndMantissa.nExponent != INT32_MIN ||
4684 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004685 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004686 }
4687
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004688 uErr = QCBORDecode_GetNext(&DC, &item);
4689 if(uErr != QCBOR_SUCCESS) {
4690 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004691 }
4692
4693 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4694 item.val.expAndMantissa.nExponent != INT32_MAX ||
4695 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004696 return 106;
4697 }
4698
4699
4700 int64_t nExp, nMant;
4701 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4702 UsefulBufC Mant;
4703 bool bIsNeg;
4704
4705 QCBORDecode_Init(&DC,
4706 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4707 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004708 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004709
4710 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004711 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004712
4713 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004714 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4715 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004716
4717 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004718 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4719 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004720
4721 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004722 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004723
4724 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004725 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4726 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004727
4728 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004729 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4730 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004731
4732 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004733 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4734 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004735
4736 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004737 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4738 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004739
4740 QCBORDecode_ExitArray(&DC);
4741
4742 uErr = QCBORDecode_Finish(&DC);
4743 if(uErr != QCBOR_SUCCESS) {
4744 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004745 }
4746
4747 return 0;
4748}
4749
4750
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004751static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004752 // Exponent > INT64_MAX
4753 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4754 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4755 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4756 // Mantissa > INT64_MAX
4757 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4758 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4759 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4760 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004761 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004762 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004763 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004764 // bad content for big num
4765 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4766 // bad content for big num
4767 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4768 // Bad integer for exponent
4769 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4770 // Bad integer for mantissa
4771 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4772 // 3 items in array
4773 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004774#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004775 // unterminated indefinite length array
4776 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004777#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4778 // unterminated indefinite length array
4779 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4780#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004781 // Empty array
4782 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4783 // Second is not an integer
4784 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4785 // First is not an integer
4786 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4787 // Not an array
4788 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4789};
4790
4791
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004792int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004793{
4794 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004795 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4796 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004797}
4798
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004799#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004800
4801
4802
4803/*
4804 Some basic CBOR with map and array used in a lot of tests.
4805 The map labels are all strings
4806
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004807 {
4808 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004809 "an array of two strings": [
4810 "string1", "string2"
4811 ],
4812 "map in a map": {
4813 "bytes 1": h'78787878',
4814 "bytes 2": h'79797979',
4815 "another int": 98,
4816 "text 2": "lies, damn lies and statistics"
4817 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004818 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004819 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004820
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004821int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004822{
4823 QCBORItem Item1, Item2, Item3;
4824 int64_t nDecodedInt1, nDecodedInt2;
4825 UsefulBufC B1, B2, S1, S2, S3;
4826
4827 QCBORDecodeContext DCtx;
4828 QCBORError nCBORError;
4829
4830 QCBORDecode_Init(&DCtx, input, 0);
4831
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004832 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004833
4834 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4835
4836 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4837 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004838 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4839 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4840 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004841 QCBORDecode_ExitMap(&DCtx);
4842
4843 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4844 QCBORDecode_GetNext(&DCtx, &Item1);
4845 QCBORDecode_GetNext(&DCtx, &Item2);
4846 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4847 return -400;
4848 }
4849 QCBORDecode_ExitArray(&DCtx);
4850
4851 // Parse the same array again using GetText() instead of GetItem()
4852 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004853 QCBORDecode_GetTextString(&DCtx, &S2);
4854 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004855 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4856 return 5000;
4857 }
4858 /* QCBORDecode_GetText(&DCtx, &S3);
4859 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4860 return 5001;
4861 } */
4862
4863 QCBORDecode_ExitArray(&DCtx);
4864
4865 QCBORDecode_ExitMap(&DCtx);
4866
4867 nCBORError = QCBORDecode_Finish(&DCtx);
4868
4869 if(nCBORError) {
4870 return (int32_t)nCBORError;
4871 }
4872
4873 if(nDecodedInt1 != 42) {
4874 return 1001;
4875 }
4876
4877 if(nDecodedInt2 != 98) {
4878 return 1002;
4879 }
4880
4881 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004882 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004883 return 1003;
4884 }
4885
4886 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004887 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004888 return 1004;
4889 }
4890
Laurence Lundblade9b334962020-08-27 10:55:53 -07004891 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004892 return 1005;
4893 }
4894
4895 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4896 return 1006;
4897 }
4898
4899 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4900 return 1007;
4901 }
4902
4903 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4904 return 1008;
4905 }
4906
4907 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4908 return 1009;
4909 }
4910
4911 return 0;
4912}
4913
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004914/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004915 {
4916 -75008: h'05083399',
4917 88: [],
4918 100100: {
4919 "sub1": {
4920 10: [
4921 0
4922 ],
4923 -75009: h'A46823990001',
4924 100100: {
4925 "json": "{ \"ueid\", \"xyz\"}",
4926 "subsub": {
4927 100002: h'141813191001'
4928 }
4929 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004930 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004931 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004932 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004933 */
4934
4935static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004936 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
4937 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
4938 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
4939 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
4940 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
4941 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
4942 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
4943 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
4944 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
4945 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
4946 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
4947 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004948};
4949
4950/* Get item in multi-level nesting in spNestedCBOR */
4951static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4952{
4953 UsefulBufC String;
4954
4955 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4956 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4957
4958 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4959 QCBORDecode_EnterMap(pDCtx, NULL);
4960 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4961 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4962 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4963 if(QCBORDecode_GetError(pDCtx)) {
4964 return 4001;
4965 }
4966 if(UsefulBuf_Compare(String, test_oemid)) {
4967 return 4002;
4968 }
4969 QCBORDecode_ExitMap(pDCtx);
4970 QCBORDecode_ExitMap(pDCtx);
4971 QCBORDecode_ExitMap(pDCtx);
4972 QCBORDecode_ExitMap(pDCtx);
4973
4974 return 0;
4975}
4976
4977/* Iterations on the zero-length array in spNestedCBOR */
4978static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4979{
4980 QCBORItem Item;
4981 QCBORError uErr;
4982
4983 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4984 for(int x = 0; x < 20; x++) {
4985 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4986 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4987 return 4100;
4988
4989 }
4990 }
4991 QCBORDecode_ExitArray(pDCtx);
4992 if(QCBORDecode_GetError(pDCtx)) {
4993 return 4101;
4994 }
4995
4996 return 0;
4997}
4998
4999/* Various iterations on the array that contains a zero in spNestedCBOR */
5000static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5001{
5002 QCBORError uErr;
5003
5004 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5005 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5006 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5007 int64_t nInt = 99;
5008 QCBORDecode_GetInt64(pDCtx, &nInt);
5009 if(nInt != 0) {
5010 return 4200;
5011 }
5012 for(int x = 0; x < 20; x++) {
5013 QCBORItem Item;
5014 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5015 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5016 return 4201;
5017
5018 }
5019 }
5020 QCBORDecode_ExitArray(pDCtx);
5021 if(QCBORDecode_GetAndResetError(pDCtx)) {
5022 return 4202;
5023 }
5024 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5025 UsefulBufC dD;
5026 QCBORDecode_GetByteString(pDCtx, &dD);
5027 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5028 return 4203;
5029 }
5030 for(int x = 0; x < 20; x++) {
5031 QCBORDecode_GetByteString(pDCtx, &dD);
5032 uErr = QCBORDecode_GetAndResetError(pDCtx);
5033 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5034 return 4204;
5035 }
5036 }
5037 QCBORDecode_ExitArray(pDCtx);
5038 QCBORDecode_ExitMap(pDCtx);
5039 QCBORDecode_ExitMap(pDCtx);
5040
5041 return 0;
5042}
5043
5044/* Repeatedly enter and exit maps and arrays, go off the end of maps
5045 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005046static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005047{
5048 QCBORDecodeContext DCtx;
5049 int32_t nReturn;
5050 QCBORError uErr;
5051
5052 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5053 QCBORDecode_EnterMap(&DCtx, NULL);
5054
5055 for(int j = 0; j < 5; j++) {
5056 for(int i = 0; i < 20; i++) {
5057 nReturn = DecodeNestedGetSubSub(&DCtx);
5058 if(nReturn) {
5059 return nReturn;
5060 }
5061 }
5062
5063 for(int i = 0; i < 20; i++) {
5064 nReturn = DecodeNestedGetEmpty(&DCtx);
5065 if(nReturn ) {
5066 return nReturn;
5067 }
5068 }
5069
5070 for(int i = 0; i < 20; i++) {
5071 nReturn = DecodeNestedGetZero(&DCtx);
5072 if(nReturn ) {
5073 return nReturn;
5074 }
5075 }
5076 }
5077
5078 QCBORDecode_ExitMap(&DCtx);
5079 uErr = QCBORDecode_Finish(&DCtx);
5080 if(uErr) {
5081 return (int32_t)uErr + 4100;
5082 }
5083
5084 return 0;
5085}
5086
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005087
5088/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005089 [
5090 23,
5091 6000,
5092 h'67616C6163746963',
5093 h'686176656E20746F6B656E'
5094 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005095 */
5096static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005097 0x84,
5098 0x17,
5099 0x19, 0x17, 0x70,
5100 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5101 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005102
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005103/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005104static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5105
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005106/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005107static const uint8_t spEmptyMap[] = {0xa0};
5108
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005109#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005110/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005111static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005112
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005113
Laurence Lundbladef0499502020-08-01 11:55:57 -07005114/*
5115 {
5116 0: [],
5117 9: [
5118 [],
5119 []
5120 ],
5121 8: {
5122 1: [],
5123 2: {},
5124 3: []
5125 },
5126 4: {},
5127 5: [],
5128 6: [
5129 [],
5130 []
5131 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005132 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005133 */
5134static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005135 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5136 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5137 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5138 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005139
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005140#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5141
5142
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005143/*
5144 Too many tags
5145 Invalid tag content
5146 Duplicate label
5147 Integer overflow
5148 Date overflow
5149
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005150 {
5151 1: 224(225(226(227(4(0))))),
5152 2: 1(h''),
5153 3: -18446744073709551616,
5154 4: 1(1.0e+300),
5155 5: 0, 8: 8
5156 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005157 */
5158static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005159 0xa7,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005160 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5161 0x02, 0xc1, 0x40,
5162 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5163 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5164 0x05, 0x00,
5165 0x05, 0x00,
5166 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005167};
5168
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005169/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005170static const uint8_t spUnRecoverableMapError1[] = {
5171 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5172};
5173
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005174#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005175/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005176static const uint8_t spUnRecoverableMapError2[] = {
5177 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5178};
5179
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005180/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005181static const uint8_t spUnRecoverableMapError3[] = {
5182 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5183};
5184
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005185/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005186static const uint8_t spUnRecoverableMapError4[] = {
5187 0xbf,
5188 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5189 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5191 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5192 0xff
5193};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005194#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005195
Laurence Lundblade63926052021-03-29 16:05:51 -07005196const unsigned char not_well_formed_submod_section[] = {
5197 0xa1, 0x14, 0x1f,
5198};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005199
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005200int32_t EnterMapTest()
5201{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005202 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005203 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005204 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005205 int32_t nReturn;
5206 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005207
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005208#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005209 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005210 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005211
Laurence Lundbladef0499502020-08-01 11:55:57 -07005212
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005213 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005214 QCBORDecode_ExitArray(&DCtx);
5215
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005216 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5217 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005218 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005219 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005220 QCBORDecode_ExitArray(&DCtx);
5221 QCBORDecode_ExitArray(&DCtx);
5222
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005223 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5224 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005225 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005226 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005227 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005228 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005229 QCBORDecode_ExitArray(&DCtx);
5230 QCBORDecode_ExitMap(&DCtx);
5231
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005232 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005233 QCBORDecode_ExitMap(&DCtx);
5234
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005235 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005236 QCBORDecode_ExitArray(&DCtx);
5237
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005238 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5239 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005240 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005241 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005242 QCBORDecode_ExitArray(&DCtx);
5243 QCBORDecode_ExitArray(&DCtx);
5244
5245 QCBORDecode_ExitMap(&DCtx);
5246
5247 uErr = QCBORDecode_Finish(&DCtx);
5248 if(uErr != QCBOR_SUCCESS){
5249 return 3011;
5250 }
5251
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005252 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005253 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005254 if(nReturn) {
5255 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005256 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005257#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5258
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005259
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005260 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005261 if(nReturn) {
5262 return nReturn;
5263 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005264
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005265
Laurence Lundblade937ea812020-05-08 11:38:23 -07005266
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005267 // These tests confirm the cursor is at the right place after entering
5268 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005269 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005270
5271 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005272 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005273 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005274 QCBORDecode_GetNext(&DCtx, &Item1);
5275 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5276 return 2001;
5277 }
5278
5279
Laurence Lundblade9b334962020-08-27 10:55:53 -07005280 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005281 QCBORDecode_VGetNext(&DCtx, &Item1);
5282 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005283 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5284 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5285 UsefulBuf_Compare(ArrayItem.label.string,
5286 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5287 return 2051;
5288 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005289 QCBORDecode_GetNext(&DCtx, &Item1);
5290 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5291 return 2002;
5292 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005293 QCBORDecode_ExitArray(&DCtx);
5294 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5295 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5296 UsefulBuf_Compare(ArrayItem.label.string,
5297 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5298 return 2052;
5299 }
5300
Laurence Lundblade937ea812020-05-08 11:38:23 -07005301
Laurence Lundblade9b334962020-08-27 10:55:53 -07005302 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005303 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005304 QCBORDecode_GetNext(&DCtx, &Item1);
5305 QCBORDecode_GetNext(&DCtx, &Item1);
5306 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005307 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5308 QCBORDecode_GetNext(&DCtx, &Item1);
5309 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5310 return 2003;
5311 }
5312
Laurence Lundblade9b334962020-08-27 10:55:53 -07005313 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005314 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005315 QCBORDecode_GetNext(&DCtx, &Item1);
5316 QCBORDecode_GetNext(&DCtx, &Item1);
5317 QCBORDecode_GetNext(&DCtx, &Item1);
5318 QCBORDecode_GetNext(&DCtx, &Item1);
5319 QCBORDecode_GetNext(&DCtx, &Item1);
5320 QCBORDecode_GetNext(&DCtx, &Item1);
5321 QCBORDecode_GetNext(&DCtx, &Item1);
5322 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5323 QCBORDecode_GetNext(&DCtx, &Item1);
5324 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005325 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005326 }
5327
Laurence Lundblade9b334962020-08-27 10:55:53 -07005328 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005329 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005330 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5331 QCBORDecode_ExitArray(&DCtx);
5332 QCBORDecode_GetNext(&DCtx, &Item1);
5333 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5334 return 2006;
5335 }
5336 QCBORDecode_ExitMap(&DCtx);
5337 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5338 return 2007;
5339 }
5340
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005341 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005342 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005343 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005344 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5345 uErr = QCBORDecode_GetAndResetError(&DCtx);
5346 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005347 return 2008;
5348 }
5349 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005350 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005351 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005352 return 2009;
5353 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005354
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005355
5356 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005357 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005358 // This will fail because the map is empty.
5359 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5360 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005361 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005362 return 2010;
5363 }
5364 QCBORDecode_ExitMap(&DCtx);
5365 uErr = QCBORDecode_Finish(&DCtx);
5366 if(uErr != QCBOR_SUCCESS){
5367 return 2011;
5368 }
5369
5370
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005371#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005372 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005373 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005374 // This will fail because the map is empty.
5375 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5376 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005377 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005378 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005379 }
5380 QCBORDecode_ExitMap(&DCtx);
5381 uErr = QCBORDecode_Finish(&DCtx);
5382 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005383 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005384 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005385#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005386
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005387
5388 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005389 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005390 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005391 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005392 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005393 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005394 QCBORDecode_ExitArray(&DCtx);
5395 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5396 QCBORDecode_ExitArray(&DCtx);
5397 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005398 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005399 return 2014;
5400 }
5401
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005402 int64_t nInt;
5403 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005404 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005405 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005406 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005407 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5408 return 2021;
5409 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005410 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5411 return 2121;
5412 }
5413 (void)QCBORDecode_GetAndResetError(&DCtx);
5414
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005415
5416 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5417 uErr = QCBORDecode_GetAndResetError(&DCtx);
5418 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5419 return 2022;
5420 }
5421
5422 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5423 uErr = QCBORDecode_GetAndResetError(&DCtx);
5424 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5425 return 2023;
5426 }
5427
5428 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5429 uErr = QCBORDecode_GetAndResetError(&DCtx);
5430#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5431 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5432 return 2024;
5433 }
5434#else
5435 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5436 return 2027;
5437 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005438#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005439
5440 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5441 uErr = QCBORDecode_GetAndResetError(&DCtx);
5442 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5443 return 2025;
5444 }
5445
5446 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5447
5448 QCBORDecode_ExitMap(&DCtx);
5449 uErr = QCBORDecode_Finish(&DCtx);
5450 if(uErr != QCBOR_SUCCESS) {
5451 return 2026;
5452 }
5453
5454 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005455 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005456 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5457 uErr = QCBORDecode_GetAndResetError(&DCtx);
5458 if(uErr != QCBOR_ERR_BAD_BREAK) {
5459 return 2030;
5460 }
5461
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005462#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005463 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005464 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005465 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5466 uErr = QCBORDecode_GetAndResetError(&DCtx);
5467 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5468 return 2031;
5469 }
5470
5471 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005472 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005473 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5474 uErr = QCBORDecode_GetAndResetError(&DCtx);
5475 if(uErr != QCBOR_ERR_HIT_END) {
5476 return 2032;
5477 }
5478
5479 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005480 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005481 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5482 uErr = QCBORDecode_GetAndResetError(&DCtx);
5483 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5484 return 2033;
5485 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005486#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5487
Laurence Lundblade732e52d2021-02-22 20:11:01 -07005488 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5489 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5490 if(Item1.uDataType != QCBOR_TYPE_MAP) {
5491 return 2401;
5492 }
5493 if(QCBORDecode_GetError(&DCtx)) {
5494 return 2402;
5495 }
5496
5497 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5498 QCBORDecode_VGetNext(&DCtx, &Item1);
5499 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5500 Item1.val.uCount != 3 ||
5501 Item1.uNextNestLevel != 1) {
5502 return 2403;
5503 }
5504 if(QCBORDecode_GetError(&DCtx)) {
5505 return 2404;
5506 }
5507 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5508 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
5509 Item1.uNextNestLevel != 1 ||
5510 Item1.val.int64 != 42) {
5511 return 2405;
5512 }
5513 if(QCBORDecode_GetError(&DCtx)) {
5514 return 2406;
5515 }
5516 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5517 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
5518 Item1.uNestingLevel != 1 ||
5519 Item1.uNextNestLevel != 1 ||
5520 Item1.val.uCount != 2) {
5521 return 2407;
5522 }
5523 if(QCBORDecode_GetError(&DCtx)) {
5524 return 2408;
5525 }
5526 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5527 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5528 Item1.uNestingLevel != 1 ||
5529 Item1.uNextNestLevel != 0 ||
5530 Item1.val.uCount != 4) {
5531 return 2409;
5532 }
5533 if(QCBORDecode_GetError(&DCtx)) {
5534 return 2410;
5535 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005536
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005537 nReturn = DecodeNestedIterate();
5538
Laurence Lundblade63926052021-03-29 16:05:51 -07005539
5540 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
5541 QCBORDecode_EnterMap(&DCtx, NULL);
5542 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
5543 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
5544 return 2500;
5545 }
5546
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005547 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005548}
5549
5550
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005551struct NumberConversion {
5552 char *szDescription;
5553 UsefulBufC CBOR;
5554 int64_t nConvertedToInt64;
5555 QCBORError uErrorInt64;
5556 uint64_t uConvertToUInt64;
5557 QCBORError uErrorUint64;
5558 double dConvertToDouble;
5559 QCBORError uErrorDouble;
5560};
5561
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005562static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005563 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005564 "too large to fit into int64_t",
5565 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5566 0,
5567 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5568 0,
5569 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5570 ((double)INT64_MIN) + 1 ,
5571 QCBOR_SUCCESS
5572 },
5573 {
5574 "largest negative int that fits in int64_t",
5575 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5576 INT64_MIN,
5577 QCBOR_SUCCESS,
5578 0,
5579 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5580 (double)INT64_MIN,
5581 QCBOR_SUCCESS
5582 },
5583 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005584 "negative bignum -1",
5585 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5586 -1,
5587 QCBOR_SUCCESS,
5588 0,
5589 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5590 -1.0,
5591 QCBOR_SUCCESS
5592 },
5593 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005594 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005595 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5596 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005597#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005598 257000,
5599 QCBOR_SUCCESS,
5600 257000,
5601 QCBOR_SUCCESS,
5602 257000.0,
5603 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005604#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005605 0,
5606 QCBOR_ERR_UNEXPECTED_TYPE,
5607 0,
5608 QCBOR_ERR_UNEXPECTED_TYPE,
5609 0.0,
5610 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005611#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005612 },
5613 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005614 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005615 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5616 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005617#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005618 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005619 QCBOR_SUCCESS,
5620 0,
5621 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005622 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005623 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005624#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005625 0,
5626 QCBOR_ERR_UNEXPECTED_TYPE,
5627 0,
5628 QCBOR_ERR_UNEXPECTED_TYPE,
5629 0.0,
5630 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005631#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005632 },
5633 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005634 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005635 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5636 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005637#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005638 2056,
5639 QCBOR_SUCCESS,
5640 2056,
5641 QCBOR_SUCCESS,
5642 2056.0,
5643 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005644#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005645 0,
5646 QCBOR_ERR_UNEXPECTED_TYPE,
5647 0,
5648 QCBOR_ERR_UNEXPECTED_TYPE,
5649 0.0,
5650 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005651#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005652 },
5653 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005654 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005655 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5656 0,
5657 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5658 0,
5659 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5660 -18446744073709551617.0,
5661 QCBOR_SUCCESS
5662 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005663#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005664 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005665 "Positive bignum 0x01020304 indefinite length string",
5666 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5667 0x01020304,
5668 QCBOR_SUCCESS,
5669 0x01020304,
5670 QCBOR_SUCCESS,
5671 16909060.0,
5672 QCBOR_SUCCESS
5673 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005674#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005675 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005676 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005677 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5678 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005679#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005680 0,
5681 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5682 0,
5683 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5684 -INFINITY,
5685 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005686#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005687 0,
5688 QCBOR_ERR_UNEXPECTED_TYPE,
5689 0,
5690 QCBOR_ERR_UNEXPECTED_TYPE,
5691 0.0,
5692 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005693#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005694 },
5695 {
5696 "big float [9223372036854775806, 9223372036854775806]",
5697 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5698 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005699#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005700 0,
5701 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5702 0,
5703 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5704 INFINITY,
5705 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005706#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005707 0,
5708 QCBOR_ERR_UNEXPECTED_TYPE,
5709 0,
5710 QCBOR_ERR_UNEXPECTED_TYPE,
5711 0.0,
5712 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005713#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005714 },
5715 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005716 "Big float 3 * 2^^2",
5717 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005718#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005719 12,
5720 QCBOR_SUCCESS,
5721 12,
5722 QCBOR_SUCCESS,
5723 12.0,
5724 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005725#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005726 0,
5727 QCBOR_ERR_UNEXPECTED_TYPE,
5728 0,
5729 QCBOR_ERR_UNEXPECTED_TYPE,
5730 0.0,
5731 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005732#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005733 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005734 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005735 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005736 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5737 0,
5738 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5739 18446744073709551615ULL,
5740 QCBOR_SUCCESS,
5741 18446744073709551615.0,
5742 QCBOR_SUCCESS
5743 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005744 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005745 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005746 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5747 65536-1,
5748 QCBOR_SUCCESS,
5749 0xffff,
5750 QCBOR_SUCCESS,
5751 65535.0,
5752 QCBOR_SUCCESS
5753 },
5754 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005755 "Postive integer 0",
5756 {(uint8_t[]){0x0}, 1},
5757 0LL,
5758 QCBOR_SUCCESS,
5759 0ULL,
5760 QCBOR_SUCCESS,
5761 0.0,
5762 QCBOR_SUCCESS
5763 },
5764 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005765 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005766 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5767 -9223372036854775807-1, // INT64_MIN
5768 QCBOR_SUCCESS,
5769 0ULL,
5770 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5771 -9223372036854775808.0,
5772 QCBOR_SUCCESS
5773 },
5774 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005775 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005776 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005777#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005778 100L,
5779 QCBOR_SUCCESS,
5780 100ULL,
5781 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005782#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5783 0,
5784 QCBOR_ERR_HW_FLOAT_DISABLED,
5785 0,
5786 QCBOR_ERR_HW_FLOAT_DISABLED,
5787#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005788 100.3,
5789 QCBOR_SUCCESS
5790 },
5791 {
5792 "Floating point value NaN 0xfa7fc00000",
5793 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005794#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005795 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005796 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005797 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005798 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005799#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5800 0,
5801 QCBOR_ERR_HW_FLOAT_DISABLED,
5802 0,
5803 QCBOR_ERR_HW_FLOAT_DISABLED,
5804#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005805 NAN,
5806 QCBOR_SUCCESS
5807 },
5808 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005809 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005810 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005811#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5812#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5813 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005814 -4,
5815 QCBOR_SUCCESS,
5816 0,
5817 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5818 -4.0,
5819 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005820#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5821 // Float HW disabled
5822 -4,
5823 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5824 0,
5825 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5826 -4.0,
5827 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5828#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005829#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005830 // Half-precision disabled
5831 -4,
5832 QCBOR_ERR_HALF_PRECISION_DISABLED,
5833 0,
5834 QCBOR_ERR_HALF_PRECISION_DISABLED,
5835 -4.0,
5836 QCBOR_ERR_HALF_PRECISION_DISABLED
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005837#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005838 },
5839 {
5840 "Decimal fraction 3/10",
5841 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005842#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005843 0,
5844 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5845 0,
5846 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5847 0.30000000000000004,
5848 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005849#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005850 0,
5851 QCBOR_ERR_UNEXPECTED_TYPE,
5852 0,
5853 QCBOR_ERR_UNEXPECTED_TYPE,
5854 0.0,
5855 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005856#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005857 },
5858 {
5859 "+inifinity",
5860 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5861#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5862 0,
5863 QCBOR_ERR_FLOAT_EXCEPTION,
5864 0,
5865 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5866#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5867 0,
5868 QCBOR_ERR_HW_FLOAT_DISABLED,
5869 0,
5870 QCBOR_ERR_HW_FLOAT_DISABLED,
5871#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5872 INFINITY,
5873 QCBOR_SUCCESS
5874 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005875
5876 {
5877 "extreme pos bignum",
5878 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5879 // 50 rows of 8 is 400 digits.
5880 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5881 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5882 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5883 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5884 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5885 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5886 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5887 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5888 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5889 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5890 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5891 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5892 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5893 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5894 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5895 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5896 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5897 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5898 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5899 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5900 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5901 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5902 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5903 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5904 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5905 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5906 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5907 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5908 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5909 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5910 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5911 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5912 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5913 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5914 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5915 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5916 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5917 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5918 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5919 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5920 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5921 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5922 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5923 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5924 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5925 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5926 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 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},
5930 404},
5931 0,
5932 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5933 0,
5934 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5935#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5936 INFINITY,
5937 QCBOR_SUCCESS
5938#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5939 0,
5940 QCBOR_ERR_HW_FLOAT_DISABLED,
5941#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5942 },
5943
5944 {
5945 "extreme neg bignum",
5946 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5947 // 50 rows of 8 is 400 digits.
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, 0xf0,
5971 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5972 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5973 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5974 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5975 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5976 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5977 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5978 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5979 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5980 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5981 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5982 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5983 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5984 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5985 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5986 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5987 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5988 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5989 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5990 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5991 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5992 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5993 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5994 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5995 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5996 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5997 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5998 404},
5999 0,
6000 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6001 0,
6002 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6003#ifndef QCBOR_DISABLE_FLOAT_HW_USE
6004 -INFINITY,
6005 QCBOR_SUCCESS
6006#else /* QCBOR_DISABLE_FLOAT_HW_USE */
6007 0,
6008 QCBOR_ERR_HW_FLOAT_DISABLED,
6009#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
6010 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006011
6012 {
6013 "big float underflow [9223372036854775806, -9223372036854775806]",
6014 {(uint8_t[]){
6015 0xC5, 0x82,
6016 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6017 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006018#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006019 0,
6020 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6021 0,
6022 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6023 0,
6024 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006025#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006026 0,
6027 QCBOR_ERR_UNEXPECTED_TYPE,
6028 0,
6029 QCBOR_ERR_UNEXPECTED_TYPE,
6030 0.0,
6031 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006032#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006033 },
6034
6035 {
6036 "bigfloat that evaluates to -INFINITY",
6037 {(uint8_t[]){
6038 0xC5, 0x82,
6039 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6040 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006041#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006042 0,
6043 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6044 0,
6045 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6046 -INFINITY,
6047 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006048#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006049 0,
6050 QCBOR_ERR_UNEXPECTED_TYPE,
6051 0,
6052 QCBOR_ERR_UNEXPECTED_TYPE,
6053 0.0,
6054 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006055#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006056 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006057};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006058
6059
6060
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006061
6062static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6063{
6064 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6065#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6066 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6067 return 1;
6068 }
6069#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6070 (void)Pool;
6071#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6072 return 0;
6073}
6074
6075
Laurence Lundblade313b2862020-05-16 01:23:06 -07006076int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006077{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006078 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6079 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006080
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006081 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6082 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006083
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006084 // Set up the decoding context including a memory pool so that
6085 // indefinite length items can be checked
6086 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006087 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006088
6089 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006090 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6091 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006092 }
6093
6094 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006095 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006096 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006097 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006098 }
6099 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006100 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006101 }
6102
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006103 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006104 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6105 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006106 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006107
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006108 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006109 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006110 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006111 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006112 }
6113 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006114 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006115 }
6116
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006117 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006118 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6119 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006120 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006121#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006122 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006123 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006124 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006125 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006126 }
6127 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6128 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006129 // NaN's can't be compared for equality. A NaN is
6130 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006131 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006132 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006133 }
6134 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006135 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006136 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006137 }
6138 }
6139 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006140#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006141 }
6142
6143 return 0;
6144}
6145
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006146
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006147
6148
Laurence Lundbladee3553422020-05-02 11:11:17 -07006149int32_t CBORSequenceDecodeTests(void)
6150{
6151 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006152 QCBORItem Item;
6153 QCBORError uCBORError;
6154 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006155
6156 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006157
Laurence Lundbladee3553422020-05-02 11:11:17 -07006158 // The input for the date test happens to be a sequence so it
6159 // is reused. It is a sequence because it doesn't start as
6160 // an array or map.
6161 QCBORDecode_Init(&DCtx,
6162 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
6163 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006164
Laurence Lundbladee3553422020-05-02 11:11:17 -07006165 // Get the first item
6166 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6167 if(uCBORError != QCBOR_SUCCESS) {
6168 return 1;
6169 }
6170 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
6171 return 2;
6172 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006173
Laurence Lundblade87495732021-02-26 10:05:55 -07006174 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6175 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6176 uConsumed != 12) {
6177 return 102;
6178 }
6179
Laurence Lundbladee3553422020-05-02 11:11:17 -07006180 // Get a second item
6181 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07006182 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
6183 return 66;
6184 }
6185
Laurence Lundblade87495732021-02-26 10:05:55 -07006186 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6187 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6188 uConsumed != 14) {
6189 return 102;
6190 }
6191
Laurence Lundbladec7114722020-08-13 05:11:40 -07006192 // Get a third item
6193 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006194 if(uCBORError != QCBOR_SUCCESS) {
6195 return 2;
6196 }
6197 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6198 return 3;
6199 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006200
Laurence Lundbladee3553422020-05-02 11:11:17 -07006201 // A sequence can have stuff at the end that may
6202 // or may not be valid CBOR. The protocol decoder knows
6203 // when to stop by definition of the protocol, not
6204 // when the top-level map or array is ended.
6205 // Finish still has to be called to know that
6206 // maps and arrays (if there were any) were closed
6207 // off correctly. When called like this it
6208 // must return the error QCBOR_ERR_EXTRA_BYTES.
6209 uCBORError = QCBORDecode_Finish(&DCtx);
6210 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6211 return 4;
6212 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006213
6214
Laurence Lundbladee3553422020-05-02 11:11:17 -07006215 // --- Test an empty input ----
6216 uint8_t empty[1];
6217 UsefulBufC Empty = {empty, 0};
6218 QCBORDecode_Init(&DCtx,
6219 Empty,
6220 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006221
Laurence Lundbladee3553422020-05-02 11:11:17 -07006222 uCBORError = QCBORDecode_Finish(&DCtx);
6223 if(uCBORError != QCBOR_SUCCESS) {
6224 return 5;
6225 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006226
6227
Laurence Lundbladee3553422020-05-02 11:11:17 -07006228 // --- Sequence with unclosed indefinite length array ---
6229 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006230
Laurence Lundbladee3553422020-05-02 11:11:17 -07006231 QCBORDecode_Init(&DCtx,
6232 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6233 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006234
Laurence Lundbladee3553422020-05-02 11:11:17 -07006235 // Get the first item
6236 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6237 if(uCBORError != QCBOR_SUCCESS) {
6238 return 7;
6239 }
6240 if(Item.uDataType != QCBOR_TYPE_INT64) {
6241 return 8;
6242 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006243
Laurence Lundbladee3553422020-05-02 11:11:17 -07006244 // Get a second item
6245 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006246#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006247 if(uCBORError != QCBOR_SUCCESS) {
6248 return 9;
6249 }
6250 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6251 return 10;
6252 }
6253
6254 // Try to finish before consuming all bytes to confirm
6255 // that the still-open error is returned.
6256 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006257 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006258 return 11;
6259 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006260#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6261 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6262 return 20;
6263 }
6264#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006265
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006266
Laurence Lundbladee3553422020-05-02 11:11:17 -07006267 // --- Sequence with a closed indefinite length array ---
6268 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006269
Laurence Lundbladee3553422020-05-02 11:11:17 -07006270 QCBORDecode_Init(&DCtx,
6271 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6272 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006273
Laurence Lundbladee3553422020-05-02 11:11:17 -07006274 // Get the first item
6275 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6276 if(uCBORError != QCBOR_SUCCESS) {
6277 return 12;
6278 }
6279 if(Item.uDataType != QCBOR_TYPE_INT64) {
6280 return 13;
6281 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006282
Laurence Lundbladee3553422020-05-02 11:11:17 -07006283 // Get a second item
6284 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006285#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6286
Laurence Lundbladee3553422020-05-02 11:11:17 -07006287 if(uCBORError != QCBOR_SUCCESS) {
6288 return 14;
6289 }
6290 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6291 return 15;
6292 }
6293
6294 // Try to finish before consuming all bytes to confirm
6295 // that the still-open error is returned.
6296 uCBORError = QCBORDecode_Finish(&DCtx);
6297 if(uCBORError != QCBOR_SUCCESS) {
6298 return 16;
6299 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006300#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6301 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6302 return 20;
6303 }
6304#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006305
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006306
Laurence Lundbladee3553422020-05-02 11:11:17 -07006307 return 0;
6308}
6309
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006310
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006311
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006312int32_t IntToTests()
6313{
6314 int nErrCode;
6315 int32_t n32;
6316 int16_t n16;
6317 int8_t n8;
6318 uint32_t u32;
6319 uint16_t u16;
6320 uint8_t u8;
6321 uint64_t u64;
6322
6323 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6324 if(nErrCode == -1 || n32 != 1) {
6325 return 1;
6326 }
6327
6328 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6329 if(nErrCode == -1 || n32 != INT32_MAX) {
6330 return 2;
6331 }
6332
6333 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6334 if(nErrCode == -1 || n32 != INT32_MIN) {
6335 return 3;
6336 }
6337
6338 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6339 if(nErrCode != -1) {
6340 return 4;
6341 }
6342
6343 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6344 if(nErrCode != -1) {
6345 return 5;
6346 }
6347
6348
6349 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6350 if(nErrCode == -1 || n16 != INT16_MAX) {
6351 return 6;
6352 }
6353
6354 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6355 if(nErrCode == -1 || n16 != INT16_MIN) {
6356 return 7;
6357 }
6358
6359 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6360 if(nErrCode == -1 || n16 != 1) {
6361 return 8;
6362 }
6363
6364 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6365 if(nErrCode != -1) {
6366 return 9;
6367 }
6368
6369 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6370 if(nErrCode != -1) {
6371 return 10;
6372 }
6373
6374
6375 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6376 if(nErrCode == -1 || n8 != 1) {
6377 return 11;
6378 }
6379
6380 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6381 if(nErrCode == -1 || n8 != INT8_MAX) {
6382 return 12;
6383 }
6384
6385 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6386 if(nErrCode == -1 || n8 != INT8_MIN) {
6387 return 13;
6388 }
6389
6390 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6391 if(nErrCode != -1) {
6392 return 14;
6393 }
6394
6395 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6396 if(nErrCode != -1) {
6397 return 15;
6398 }
6399
6400
6401 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6402 if(nErrCode == -1 || u32 != 1) {
6403 return 16;
6404 }
6405
6406 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6407 if(nErrCode == -1 || u32 != UINT32_MAX) {
6408 return 17;
6409 }
6410
6411 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6412 if(nErrCode == -1 || u32 != 0) {
6413 return 18;
6414 }
6415
6416 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6417 if(nErrCode != -1) {
6418 return 19;
6419 }
6420
6421 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6422 if(nErrCode != -1) {
6423 return 20;
6424 }
6425
6426
6427 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6428 if(nErrCode == -1 || u16 != UINT16_MAX) {
6429 return 21;
6430 }
6431
6432 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6433 if(nErrCode == -1 || u16 != 0) {
6434 return 22;
6435 }
6436
6437 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6438 if(nErrCode == -1 || u16 != 1) {
6439 return 23;
6440 }
6441
6442 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6443 if(nErrCode != -1) {
6444 return 24;
6445 }
6446
6447 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6448 if(nErrCode != -1) {
6449 return 25;
6450 }
6451
6452
6453 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6454 if(nErrCode == -1 || u8 != UINT8_MAX) {
6455 return 26;
6456 }
6457
6458 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6459 if(nErrCode == -1 || u8 != 0) {
6460 return 27;
6461 }
6462
6463 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6464 if(nErrCode == -1 || u8 != 1) {
6465 return 28;
6466 }
6467
6468 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6469 if(nErrCode != -1) {
6470 return 29;
6471 }
6472
6473 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6474 if(nErrCode != -1) {
6475 return 30;
6476 }
6477
6478
6479 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6480 if(nErrCode == -1 || u64 != 1) {
6481 return 31;
6482 }
6483
6484 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6485 if(nErrCode == -1 || u64 != INT64_MAX) {
6486 return 32;
6487 }
6488
6489 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6490 if(nErrCode == -1 || u64 != 0) {
6491 return 33;
6492 }
6493
6494 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6495 if(nErrCode != -1) {
6496 return 34;
6497 }
6498
6499 return 0;
6500}
6501
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006502
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006503
6504
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006505/*
6506A sequence with
6507 A wrapping bstr
6508 containing a map
6509 1
6510 2
6511 A wrapping bstr
6512 containing an array
6513 3
6514 wrapping bstr
6515 4
6516 5
6517 6
6518 array
6519 7
6520 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006521 */
6522
Laurence Lundblade55013642020-09-23 05:39:22 -07006523static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006524{
Laurence Lundblade55013642020-09-23 05:39:22 -07006525 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006526 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006527 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006528
Laurence Lundblade55013642020-09-23 05:39:22 -07006529 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006530
6531 QCBOREncode_BstrWrap(&EC);
6532 QCBOREncode_OpenMap(&EC);
6533 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6534 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6535 QCBOREncode_CloseMap(&EC);
6536 QCBOREncode_BstrWrap(&EC);
6537 QCBOREncode_OpenArray(&EC);
6538 QCBOREncode_AddInt64(&EC, 3);
6539 QCBOREncode_BstrWrap(&EC);
6540 QCBOREncode_AddInt64(&EC, 4);
6541 QCBOREncode_CloseBstrWrap(&EC, NULL);
6542 QCBOREncode_AddInt64(&EC, 5);
6543 QCBOREncode_CloseArray(&EC);
6544 QCBOREncode_CloseBstrWrap(&EC, NULL);
6545 QCBOREncode_AddInt64(&EC, 6);
6546 QCBOREncode_CloseBstrWrap(&EC, NULL);
6547 QCBOREncode_OpenArray(&EC);
6548 QCBOREncode_AddInt64(&EC, 7);
6549 QCBOREncode_AddInt64(&EC, 8);
6550 QCBOREncode_CloseArray(&EC);
6551
6552 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006553 if(uErr) {
6554 Encoded = NULLUsefulBufC;
6555 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006556
6557 return Encoded;
6558}
6559
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006560/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006561static const uint8_t spBreakInByteString[] = {
6562 0x41, 0xff
6563};
6564
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006565
6566int32_t EnterBstrTest()
6567{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006568 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006569
6570 QCBORDecodeContext DC;
6571
Laurence Lundblade55013642020-09-23 05:39:22 -07006572 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006573
Laurence Lundblade55013642020-09-23 05:39:22 -07006574 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006575
6576
Laurence Lundblade9b334962020-08-27 10:55:53 -07006577 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006578 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006579 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6580 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006581 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006582 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006583 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006584 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006585 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006586 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006587 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006588 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006589 QCBORDecode_ExitArray(&DC);
6590 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006591 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006592 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006593 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006594 QCBORDecode_GetInt64(&DC, &n7);
6595 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006596 QCBORDecode_ExitArray(&DC);
6597
6598 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006599 if(uErr) {
6600 return (int32_t)uErr;
6601 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006602
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006603
6604 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6605 * Successful because no items are fetched from byte string.
6606 */
6607 QCBORDecode_Init(&DC,
6608 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6609 0);
6610 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6611 uErr = QCBORDecode_GetError(&DC);
6612 if(uErr) {
6613 return 100 + (int32_t)uErr;
6614 }
6615
6616 QCBORDecode_ExitBstrWrapped(&DC);
6617 uErr = QCBORDecode_GetError(&DC);
6618 if(uErr) {
6619 return 200 + (int32_t)uErr;
6620 }
6621
6622 /* Try to get item that is a break out of a byte string wrapped CBOR.
6623 * It fails because there should be no break.
6624 */
6625 QCBORDecode_Init(&DC,
6626 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6627 0);
6628 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6629 QCBORItem Item;
6630 uErr = QCBORDecode_GetNext(&DC, &Item);
6631 if(uErr != QCBOR_ERR_BAD_BREAK) {
6632 return 300 + (int32_t)uErr;
6633 }
6634
6635 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006636}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006637
6638
6639
6640
6641static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006642 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006643
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006644 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006645 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006646 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6647 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6648 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006649
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006650 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006651 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6652 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6653 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006654
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006655 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006656 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006657 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6658 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006659
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006660 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006661 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6662 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006663
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006664 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006665 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006666 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6667 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006668
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006669 21,
6670 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6671 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6672
6673 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006674 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006675 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6676 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006677
6678 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006679 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6680 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006681
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006682 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006683 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006684 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6685 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006686
6687 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006688 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6689 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006690
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006691 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006692 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006693 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6694 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006695
6696 0x18, 0x33,
6697 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6698
6699 // MIME
6700 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006701 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6702 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6703 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006704
6705 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006706 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6707 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006708
6709 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006710 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6711 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6712 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006713
6714 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006715 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6716 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006717
6718 // UUID
6719 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006720 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6721 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006722
6723 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006724 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6725 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006726};
6727
6728int32_t DecodeTaggedTypeTests()
6729{
6730 QCBORDecodeContext DC;
6731 QCBORError uErr;
6732
6733 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6734
6735 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006736 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006737
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006738 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006739 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006740 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006741 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6742 return 1;
6743 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006744 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006745 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6746 return 2;
6747 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006748 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006749 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6750 return 3;
6751 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006752 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006753 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006754 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6755 return 4;
6756 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006757 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006758 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006759 return 5;
6760 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006761
Laurence Lundblade9b334962020-08-27 10:55:53 -07006762 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006763 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6764 bNeg != false) {
6765 return 10;
6766 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006767 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006768 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6769 bNeg != true) {
6770 return 11;
6771 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006772 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006773 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6774 return 12;
6775 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006776 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006777 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006778 return 13;
6779 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006780 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006781 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006782 return 14;
6783 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006784
Laurence Lundblade9b334962020-08-27 10:55:53 -07006785 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006786 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6787 return 20;
6788 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006789 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006790 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6791 return 21;
6792 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006793 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006794 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006795 return 22;
6796 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006797 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006798 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006799 return 23;
6800 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006801
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006802#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006803 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006804 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6805 return 30;
6806 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006807#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006808 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006809 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6810 return 31;
6811 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006812 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006813 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006814 return 32;
6815 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006816 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006817 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006818 return 33;
6819 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006820
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006821#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006822 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006823 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6824 return 40;
6825 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006826#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006827 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006828 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6829 return 41;
6830 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006831 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006832 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006833 return 42;
6834 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006835 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006836 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006837 return 43;
6838 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006839
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006840#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006841 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006842 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6843 return 50;
6844 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006845#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006846 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006847 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6848 return 51;
6849 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006850 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006851 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006852 return 52;
6853 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006854 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006855 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006856 return 53;
6857 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006858
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006859#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006860 // MIME
6861 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006862 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006863 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6864 bIsNot7Bit == true) {
6865 return 60;
6866 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006867 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006868 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6869 bIsNot7Bit == true) {
6870 return 61;
6871 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006872 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006873 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6874 bIsNot7Bit == false) {
6875 return 62;
6876 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006877 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006878 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6879 bIsNot7Bit == false) {
6880 return 63;
6881 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006882 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006883 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006884 return 64;
6885 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006886 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006887 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006888 return 65;
6889 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006890
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006891
Laurence Lundblade9b334962020-08-27 10:55:53 -07006892 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006893 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6894 return 70;
6895 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006896#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
6897
Laurence Lundblade9b334962020-08-27 10:55:53 -07006898 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006899 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6900 return 71;
6901 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006902 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006903 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006904 return 72;
6905 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006906 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006907 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006908 return 73;
6909 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006910
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006911 // Improvement: add some more error test cases
6912
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006913 QCBORDecode_ExitMap(&DC);
6914
6915 uErr = QCBORDecode_Finish(&DC);
6916 if(uErr != QCBOR_SUCCESS) {
6917 return 100;
6918 }
6919
6920 return 0;
6921}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006922
6923
6924
6925
6926/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006927 [
6928 "aaaaaaaaaa",
6929 {}
6930 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006931 */
6932static const uint8_t spTooLarge1[] = {
6933 0x9f,
6934 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6935 0xa0,
6936 0xff
6937};
6938
6939/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006940 [
6941 {
6942 0: "aaaaaaaaaa"
6943 }
6944 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006945 */
6946static const uint8_t spTooLarge2[] = {
6947 0x9f,
6948 0xa1,
6949 0x00,
6950 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6951 0xff
6952};
6953
6954/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006955 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006956
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006957 {
6958 0: "aaaaaaaaaa"
6959 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006960 */
6961static const uint8_t spTooLarge3[] = {
6962 0x4d,
6963 0xa1,
6964 0x00,
6965 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6966};
6967
6968int32_t TooLargeInputTest(void)
6969{
6970 QCBORDecodeContext DC;
6971 QCBORError uErr;
6972 UsefulBufC String;
6973
6974 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6975 // to 10 There's not really any way to test this error
6976 // condition. The error condition is not complex, so setting
6977 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6978
6979 // The input CBOR is only too large because the
6980 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6981 //
6982 // This test is disabled for the normal test runs because of the
6983 // special build requirement.
6984
6985
6986 // Tests the start of a map being too large
6987 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006988 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006989 QCBORDecode_GetTextString(&DC, &String);
6990 uErr = QCBORDecode_GetError(&DC);
6991 if(uErr != QCBOR_SUCCESS) {
6992 return 1;
6993 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006994 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006995 uErr = QCBORDecode_GetError(&DC);
6996 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6997 return 2;
6998 }
6999
7000 // Tests the end of a map being too large
7001 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007002 QCBORDecode_EnterArray(&DC, NULL);
7003 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007004 uErr = QCBORDecode_GetError(&DC);
7005 if(uErr != QCBOR_SUCCESS) {
7006 return 3;
7007 }
7008 QCBORDecode_ExitMap(&DC);
7009 uErr = QCBORDecode_GetError(&DC);
7010 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7011 return 4;
7012 }
7013
7014 // Tests the entire input CBOR being too large when processing bstr wrapping
7015 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7016 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7017 uErr = QCBORDecode_GetError(&DC);
7018 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7019 return 5;
7020 }
7021
7022 return 0;
7023}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007024
7025
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007026#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7027
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007028static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007029 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007030 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7031 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7032 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7033 0x03,
7034 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7035 0xc3,
7036 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007037};
7038
7039int32_t SpiffyIndefiniteLengthStringsTests()
7040{
7041 QCBORDecodeContext DCtx;
7042
7043 QCBORDecode_Init(&DCtx,
7044 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7045 QCBOR_DECODE_MODE_NORMAL);
7046
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007047 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007048 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7049
7050 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007051 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007052 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
7053 if(QCBORDecode_GetAndResetError(&DCtx)) {
7054 return 1;
7055 }
7056
7057 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7058 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7059 return 2;
7060 }
7061
7062 uint64_t uInt;
7063 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7064 if(QCBORDecode_GetAndResetError(&DCtx)) {
7065 return 3;
7066 }
7067 if(uInt != 3) {
7068 return 4;
7069 }
7070
7071 double uDouble;
7072 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7073 "label2",
7074 0xff,
7075 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007076#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007077 if(QCBORDecode_GetAndResetError(&DCtx)) {
7078 return 5;
7079 }
7080 if(uDouble != -16777474) {
7081 return 6;
7082 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007083#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007084 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7085 return 7;
7086 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007087#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007088
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007089
7090 QCBORDecode_ExitMap(&DCtx);
7091
7092 if(QCBORDecode_Finish(&DCtx)) {
7093 return 99;
7094 }
7095
7096 return 0;
7097}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007098#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007099
7100
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007101/*
7102 * An array of an integer and an array. The second array contains
7103 * a bstr-wrapped map.
7104 *
7105 * [7, [h'A36D6669... (see next lines) 73']]
7106 *
7107 * {"first integer": 42,
7108 * "an array of two strings": ["string1", "string2"],
7109 * "map in a map":
7110 * { "bytes 1": h'78787878',
7111 * "bytes 2": h'79797979',
7112 * "another int": 98,
7113 * "text 2": "lies, damn lies and statistics"
7114 * }
7115 * }
7116 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007117
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007118static const uint8_t pValidWrappedMapEncoded[] = {
7119 0x82, 0x07, 0x81, 0x58, 0x97,
7120 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7121 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7122 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7123 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7124 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7125 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7126 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7127 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7128 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7129 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7130 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7131 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7132 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7133 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7134 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7135 0x73
7136};
7137
7138#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7139
7140/* As above, but the arrays are indefinite length */
7141static const uint8_t pValidIndefWrappedMapEncoded[] = {
7142 0x9f, 0x07, 0x9f, 0x58, 0x97,
7143 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7144 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7145 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7146 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7147 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7148 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7149 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7150 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7151 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7152 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7153 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7154 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7155 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7156 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7157 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7158 0x73,
7159 0xff, 0xff
7160};
7161#endif
7162
7163
7164static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7165
7166#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7167static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7168#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7169
7170#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7171static const uint8_t pWrappedByIndefiniteLength[] = {
7172 0x81,
7173 0xd8, 0x18,
7174 0x5f,
7175 0x41, 0x83,
7176 0x41, 0x18,
7177 0x43, 0x2A, 0x18, 0x2B,
7178 0x42, 0x18, 0x2C,
7179 0xff
7180};
7181#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7182
7183
7184int32_t PeekAndRewindTest()
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007185{
7186 QCBORItem Item;
7187 QCBORError nCBORError;
7188 QCBORDecodeContext DCtx;
7189
7190 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7191
7192 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7193 return 100+(int32_t)nCBORError;
7194 }
7195 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7196 return 200;
7197 }
7198
7199 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7200 return (int32_t)nCBORError;
7201 }
7202 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7203 return 300;
7204 }
7205
7206 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7207 return 400 + (int32_t)nCBORError;
7208 }
7209 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7210 return 500;
7211 }
7212
7213 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7214 return (int32_t)nCBORError;
7215 }
7216 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7217 return 600;
7218 }
7219
7220 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7221 return 900 + (int32_t)nCBORError;
7222 }
7223 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7224 Item.uDataType != QCBOR_TYPE_INT64 ||
7225 Item.val.int64 != 42 ||
7226 Item.uDataAlloc ||
7227 Item.uLabelAlloc ||
7228 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7229 return 1000;
7230 }
7231
7232 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7233 return 1100 + (int32_t)nCBORError;
7234 }
7235
7236 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7237 Item.uDataType != QCBOR_TYPE_INT64 ||
7238 Item.val.int64 != 42 ||
7239 Item.uDataAlloc ||
7240 Item.uLabelAlloc ||
7241 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7242 return 1200;
7243 }
7244
7245
7246 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7247 return 1300 + (int32_t)nCBORError;
7248 }
7249 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7250 Item.uDataAlloc ||
7251 Item.uLabelAlloc ||
7252 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7253 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007254 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007255 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007256 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007257
7258 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7259 return 1500 + (int32_t)nCBORError;
7260 }
7261 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7262 Item.uDataAlloc ||
7263 Item.uLabelAlloc ||
7264 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7265 return 1600;
7266 }
7267
7268 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7269 return 1700 + (int32_t)nCBORError;
7270 }
7271 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7272 Item.uDataAlloc ||
7273 Item.uLabelAlloc ||
7274 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7275 return 1800;
7276 }
7277
7278 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7279 return (int32_t)nCBORError;
7280 }
7281 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7282 Item.uDataAlloc ||
7283 Item.uLabelAlloc ||
7284 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7285 return 1900;
7286 }
7287
7288 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7289 return (int32_t)nCBORError;
7290 }
7291 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7292 Item.uDataAlloc ||
7293 Item.uLabelAlloc ||
7294 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7295 return 2000;
7296 }
7297
7298
7299 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7300 return 2100 + (int32_t)nCBORError;
7301 }
7302 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7303 Item.uDataAlloc ||
7304 Item.uLabelAlloc ||
7305 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7306 Item.uDataType != QCBOR_TYPE_MAP ||
7307 Item.val.uCount != 4) {
7308 return 2100;
7309 }
7310
7311 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7312 return 2200 + (int32_t)nCBORError;
7313 }
7314 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7315 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7316 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7317 Item.uDataAlloc ||
7318 Item.uLabelAlloc ||
7319 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7320 return 2300;
7321 }
7322
7323 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7324 return 2400 + (int32_t)nCBORError;
7325 }
7326 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7327 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7328 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7329 Item.uDataAlloc ||
7330 Item.uLabelAlloc ||
7331 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7332 return 2500;
7333 }
7334
7335 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7336 return 2600 + (int32_t)nCBORError;
7337 }
7338 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7339 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7340 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7341 Item.uDataAlloc ||
7342 Item.uLabelAlloc ||
7343 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7344 return 2700;
7345 }
7346
7347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7348 return 2800 + (int32_t)nCBORError;
7349 }
7350 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7351 Item.uDataAlloc ||
7352 Item.uLabelAlloc ||
7353 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7354 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007355 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007356 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007357 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007358
7359 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7360 return 3000 + (int32_t)nCBORError;
7361 }
7362 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7363 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7364 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7365 Item.uDataAlloc ||
7366 Item.uLabelAlloc ||
7367 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7368 return 3100;
7369 }
7370
7371 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7372 return 3200 + (int32_t)nCBORError;
7373 }
7374 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7375 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7376 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7377 Item.uDataAlloc ||
7378 Item.uLabelAlloc ||
7379 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7380 return 3300;
7381 }
7382
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007383
7384
7385 // Rewind to top level after entering several maps
7386 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7387
7388 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7389 return (int32_t)nCBORError;
7390 }
7391 if(Item.uDataType != QCBOR_TYPE_MAP ||
7392 Item.val.uCount != 3) {
7393 return 400;
7394 }
7395
7396 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7397 return 4000+(int32_t)nCBORError;
7398 }
7399
7400 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7401 Item.uDataType != QCBOR_TYPE_INT64 ||
7402 Item.val.int64 != 42 ||
7403 Item.uDataAlloc ||
7404 Item.uLabelAlloc ||
7405 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7406 return 4100;
7407 }
7408
7409 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7410 return 4100+(int32_t)nCBORError;
7411 }
7412 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7413 Item.uDataAlloc ||
7414 Item.uLabelAlloc ||
7415 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7416 Item.uDataType != QCBOR_TYPE_ARRAY ||
7417 Item.val.uCount != 2) {
7418 return 4200;
7419 }
7420
7421 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7422 return 4200+(int32_t)nCBORError;
7423 }
7424 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7425 Item.uDataAlloc ||
7426 Item.uLabelAlloc ||
7427 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7428 return 4300;
7429 }
7430
7431 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7432 return 4300+(int32_t)nCBORError;
7433 }
7434 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7435 Item.uDataAlloc ||
7436 Item.uLabelAlloc ||
7437 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7438 return 4400;
7439 }
7440
7441 QCBORDecode_Rewind(&DCtx);
7442
7443 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7444 return 4400+(int32_t)nCBORError;
7445 }
7446 if(Item.uDataType != QCBOR_TYPE_MAP ||
7447 Item.val.uCount != 3) {
7448 return 4500;
7449 }
7450
7451 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7452 return (int32_t)nCBORError;
7453 }
7454
7455 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7456 Item.uDataType != QCBOR_TYPE_INT64 ||
7457 Item.val.int64 != 42 ||
7458 Item.uDataAlloc ||
7459 Item.uLabelAlloc ||
7460 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7461 return 4600;
7462 }
7463
7464 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7465 return (int32_t)nCBORError;
7466 }
7467 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7468 Item.uDataAlloc ||
7469 Item.uLabelAlloc ||
7470 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7471 Item.uDataType != QCBOR_TYPE_ARRAY ||
7472 Item.val.uCount != 2) {
7473 return 4700;
7474 }
7475
7476 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7477 return (int32_t)nCBORError;
7478 }
7479 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7480 Item.uDataAlloc ||
7481 Item.uLabelAlloc ||
7482 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7483 return 4800;
7484 }
7485
7486 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7487 return 4900+(int32_t)nCBORError;
7488 }
7489 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7490 Item.uDataAlloc ||
7491 Item.uLabelAlloc ||
7492 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7493 return 5000;
7494 }
7495
7496
7497 // Rewind an entered map
7498 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7499
7500 QCBORDecode_EnterMap(&DCtx, NULL);
7501
7502 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7503 return 5100+(int32_t)nCBORError;
7504 }
7505
7506 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7507 Item.uDataType != QCBOR_TYPE_INT64 ||
7508 Item.val.int64 != 42 ||
7509 Item.uDataAlloc ||
7510 Item.uLabelAlloc ||
7511 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7512 return 5200;
7513 }
7514
7515 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7516 return 5200+(int32_t)nCBORError;
7517 }
7518 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7519 Item.uDataAlloc ||
7520 Item.uLabelAlloc ||
7521 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7522 Item.uDataType != QCBOR_TYPE_ARRAY ||
7523 Item.val.uCount != 2) {
7524 return -5300;
7525 }
7526
7527 QCBORDecode_Rewind(&DCtx);
7528
7529 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7530 return 5300+(int32_t)nCBORError;
7531 }
7532
7533 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7534 Item.uDataType != QCBOR_TYPE_INT64 ||
7535 Item.val.int64 != 42 ||
7536 Item.uDataAlloc ||
7537 Item.uLabelAlloc ||
7538 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7539 return 5400;
7540 }
7541
7542 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7543 return 5400+(int32_t)nCBORError;
7544 }
7545 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7546 Item.uDataAlloc ||
7547 Item.uLabelAlloc ||
7548 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7549 Item.uDataType != QCBOR_TYPE_ARRAY ||
7550 Item.val.uCount != 2) {
7551 return 5500;
7552 }
7553
7554
7555 // Rewind and entered array inside an entered map
7556 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7557
7558 QCBORDecode_EnterMap(&DCtx, NULL);
7559
7560 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7561
7562 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7563 return 5600+(int32_t)nCBORError;
7564 }
7565 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7566 Item.uDataAlloc ||
7567 Item.uLabelAlloc ||
7568 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7569 return 5700;
7570 }
7571
7572 QCBORDecode_Rewind(&DCtx);
7573
7574 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7575 return 5700+(int32_t)nCBORError;
7576 }
7577 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7578 Item.uDataAlloc ||
7579 Item.uLabelAlloc ||
7580 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7581 return 5800;
7582 }
7583
7584 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7585 return (int32_t)nCBORError;
7586 }
7587 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7588 Item.uDataAlloc ||
7589 Item.uLabelAlloc ||
7590 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7591 return 5900;
7592 }
7593
7594 QCBORDecode_Rewind(&DCtx);
7595
7596 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7597 return 5900+(int32_t)nCBORError;
7598 }
7599 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7600 Item.uDataAlloc ||
7601 Item.uLabelAlloc ||
7602 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7603 return 6000;
7604 }
7605
7606
7607 // Rewind a byte string inside an array inside an array
7608 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
7609
7610 QCBORDecode_EnterArray(&DCtx, NULL);
7611
7612 uint64_t i;
7613 QCBORDecode_GetUInt64(&DCtx, &i);
7614
7615 QCBORDecode_EnterArray(&DCtx, NULL);
7616
7617 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7618 if(QCBORDecode_GetError(&DCtx)) {
7619 return 6100;
7620 }
7621
7622 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7623 return (int32_t)nCBORError;
7624 }
7625 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7626 return 6200;
7627 }
7628
7629 QCBORDecode_Rewind(&DCtx);
7630
7631 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7632 return 6300+(int32_t)nCBORError;
7633 }
7634 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7635 return 6400;
7636 }
7637
7638#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7639 // Rewind a byte string inside an indefinite-length array inside
7640 // indefinite-length array
7641
7642 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
7643
7644 QCBORDecode_EnterArray(&DCtx, NULL);
7645
7646 QCBORDecode_GetUInt64(&DCtx, &i);
7647
7648 QCBORDecode_EnterArray(&DCtx, NULL);
7649
7650 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7651 if(QCBORDecode_GetError(&DCtx)) {
7652 return 6500;
7653 }
7654
7655 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7656 return 6600+(int32_t)nCBORError;
7657 }
7658 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7659 return 6700;
7660 }
7661
7662 QCBORDecode_Rewind(&DCtx);
7663
7664 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7665 return 6800+(int32_t)nCBORError;
7666 }
7667 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7668 return 6900;
7669 }
7670#endif
7671
7672 // Rewind an empty map
7673 // [100, {}]
7674 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
7675 QCBORDecode_EnterArray(&DCtx, NULL);
7676 QCBORDecode_GetUInt64(&DCtx, &i);
7677 if(i != 100) {
7678 return 7010;
7679 }
7680 QCBORDecode_EnterMap(&DCtx, NULL);
7681
7682 /* Do it 5 times to be sure multiple rewinds work */
7683 for(int n = 0; n < 5; n++) {
7684 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7685 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7686 return 7000 + n;
7687 }
7688 QCBORDecode_Rewind(&DCtx);
7689 }
7690 QCBORDecode_ExitMap(&DCtx);
7691 QCBORDecode_Rewind(&DCtx);
7692 QCBORDecode_GetUInt64(&DCtx, &i);
7693 if(i != 100) {
7694 return 7010;
7695 }
7696 QCBORDecode_ExitArray(&DCtx);
7697 QCBORDecode_Rewind(&DCtx);
7698 QCBORDecode_EnterArray(&DCtx, NULL);
7699 i = 9;
7700 QCBORDecode_GetUInt64(&DCtx, &i);
7701 if(i != 100) {
7702 return 7020;
7703 }
7704 if(QCBORDecode_GetError(&DCtx)){
7705 return 7030;
7706 }
7707
7708 // Rewind an empty indefinite length map
7709#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7710 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
7711 QCBORDecode_EnterArray(&DCtx, NULL);
7712 QCBORDecode_GetUInt64(&DCtx, &i);
7713 if(i != 100) {
7714 return 7810;
7715 }
7716 QCBORDecode_EnterMap(&DCtx, NULL);
7717
7718 /* Do it 5 times to be sure multiple rewinds work */
7719 for(int n = 0; n < 5; n++) {
7720 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7721 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7722 return 7800 + n;
7723 }
7724 QCBORDecode_Rewind(&DCtx);
7725 }
7726 QCBORDecode_ExitMap(&DCtx);
7727 QCBORDecode_Rewind(&DCtx);
7728 QCBORDecode_GetUInt64(&DCtx, &i);
7729 if(i != 100) {
7730 return 7810;
7731 }
7732 QCBORDecode_ExitArray(&DCtx);
7733 QCBORDecode_Rewind(&DCtx);
7734 QCBORDecode_EnterArray(&DCtx, NULL);
7735 i = 9;
7736 QCBORDecode_GetUInt64(&DCtx, &i);
7737 if(i != 100) {
7738 return 7820;
7739 }
7740 if(QCBORDecode_GetError(&DCtx)){
7741 return 7830;
7742 }
7743#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7744
7745 // Rewind an indefnite length byte-string wrapped sequence
7746#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7747 QCBORDecode_Init(&DCtx,
7748 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
7749 0);
7750 UsefulBuf_MAKE_STACK_UB(Pool, 100);
7751 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
7752
7753 QCBORDecode_EnterArray(&DCtx, NULL);
7754 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
7755 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007756 /* this is what happens when trying to enter
7757 indefinite-length byte string
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007758 wrapped CBOR. Tolerate for now. Eventually it needs
7759 to be fixed so this works, but that is not simple. */
7760 return 7300;
7761 }
7762
7763 /*
7764 QCBORDecode_GetUInt64(&DCtx, &i);
7765 if(i != 42) {
7766 return 7110;
7767 }
7768 QCBORDecode_Rewind(&DCtx);
7769 QCBORDecode_GetUInt64(&DCtx, &i);
7770 if(i != 42) {
7771 return 7220;
7772 }*/
7773#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7774
7775
7776 // Rewind an indefnite length byte-string wrapped sequence
7777
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007778 return 0;
7779}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007780
7781
7782
7783
7784static const uint8_t spBooleansInMap[] =
7785{
7786 0xa1, 0x08, 0xf5
7787};
7788
7789static const uint8_t spBooleansInMapWrongType[] =
7790{
7791 0xa1, 0x08, 0xf6
7792};
7793
7794static const uint8_t spBooleansInMapNWF[] =
7795{
7796 0xa1, 0x08, 0x1a
7797};
7798
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007799static const uint8_t spNullInMap[] =
7800{
7801 0xa1, 0x08, 0xf6
7802};
7803
7804static const uint8_t spUndefinedInMap[] =
7805{
7806 0xa1, 0x08, 0xf7
7807};
7808
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007809
7810int32_t BoolTest(void)
7811{
7812 QCBORDecodeContext DCtx;
7813 bool b;
7814
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007815 QCBORDecode_Init(&DCtx,
7816 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7817 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007818 QCBORDecode_EnterMap(&DCtx, NULL);
7819 QCBORDecode_GetBool(&DCtx, &b);
7820 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7821 return 1;
7822 }
7823
7824 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
7825 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7826 return 2;
7827 }
7828
7829 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
7830 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7831 return 3;
7832 }
7833
7834
7835 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
7836 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7837 return 4;
7838 }
7839
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007840 QCBORDecode_Init(&DCtx,
7841 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
7842 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007843 QCBORDecode_EnterMap(&DCtx, NULL);
7844 QCBORDecode_GetBool(&DCtx, &b);
7845 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7846 return 5;
7847 }
7848
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007849 QCBORDecode_Init(&DCtx,
7850 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7851 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007852 QCBORDecode_EnterMap(&DCtx, NULL);
7853 QCBORDecode_GetBool(&DCtx, &b);
7854 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7855 return 6;
7856 }
7857
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007858
7859 QCBORDecode_Init(&DCtx,
7860 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7861 0);
7862 QCBORDecode_EnterMap(&DCtx, NULL);
7863 QCBORDecode_GetNull(&DCtx);
7864 if(QCBORDecode_GetAndResetError(&DCtx)) {
7865 return 7;
7866 }
7867
7868 QCBORDecode_Init(&DCtx,
7869 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7870 0);
7871 QCBORDecode_EnterMap(&DCtx, NULL);
7872 QCBORDecode_GetNull(&DCtx);
7873 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7874 return 8;
7875 }
7876
7877 QCBORDecode_Init(&DCtx,
7878 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7879 0);
7880 QCBORDecode_EnterMap(&DCtx, NULL);
7881 QCBORDecode_GetNullInMapN(&DCtx, 8);
7882 if(QCBORDecode_GetAndResetError(&DCtx)) {
7883 return 9;
7884 }
7885
7886 QCBORDecode_Init(&DCtx,
7887 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7888 0);
7889 QCBORDecode_EnterMap(&DCtx, NULL);
7890 QCBORDecode_GetNullInMapN(&DCtx, 8);
7891 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7892 return 10;
7893 }
7894
7895 QCBORDecode_Init(&DCtx,
7896 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7897 0);
7898 QCBORDecode_EnterMap(&DCtx, NULL);
7899 QCBORDecode_GetUndefined(&DCtx);
7900 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7901 return 11;
7902 }
7903
7904 QCBORDecode_Init(&DCtx,
7905 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7906 0);
7907 QCBORDecode_EnterMap(&DCtx, NULL);
7908 QCBORDecode_GetUndefined(&DCtx);
7909 if(QCBORDecode_GetAndResetError(&DCtx)) {
7910 return 12;
7911 }
7912
7913 QCBORDecode_Init(&DCtx,
7914 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7915 0);
7916 QCBORDecode_EnterMap(&DCtx, NULL);
7917 QCBORDecode_GetUndefined(&DCtx);
7918 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7919 return 13;
7920 }
7921
7922 QCBORDecode_Init(&DCtx,
7923 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7924 0);
7925 QCBORDecode_EnterMap(&DCtx, NULL);
7926 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
7927 if(QCBORDecode_GetAndResetError(&DCtx)) {
7928 return 14;
7929 }
7930
7931 QCBORDecode_Init(&DCtx,
7932 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7933 0);
7934 QCBORDecode_EnterMap(&DCtx, NULL);
7935 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
7936 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7937 return 15;
7938 }
7939
7940 QCBORDecode_Init(&DCtx,
7941 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7942 0);
7943 QCBORDecode_EnterMap(&DCtx, NULL);
7944 QCBORDecode_GetUndefined(&DCtx);
7945 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7946 return 15;
7947 }
7948
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007949 return 0;
7950}