blob: 4b20e7137acc9cdfb3a6cfeb2f313b0e7ee5d50b [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 Lundblade46d63e92021-05-13 11:37:10 -07002579 0xad, // 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 Lundblade46d63e92021-05-13 11:37:10 -07002618
2619 /* Tagged date-only date string */
2620 0x63, 0x53, 0x44, 0x53,
2621 0xD9, 0x03, 0xEC,
2622 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2623
2624 /* Untagged date-only date string */
2625 0x18, 0x63,
2626 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2627
2628 /* Tagged days-count epoch date */
2629 0x63, 0x53, 0x44, 0x45,
2630 0xD8, 0x64, /* tag(100) */
2631 0x39, 0x29, 0xB3, /* -10676 */
2632
2633 /* Untagged days-count epoch date */
2634 0x11,
2635 0x19, 0x0F, 0x9A /* 3994 */
2636
Laurence Lundbladec7114722020-08-13 05:11:40 -07002637};
2638
2639int32_t SpiffyDateDecodeTest()
2640{
2641 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002642 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002643 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2644 nEpochDate4, nEpochDate6, nEpochDateFail,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002645 nEpochDate1400000000, nEpochDays1, nEpochDays2;
2646 UsefulBufC StringDate1, StringDate2, StringDays1, StringDays2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002647 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002648
2649 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002650 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002651 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002652 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002653
Laurence Lundblade9b334962020-08-27 10:55:53 -07002654 // Too-negative float, -9.2233720368547748E+18
2655 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002656 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002657#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002658 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2659 return 1111;
2660 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002661#else
2662 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2663 return 1112;
2664 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002665#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade4b270642020-08-14 12:53:07 -07002666
2667 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002668 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002669 uError = QCBORDecode_GetAndResetError(&DC);
2670 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002671 return 1;
2672 }
2673
Laurence Lundblade4b270642020-08-14 12:53:07 -07002674 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002675 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002676 uError = QCBORDecode_GetAndResetError(&DC);
2677#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2678#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2679 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2680#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2681 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2682#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2683#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2684 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2685#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2686 if(uError != uExpectedforHalfMinusInfinity) {
2687 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002688 }
2689
Laurence Lundblade4b270642020-08-14 12:53:07 -07002690 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002691 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002692 uError = QCBORDecode_GetAndResetError(&DC);
2693 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2694 return 3;
2695 }
2696
2697 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002698 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002699 uError = QCBORDecode_GetAndResetError(&DC);
2700 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2701 return 4;
2702 }
2703
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002704 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002705
2706 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002707 QCBORDecode_GetEpochDateInMapN(&DC,
2708 5,
2709 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2710 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2711 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002712#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2713 if(nEpochDate2 != -9223372036854773760LL) {
2714 return 101;
2715 }
2716#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2717 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002718 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002719 return 102;
2720 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002721#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002722
Laurence Lundblade4b270642020-08-14 12:53:07 -07002723 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002724 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2725 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002726#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2727 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002728 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002729 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002730#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2731 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002732 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002733 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002734 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002735#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2736
2737 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002738 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2739 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002740#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2741 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002742 return 103;
2743 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002744#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2745 uError = QCBORDecode_GetAndResetError(&DC);
2746 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2747 return 104;
2748 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002749#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2750
Laurence Lundblade9b334962020-08-27 10:55:53 -07002751 // A half-precision date with value -2 FFF
2752 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2753 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002754#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2755 if(nEpochDate4 != -2) {
2756 return 105;
2757 }
2758#else
2759 uError = QCBORDecode_GetAndResetError(&DC);
2760 if(uError == QCBOR_SUCCESS) {
2761 return 106;
2762 }
2763#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002764
Laurence Lundblade4b270642020-08-14 12:53:07 -07002765
2766 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002767 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2768 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2769 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002770 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002771 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002772 return 107;
2773 }
2774
2775 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002776 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2777 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002778 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002779 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002780 return 108;
2781 }
2782
2783 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002784 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2785 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2786 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002787 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002788 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002789 return 109;
2790 }
2791
2792 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002793 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2794 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002795 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002796 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002797 return 110;
2798 }
2799
2800 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002801
Laurence Lundblade4b270642020-08-14 12:53:07 -07002802 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002803 QCBORDecode_GetEpochDateInMapN(&DC,
2804 1,
2805 QCBOR_TAG_REQUIREMENT_TAG |
2806 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2807 &nEpochDate1400000000);
2808 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002809 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002810 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2811 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002812 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002813 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2814 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002815 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002816 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2817 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002818 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002819 QCBORDecode_GetEpochDateInMapSZ(&DC,
2820 "z",
2821 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2822 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2823 &nEpochDate6);
2824 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002825
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002826 /* The days format is much simpler than the date format
2827 * because it can't be a floating point value. The test
2828 * of the spiffy decode functions sufficiently covers
2829 * the test of the non-spiffy decode days date decoding.
2830 * There is no full fan out of the error conditions
2831 * and decode options as that is implemented by code
2832 * that is tested well by the date testing above.
2833 */
2834 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
2835 &StringDays1);
2836
2837 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2838 &StringDays2);
2839
2840 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
2841 &nEpochDays1);
2842
2843 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2844 &nEpochDays2);
2845
Laurence Lundblade4b270642020-08-14 12:53:07 -07002846 QCBORDecode_ExitMap(&DC);
2847 QCBORDecode_ExitArray(&DC);
2848 uError = QCBORDecode_Finish(&DC);
2849 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002850 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002851 }
2852
Laurence Lundblade9b334962020-08-27 10:55:53 -07002853 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002854 return 200;
2855 }
2856
Laurence Lundblade9b334962020-08-27 10:55:53 -07002857 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002858 return 201;
2859 }
2860
Laurence Lundblade9b334962020-08-27 10:55:53 -07002861 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002862 return 202;
2863 }
2864
Laurence Lundblade9b334962020-08-27 10:55:53 -07002865 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002866 return 203;
2867 }
2868
Laurence Lundblade9b334962020-08-27 10:55:53 -07002869 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002870 return 204;
2871 }
2872
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002873 if(nEpochDays1 != -10676) {
2874 return 205;
2875 }
2876
2877 if(nEpochDays2 != 3994) {
2878 return 206;
2879 }
2880
Laurence Lundblade9b334962020-08-27 10:55:53 -07002881 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2882 return 205;
2883 }
2884
2885 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2886 return 206;
2887 }
2888
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002889 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
2890 return 207;
2891 }
2892
2893 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
2894 return 208;
2895 }
2896
Laurence Lundbladec7114722020-08-13 05:11:40 -07002897 return 0;
2898}
2899
2900
2901
Laurence Lundblade9b334962020-08-27 10:55:53 -07002902// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002903static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002904 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002905 0x81, // Array of one
2906 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2907 0x82, // Array of two that is the faction 1/3
2908 0x01,
2909 0x03,
2910
2911 /*
2912 More than 4 tags on an item 225(226(227(228(229([])))))
2913 */
2914 0xd8, 0xe1,
2915 0xd8, 0xe2,
2916 0xd8, 0xe3,
2917 0xd8, 0xe4,
2918 0xd8, 0xe5,
2919 0x80,
2920
2921 /* tag 10489608748473423768(
2922 2442302356(
2923 21590(
2924 240(
2925 []))))
2926 */
2927 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2928 0xda, 0x91, 0x92, 0x93, 0x94,
2929 0xd9, 0x54, 0x56,
2930 0xd8, 0xf0,
2931 0x80,
2932
2933 /* tag 21590(
2934 10489608748473423768(
2935 2442302357(
2936 65534(
2937 []))))
2938 */
2939 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2940 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2941 0xda, 0x91, 0x92, 0x93, 0x95,
2942 0xd9, 0xff, 0xfe,
2943 0x80,
2944
2945 /* Make sure to blow past the limit of tags that must be mapped.
2946 works in conjuntion with entries above.
2947 269488144(269488145(269488146(269488147([]))))
2948 */
2949 0xda, 0x10, 0x10, 0x10, 0x10,
2950 0xda, 0x10, 0x10, 0x10, 0x11,
2951 0xda, 0x10, 0x10, 0x10, 0x12,
2952 0xda, 0x10, 0x10, 0x10, 0x13,
2953 0x80,
2954
2955 /* An invalid decimal fraction with an additional tag */
2956 0xd9, 0xff, 0xfa,
2957 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2958 0x00, // the integer 0; should be a byte string
2959};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002960
Laurence Lundblade59289e52019-12-30 13:44:37 -08002961/*
2962 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002963 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002964 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002965static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002966 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002967
Laurence Lundblade59289e52019-12-30 13:44:37 -08002968/*
2969DB 9192939495969798 # tag(10489608748473423768)
2970 D8 88 # tag(136)
2971 C6 # tag(6)
2972 C7 # tag(7)
2973 80 # array(0)
2974*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002975static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002976 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002977
2978/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002979 55799(55799(55799({
2980 6(7(-23)): 5859837686836516696(7({
2981 7(-20): 11({
2982 17(-18): 17(17(17("Organization"))),
2983 9(-17): 773("SSG"),
2984 -15: 16(17(6(7("Confusion")))),
2985 17(-16): 17("San Diego"),
2986 17(-14): 17("US")
2987 }),
2988 23(-19): 19({
2989 -11: 9({
2990 -9: -7
2991 }),
2992 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2993 })
2994 })),
2995 16(-22): 23({
2996 11(8(7(-5))): 8(-3)
2997 })
2998 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002999 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003000static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003001 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3002 0xc6, 0xc7, 0x36,
3003 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3004 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3005 0xcb, 0xa5,
3006 0xd1, 0x31,
3007 0xd1, 0xd1, 0xd1, 0x6c,
3008 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3009 0xc9, 0x30,
3010 0xd9, 0x03, 0x05, 0x63,
3011 0x53, 0x53, 0x47,
3012 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003013 0xd0, 0xd1, 0xc6, 0xc7,
3014 0x69,
3015 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003016 0xd1, 0x2f,
3017 0xd1, 0x69,
3018 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3019 0xd1, 0x2d,
3020 0xd1, 0x62,
3021 0x55, 0x53,
3022 0xd7, 0x32,
3023 0xd3, 0xa2,
3024 0x2a,
3025 0xc9, 0xa1,
3026 0x28,
3027 0x26,
3028 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3029 0xcc, 0x4a,
3030 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3031 0xd0, 0x35,
3032 0xd7, 0xa1,
3033 0xcb, 0xc8, 0xc7, 0x24,
3034 0xc8, 0x22};
3035
Laurence Lundblade9b334962020-08-27 10:55:53 -07003036
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003037static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003038 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003039
3040 0xc0, // tag for string date
3041 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3042
3043 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3044
3045 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3046
3047 0xd8, 0x23, // tag for regex
3048 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3049
3050 0xc0, // tag for string date
3051 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundblade9b334962020-08-27 10:55:53 -07003052};
3053
3054
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003055static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003056
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003057
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003058int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003059{
3060 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003061 QCBORItem Item;
3062 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003063
Laurence Lundbladeee851742020-01-08 08:37:05 -08003064 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003065 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003066 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003067
Laurence Lundblade9b334962020-08-27 10:55:53 -07003068 /*
3069 This test matches the magic number tag and the fraction tag
3070 55799([...])
3071 */
3072 uError = QCBORDecode_GetNext(&DCtx, &Item);
3073 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003074 return -2;
3075 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003076 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003077 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3078 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003079 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003080
Laurence Lundblade9b334962020-08-27 10:55:53 -07003081 /*
3082 4([1,3])
3083 */
3084 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003085#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003086 if(uError != QCBOR_SUCCESS ||
3087 Item.uDataType != QCBOR_TYPE_ARRAY ||
3088 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3089 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3090 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3091 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3092 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3093 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3094 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003095 return -4;
3096 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003097 // consume the items in the array
3098 uError = QCBORDecode_GetNext(&DCtx, &Item);
3099 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003100
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003101#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003102 if(uError != QCBOR_SUCCESS ||
3103 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3104 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3105 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3106 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3107 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3108 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3109 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003110 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003111#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003112
Laurence Lundblade9b334962020-08-27 10:55:53 -07003113 /*
3114 More than 4 tags on an item 225(226(227(228(229([])))))
3115 */
3116 uError = QCBORDecode_GetNext(&DCtx, &Item);
3117 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003118 return -6;
3119 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003120
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003121 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3122 return -106;
3123 }
3124
3125
Laurence Lundblade9b334962020-08-27 10:55:53 -07003126 /* tag 10489608748473423768(
3127 2442302356(
3128 21590(
3129 240(
3130 []))))
3131 */
3132 uError = QCBORDecode_GetNext(&DCtx, &Item);
3133 if(uError != QCBOR_SUCCESS ||
3134 Item.uDataType != QCBOR_TYPE_ARRAY ||
3135 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3136 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3137 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3138 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003139 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003140 }
3141
3142 /* tag 21590(
3143 10489608748473423768(
3144 2442302357(
3145 21591(
3146 []))))
3147 */
3148 uError = QCBORDecode_GetNext(&DCtx, &Item);
3149 if(uError != QCBOR_SUCCESS ||
3150 Item.uDataType != QCBOR_TYPE_ARRAY ||
3151 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3152 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3153 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3154 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3155 return -8;
3156 }
3157
3158 /* Make sure to blow past the limit of tags that must be mapped.
3159 works in conjuntion with entries above.
3160 269488144(269488145(269488146(269488147([]))))
3161 */
3162 uError = QCBORDecode_GetNext(&DCtx, &Item);
3163 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3164 return -9;
3165 }
3166
3167 uError = QCBORDecode_GetNext(&DCtx, &Item);
3168 if(uError == QCBOR_SUCCESS) {
3169 return -10;
3170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003171
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003172 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003173 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003174 // tage and then matches it. Caller-config lists are no longer
3175 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003176 QCBORDecode_Init(&DCtx,
3177 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3178 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003179 const uint64_t puList[] = {0x9192939495969798, 257};
3180 const QCBORTagListIn TL = {2, puList};
3181 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003182
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003183 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3184 return -8;
3185 }
3186 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3187 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3188 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3189 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3190 Item.val.uCount != 0) {
3191 return -9;
3192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003193
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003194 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003195 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003196 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003197 const uint64_t puLongList[17] = {1,2,1};
3198 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003199 QCBORDecode_Init(&DCtx,
3200 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3201 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003202 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3203 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3204 return -11;
3205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003206
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003207 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003208 QCBORTagListOut Out = {0, 4, puTags};
3209
3210
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003211 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003212 QCBORDecode_Init(&DCtx,
3213 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3214 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003215 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3216 return -12;
3217 }
3218 if(puTags[0] != 0x9192939495969798 ||
3219 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003220 puTags[2] != 0x06 ||
3221 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003222 return -13;
3223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003224
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003225 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003226 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003227 QCBORDecode_Init(&DCtx,
3228 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3229 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003230 QCBORTagListOut OutSmall = {0, 3, puTags};
3231 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3232 return -14;
3233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003234
Laurence Lundblade9b334962020-08-27 10:55:53 -07003235
3236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003237 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003238 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3239 // It is a bit of a messy test and maybe could be improved, but
3240 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003241 QCBORDecode_Init(&DCtx,
3242 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3243 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003244 int n = CheckCSRMaps(&DCtx);
3245 if(n) {
3246 return n-2000;
3247 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003248
Laurence Lundblade59289e52019-12-30 13:44:37 -08003249 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003250 QCBORDecode_Init(&DCtx,
3251 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3252 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003253
Laurence Lundblade9b334962020-08-27 10:55:53 -07003254 /* With the spiffy decode revision, this tag list is not used.
3255 It doesn't matter if a tag is in this list or not so some
3256 tests that couldn't process a tag because it isn't in this list
3257 now can process these unlisted tags. The tests have been
3258 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003259 const uint64_t puTagList[] = {773, 1, 90599561};
3260 const QCBORTagListIn TagList = {3, puTagList};
3261 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003262
3263
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003264 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3265 return -100;
3266 }
3267 if(Item.uDataType != QCBOR_TYPE_MAP ||
3268 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3269 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3270 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3271 Item.val.uCount != 2 ||
3272 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3273 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3274 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3275 Out.uNumUsed != 3) {
3276 return -101;
3277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003278
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003279 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3280 return -102;
3281 }
3282 if(Item.uDataType != QCBOR_TYPE_MAP ||
3283 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3284 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003285 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003286 Item.val.uCount != 2 ||
3287 puTags[0] != 5859837686836516696 ||
3288 puTags[1] != 7 ||
3289 Out.uNumUsed != 2) {
3290 return -103;
3291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003292
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003293 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3294 return -104;
3295 }
3296 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003297 Item.val.uCount != 5 ||
3298 puTags[0] != 0x0b ||
3299 Out.uNumUsed != 1) {
3300 return -105;
3301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003303 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3304 return -106;
3305 }
3306 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3307 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3308 Item.val.string.len != 12 ||
3309 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3310 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3311 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3312 Out.uNumUsed != 3) {
3313 return -105;
3314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003315
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003316 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3317 return -107;
3318 }
3319 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3320 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3321 Item.val.string.len != 3 ||
3322 puTags[0] != 773 ||
3323 Out.uNumUsed != 1) {
3324 return -108;
3325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003326
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003327 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3328 return -109;
3329 }
3330 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003331 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003332 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003333 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003334 puTags[3] != 7 ||
3335 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003336 return -110;
3337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003338
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003339 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3340 return -111;
3341 }
3342 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3343 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3344 Item.val.string.len != 9 ||
3345 puTags[0] != 17 ||
3346 Out.uNumUsed != 1) {
3347 return -112;
3348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003349
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003350 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3351 return -111;
3352 }
3353 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3354 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3355 Item.val.string.len != 2 ||
3356 puTags[0] != 17 ||
3357 Out.uNumUsed != 1) {
3358 return -112;
3359 }
3360
3361 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3362 return -113;
3363 }
3364 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003365 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003366 Item.val.uCount != 2 ||
3367 puTags[0] != 19 ||
3368 Out.uNumUsed != 1) {
3369 return -114;
3370 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003371
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003372 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3373 return -115;
3374 }
3375 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003376 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003377 Item.val.uCount != 1 ||
3378 puTags[0] != 9 ||
3379 Out.uNumUsed != 1) {
3380 return -116;
3381 }
3382
3383 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3384 return -116;
3385 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003386 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003387 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003388 Out.uNumUsed != 0) {
3389 return -117;
3390 }
3391
3392 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3393 return -118;
3394 }
3395 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3396 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003397 puTags[0] != 12 ||
3398 Out.uNumUsed != 1) {
3399 return -119;
3400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003401
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003402 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3403 return -120;
3404 }
3405 if(Item.uDataType != QCBOR_TYPE_MAP ||
3406 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3407 Item.val.uCount != 1 ||
3408 puTags[0] != 0x17 ||
3409 Out.uNumUsed != 1) {
3410 return -121;
3411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003412
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003413 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3414 return -122;
3415 }
3416 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003417 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003418 Item.val.int64 != -3 ||
3419 puTags[0] != 8 ||
3420 Out.uNumUsed != 1) {
3421 return -123;
3422 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003423
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003424 if(QCBORDecode_Finish(&DCtx)) {
3425 return -124;
3426 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003427
3428 UsefulBufC DateString;
3429 QCBORDecode_Init(&DCtx,
3430 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3431 QCBOR_DECODE_MODE_NORMAL);
3432
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003433 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003434 // tagged date string
3435 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3436 // untagged date string
3437 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3438 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3439 return 100;
3440 }
3441 // untagged byte string
3442 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3443 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3444 return 101;
3445 }
3446 // tagged regex
3447 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3448 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3449 return 102;
3450 }
3451 // tagged date string with a byte string
3452 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3453 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3454 return 103;
3455 }
3456 QCBORDecode_ExitArray(&DCtx);
3457 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3458 return 104;
3459 }
3460
3461
3462 QCBORDecode_Init(&DCtx,
3463 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3464 QCBOR_DECODE_MODE_NORMAL);
3465
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003466 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003467 // tagged date string
3468 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3469 // untagged date string
3470 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3471 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3472 return 200;
3473 }
3474 // untagged byte string
3475 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3476 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3477 return 201;
3478 }
3479 // tagged regex
3480 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3481 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3482 return 202;
3483 }
3484 // tagged date string with a byte string
3485 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3486 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3487 return 203;
3488 }
3489 QCBORDecode_ExitArray(&DCtx);
3490 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3491 return 204;
3492 }
3493
3494 QCBORDecode_Init(&DCtx,
3495 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3496 QCBOR_DECODE_MODE_NORMAL);
3497
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003498 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003499 // tagged date string
3500 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3501 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3502 return 300;
3503 }
3504 // untagged date string
3505 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3506 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3507 return 301;
3508 }
3509 // untagged byte string
3510 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3511 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3512 return 302;
3513 }
3514 // tagged regex
3515 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3516 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3517 return 303;
3518 }
3519 // tagged date string with a byte string
3520 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3521 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3522 return 304;
3523 }
3524 QCBORDecode_ExitArray(&DCtx);
3525 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3526 return 305;
3527 }
3528
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003529 return 0;
3530}
3531
3532
3533
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003534
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003535static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003536 0x83,
3537 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3538 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3539 0xA4,
3540 0x63, 0x42, 0x4E, 0x2B,
3541 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3542 0x18, 0x40,
3543 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3544 0x63, 0x42, 0x4E, 0x2D,
3545 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3546 0x38, 0x3F,
3547 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3548
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003549/* The expected big num */
3550static const uint8_t spBigNum[] = {
3551 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3552 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003553
3554
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003555int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003556{
3557 QCBORDecodeContext DCtx;
3558 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003559 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003560
Laurence Lundbladeee851742020-01-08 08:37:05 -08003561 QCBORDecode_Init(&DCtx,
3562 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3563 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003564
3565
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003566 //
3567 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3568 return -1;
3569 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003570 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003571 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003572
3573 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003574 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003575 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003576 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003577 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003578 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003579 }
3580
3581 //
3582 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003583 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003584 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003585 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003586 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003587 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003588
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003589 //
3590 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003591 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003592 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003593 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003594 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003595
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003596 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003597 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003598 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3599 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003600 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003601 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003602 }
3603
3604 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003605 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003606 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3607 Item.uLabelType != QCBOR_TYPE_INT64 ||
3608 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003609 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003610 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003611 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003612
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003613 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003614 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003615 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3616 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003617 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003618 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003619 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003620
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003621 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003622 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003623 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3624 Item.uLabelType != QCBOR_TYPE_INT64 ||
3625 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003626 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003627 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003628 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003629
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003630 return 0;
3631}
3632
3633
3634
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003635static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003636 uint8_t uDataType,
3637 uint8_t uNestingLevel,
3638 uint8_t uNextNest,
3639 int64_t nLabel,
3640 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003641{
3642 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003643 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003644
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003645 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3646 if(Item.uDataType != uDataType) return -1;
3647 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003648 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3649 Item.uLabelType != QCBOR_TYPE_UINT64) {
3650 return -1;
3651 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003652 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3653 if(Item.label.int64 != nLabel) return -1;
3654 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003655 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003656 }
3657 }
3658 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303659 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003660
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003661 if(pItem) {
3662 *pItem = Item;
3663 }
3664 return 0;
3665}
3666
3667
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003668// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003669static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003670{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303671 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003672
Laurence Lundblade9b334962020-08-27 10:55:53 -07003673 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003674
Laurence Lundblade9b334962020-08-27 10:55:53 -07003675 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003676
Laurence Lundblade9b334962020-08-27 10:55:53 -07003677 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3678 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3679 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3680 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3681 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003682
Laurence Lundblade9b334962020-08-27 10:55:53 -07003683 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3684 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003685
Laurence Lundblade9b334962020-08-27 10:55:53 -07003686 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3687 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003688
Laurence Lundblade9b334962020-08-27 10:55:53 -07003689 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3690 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003691
Laurence Lundblade9b334962020-08-27 10:55:53 -07003692 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003693
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003694 return 0;
3695}
3696
3697
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003698/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003699{
3700 -23: {
3701 -20: {
3702 -18: "Organization",
3703 -17: "SSG",
3704 -15: "Confusion",
3705 -16: "San Diego",
3706 -14: "US"
3707 },
3708 -19: {
3709 -11: {
3710 -9: -7
3711 },
3712 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3713 }
3714 },
3715 -22: {
3716 -5: -3
3717 }
3718}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003719*/
3720static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003721 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3722 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3723 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3724 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3725 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3726 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3727 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3728 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3729 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3730
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003731// Same map as above, but using indefinite lengths
3732static const uint8_t spCSRInputIndefLen[] = {
3733 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3734 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3735 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3736 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3737 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3738 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3739 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3740 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3741 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3742 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3743
3744
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003745int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003746{
3747 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003748
Laurence Lundbladeee851742020-01-08 08:37:05 -08003749 QCBORDecode_Init(&DCtx,
3750 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3751 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003752
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003753 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003754}
3755
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003756
3757
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003758int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003759{
3760 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003761
Laurence Lundbladeee851742020-01-08 08:37:05 -08003762 QCBORDecode_Init(&DCtx,
3763 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3764 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003765
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003766 QCBORItem Item;
3767 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003768
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003769 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3770 return -1;
3771 }
3772 if(Item.uDataType != QCBOR_TYPE_MAP) {
3773 return -2;
3774 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003775
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003776 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3777 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3778 return -3;
3779 }
3780
3781 return 0;
3782}
3783
3784
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003785
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003786int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003787{
3788 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003789
Laurence Lundbladeee851742020-01-08 08:37:05 -08003790 QCBORDecode_Init(&DCtx,
3791 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3792 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003793
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003794 return CheckCSRMaps(&DCtx);
3795}
3796
3797
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003798
Laurence Lundblade17ede402018-10-13 11:43:07 +08003799static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3800{
3801 UsefulOutBuf UOB;
3802 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003803
Laurence Lundblade17ede402018-10-13 11:43:07 +08003804 int i;
3805 for(i = 0; i < n; i++) {
3806 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3807 }
3808
3809 for(i = 0; i < n; i++) {
3810 UsefulOutBuf_AppendByte(&UOB, 0xff);
3811 }
3812 return UsefulOutBuf_OutUBuf(&UOB);
3813}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003814
3815
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003816static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003817{
3818 QCBORDecodeContext DC;
3819 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003820
Laurence Lundblade17ede402018-10-13 11:43:07 +08003821 int j;
3822 for(j = 0; j < nNestLevel; j++) {
3823 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003824 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003825 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3826 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003827 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003828 return -4;
3829 } else {
3830 return 0; // Decoding doesn't recover after an error
3831 }
3832 } else {
3833 // Should be no error
3834 if(nReturn) {
3835 return -9; // Should not have got an error
3836 }
3837 }
3838 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3839 return -7;
3840 }
3841 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003842 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003843 if(nReturn) {
3844 return -3;
3845 }
3846 return 0;
3847}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003848
3849
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003850int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003851{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303852 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003853 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003854 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003855 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003856 int nReturn = parse_indeflen_nested(Nested, i);
3857 if(nReturn) {
3858 return nReturn;
3859 }
3860 }
3861 return 0;
3862}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003863
Laurence Lundbladeee851742020-01-08 08:37:05 -08003864// [1, [2, 3]]
3865static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3866// No closing break
3867static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3868// Not enough closing breaks
3869static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3870// Too many closing breaks
3871static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3872// Unclosed indeflen inside def len
3873static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3874// confused tag
3875static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003876
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003877int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003878{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003879 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003880 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003881 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003882
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003883 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003884 QCBORDecodeContext DC;
3885 QCBORItem Item;
3886 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003887
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003888 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303889
3890 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3891 Item.uNestingLevel != 0 ||
3892 Item.uNextNestLevel != 1) {
3893 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003894 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003895
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003896 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303897 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3898 Item.uNestingLevel != 1 ||
3899 Item.uNextNestLevel != 1) {
3900 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003901 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003902
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003903 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303904 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3905 Item.uNestingLevel != 1 ||
3906 Item.uNextNestLevel != 2) {
3907 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003908 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003909
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003910 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003911 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303912 Item.uNestingLevel != 2 ||
3913 Item.uNextNestLevel != 2) {
3914 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003915 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003916
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003917 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003918 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303919 Item.uNestingLevel != 2 ||
3920 Item.uNextNestLevel != 0) {
3921 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003922 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003923
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003924 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303925 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003926 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003927
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003928 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003929 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003930
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003931 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003932
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003933 nResult = QCBORDecode_GetNext(&DC, &Item);
3934 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303935 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003936 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003937
Laurence Lundblade570fab52018-10-13 18:28:27 +08003938 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003939 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303940 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003941 }
3942
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003943
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003944 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003945 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003946
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003947 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003948
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003949 nResult = QCBORDecode_GetNext(&DC, &Item);
3950 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303951 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003952 }
3953
3954 nResult = QCBORDecode_GetNext(&DC, &Item);
3955 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303956 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003957 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003959 nResult = QCBORDecode_GetNext(&DC, &Item);
3960 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303961 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003963
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003964 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003965 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303966 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003967 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003968
3969
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003970 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003971 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003972
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003973 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003974
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003975 nResult = QCBORDecode_GetNext(&DC, &Item);
3976 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303977 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003978 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003979
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003980 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003981 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303982 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003983 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303984
Laurence Lundblade642282a2020-06-23 12:00:33 -07003985 nResult = QCBORDecode_GetNext(&DC, &Item);
3986 if(nResult != QCBOR_ERR_BAD_BREAK) {
3987 return -140;
3988 }
3989
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade570fab52018-10-13 18:28:27 +08003991 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003992 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003993
Laurence Lundblade570fab52018-10-13 18:28:27 +08003994 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003995
Laurence Lundblade570fab52018-10-13 18:28:27 +08003996 nResult = QCBORDecode_GetNext(&DC, &Item);
3997 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303998 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004000
Laurence Lundblade570fab52018-10-13 18:28:27 +08004001 nResult = QCBORDecode_GetNext(&DC, &Item);
4002 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304003 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004005
Laurence Lundblade570fab52018-10-13 18:28:27 +08004006 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004007 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304008 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004009 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004010
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304011 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004012 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004013
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304014 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004015
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304016 nResult = QCBORDecode_GetNext(&DC, &Item);
4017 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304018 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304019 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004020
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304021 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304022 if(nResult != QCBOR_ERR_BAD_BREAK) {
4023 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004025
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004026 return 0;
4027}
4028
Laurence Lundblade17ede402018-10-13 11:43:07 +08004029
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004030#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4031
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004032static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004033 0x81, // Array of length one
4034 0x7f, // text string marked with indefinite length
4035 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4036 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4037 0xff // ending break
4038};
4039
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004040static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304041 0x81, // Array of length one
4042 0x7f, // text string marked with indefinite length
4043 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4044 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4045 0xff // ending break
4046};
4047
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004048static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304049 0x81, // Array of length one
4050 0x7f, // text string marked with indefinite length
4051 0x01, 0x02, // Not a string
4052 0xff // ending break
4053};
4054
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004055static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304056 0x81, // Array of length one
4057 0x7f, // text string marked with indefinite length
4058 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4059 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4060 // missing end of string
4061};
4062
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004063static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304064 0xa1, // Array of length one
4065 0x7f, // text string marked with indefinite length
4066 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4067 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4068 0xff, // ending break
4069 0x01 // integer being labeled.
4070};
4071
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004072/**
4073 Make an indefinite length string
4074
4075 @param Storage Storage for string, must be 144 bytes in size
4076 @return The indefinite length string
4077
4078 This makes an array with one indefinite length string that has 7 chunks
4079 from size of 1 byte up to 64 bytes.
4080 */
4081static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304082{
4083 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004084
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304085 UsefulOutBuf_Init(&UOB, Storage);
4086 UsefulOutBuf_AppendByte(&UOB, 0x81);
4087 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004088
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004089 uint8_t uStringByte = 0;
4090 // Use of type int is intentional
4091 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4092 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304093 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004094 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4095 for(int j = 0; j < uChunkSize; j++) {
4096 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4097 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304098 }
4099 }
4100 UsefulOutBuf_AppendByte(&UOB, 0xff);
4101
4102 return UsefulOutBuf_OutUBuf(&UOB);
4103}
4104
4105static int CheckBigString(UsefulBufC BigString)
4106{
4107 if(BigString.len != 255) {
4108 return 1;
4109 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004110
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304111 for(uint8_t i = 0; i < 255; i++){
4112 if(((const uint8_t *)BigString.ptr)[i] != i) {
4113 return 1;
4114 }
4115 }
4116 return 0;
4117}
4118
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304119
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004120int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304121{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304122 QCBORDecodeContext DC;
4123 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304124 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004125 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004126
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304127 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004128 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304129 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004130
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304131 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304132 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304133 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004134
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304135 if(QCBORDecode_GetNext(&DC, &Item)) {
4136 return -2;
4137 }
4138 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4139 return -3;
4140 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004141
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304142 if(QCBORDecode_GetNext(&DC, &Item)) {
4143 return -4;
4144 }
4145 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4146 return -5;
4147 }
4148 if(QCBORDecode_Finish(&DC)) {
4149 return -6;
4150 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304151
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304152 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004153 QCBORDecode_Init(&DC,
4154 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4155 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004156
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304157 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4158 return -7;
4159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004160
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304161 if(QCBORDecode_GetNext(&DC, &Item)) {
4162 return -8;
4163 }
4164 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4165 return -9;
4166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004167
Laurence Lundblade30816f22018-11-10 13:40:22 +07004168 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304169 return -10;
4170 }
4171
4172 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004173 QCBORDecode_Init(&DC,
4174 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4175 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004176
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304177 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4178 return -11;
4179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004180
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304181 if(QCBORDecode_GetNext(&DC, &Item)) {
4182 return -12;
4183 }
4184 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4185 return -13;
4186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004187
Laurence Lundblade30816f22018-11-10 13:40:22 +07004188 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304189 return -14;
4190 }
4191
4192 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004193 QCBORDecode_Init(&DC,
4194 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4195 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004196
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304197 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4198 return -15;
4199 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004200
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304201 if(QCBORDecode_GetNext(&DC, &Item)) {
4202 return -16;
4203 }
4204 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4205 return -17;
4206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004207
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304208 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4209 return -18;
4210 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004211
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304212 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304213 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004214
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304215 QCBORDecode_GetNext(&DC, &Item);
4216 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304217 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004219
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304220 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304221 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004223
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304224 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004225 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304226
4227 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4228 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304229 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004231
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304232 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304233 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004234 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004235
Laurence Lundbladeee851742020-01-08 08:37:05 -08004236 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4237 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004238
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304239 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304240 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304241 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004243
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304244 QCBORDecode_GetNext(&DC, &Item);
4245 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304246 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304247 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004248 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304249 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004251
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304252 // ---- big bstr -----
4253 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004254
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304255 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4256 return -25;
4257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004258
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304259 if(QCBORDecode_GetNext(&DC, &Item)) {
4260 return -26;
4261 }
4262 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304263 return -26;
4264 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004265
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304266 if(QCBORDecode_GetNext(&DC, &Item)) {
4267 return -27;
4268 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304269 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304270 return -28;
4271 }
4272 if(CheckBigString(Item.val.string)) {
4273 return -3;
4274 }
4275 if(QCBORDecode_Finish(&DC)) {
4276 return -29;
4277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004278
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304279 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004280 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004281
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304282 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4283 return -30;
4284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004285
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304286 QCBORDecode_GetNext(&DC, &Item);
4287 if(Item.uDataType != QCBOR_TYPE_MAP) {
4288 return -31;
4289 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004290
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304291 if(QCBORDecode_GetNext(&DC, &Item)){
4292 return -32;
4293 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004294 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4295 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304296 Item.uDataAlloc || !Item.uLabelAlloc ||
4297 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4298 return -33;
4299 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004300
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304301 if(QCBORDecode_Finish(&DC)) {
4302 return -34;
4303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004304
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004305 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004306}
4307
4308
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004309int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304310{
4311 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004312 QCBORError nCBORError;
4313
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004314
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304315 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004316 QCBORDecode_Init(&DC,
4317 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4318 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004319
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004320 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004321
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004322 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4323 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304324 return -1;
4325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004326
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004327 if(CheckCSRMaps(&DC)) {
4328 return -2;
4329 }
4330
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004331 // Next parse, save pointers to a few strings, destroy original and
4332 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004333 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004334 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004335
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304336 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004337 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304338 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004339
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304340 QCBORItem Item1, Item2, Item3, Item4;
4341 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004342 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304343 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4344 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004345 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304346 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004347 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304348 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004349 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304350 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004351 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304352 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004353 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004354
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304355 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004356
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304357 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304358 Item1.uDataType != QCBOR_TYPE_INT64 ||
4359 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004360 Item1.uDataAlloc != 0 ||
4361 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004362 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004363 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004365
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304366
4367 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004368 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304369 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004370 Item2.uDataAlloc != 0 ||
4371 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304372 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004373 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004374
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304375 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004376 Item3.uDataAlloc == 0 ||
4377 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004378 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004379 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004381
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304382 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004383 Item4.uDataAlloc == 0 ||
4384 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004385 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004386 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004388
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304389 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004390 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004391 QCBORDecode_Init(&DC,
4392 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4393 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304394 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4395 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004396 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304397 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004398 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004399 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004400 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304401 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4402 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4403 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4404 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4405 }
4406 }
4407 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004408 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004409 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304410 }
4411
4412 return 0;
4413}
4414
Laurence Lundbladef6531662018-12-04 10:42:22 +09004415
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004416int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004417{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004418 // Set up the decoder with a tiny bit of CBOR to parse because
4419 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004420 QCBORDecodeContext DC;
4421 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4422 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004423
Laurence Lundbladef6531662018-12-04 10:42:22 +09004424 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004425 // Then fish into the internals of the decode context
4426 // to get the allocator function so it can be called directly.
4427 // Also figure out how much pool is available for use
4428 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004429 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004430 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4431 if(nError) {
4432 return -9;
4433 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004434 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4435 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4436 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004437
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004438 // First test -- ask for one more byte than available and see failure
4439 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004440 if(!UsefulBuf_IsNULL(Allocated)) {
4441 return -1;
4442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004443
Laurence Lundbladef6531662018-12-04 10:42:22 +09004444 // Re do the set up for the next test that will do a successful alloc,
4445 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004446 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004447 pAlloc = DC.StringAllocator.pfAllocator;
4448 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4449 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004450
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004451 // Allocate one byte less than available and see success
4452 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004453 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4454 return -2;
4455 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004456 // Ask for some more and see failure
4457 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004458 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4459 return -3;
4460 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004461 // Free the first allocate, retry the second and see success
4462 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4463 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004464 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4465 return -4;
4466 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004467
Laurence Lundbladef6531662018-12-04 10:42:22 +09004468 // Re do set up for next test that involves a successful alloc,
4469 // and a successful realloc and a failed realloc
4470 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004471 pAlloc = DC.StringAllocator.pfAllocator;
4472 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004473
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004474 // Allocate half the pool and see success
4475 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004476 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4477 return -5;
4478 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004479 // Reallocate to take up the whole pool and see success
4480 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004481 if(UsefulBuf_IsNULL(Allocated2)) {
4482 return -6;
4483 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004484 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004485 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4486 return -7;
4487 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004488 // Try to allocate more to be sure there is failure after a realloc
4489 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4490 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004491 return -8;
4492 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004493
Laurence Lundbladef6531662018-12-04 10:42:22 +09004494 return 0;
4495}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004496
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004497
4498/* Just enough of an allocator to test configuration of one */
4499static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4500{
4501 (void)pOldMem; // unused variable
4502
4503 if(uNewSize) {
4504 // Assumes the context pointer is the buffer and
4505 // nothing too big will ever be asked for.
4506 // This is only good for this basic test!
4507 return (UsefulBuf) {pCtx, uNewSize};
4508 } else {
4509 return NULLUsefulBuf;
4510 }
4511}
4512
4513
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004514int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004515{
4516 // Set up the decoder with a tiny bit of CBOR to parse because
4517 // nothing can be done with it unless that is set up.
4518 QCBORDecodeContext DC;
4519 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4520 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4521
4522 uint8_t pAllocatorBuffer[50];
4523
4524 // This is really just to test that this call works.
4525 // The full functionality of string allocators is tested
4526 // elsewhere with the MemPool internal allocator.
4527 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4528
4529 QCBORItem Item;
4530 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4531 return -1;
4532 }
4533
4534 if(Item.uDataAlloc == 0 ||
4535 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4536 Item.val.string.ptr != pAllocatorBuffer) {
4537 return -2;
4538 }
4539
4540 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4541 return -3;
4542 }
4543
4544 return 0;
4545}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004546#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4547
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004548
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004549#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004550
Laurence Lundbladea826c502020-05-10 21:07:00 -07004551/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004552 [
4553 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004554 4([-20, 4759477275222530853136]),
4555 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004556 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004557 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004558 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004559 5([ 9223372036854775806, -4759477275222530853137])
4560 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004561 ]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004562 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004563static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004564 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004565 0xC4, 0x82, 0x20,
4566 0x03,
4567 0xC4, 0x82, 0x33,
4568 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4569 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4570 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4571 0xC5, 0x82, 0x19, 0x01, 0x2C,
4572 0x18, 0x64,
4573 0xC5, 0x82, 0x33,
4574 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4575 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4576 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004577 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4578 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004579 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4580 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4581};
4582
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004583
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004584int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004585{
4586 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004587 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004588 QCBORItem item;
4589
Laurence Lundblade17af4902020-01-07 19:11:55 -08004590 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4591 0x06, 0x07, 0x08, 0x09, 0x010};
4592 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004593
4594
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004595 QCBORDecode_Init(&DC,
4596 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4597 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004598
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004599 uErr = QCBORDecode_GetNext(&DC, &item);
4600 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004601 return 1;
4602 }
4603
4604 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4605 return 2;
4606 }
4607
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004608 uErr = QCBORDecode_GetNext(&DC, &item);
4609 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004610 return 3;
4611 }
4612
4613 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4614 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4615 item.val.expAndMantissa.nExponent != -1) {
4616 return 4;
4617 }
4618
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004619 uErr = QCBORDecode_GetNext(&DC, &item);
4620 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004621 return 5;
4622 }
4623
4624 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4625 item.val.expAndMantissa.nExponent != -20 ||
4626 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4627 return 6;
4628 }
4629
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004630 uErr = QCBORDecode_GetNext(&DC, &item);
4631 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004632 return 7;
4633 }
4634
4635 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4636 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4637 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4638 return 8;
4639 }
4640
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004641 uErr = QCBORDecode_GetNext(&DC, &item);
4642 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004643 return 9;
4644 }
4645
4646 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4647 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4648 item.val.expAndMantissa.nExponent != 300) {
4649 return 10;
4650 }
4651
Laurence Lundbladea826c502020-05-10 21:07:00 -07004652 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004653 uErr = QCBORDecode_GetNext(&DC, &item);
4654 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004655 return 11;
4656 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004657 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4658 item.val.expAndMantissa.nExponent != -20 ||
4659 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4660 return 12;
4661 }
4662
Laurence Lundbladea826c502020-05-10 21:07:00 -07004663 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004664 uErr = QCBORDecode_GetNext(&DC, &item);
4665 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004666 return 13;
4667 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004668 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4669 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4670 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4671 return 14;
4672 }
4673
Laurence Lundbladea826c502020-05-10 21:07:00 -07004674 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004675 uErr = QCBORDecode_GetNext(&DC, &item);
4676 if(uErr != QCBOR_SUCCESS) {
4677 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004678 }
4679 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4680 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4681 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004682 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004683 }
4684
Laurence Lundbladea826c502020-05-10 21:07:00 -07004685 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004686 uErr = QCBORDecode_GetNext(&DC, &item);
4687 if(uErr != QCBOR_SUCCESS) {
4688 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004689 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004690 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4691 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4692 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004693 return 18;
4694 }
4695
4696 uErr = QCBORDecode_Finish(&DC);
4697 if(uErr != QCBOR_SUCCESS) {
4698 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004699 }
4700
4701 /* Now encode some stuff and then decode it */
4702 uint8_t pBuf[40];
4703 QCBOREncodeContext EC;
4704 UsefulBufC Encoded;
4705
4706 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4707 QCBOREncode_OpenArray(&EC);
4708 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4709 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4710 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4711 QCBOREncode_CloseArray(&EC);
4712 QCBOREncode_Finish(&EC, &Encoded);
4713
4714
4715 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004716 uErr = QCBORDecode_GetNext(&DC, &item);
4717 if(uErr != QCBOR_SUCCESS) {
4718 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004719 }
4720
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004721 uErr = QCBORDecode_GetNext(&DC, &item);
4722 if(uErr != QCBOR_SUCCESS) {
4723 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004724 }
4725
4726 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4727 item.val.expAndMantissa.nExponent != 1000 ||
4728 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004729 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004730 }
4731
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004732 uErr = QCBORDecode_GetNext(&DC, &item);
4733 if(uErr != QCBOR_SUCCESS) {
4734 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004735 }
4736
4737 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4738 item.val.expAndMantissa.nExponent != INT32_MIN ||
4739 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004740 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004741 }
4742
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004743 uErr = QCBORDecode_GetNext(&DC, &item);
4744 if(uErr != QCBOR_SUCCESS) {
4745 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004746 }
4747
4748 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4749 item.val.expAndMantissa.nExponent != INT32_MAX ||
4750 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004751 return 106;
4752 }
4753
4754
4755 int64_t nExp, nMant;
4756 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4757 UsefulBufC Mant;
4758 bool bIsNeg;
4759
4760 QCBORDecode_Init(&DC,
4761 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4762 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004763 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004764
4765 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004766 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004767
4768 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004769 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4770 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004771
4772 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004773 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4774 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004775
4776 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004777 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004778
4779 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004780 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4781 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004782
4783 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004784 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4785 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004786
4787 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004788 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4789 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004790
4791 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004792 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4793 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004794
4795 QCBORDecode_ExitArray(&DC);
4796
4797 uErr = QCBORDecode_Finish(&DC);
4798 if(uErr != QCBOR_SUCCESS) {
4799 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004800 }
4801
4802 return 0;
4803}
4804
4805
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004806static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004807 // Exponent > INT64_MAX
4808 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4809 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4810 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4811 // Mantissa > INT64_MAX
4812 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4813 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4814 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4815 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004816 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004817 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004818 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004819 // bad content for big num
4820 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4821 // bad content for big num
4822 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4823 // Bad integer for exponent
4824 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4825 // Bad integer for mantissa
4826 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4827 // 3 items in array
4828 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004829#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004830 // unterminated indefinite length array
4831 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004832#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4833 // unterminated indefinite length array
4834 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4835#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004836 // Empty array
4837 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4838 // Second is not an integer
4839 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4840 // First is not an integer
4841 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4842 // Not an array
4843 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4844};
4845
4846
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004847int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004848{
4849 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004850 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4851 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004852}
4853
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004854#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004855
4856
4857
4858/*
4859 Some basic CBOR with map and array used in a lot of tests.
4860 The map labels are all strings
4861
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004862 {
4863 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004864 "an array of two strings": [
4865 "string1", "string2"
4866 ],
4867 "map in a map": {
4868 "bytes 1": h'78787878',
4869 "bytes 2": h'79797979',
4870 "another int": 98,
4871 "text 2": "lies, damn lies and statistics"
4872 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004873 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004874 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004875
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004876int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004877{
4878 QCBORItem Item1, Item2, Item3;
4879 int64_t nDecodedInt1, nDecodedInt2;
4880 UsefulBufC B1, B2, S1, S2, S3;
4881
4882 QCBORDecodeContext DCtx;
4883 QCBORError nCBORError;
4884
4885 QCBORDecode_Init(&DCtx, input, 0);
4886
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004887 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004888
4889 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4890
4891 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4892 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004893 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4894 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4895 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004896 QCBORDecode_ExitMap(&DCtx);
4897
4898 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4899 QCBORDecode_GetNext(&DCtx, &Item1);
4900 QCBORDecode_GetNext(&DCtx, &Item2);
4901 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4902 return -400;
4903 }
4904 QCBORDecode_ExitArray(&DCtx);
4905
4906 // Parse the same array again using GetText() instead of GetItem()
4907 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004908 QCBORDecode_GetTextString(&DCtx, &S2);
4909 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004910 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4911 return 5000;
4912 }
4913 /* QCBORDecode_GetText(&DCtx, &S3);
4914 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4915 return 5001;
4916 } */
4917
4918 QCBORDecode_ExitArray(&DCtx);
4919
4920 QCBORDecode_ExitMap(&DCtx);
4921
4922 nCBORError = QCBORDecode_Finish(&DCtx);
4923
4924 if(nCBORError) {
4925 return (int32_t)nCBORError;
4926 }
4927
4928 if(nDecodedInt1 != 42) {
4929 return 1001;
4930 }
4931
4932 if(nDecodedInt2 != 98) {
4933 return 1002;
4934 }
4935
4936 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004937 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004938 return 1003;
4939 }
4940
4941 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004942 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004943 return 1004;
4944 }
4945
Laurence Lundblade9b334962020-08-27 10:55:53 -07004946 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004947 return 1005;
4948 }
4949
4950 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4951 return 1006;
4952 }
4953
4954 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4955 return 1007;
4956 }
4957
4958 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4959 return 1008;
4960 }
4961
4962 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4963 return 1009;
4964 }
4965
4966 return 0;
4967}
4968
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004969/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004970 {
4971 -75008: h'05083399',
4972 88: [],
4973 100100: {
4974 "sub1": {
4975 10: [
4976 0
4977 ],
4978 -75009: h'A46823990001',
4979 100100: {
4980 "json": "{ \"ueid\", \"xyz\"}",
4981 "subsub": {
4982 100002: h'141813191001'
4983 }
4984 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004985 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004986 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004987 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004988 */
4989
4990static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004991 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
4992 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
4993 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
4994 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
4995 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
4996 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
4997 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
4998 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
4999 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
5000 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
5001 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
5002 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005003};
5004
5005/* Get item in multi-level nesting in spNestedCBOR */
5006static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
5007{
5008 UsefulBufC String;
5009
5010 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
5011 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
5012
5013 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5014 QCBORDecode_EnterMap(pDCtx, NULL);
5015 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5016 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
5017 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
5018 if(QCBORDecode_GetError(pDCtx)) {
5019 return 4001;
5020 }
5021 if(UsefulBuf_Compare(String, test_oemid)) {
5022 return 4002;
5023 }
5024 QCBORDecode_ExitMap(pDCtx);
5025 QCBORDecode_ExitMap(pDCtx);
5026 QCBORDecode_ExitMap(pDCtx);
5027 QCBORDecode_ExitMap(pDCtx);
5028
5029 return 0;
5030}
5031
5032/* Iterations on the zero-length array in spNestedCBOR */
5033static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
5034{
5035 QCBORItem Item;
5036 QCBORError uErr;
5037
5038 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
5039 for(int x = 0; x < 20; x++) {
5040 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5041 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5042 return 4100;
5043
5044 }
5045 }
5046 QCBORDecode_ExitArray(pDCtx);
5047 if(QCBORDecode_GetError(pDCtx)) {
5048 return 4101;
5049 }
5050
5051 return 0;
5052}
5053
5054/* Various iterations on the array that contains a zero in spNestedCBOR */
5055static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5056{
5057 QCBORError uErr;
5058
5059 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5060 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5061 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5062 int64_t nInt = 99;
5063 QCBORDecode_GetInt64(pDCtx, &nInt);
5064 if(nInt != 0) {
5065 return 4200;
5066 }
5067 for(int x = 0; x < 20; x++) {
5068 QCBORItem Item;
5069 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5070 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5071 return 4201;
5072
5073 }
5074 }
5075 QCBORDecode_ExitArray(pDCtx);
5076 if(QCBORDecode_GetAndResetError(pDCtx)) {
5077 return 4202;
5078 }
5079 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5080 UsefulBufC dD;
5081 QCBORDecode_GetByteString(pDCtx, &dD);
5082 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5083 return 4203;
5084 }
5085 for(int x = 0; x < 20; x++) {
5086 QCBORDecode_GetByteString(pDCtx, &dD);
5087 uErr = QCBORDecode_GetAndResetError(pDCtx);
5088 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5089 return 4204;
5090 }
5091 }
5092 QCBORDecode_ExitArray(pDCtx);
5093 QCBORDecode_ExitMap(pDCtx);
5094 QCBORDecode_ExitMap(pDCtx);
5095
5096 return 0;
5097}
5098
5099/* Repeatedly enter and exit maps and arrays, go off the end of maps
5100 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005101static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005102{
5103 QCBORDecodeContext DCtx;
5104 int32_t nReturn;
5105 QCBORError uErr;
5106
5107 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5108 QCBORDecode_EnterMap(&DCtx, NULL);
5109
5110 for(int j = 0; j < 5; j++) {
5111 for(int i = 0; i < 20; i++) {
5112 nReturn = DecodeNestedGetSubSub(&DCtx);
5113 if(nReturn) {
5114 return nReturn;
5115 }
5116 }
5117
5118 for(int i = 0; i < 20; i++) {
5119 nReturn = DecodeNestedGetEmpty(&DCtx);
5120 if(nReturn ) {
5121 return nReturn;
5122 }
5123 }
5124
5125 for(int i = 0; i < 20; i++) {
5126 nReturn = DecodeNestedGetZero(&DCtx);
5127 if(nReturn ) {
5128 return nReturn;
5129 }
5130 }
5131 }
5132
5133 QCBORDecode_ExitMap(&DCtx);
5134 uErr = QCBORDecode_Finish(&DCtx);
5135 if(uErr) {
5136 return (int32_t)uErr + 4100;
5137 }
5138
5139 return 0;
5140}
5141
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005142
5143/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005144 [
5145 23,
5146 6000,
5147 h'67616C6163746963',
5148 h'686176656E20746F6B656E'
5149 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005150 */
5151static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005152 0x84,
5153 0x17,
5154 0x19, 0x17, 0x70,
5155 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5156 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005157
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005158/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005159static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5160
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005161/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005162static const uint8_t spEmptyMap[] = {0xa0};
5163
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005164#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005165/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005166static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005167
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005168
Laurence Lundbladef0499502020-08-01 11:55:57 -07005169/*
5170 {
5171 0: [],
5172 9: [
5173 [],
5174 []
5175 ],
5176 8: {
5177 1: [],
5178 2: {},
5179 3: []
5180 },
5181 4: {},
5182 5: [],
5183 6: [
5184 [],
5185 []
5186 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005187 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005188 */
5189static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005190 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5191 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5192 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5193 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005194
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005195#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5196
5197
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005198/*
5199 Too many tags
5200 Invalid tag content
5201 Duplicate label
5202 Integer overflow
5203 Date overflow
5204
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005205 {
5206 1: 224(225(226(227(4(0))))),
5207 2: 1(h''),
5208 3: -18446744073709551616,
5209 4: 1(1.0e+300),
5210 5: 0, 8: 8
5211 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005212 */
5213static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005214 0xa7,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005215 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5216 0x02, 0xc1, 0x40,
5217 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5218 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5219 0x05, 0x00,
5220 0x05, 0x00,
5221 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005222};
5223
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005224/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005225static const uint8_t spUnRecoverableMapError1[] = {
5226 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5227};
5228
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005229#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005230/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005231static const uint8_t spUnRecoverableMapError2[] = {
5232 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5233};
5234
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005235/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005236static const uint8_t spUnRecoverableMapError3[] = {
5237 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5238};
5239
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005240/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005241static const uint8_t spUnRecoverableMapError4[] = {
5242 0xbf,
5243 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5244 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5246 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5247 0xff
5248};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005249#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005250
Laurence Lundblade63926052021-03-29 16:05:51 -07005251const unsigned char not_well_formed_submod_section[] = {
5252 0xa1, 0x14, 0x1f,
5253};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005254
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005255int32_t EnterMapTest()
5256{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005257 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005258 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005259 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005260 int32_t nReturn;
5261 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005262
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005263#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005264 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005265 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005266
Laurence Lundbladef0499502020-08-01 11:55:57 -07005267
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005268 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005269 QCBORDecode_ExitArray(&DCtx);
5270
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005271 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5272 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005273 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005274 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005275 QCBORDecode_ExitArray(&DCtx);
5276 QCBORDecode_ExitArray(&DCtx);
5277
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005278 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5279 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005280 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005281 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005282 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005283 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005284 QCBORDecode_ExitArray(&DCtx);
5285 QCBORDecode_ExitMap(&DCtx);
5286
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005287 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005288 QCBORDecode_ExitMap(&DCtx);
5289
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005290 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005291 QCBORDecode_ExitArray(&DCtx);
5292
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005293 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5294 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005295 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005296 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005297 QCBORDecode_ExitArray(&DCtx);
5298 QCBORDecode_ExitArray(&DCtx);
5299
5300 QCBORDecode_ExitMap(&DCtx);
5301
5302 uErr = QCBORDecode_Finish(&DCtx);
5303 if(uErr != QCBOR_SUCCESS){
5304 return 3011;
5305 }
5306
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005307 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005308 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005309 if(nReturn) {
5310 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005311 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005312#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5313
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005314
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005315 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005316 if(nReturn) {
5317 return nReturn;
5318 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005319
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005320
Laurence Lundblade937ea812020-05-08 11:38:23 -07005321
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005322 // These tests confirm the cursor is at the right place after entering
5323 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005324 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005325
5326 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005327 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005328 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005329 QCBORDecode_GetNext(&DCtx, &Item1);
5330 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5331 return 2001;
5332 }
5333
5334
Laurence Lundblade9b334962020-08-27 10:55:53 -07005335 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005336 QCBORDecode_VGetNext(&DCtx, &Item1);
5337 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005338 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5339 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5340 UsefulBuf_Compare(ArrayItem.label.string,
5341 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5342 return 2051;
5343 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005344 QCBORDecode_GetNext(&DCtx, &Item1);
5345 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5346 return 2002;
5347 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005348 QCBORDecode_ExitArray(&DCtx);
5349 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5350 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5351 UsefulBuf_Compare(ArrayItem.label.string,
5352 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5353 return 2052;
5354 }
5355
Laurence Lundblade937ea812020-05-08 11:38:23 -07005356
Laurence Lundblade9b334962020-08-27 10:55:53 -07005357 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005358 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005359 QCBORDecode_GetNext(&DCtx, &Item1);
5360 QCBORDecode_GetNext(&DCtx, &Item1);
5361 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005362 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5363 QCBORDecode_GetNext(&DCtx, &Item1);
5364 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5365 return 2003;
5366 }
5367
Laurence Lundblade9b334962020-08-27 10:55:53 -07005368 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005369 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005370 QCBORDecode_GetNext(&DCtx, &Item1);
5371 QCBORDecode_GetNext(&DCtx, &Item1);
5372 QCBORDecode_GetNext(&DCtx, &Item1);
5373 QCBORDecode_GetNext(&DCtx, &Item1);
5374 QCBORDecode_GetNext(&DCtx, &Item1);
5375 QCBORDecode_GetNext(&DCtx, &Item1);
5376 QCBORDecode_GetNext(&DCtx, &Item1);
5377 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5378 QCBORDecode_GetNext(&DCtx, &Item1);
5379 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005380 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005381 }
5382
Laurence Lundblade9b334962020-08-27 10:55:53 -07005383 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005384 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005385 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5386 QCBORDecode_ExitArray(&DCtx);
5387 QCBORDecode_GetNext(&DCtx, &Item1);
5388 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5389 return 2006;
5390 }
5391 QCBORDecode_ExitMap(&DCtx);
5392 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5393 return 2007;
5394 }
5395
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005396 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005397 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005398 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005399 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5400 uErr = QCBORDecode_GetAndResetError(&DCtx);
5401 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005402 return 2008;
5403 }
5404 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005405 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005406 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005407 return 2009;
5408 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005409
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005410
5411 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005412 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005413 // This will fail because the map is empty.
5414 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5415 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005416 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005417 return 2010;
5418 }
5419 QCBORDecode_ExitMap(&DCtx);
5420 uErr = QCBORDecode_Finish(&DCtx);
5421 if(uErr != QCBOR_SUCCESS){
5422 return 2011;
5423 }
5424
5425
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005426#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005427 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005428 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005429 // This will fail because the map is empty.
5430 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5431 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005432 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005433 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005434 }
5435 QCBORDecode_ExitMap(&DCtx);
5436 uErr = QCBORDecode_Finish(&DCtx);
5437 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005438 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005439 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005440#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005441
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005442
5443 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005444 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005445 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005446 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005447 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005448 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005449 QCBORDecode_ExitArray(&DCtx);
5450 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5451 QCBORDecode_ExitArray(&DCtx);
5452 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005453 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005454 return 2014;
5455 }
5456
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005457 int64_t nInt;
5458 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005459 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005460 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005461 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005462 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5463 return 2021;
5464 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005465 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5466 return 2121;
5467 }
5468 (void)QCBORDecode_GetAndResetError(&DCtx);
5469
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005470
5471 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5472 uErr = QCBORDecode_GetAndResetError(&DCtx);
5473 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5474 return 2022;
5475 }
5476
5477 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5478 uErr = QCBORDecode_GetAndResetError(&DCtx);
5479 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5480 return 2023;
5481 }
5482
5483 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5484 uErr = QCBORDecode_GetAndResetError(&DCtx);
5485#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5486 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5487 return 2024;
5488 }
5489#else
5490 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5491 return 2027;
5492 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005493#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005494
5495 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5496 uErr = QCBORDecode_GetAndResetError(&DCtx);
5497 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5498 return 2025;
5499 }
5500
5501 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5502
5503 QCBORDecode_ExitMap(&DCtx);
5504 uErr = QCBORDecode_Finish(&DCtx);
5505 if(uErr != QCBOR_SUCCESS) {
5506 return 2026;
5507 }
5508
5509 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005510 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005511 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5512 uErr = QCBORDecode_GetAndResetError(&DCtx);
5513 if(uErr != QCBOR_ERR_BAD_BREAK) {
5514 return 2030;
5515 }
5516
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005517#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005518 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005519 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005520 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5521 uErr = QCBORDecode_GetAndResetError(&DCtx);
5522 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5523 return 2031;
5524 }
5525
5526 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005527 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005528 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5529 uErr = QCBORDecode_GetAndResetError(&DCtx);
5530 if(uErr != QCBOR_ERR_HIT_END) {
5531 return 2032;
5532 }
5533
5534 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005535 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005536 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5537 uErr = QCBORDecode_GetAndResetError(&DCtx);
5538 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5539 return 2033;
5540 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005541#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5542
Laurence Lundblade732e52d2021-02-22 20:11:01 -07005543 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5544 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5545 if(Item1.uDataType != QCBOR_TYPE_MAP) {
5546 return 2401;
5547 }
5548 if(QCBORDecode_GetError(&DCtx)) {
5549 return 2402;
5550 }
5551
5552 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5553 QCBORDecode_VGetNext(&DCtx, &Item1);
5554 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5555 Item1.val.uCount != 3 ||
5556 Item1.uNextNestLevel != 1) {
5557 return 2403;
5558 }
5559 if(QCBORDecode_GetError(&DCtx)) {
5560 return 2404;
5561 }
5562 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5563 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
5564 Item1.uNextNestLevel != 1 ||
5565 Item1.val.int64 != 42) {
5566 return 2405;
5567 }
5568 if(QCBORDecode_GetError(&DCtx)) {
5569 return 2406;
5570 }
5571 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5572 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
5573 Item1.uNestingLevel != 1 ||
5574 Item1.uNextNestLevel != 1 ||
5575 Item1.val.uCount != 2) {
5576 return 2407;
5577 }
5578 if(QCBORDecode_GetError(&DCtx)) {
5579 return 2408;
5580 }
5581 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5582 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5583 Item1.uNestingLevel != 1 ||
5584 Item1.uNextNestLevel != 0 ||
5585 Item1.val.uCount != 4) {
5586 return 2409;
5587 }
5588 if(QCBORDecode_GetError(&DCtx)) {
5589 return 2410;
5590 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005591
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005592 nReturn = DecodeNestedIterate();
5593
Laurence Lundblade63926052021-03-29 16:05:51 -07005594
5595 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
5596 QCBORDecode_EnterMap(&DCtx, NULL);
5597 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
5598 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
5599 return 2500;
5600 }
5601
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005602 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005603}
5604
5605
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005606struct NumberConversion {
5607 char *szDescription;
5608 UsefulBufC CBOR;
5609 int64_t nConvertedToInt64;
5610 QCBORError uErrorInt64;
5611 uint64_t uConvertToUInt64;
5612 QCBORError uErrorUint64;
5613 double dConvertToDouble;
5614 QCBORError uErrorDouble;
5615};
5616
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005617static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005618 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005619 "too large to fit into int64_t",
5620 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5621 0,
5622 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5623 0,
5624 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5625 ((double)INT64_MIN) + 1 ,
5626 QCBOR_SUCCESS
5627 },
5628 {
5629 "largest negative int that fits in int64_t",
5630 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5631 INT64_MIN,
5632 QCBOR_SUCCESS,
5633 0,
5634 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5635 (double)INT64_MIN,
5636 QCBOR_SUCCESS
5637 },
5638 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005639 "negative bignum -1",
5640 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5641 -1,
5642 QCBOR_SUCCESS,
5643 0,
5644 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5645 -1.0,
5646 QCBOR_SUCCESS
5647 },
5648 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005649 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005650 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5651 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005652#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005653 257000,
5654 QCBOR_SUCCESS,
5655 257000,
5656 QCBOR_SUCCESS,
5657 257000.0,
5658 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005659#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005660 0,
5661 QCBOR_ERR_UNEXPECTED_TYPE,
5662 0,
5663 QCBOR_ERR_UNEXPECTED_TYPE,
5664 0.0,
5665 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005666#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005667 },
5668 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005669 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005670 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5671 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005672#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005673 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005674 QCBOR_SUCCESS,
5675 0,
5676 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005677 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005678 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005679#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005680 0,
5681 QCBOR_ERR_UNEXPECTED_TYPE,
5682 0,
5683 QCBOR_ERR_UNEXPECTED_TYPE,
5684 0.0,
5685 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005686#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005687 },
5688 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005689 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005690 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5691 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005692#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005693 2056,
5694 QCBOR_SUCCESS,
5695 2056,
5696 QCBOR_SUCCESS,
5697 2056.0,
5698 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005699#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005700 0,
5701 QCBOR_ERR_UNEXPECTED_TYPE,
5702 0,
5703 QCBOR_ERR_UNEXPECTED_TYPE,
5704 0.0,
5705 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005706#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005707 },
5708 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005709 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005710 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5711 0,
5712 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5713 0,
5714 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5715 -18446744073709551617.0,
5716 QCBOR_SUCCESS
5717 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005718#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005719 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005720 "Positive bignum 0x01020304 indefinite length string",
5721 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5722 0x01020304,
5723 QCBOR_SUCCESS,
5724 0x01020304,
5725 QCBOR_SUCCESS,
5726 16909060.0,
5727 QCBOR_SUCCESS
5728 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005729#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005730 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005731 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005732 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5733 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005734#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005735 0,
5736 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5737 0,
5738 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5739 -INFINITY,
5740 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005741#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005742 0,
5743 QCBOR_ERR_UNEXPECTED_TYPE,
5744 0,
5745 QCBOR_ERR_UNEXPECTED_TYPE,
5746 0.0,
5747 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005748#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005749 },
5750 {
5751 "big float [9223372036854775806, 9223372036854775806]",
5752 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5753 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005754#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005755 0,
5756 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5757 0,
5758 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5759 INFINITY,
5760 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005761#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005762 0,
5763 QCBOR_ERR_UNEXPECTED_TYPE,
5764 0,
5765 QCBOR_ERR_UNEXPECTED_TYPE,
5766 0.0,
5767 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005768#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005769 },
5770 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005771 "Big float 3 * 2^^2",
5772 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005773#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005774 12,
5775 QCBOR_SUCCESS,
5776 12,
5777 QCBOR_SUCCESS,
5778 12.0,
5779 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005780#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005781 0,
5782 QCBOR_ERR_UNEXPECTED_TYPE,
5783 0,
5784 QCBOR_ERR_UNEXPECTED_TYPE,
5785 0.0,
5786 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005787#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005788 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005789 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005790 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005791 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5792 0,
5793 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5794 18446744073709551615ULL,
5795 QCBOR_SUCCESS,
5796 18446744073709551615.0,
5797 QCBOR_SUCCESS
5798 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005799 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005800 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005801 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5802 65536-1,
5803 QCBOR_SUCCESS,
5804 0xffff,
5805 QCBOR_SUCCESS,
5806 65535.0,
5807 QCBOR_SUCCESS
5808 },
5809 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005810 "Postive integer 0",
5811 {(uint8_t[]){0x0}, 1},
5812 0LL,
5813 QCBOR_SUCCESS,
5814 0ULL,
5815 QCBOR_SUCCESS,
5816 0.0,
5817 QCBOR_SUCCESS
5818 },
5819 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005820 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005821 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5822 -9223372036854775807-1, // INT64_MIN
5823 QCBOR_SUCCESS,
5824 0ULL,
5825 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5826 -9223372036854775808.0,
5827 QCBOR_SUCCESS
5828 },
5829 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005830 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005831 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005832#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005833 100L,
5834 QCBOR_SUCCESS,
5835 100ULL,
5836 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005837#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5838 0,
5839 QCBOR_ERR_HW_FLOAT_DISABLED,
5840 0,
5841 QCBOR_ERR_HW_FLOAT_DISABLED,
5842#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005843 100.3,
5844 QCBOR_SUCCESS
5845 },
5846 {
5847 "Floating point value NaN 0xfa7fc00000",
5848 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005849#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005850 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005851 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005852 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005853 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005854#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5855 0,
5856 QCBOR_ERR_HW_FLOAT_DISABLED,
5857 0,
5858 QCBOR_ERR_HW_FLOAT_DISABLED,
5859#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005860 NAN,
5861 QCBOR_SUCCESS
5862 },
5863 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005864 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005865 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005866#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5867#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5868 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005869 -4,
5870 QCBOR_SUCCESS,
5871 0,
5872 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5873 -4.0,
5874 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005875#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5876 // Float HW disabled
5877 -4,
5878 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5879 0,
5880 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5881 -4.0,
5882 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5883#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005884#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005885 // Half-precision disabled
5886 -4,
5887 QCBOR_ERR_HALF_PRECISION_DISABLED,
5888 0,
5889 QCBOR_ERR_HALF_PRECISION_DISABLED,
5890 -4.0,
5891 QCBOR_ERR_HALF_PRECISION_DISABLED
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005892#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005893 },
5894 {
5895 "Decimal fraction 3/10",
5896 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005897#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005898 0,
5899 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5900 0,
5901 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5902 0.30000000000000004,
5903 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005904#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005905 0,
5906 QCBOR_ERR_UNEXPECTED_TYPE,
5907 0,
5908 QCBOR_ERR_UNEXPECTED_TYPE,
5909 0.0,
5910 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005911#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005912 },
5913 {
5914 "+inifinity",
5915 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5916#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5917 0,
5918 QCBOR_ERR_FLOAT_EXCEPTION,
5919 0,
5920 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5921#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5922 0,
5923 QCBOR_ERR_HW_FLOAT_DISABLED,
5924 0,
5925 QCBOR_ERR_HW_FLOAT_DISABLED,
5926#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5927 INFINITY,
5928 QCBOR_SUCCESS
5929 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005930
5931 {
5932 "extreme pos bignum",
5933 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5934 // 50 rows of 8 is 400 digits.
5935 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5936 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5937 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5938 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5939 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5940 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5941 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5942 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5943 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5944 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5945 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5946 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5947 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5948 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5949 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5950 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5951 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5952 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5953 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5954 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5955 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5956 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5957 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5958 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5959 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5960 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5961 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5962 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5963 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5964 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5965 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5966 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5967 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5968 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5969 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5970 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 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},
5985 404},
5986 0,
5987 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5988 0,
5989 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5990#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5991 INFINITY,
5992 QCBOR_SUCCESS
5993#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5994 0,
5995 QCBOR_ERR_HW_FLOAT_DISABLED,
5996#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5997 },
5998
5999 {
6000 "extreme neg bignum",
6001 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
6002 // 50 rows of 8 is 400 digits.
6003 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6004 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6005 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6006 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6007 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6008 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6009 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6010 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6011 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6012 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6013 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6014 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6015 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6016 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6017 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6018 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6019 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6020 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6021 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6022 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6023 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6024 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6025 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6026 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6027 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6028 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6029 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6030 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6031 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6032 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6033 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6034 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6035 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6036 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6037 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6038 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6039 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6040 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6041 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6042 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6043 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6044 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6045 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6046 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6047 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6048 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6049 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6050 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6051 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6052 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
6053 404},
6054 0,
6055 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6056 0,
6057 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6058#ifndef QCBOR_DISABLE_FLOAT_HW_USE
6059 -INFINITY,
6060 QCBOR_SUCCESS
6061#else /* QCBOR_DISABLE_FLOAT_HW_USE */
6062 0,
6063 QCBOR_ERR_HW_FLOAT_DISABLED,
6064#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
6065 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006066
6067 {
6068 "big float underflow [9223372036854775806, -9223372036854775806]",
6069 {(uint8_t[]){
6070 0xC5, 0x82,
6071 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6072 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006073#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006074 0,
6075 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6076 0,
6077 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6078 0,
6079 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006080#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006081 0,
6082 QCBOR_ERR_UNEXPECTED_TYPE,
6083 0,
6084 QCBOR_ERR_UNEXPECTED_TYPE,
6085 0.0,
6086 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006087#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006088 },
6089
6090 {
6091 "bigfloat that evaluates to -INFINITY",
6092 {(uint8_t[]){
6093 0xC5, 0x82,
6094 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6095 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006096#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006097 0,
6098 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6099 0,
6100 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6101 -INFINITY,
6102 QCBOR_SUCCESS
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006103#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006104 0,
6105 QCBOR_ERR_UNEXPECTED_TYPE,
6106 0,
6107 QCBOR_ERR_UNEXPECTED_TYPE,
6108 0.0,
6109 QCBOR_ERR_UNEXPECTED_TYPE
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006110#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006111 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006112};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006113
6114
6115
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006116
6117static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6118{
6119 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6120#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6121 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6122 return 1;
6123 }
6124#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6125 (void)Pool;
6126#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6127 return 0;
6128}
6129
6130
Laurence Lundblade313b2862020-05-16 01:23:06 -07006131int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006132{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006133 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6134 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006135
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006136 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6137 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006138
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006139 // Set up the decoding context including a memory pool so that
6140 // indefinite length items can be checked
6141 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006142 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006143
6144 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006145 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6146 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006147 }
6148
6149 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006150 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006151 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006152 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006153 }
6154 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006155 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006156 }
6157
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006158 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006159 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6160 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006161 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006162
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006163 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006164 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006165 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006166 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006167 }
6168 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006169 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006170 }
6171
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006172 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006173 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6174 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006175 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006176#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006177 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006178 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006179 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006180 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006181 }
6182 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6183 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006184 // NaN's can't be compared for equality. A NaN is
6185 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006186 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006187 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006188 }
6189 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006190 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006191 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006192 }
6193 }
6194 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006195#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006196 }
6197
6198 return 0;
6199}
6200
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006201
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006202
6203
Laurence Lundbladee3553422020-05-02 11:11:17 -07006204int32_t CBORSequenceDecodeTests(void)
6205{
6206 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006207 QCBORItem Item;
6208 QCBORError uCBORError;
6209 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006210
6211 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006212
Laurence Lundbladee3553422020-05-02 11:11:17 -07006213 // The input for the date test happens to be a sequence so it
6214 // is reused. It is a sequence because it doesn't start as
6215 // an array or map.
6216 QCBORDecode_Init(&DCtx,
6217 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
6218 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006219
Laurence Lundbladee3553422020-05-02 11:11:17 -07006220 // Get the first item
6221 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6222 if(uCBORError != QCBOR_SUCCESS) {
6223 return 1;
6224 }
6225 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
6226 return 2;
6227 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006228
Laurence Lundblade87495732021-02-26 10:05:55 -07006229 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6230 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6231 uConsumed != 12) {
6232 return 102;
6233 }
6234
Laurence Lundbladee3553422020-05-02 11:11:17 -07006235 // Get a second item
6236 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07006237 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
6238 return 66;
6239 }
6240
Laurence Lundblade87495732021-02-26 10:05:55 -07006241 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6242 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6243 uConsumed != 14) {
6244 return 102;
6245 }
6246
Laurence Lundbladec7114722020-08-13 05:11:40 -07006247 // Get a third item
6248 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006249 if(uCBORError != QCBOR_SUCCESS) {
6250 return 2;
6251 }
6252 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6253 return 3;
6254 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006255
Laurence Lundbladee3553422020-05-02 11:11:17 -07006256 // A sequence can have stuff at the end that may
6257 // or may not be valid CBOR. The protocol decoder knows
6258 // when to stop by definition of the protocol, not
6259 // when the top-level map or array is ended.
6260 // Finish still has to be called to know that
6261 // maps and arrays (if there were any) were closed
6262 // off correctly. When called like this it
6263 // must return the error QCBOR_ERR_EXTRA_BYTES.
6264 uCBORError = QCBORDecode_Finish(&DCtx);
6265 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6266 return 4;
6267 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006268
6269
Laurence Lundbladee3553422020-05-02 11:11:17 -07006270 // --- Test an empty input ----
6271 uint8_t empty[1];
6272 UsefulBufC Empty = {empty, 0};
6273 QCBORDecode_Init(&DCtx,
6274 Empty,
6275 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006276
Laurence Lundbladee3553422020-05-02 11:11:17 -07006277 uCBORError = QCBORDecode_Finish(&DCtx);
6278 if(uCBORError != QCBOR_SUCCESS) {
6279 return 5;
6280 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006281
6282
Laurence Lundbladee3553422020-05-02 11:11:17 -07006283 // --- Sequence with unclosed indefinite length array ---
6284 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006285
Laurence Lundbladee3553422020-05-02 11:11:17 -07006286 QCBORDecode_Init(&DCtx,
6287 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6288 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006289
Laurence Lundbladee3553422020-05-02 11:11:17 -07006290 // Get the first item
6291 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6292 if(uCBORError != QCBOR_SUCCESS) {
6293 return 7;
6294 }
6295 if(Item.uDataType != QCBOR_TYPE_INT64) {
6296 return 8;
6297 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006298
Laurence Lundbladee3553422020-05-02 11:11:17 -07006299 // Get a second item
6300 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006301#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006302 if(uCBORError != QCBOR_SUCCESS) {
6303 return 9;
6304 }
6305 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6306 return 10;
6307 }
6308
6309 // Try to finish before consuming all bytes to confirm
6310 // that the still-open error is returned.
6311 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006312 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006313 return 11;
6314 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006315#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6316 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6317 return 20;
6318 }
6319#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006320
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006321
Laurence Lundbladee3553422020-05-02 11:11:17 -07006322 // --- Sequence with a closed indefinite length array ---
6323 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006324
Laurence Lundbladee3553422020-05-02 11:11:17 -07006325 QCBORDecode_Init(&DCtx,
6326 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6327 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006328
Laurence Lundbladee3553422020-05-02 11:11:17 -07006329 // Get the first item
6330 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6331 if(uCBORError != QCBOR_SUCCESS) {
6332 return 12;
6333 }
6334 if(Item.uDataType != QCBOR_TYPE_INT64) {
6335 return 13;
6336 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006337
Laurence Lundbladee3553422020-05-02 11:11:17 -07006338 // Get a second item
6339 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006340#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6341
Laurence Lundbladee3553422020-05-02 11:11:17 -07006342 if(uCBORError != QCBOR_SUCCESS) {
6343 return 14;
6344 }
6345 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6346 return 15;
6347 }
6348
6349 // Try to finish before consuming all bytes to confirm
6350 // that the still-open error is returned.
6351 uCBORError = QCBORDecode_Finish(&DCtx);
6352 if(uCBORError != QCBOR_SUCCESS) {
6353 return 16;
6354 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006355#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6356 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6357 return 20;
6358 }
6359#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006360
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006361
Laurence Lundbladee3553422020-05-02 11:11:17 -07006362 return 0;
6363}
6364
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006365
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006366
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006367int32_t IntToTests()
6368{
6369 int nErrCode;
6370 int32_t n32;
6371 int16_t n16;
6372 int8_t n8;
6373 uint32_t u32;
6374 uint16_t u16;
6375 uint8_t u8;
6376 uint64_t u64;
6377
6378 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6379 if(nErrCode == -1 || n32 != 1) {
6380 return 1;
6381 }
6382
6383 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6384 if(nErrCode == -1 || n32 != INT32_MAX) {
6385 return 2;
6386 }
6387
6388 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6389 if(nErrCode == -1 || n32 != INT32_MIN) {
6390 return 3;
6391 }
6392
6393 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6394 if(nErrCode != -1) {
6395 return 4;
6396 }
6397
6398 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6399 if(nErrCode != -1) {
6400 return 5;
6401 }
6402
6403
6404 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6405 if(nErrCode == -1 || n16 != INT16_MAX) {
6406 return 6;
6407 }
6408
6409 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6410 if(nErrCode == -1 || n16 != INT16_MIN) {
6411 return 7;
6412 }
6413
6414 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6415 if(nErrCode == -1 || n16 != 1) {
6416 return 8;
6417 }
6418
6419 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6420 if(nErrCode != -1) {
6421 return 9;
6422 }
6423
6424 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6425 if(nErrCode != -1) {
6426 return 10;
6427 }
6428
6429
6430 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6431 if(nErrCode == -1 || n8 != 1) {
6432 return 11;
6433 }
6434
6435 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6436 if(nErrCode == -1 || n8 != INT8_MAX) {
6437 return 12;
6438 }
6439
6440 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6441 if(nErrCode == -1 || n8 != INT8_MIN) {
6442 return 13;
6443 }
6444
6445 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6446 if(nErrCode != -1) {
6447 return 14;
6448 }
6449
6450 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6451 if(nErrCode != -1) {
6452 return 15;
6453 }
6454
6455
6456 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6457 if(nErrCode == -1 || u32 != 1) {
6458 return 16;
6459 }
6460
6461 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6462 if(nErrCode == -1 || u32 != UINT32_MAX) {
6463 return 17;
6464 }
6465
6466 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6467 if(nErrCode == -1 || u32 != 0) {
6468 return 18;
6469 }
6470
6471 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6472 if(nErrCode != -1) {
6473 return 19;
6474 }
6475
6476 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6477 if(nErrCode != -1) {
6478 return 20;
6479 }
6480
6481
6482 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6483 if(nErrCode == -1 || u16 != UINT16_MAX) {
6484 return 21;
6485 }
6486
6487 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6488 if(nErrCode == -1 || u16 != 0) {
6489 return 22;
6490 }
6491
6492 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6493 if(nErrCode == -1 || u16 != 1) {
6494 return 23;
6495 }
6496
6497 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6498 if(nErrCode != -1) {
6499 return 24;
6500 }
6501
6502 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6503 if(nErrCode != -1) {
6504 return 25;
6505 }
6506
6507
6508 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6509 if(nErrCode == -1 || u8 != UINT8_MAX) {
6510 return 26;
6511 }
6512
6513 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6514 if(nErrCode == -1 || u8 != 0) {
6515 return 27;
6516 }
6517
6518 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6519 if(nErrCode == -1 || u8 != 1) {
6520 return 28;
6521 }
6522
6523 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6524 if(nErrCode != -1) {
6525 return 29;
6526 }
6527
6528 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6529 if(nErrCode != -1) {
6530 return 30;
6531 }
6532
6533
6534 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6535 if(nErrCode == -1 || u64 != 1) {
6536 return 31;
6537 }
6538
6539 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6540 if(nErrCode == -1 || u64 != INT64_MAX) {
6541 return 32;
6542 }
6543
6544 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6545 if(nErrCode == -1 || u64 != 0) {
6546 return 33;
6547 }
6548
6549 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6550 if(nErrCode != -1) {
6551 return 34;
6552 }
6553
6554 return 0;
6555}
6556
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006557
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006558
6559
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006560/*
6561A sequence with
6562 A wrapping bstr
6563 containing a map
6564 1
6565 2
6566 A wrapping bstr
6567 containing an array
6568 3
6569 wrapping bstr
6570 4
6571 5
6572 6
6573 array
6574 7
6575 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006576 */
6577
Laurence Lundblade55013642020-09-23 05:39:22 -07006578static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006579{
Laurence Lundblade55013642020-09-23 05:39:22 -07006580 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006581 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006582 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006583
Laurence Lundblade55013642020-09-23 05:39:22 -07006584 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006585
6586 QCBOREncode_BstrWrap(&EC);
6587 QCBOREncode_OpenMap(&EC);
6588 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6589 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6590 QCBOREncode_CloseMap(&EC);
6591 QCBOREncode_BstrWrap(&EC);
6592 QCBOREncode_OpenArray(&EC);
6593 QCBOREncode_AddInt64(&EC, 3);
6594 QCBOREncode_BstrWrap(&EC);
6595 QCBOREncode_AddInt64(&EC, 4);
6596 QCBOREncode_CloseBstrWrap(&EC, NULL);
6597 QCBOREncode_AddInt64(&EC, 5);
6598 QCBOREncode_CloseArray(&EC);
6599 QCBOREncode_CloseBstrWrap(&EC, NULL);
6600 QCBOREncode_AddInt64(&EC, 6);
6601 QCBOREncode_CloseBstrWrap(&EC, NULL);
6602 QCBOREncode_OpenArray(&EC);
6603 QCBOREncode_AddInt64(&EC, 7);
6604 QCBOREncode_AddInt64(&EC, 8);
6605 QCBOREncode_CloseArray(&EC);
6606
6607 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006608 if(uErr) {
6609 Encoded = NULLUsefulBufC;
6610 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006611
6612 return Encoded;
6613}
6614
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006615/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006616static const uint8_t spBreakInByteString[] = {
6617 0x41, 0xff
6618};
6619
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006620
6621int32_t EnterBstrTest()
6622{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006623 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006624
6625 QCBORDecodeContext DC;
6626
Laurence Lundblade55013642020-09-23 05:39:22 -07006627 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006628
Laurence Lundblade55013642020-09-23 05:39:22 -07006629 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006630
6631
Laurence Lundblade9b334962020-08-27 10:55:53 -07006632 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006633 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006634 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6635 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006636 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006637 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006638 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006639 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006640 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006641 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006642 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006643 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006644 QCBORDecode_ExitArray(&DC);
6645 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006646 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006647 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006648 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006649 QCBORDecode_GetInt64(&DC, &n7);
6650 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006651 QCBORDecode_ExitArray(&DC);
6652
6653 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006654 if(uErr) {
6655 return (int32_t)uErr;
6656 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006657
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006658
6659 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6660 * Successful because no items are fetched from byte string.
6661 */
6662 QCBORDecode_Init(&DC,
6663 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6664 0);
6665 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6666 uErr = QCBORDecode_GetError(&DC);
6667 if(uErr) {
6668 return 100 + (int32_t)uErr;
6669 }
6670
6671 QCBORDecode_ExitBstrWrapped(&DC);
6672 uErr = QCBORDecode_GetError(&DC);
6673 if(uErr) {
6674 return 200 + (int32_t)uErr;
6675 }
6676
6677 /* Try to get item that is a break out of a byte string wrapped CBOR.
6678 * It fails because there should be no break.
6679 */
6680 QCBORDecode_Init(&DC,
6681 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6682 0);
6683 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6684 QCBORItem Item;
6685 uErr = QCBORDecode_GetNext(&DC, &Item);
6686 if(uErr != QCBOR_ERR_BAD_BREAK) {
6687 return 300 + (int32_t)uErr;
6688 }
6689
6690 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006691}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006692
6693
6694
6695
6696static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006697 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006698
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006699 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006700 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006701 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6702 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6703 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006704
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006705 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006706 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6707 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6708 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006709
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006710 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006711 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006712 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6713 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006714
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006715 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006716 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6717 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006718
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006719 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006720 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006721 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6722 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006723
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006724 21,
6725 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6726 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6727
6728 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006729 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006730 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6731 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006732
6733 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006734 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6735 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006736
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006737 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006738 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006739 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6740 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006741
6742 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006743 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6744 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006745
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006746 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006747 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006748 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6749 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006750
6751 0x18, 0x33,
6752 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6753
6754 // MIME
6755 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006756 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6757 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6758 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006759
6760 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006761 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6762 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006763
6764 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006765 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6766 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6767 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006768
6769 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006770 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6771 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006772
6773 // UUID
6774 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006775 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6776 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006777
6778 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006779 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6780 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006781};
6782
6783int32_t DecodeTaggedTypeTests()
6784{
6785 QCBORDecodeContext DC;
6786 QCBORError uErr;
6787
6788 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6789
6790 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006791 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006792
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006793 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006794 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006795 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006796 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6797 return 1;
6798 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006799 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006800 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6801 return 2;
6802 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006803 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006804 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6805 return 3;
6806 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006807 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006808 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006809 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6810 return 4;
6811 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006812 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_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 5;
6815 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006816
Laurence Lundblade9b334962020-08-27 10:55:53 -07006817 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006818 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6819 bNeg != false) {
6820 return 10;
6821 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006822 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006823 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6824 bNeg != true) {
6825 return 11;
6826 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006827 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006828 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6829 return 12;
6830 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006831 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
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 13;
6834 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006835 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
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 14;
6838 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006839
Laurence Lundblade9b334962020-08-27 10:55:53 -07006840 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006841 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6842 return 20;
6843 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006844 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006845 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6846 return 21;
6847 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006848 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006849 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006850 return 22;
6851 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006852 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006853 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006854 return 23;
6855 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006856
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006857#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006858 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006859 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6860 return 30;
6861 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006862#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006863 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006864 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6865 return 31;
6866 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006867 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006868 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006869 return 32;
6870 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006871 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006872 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006873 return 33;
6874 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006875
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006876#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006877 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006878 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6879 return 40;
6880 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006881#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006882 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006883 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6884 return 41;
6885 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006886 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
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 42;
6889 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006890 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006891 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006892 return 43;
6893 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006894
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006895#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006896 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006897 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6898 return 50;
6899 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006900#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006901 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006902 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6903 return 51;
6904 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006905 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006906 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006907 return 52;
6908 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006909 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006910 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006911 return 53;
6912 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006913
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006914#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006915 // MIME
6916 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006917 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006918 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6919 bIsNot7Bit == true) {
6920 return 60;
6921 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006922 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006923 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6924 bIsNot7Bit == true) {
6925 return 61;
6926 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006927 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006928 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6929 bIsNot7Bit == false) {
6930 return 62;
6931 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006932 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006933 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6934 bIsNot7Bit == false) {
6935 return 63;
6936 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006937 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006938 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006939 return 64;
6940 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006941 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006942 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006943 return 65;
6944 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006945
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006946
Laurence Lundblade9b334962020-08-27 10:55:53 -07006947 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006948 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6949 return 70;
6950 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006951#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
6952
Laurence Lundblade9b334962020-08-27 10:55:53 -07006953 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006954 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6955 return 71;
6956 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006957 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006958 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006959 return 72;
6960 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006961 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006962 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006963 return 73;
6964 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006965
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006966 // Improvement: add some more error test cases
6967
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006968 QCBORDecode_ExitMap(&DC);
6969
6970 uErr = QCBORDecode_Finish(&DC);
6971 if(uErr != QCBOR_SUCCESS) {
6972 return 100;
6973 }
6974
6975 return 0;
6976}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006977
6978
6979
6980
6981/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006982 [
6983 "aaaaaaaaaa",
6984 {}
6985 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006986 */
6987static const uint8_t spTooLarge1[] = {
6988 0x9f,
6989 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6990 0xa0,
6991 0xff
6992};
6993
6994/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006995 [
6996 {
6997 0: "aaaaaaaaaa"
6998 }
6999 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007000 */
7001static const uint8_t spTooLarge2[] = {
7002 0x9f,
7003 0xa1,
7004 0x00,
7005 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7006 0xff
7007};
7008
7009/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007010 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007011
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007012 {
7013 0: "aaaaaaaaaa"
7014 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007015 */
7016static const uint8_t spTooLarge3[] = {
7017 0x4d,
7018 0xa1,
7019 0x00,
7020 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7021};
7022
7023int32_t TooLargeInputTest(void)
7024{
7025 QCBORDecodeContext DC;
7026 QCBORError uErr;
7027 UsefulBufC String;
7028
7029 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7030 // to 10 There's not really any way to test this error
7031 // condition. The error condition is not complex, so setting
7032 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7033
7034 // The input CBOR is only too large because the
7035 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7036 //
7037 // This test is disabled for the normal test runs because of the
7038 // special build requirement.
7039
7040
7041 // Tests the start of a map being too large
7042 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007043 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007044 QCBORDecode_GetTextString(&DC, &String);
7045 uErr = QCBORDecode_GetError(&DC);
7046 if(uErr != QCBOR_SUCCESS) {
7047 return 1;
7048 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007049 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007050 uErr = QCBORDecode_GetError(&DC);
7051 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7052 return 2;
7053 }
7054
7055 // Tests the end of a map being too large
7056 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007057 QCBORDecode_EnterArray(&DC, NULL);
7058 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007059 uErr = QCBORDecode_GetError(&DC);
7060 if(uErr != QCBOR_SUCCESS) {
7061 return 3;
7062 }
7063 QCBORDecode_ExitMap(&DC);
7064 uErr = QCBORDecode_GetError(&DC);
7065 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7066 return 4;
7067 }
7068
7069 // Tests the entire input CBOR being too large when processing bstr wrapping
7070 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7071 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7072 uErr = QCBORDecode_GetError(&DC);
7073 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7074 return 5;
7075 }
7076
7077 return 0;
7078}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007079
7080
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007081#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7082
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007083static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007084 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007085 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7086 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7087 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7088 0x03,
7089 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7090 0xc3,
7091 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007092};
7093
7094int32_t SpiffyIndefiniteLengthStringsTests()
7095{
7096 QCBORDecodeContext DCtx;
7097
7098 QCBORDecode_Init(&DCtx,
7099 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7100 QCBOR_DECODE_MODE_NORMAL);
7101
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007102 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007103 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7104
7105 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007106 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007107 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
7108 if(QCBORDecode_GetAndResetError(&DCtx)) {
7109 return 1;
7110 }
7111
7112 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7113 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7114 return 2;
7115 }
7116
7117 uint64_t uInt;
7118 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7119 if(QCBORDecode_GetAndResetError(&DCtx)) {
7120 return 3;
7121 }
7122 if(uInt != 3) {
7123 return 4;
7124 }
7125
7126 double uDouble;
7127 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7128 "label2",
7129 0xff,
7130 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007131#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007132 if(QCBORDecode_GetAndResetError(&DCtx)) {
7133 return 5;
7134 }
7135 if(uDouble != -16777474) {
7136 return 6;
7137 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007138#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007139 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7140 return 7;
7141 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007142#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007143
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007144
7145 QCBORDecode_ExitMap(&DCtx);
7146
7147 if(QCBORDecode_Finish(&DCtx)) {
7148 return 99;
7149 }
7150
7151 return 0;
7152}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007153#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007154
7155
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007156/*
7157 * An array of an integer and an array. The second array contains
7158 * a bstr-wrapped map.
7159 *
7160 * [7, [h'A36D6669... (see next lines) 73']]
7161 *
7162 * {"first integer": 42,
7163 * "an array of two strings": ["string1", "string2"],
7164 * "map in a map":
7165 * { "bytes 1": h'78787878',
7166 * "bytes 2": h'79797979',
7167 * "another int": 98,
7168 * "text 2": "lies, damn lies and statistics"
7169 * }
7170 * }
7171 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007172
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007173static const uint8_t pValidWrappedMapEncoded[] = {
7174 0x82, 0x07, 0x81, 0x58, 0x97,
7175 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7176 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7177 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7178 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7179 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7180 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7181 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7182 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7183 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7184 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7185 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7186 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7187 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7188 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7189 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7190 0x73
7191};
7192
7193#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7194
7195/* As above, but the arrays are indefinite length */
7196static const uint8_t pValidIndefWrappedMapEncoded[] = {
7197 0x9f, 0x07, 0x9f, 0x58, 0x97,
7198 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7199 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7200 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7201 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7202 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7203 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7204 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7205 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7206 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7207 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7208 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7209 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7210 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7211 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7212 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7213 0x73,
7214 0xff, 0xff
7215};
7216#endif
7217
7218
7219static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7220
7221#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7222static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7223#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7224
7225#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7226static const uint8_t pWrappedByIndefiniteLength[] = {
7227 0x81,
7228 0xd8, 0x18,
7229 0x5f,
7230 0x41, 0x83,
7231 0x41, 0x18,
7232 0x43, 0x2A, 0x18, 0x2B,
7233 0x42, 0x18, 0x2C,
7234 0xff
7235};
7236#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7237
7238
7239int32_t PeekAndRewindTest()
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007240{
7241 QCBORItem Item;
7242 QCBORError nCBORError;
7243 QCBORDecodeContext DCtx;
7244
7245 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7246
7247 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7248 return 100+(int32_t)nCBORError;
7249 }
7250 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7251 return 200;
7252 }
7253
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007254 QCBORDecode_VPeekNext(&DCtx, &Item);
7255 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
7256 return 150+(int32_t)nCBORError;
7257 }
7258 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7259 return 250;
7260 }
7261
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007262 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7263 return (int32_t)nCBORError;
7264 }
7265 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7266 return 300;
7267 }
7268
7269 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7270 return 400 + (int32_t)nCBORError;
7271 }
7272 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7273 return 500;
7274 }
7275
7276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7277 return (int32_t)nCBORError;
7278 }
7279 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7280 return 600;
7281 }
7282
7283 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7284 return 900 + (int32_t)nCBORError;
7285 }
7286 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7287 Item.uDataType != QCBOR_TYPE_INT64 ||
7288 Item.val.int64 != 42 ||
7289 Item.uDataAlloc ||
7290 Item.uLabelAlloc ||
7291 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7292 return 1000;
7293 }
7294
7295 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7296 return 1100 + (int32_t)nCBORError;
7297 }
7298
7299 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7300 Item.uDataType != QCBOR_TYPE_INT64 ||
7301 Item.val.int64 != 42 ||
7302 Item.uDataAlloc ||
7303 Item.uLabelAlloc ||
7304 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7305 return 1200;
7306 }
7307
7308
7309 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7310 return 1300 + (int32_t)nCBORError;
7311 }
7312 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7313 Item.uDataAlloc ||
7314 Item.uLabelAlloc ||
7315 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7316 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007317 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007318 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007319 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007320
7321 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7322 return 1500 + (int32_t)nCBORError;
7323 }
7324 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7325 Item.uDataAlloc ||
7326 Item.uLabelAlloc ||
7327 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7328 return 1600;
7329 }
7330
7331 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7332 return 1700 + (int32_t)nCBORError;
7333 }
7334 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7335 Item.uDataAlloc ||
7336 Item.uLabelAlloc ||
7337 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7338 return 1800;
7339 }
7340
7341 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7342 return (int32_t)nCBORError;
7343 }
7344 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7345 Item.uDataAlloc ||
7346 Item.uLabelAlloc ||
7347 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7348 return 1900;
7349 }
7350
7351 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7352 return (int32_t)nCBORError;
7353 }
7354 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7355 Item.uDataAlloc ||
7356 Item.uLabelAlloc ||
7357 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7358 return 2000;
7359 }
7360
7361
7362 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7363 return 2100 + (int32_t)nCBORError;
7364 }
7365 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7366 Item.uDataAlloc ||
7367 Item.uLabelAlloc ||
7368 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7369 Item.uDataType != QCBOR_TYPE_MAP ||
7370 Item.val.uCount != 4) {
7371 return 2100;
7372 }
7373
7374 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7375 return 2200 + (int32_t)nCBORError;
7376 }
7377 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7378 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7379 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7380 Item.uDataAlloc ||
7381 Item.uLabelAlloc ||
7382 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7383 return 2300;
7384 }
7385
7386 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7387 return 2400 + (int32_t)nCBORError;
7388 }
7389 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7390 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7391 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7392 Item.uDataAlloc ||
7393 Item.uLabelAlloc ||
7394 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7395 return 2500;
7396 }
7397
7398 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7399 return 2600 + (int32_t)nCBORError;
7400 }
7401 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7402 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7403 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7404 Item.uDataAlloc ||
7405 Item.uLabelAlloc ||
7406 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7407 return 2700;
7408 }
7409
7410 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7411 return 2800 + (int32_t)nCBORError;
7412 }
7413 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7414 Item.uDataAlloc ||
7415 Item.uLabelAlloc ||
7416 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7417 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007418 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007419 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007420 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007421
7422 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7423 return 3000 + (int32_t)nCBORError;
7424 }
7425 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7426 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7427 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7428 Item.uDataAlloc ||
7429 Item.uLabelAlloc ||
7430 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7431 return 3100;
7432 }
7433
7434 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7435 return 3200 + (int32_t)nCBORError;
7436 }
7437 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7438 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7439 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7440 Item.uDataAlloc ||
7441 Item.uLabelAlloc ||
7442 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7443 return 3300;
7444 }
7445
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007446 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
7447 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7448 return 3300 + (int32_t)nCBORError;
7449 }
7450
7451 QCBORDecode_VPeekNext(&DCtx, &Item);
7452 nCBORError = QCBORDecode_GetError(&DCtx);
7453 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7454 return 3400 + (int32_t)nCBORError;
7455 }
7456
7457 QCBORDecode_VPeekNext(&DCtx, &Item);
7458 nCBORError = QCBORDecode_GetError(&DCtx);
7459 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7460 return 3500 + (int32_t)nCBORError;
7461 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007462
7463
7464 // Rewind to top level after entering several maps
7465 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7466
7467 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7468 return (int32_t)nCBORError;
7469 }
7470 if(Item.uDataType != QCBOR_TYPE_MAP ||
7471 Item.val.uCount != 3) {
7472 return 400;
7473 }
7474
7475 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7476 return 4000+(int32_t)nCBORError;
7477 }
7478
7479 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7480 Item.uDataType != QCBOR_TYPE_INT64 ||
7481 Item.val.int64 != 42 ||
7482 Item.uDataAlloc ||
7483 Item.uLabelAlloc ||
7484 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7485 return 4100;
7486 }
7487
7488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7489 return 4100+(int32_t)nCBORError;
7490 }
7491 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7492 Item.uDataAlloc ||
7493 Item.uLabelAlloc ||
7494 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7495 Item.uDataType != QCBOR_TYPE_ARRAY ||
7496 Item.val.uCount != 2) {
7497 return 4200;
7498 }
7499
7500 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7501 return 4200+(int32_t)nCBORError;
7502 }
7503 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7504 Item.uDataAlloc ||
7505 Item.uLabelAlloc ||
7506 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7507 return 4300;
7508 }
7509
7510 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7511 return 4300+(int32_t)nCBORError;
7512 }
7513 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7514 Item.uDataAlloc ||
7515 Item.uLabelAlloc ||
7516 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7517 return 4400;
7518 }
7519
7520 QCBORDecode_Rewind(&DCtx);
7521
7522 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7523 return 4400+(int32_t)nCBORError;
7524 }
7525 if(Item.uDataType != QCBOR_TYPE_MAP ||
7526 Item.val.uCount != 3) {
7527 return 4500;
7528 }
7529
7530 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7531 return (int32_t)nCBORError;
7532 }
7533
7534 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7535 Item.uDataType != QCBOR_TYPE_INT64 ||
7536 Item.val.int64 != 42 ||
7537 Item.uDataAlloc ||
7538 Item.uLabelAlloc ||
7539 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7540 return 4600;
7541 }
7542
7543 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7544 return (int32_t)nCBORError;
7545 }
7546 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7547 Item.uDataAlloc ||
7548 Item.uLabelAlloc ||
7549 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7550 Item.uDataType != QCBOR_TYPE_ARRAY ||
7551 Item.val.uCount != 2) {
7552 return 4700;
7553 }
7554
7555 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7556 return (int32_t)nCBORError;
7557 }
7558 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7559 Item.uDataAlloc ||
7560 Item.uLabelAlloc ||
7561 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7562 return 4800;
7563 }
7564
7565 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7566 return 4900+(int32_t)nCBORError;
7567 }
7568 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7569 Item.uDataAlloc ||
7570 Item.uLabelAlloc ||
7571 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7572 return 5000;
7573 }
7574
7575
7576 // Rewind an entered map
7577 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7578
7579 QCBORDecode_EnterMap(&DCtx, NULL);
7580
7581 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7582 return 5100+(int32_t)nCBORError;
7583 }
7584
7585 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7586 Item.uDataType != QCBOR_TYPE_INT64 ||
7587 Item.val.int64 != 42 ||
7588 Item.uDataAlloc ||
7589 Item.uLabelAlloc ||
7590 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7591 return 5200;
7592 }
7593
7594 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7595 return 5200+(int32_t)nCBORError;
7596 }
7597 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7598 Item.uDataAlloc ||
7599 Item.uLabelAlloc ||
7600 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7601 Item.uDataType != QCBOR_TYPE_ARRAY ||
7602 Item.val.uCount != 2) {
7603 return -5300;
7604 }
7605
7606 QCBORDecode_Rewind(&DCtx);
7607
7608 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7609 return 5300+(int32_t)nCBORError;
7610 }
7611
7612 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7613 Item.uDataType != QCBOR_TYPE_INT64 ||
7614 Item.val.int64 != 42 ||
7615 Item.uDataAlloc ||
7616 Item.uLabelAlloc ||
7617 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7618 return 5400;
7619 }
7620
7621 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7622 return 5400+(int32_t)nCBORError;
7623 }
7624 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7625 Item.uDataAlloc ||
7626 Item.uLabelAlloc ||
7627 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7628 Item.uDataType != QCBOR_TYPE_ARRAY ||
7629 Item.val.uCount != 2) {
7630 return 5500;
7631 }
7632
7633
7634 // Rewind and entered array inside an entered map
7635 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7636
7637 QCBORDecode_EnterMap(&DCtx, NULL);
7638
7639 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7640
7641 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7642 return 5600+(int32_t)nCBORError;
7643 }
7644 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7645 Item.uDataAlloc ||
7646 Item.uLabelAlloc ||
7647 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7648 return 5700;
7649 }
7650
7651 QCBORDecode_Rewind(&DCtx);
7652
7653 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7654 return 5700+(int32_t)nCBORError;
7655 }
7656 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7657 Item.uDataAlloc ||
7658 Item.uLabelAlloc ||
7659 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7660 return 5800;
7661 }
7662
7663 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7664 return (int32_t)nCBORError;
7665 }
7666 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7667 Item.uDataAlloc ||
7668 Item.uLabelAlloc ||
7669 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7670 return 5900;
7671 }
7672
7673 QCBORDecode_Rewind(&DCtx);
7674
7675 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7676 return 5900+(int32_t)nCBORError;
7677 }
7678 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7679 Item.uDataAlloc ||
7680 Item.uLabelAlloc ||
7681 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7682 return 6000;
7683 }
7684
7685
7686 // Rewind a byte string inside an array inside an array
7687 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
7688
7689 QCBORDecode_EnterArray(&DCtx, NULL);
7690
7691 uint64_t i;
7692 QCBORDecode_GetUInt64(&DCtx, &i);
7693
7694 QCBORDecode_EnterArray(&DCtx, NULL);
7695
7696 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7697 if(QCBORDecode_GetError(&DCtx)) {
7698 return 6100;
7699 }
7700
7701 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7702 return (int32_t)nCBORError;
7703 }
7704 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7705 return 6200;
7706 }
7707
7708 QCBORDecode_Rewind(&DCtx);
7709
7710 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7711 return 6300+(int32_t)nCBORError;
7712 }
7713 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7714 return 6400;
7715 }
7716
7717#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7718 // Rewind a byte string inside an indefinite-length array inside
7719 // indefinite-length array
7720
7721 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
7722
7723 QCBORDecode_EnterArray(&DCtx, NULL);
7724
7725 QCBORDecode_GetUInt64(&DCtx, &i);
7726
7727 QCBORDecode_EnterArray(&DCtx, NULL);
7728
7729 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7730 if(QCBORDecode_GetError(&DCtx)) {
7731 return 6500;
7732 }
7733
7734 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7735 return 6600+(int32_t)nCBORError;
7736 }
7737 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7738 return 6700;
7739 }
7740
7741 QCBORDecode_Rewind(&DCtx);
7742
7743 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7744 return 6800+(int32_t)nCBORError;
7745 }
7746 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7747 return 6900;
7748 }
7749#endif
7750
7751 // Rewind an empty map
7752 // [100, {}]
7753 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
7754 QCBORDecode_EnterArray(&DCtx, NULL);
7755 QCBORDecode_GetUInt64(&DCtx, &i);
7756 if(i != 100) {
7757 return 7010;
7758 }
7759 QCBORDecode_EnterMap(&DCtx, NULL);
7760
7761 /* Do it 5 times to be sure multiple rewinds work */
7762 for(int n = 0; n < 5; n++) {
7763 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7764 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7765 return 7000 + n;
7766 }
7767 QCBORDecode_Rewind(&DCtx);
7768 }
7769 QCBORDecode_ExitMap(&DCtx);
7770 QCBORDecode_Rewind(&DCtx);
7771 QCBORDecode_GetUInt64(&DCtx, &i);
7772 if(i != 100) {
7773 return 7010;
7774 }
7775 QCBORDecode_ExitArray(&DCtx);
7776 QCBORDecode_Rewind(&DCtx);
7777 QCBORDecode_EnterArray(&DCtx, NULL);
7778 i = 9;
7779 QCBORDecode_GetUInt64(&DCtx, &i);
7780 if(i != 100) {
7781 return 7020;
7782 }
7783 if(QCBORDecode_GetError(&DCtx)){
7784 return 7030;
7785 }
7786
7787 // Rewind an empty indefinite length map
7788#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7789 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
7790 QCBORDecode_EnterArray(&DCtx, NULL);
7791 QCBORDecode_GetUInt64(&DCtx, &i);
7792 if(i != 100) {
7793 return 7810;
7794 }
7795 QCBORDecode_EnterMap(&DCtx, NULL);
7796
7797 /* Do it 5 times to be sure multiple rewinds work */
7798 for(int n = 0; n < 5; n++) {
7799 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7800 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7801 return 7800 + n;
7802 }
7803 QCBORDecode_Rewind(&DCtx);
7804 }
7805 QCBORDecode_ExitMap(&DCtx);
7806 QCBORDecode_Rewind(&DCtx);
7807 QCBORDecode_GetUInt64(&DCtx, &i);
7808 if(i != 100) {
7809 return 7810;
7810 }
7811 QCBORDecode_ExitArray(&DCtx);
7812 QCBORDecode_Rewind(&DCtx);
7813 QCBORDecode_EnterArray(&DCtx, NULL);
7814 i = 9;
7815 QCBORDecode_GetUInt64(&DCtx, &i);
7816 if(i != 100) {
7817 return 7820;
7818 }
7819 if(QCBORDecode_GetError(&DCtx)){
7820 return 7830;
7821 }
7822#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7823
7824 // Rewind an indefnite length byte-string wrapped sequence
7825#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7826 QCBORDecode_Init(&DCtx,
7827 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
7828 0);
7829 UsefulBuf_MAKE_STACK_UB(Pool, 100);
7830 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
7831
7832 QCBORDecode_EnterArray(&DCtx, NULL);
7833 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
7834 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007835 /* this is what happens when trying to enter
7836 indefinite-length byte string
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007837 wrapped CBOR. Tolerate for now. Eventually it needs
7838 to be fixed so this works, but that is not simple. */
7839 return 7300;
7840 }
7841
7842 /*
7843 QCBORDecode_GetUInt64(&DCtx, &i);
7844 if(i != 42) {
7845 return 7110;
7846 }
7847 QCBORDecode_Rewind(&DCtx);
7848 QCBORDecode_GetUInt64(&DCtx, &i);
7849 if(i != 42) {
7850 return 7220;
7851 }*/
7852#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7853
7854
7855 // Rewind an indefnite length byte-string wrapped sequence
7856
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007857 return 0;
7858}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007859
7860
7861
7862
7863static const uint8_t spBooleansInMap[] =
7864{
7865 0xa1, 0x08, 0xf5
7866};
7867
7868static const uint8_t spBooleansInMapWrongType[] =
7869{
7870 0xa1, 0x08, 0xf6
7871};
7872
7873static const uint8_t spBooleansInMapNWF[] =
7874{
7875 0xa1, 0x08, 0x1a
7876};
7877
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007878static const uint8_t spNullInMap[] =
7879{
7880 0xa1, 0x08, 0xf6
7881};
7882
7883static const uint8_t spUndefinedInMap[] =
7884{
7885 0xa1, 0x08, 0xf7
7886};
7887
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007888
7889int32_t BoolTest(void)
7890{
7891 QCBORDecodeContext DCtx;
7892 bool b;
7893
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007894 QCBORDecode_Init(&DCtx,
7895 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7896 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007897 QCBORDecode_EnterMap(&DCtx, NULL);
7898 QCBORDecode_GetBool(&DCtx, &b);
7899 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7900 return 1;
7901 }
7902
7903 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
7904 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7905 return 2;
7906 }
7907
7908 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
7909 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7910 return 3;
7911 }
7912
7913
7914 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
7915 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7916 return 4;
7917 }
7918
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007919 QCBORDecode_Init(&DCtx,
7920 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
7921 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007922 QCBORDecode_EnterMap(&DCtx, NULL);
7923 QCBORDecode_GetBool(&DCtx, &b);
7924 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7925 return 5;
7926 }
7927
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007928 QCBORDecode_Init(&DCtx,
7929 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7930 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007931 QCBORDecode_EnterMap(&DCtx, NULL);
7932 QCBORDecode_GetBool(&DCtx, &b);
7933 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7934 return 6;
7935 }
7936
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007937
7938 QCBORDecode_Init(&DCtx,
7939 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7940 0);
7941 QCBORDecode_EnterMap(&DCtx, NULL);
7942 QCBORDecode_GetNull(&DCtx);
7943 if(QCBORDecode_GetAndResetError(&DCtx)) {
7944 return 7;
7945 }
7946
7947 QCBORDecode_Init(&DCtx,
7948 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7949 0);
7950 QCBORDecode_EnterMap(&DCtx, NULL);
7951 QCBORDecode_GetNull(&DCtx);
7952 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7953 return 8;
7954 }
7955
7956 QCBORDecode_Init(&DCtx,
7957 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7958 0);
7959 QCBORDecode_EnterMap(&DCtx, NULL);
7960 QCBORDecode_GetNullInMapN(&DCtx, 8);
7961 if(QCBORDecode_GetAndResetError(&DCtx)) {
7962 return 9;
7963 }
7964
7965 QCBORDecode_Init(&DCtx,
7966 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7967 0);
7968 QCBORDecode_EnterMap(&DCtx, NULL);
7969 QCBORDecode_GetNullInMapN(&DCtx, 8);
7970 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7971 return 10;
7972 }
7973
7974 QCBORDecode_Init(&DCtx,
7975 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7976 0);
7977 QCBORDecode_EnterMap(&DCtx, NULL);
7978 QCBORDecode_GetUndefined(&DCtx);
7979 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7980 return 11;
7981 }
7982
7983 QCBORDecode_Init(&DCtx,
7984 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7985 0);
7986 QCBORDecode_EnterMap(&DCtx, NULL);
7987 QCBORDecode_GetUndefined(&DCtx);
7988 if(QCBORDecode_GetAndResetError(&DCtx)) {
7989 return 12;
7990 }
7991
7992 QCBORDecode_Init(&DCtx,
7993 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7994 0);
7995 QCBORDecode_EnterMap(&DCtx, NULL);
7996 QCBORDecode_GetUndefined(&DCtx);
7997 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7998 return 13;
7999 }
8000
8001 QCBORDecode_Init(&DCtx,
8002 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8003 0);
8004 QCBORDecode_EnterMap(&DCtx, NULL);
8005 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8006 if(QCBORDecode_GetAndResetError(&DCtx)) {
8007 return 14;
8008 }
8009
8010 QCBORDecode_Init(&DCtx,
8011 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8012 0);
8013 QCBORDecode_EnterMap(&DCtx, NULL);
8014 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8015 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8016 return 15;
8017 }
8018
8019 QCBORDecode_Init(&DCtx,
8020 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8021 0);
8022 QCBORDecode_EnterMap(&DCtx, NULL);
8023 QCBORDecode_GetUndefined(&DCtx);
8024 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8025 return 15;
8026 }
8027
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008028 return 0;
8029}