blob: 16f08317b3b00c87c8bb6254ef0bcfb54004e97a [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 Lundblade4bf6e712020-12-10 11:53:21 -08001816
Laurence Lundblade59289e52019-12-30 13:44:37 -08001817 // Iterate until there is an error of some sort error
1818 QCBORItem Item;
1819 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001820 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001821 memset(&Item, 0x33, sizeof(Item));
1822
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001823 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1824 } while(uCBORError == QCBOR_SUCCESS);
1825
1826
Laurence Lundblade59289e52019-12-30 13:44:37 -08001827
1828 // Must get the expected error or the this test fails
1829 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001830 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001831 Item.uDataType != QCBOR_TYPE_NONE ||
1832 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001833 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001834 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001835 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001836 }
1837 }
1838
1839 return 0;
1840}
1841
1842
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001843static const struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001844 // Most of this is copied from not_well_formed.h. Here the error code
1845 // returned is also checked.
1846
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001847#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001848 // Indefinite length strings must be closed off
1849 // An indefinite length byte string not closed off
1850 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1851 // An indefinite length text string not closed off
1852 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1853
1854
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001855 // All the chunks in an indefinite length string must be of the type of
1856 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001857 // indefinite length byte string with text string chunk
1858 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1859 // indefinite length text string with a byte string chunk
1860 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1861 // indefinite length byte string with an positive integer chunk
1862 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1863 // indefinite length byte string with an negative integer chunk
1864 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1865 // indefinite length byte string with an array chunk
1866 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1867 // indefinite length byte string with an map chunk
1868 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1869 // indefinite length byte string with tagged integer chunk
1870 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1871 // indefinite length byte string with an simple type chunk
1872 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1873 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1874 // indefinite length text string with indefinite string inside
1875 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1876
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001877#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1878
1879 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1880 // An indefinite length text string not closed off
1881 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1882
1883
1884 // All the chunks in an indefinite length string must be of the type of
1885 // indefinite length string
1886 // indefinite length byte string with text string chunk
1887 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1888 // indefinite length text string with a byte string chunk
1889 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1890 // indefinite length byte string with an positive integer chunk
1891 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1892 // indefinite length byte string with an negative integer chunk
1893 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1894 // indefinite length byte string with an array chunk
1895 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1896 // indefinite length byte string with an map chunk
1897 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1898 // indefinite length byte string with tagged integer chunk
1899 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1900 // indefinite length byte string with an simple type chunk
1901 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1902 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1903 // indefinite length text string with indefinite string inside
1904 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1905#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1906
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001907
1908 // Definte length maps and arrays must be closed by having the right number of items
1909 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001910 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001911 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001912 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001913 // A definte length array that is supposed to have 511 items, but has only 1
1914 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1915 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001916 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001917 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001918 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001919
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001920#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001921 // Indefinte length maps and arrays must be ended by a break
1922 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001923 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001924 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001925 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001926 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001927 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001928 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001929 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001930
1931
1932 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001933 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001934 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001935 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001936 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001937 // Unclosed indefinite map containing a closed definite length array
1938 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1939 // Definite length map containing an unclosed indefinite length array
1940 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001941 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001942 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001943 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001944 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001945 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001946 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001947 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001948 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001949 // Unclosed indefinite length map in definite length maps
1950 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1951 QCBOR_ERR_NO_MORE_ITEMS},
1952 // Unclosed definite length map in indefinite length maps
1953 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1954 // Unclosed indefinite length array in definite length maps
1955 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1956 QCBOR_ERR_NO_MORE_ITEMS},
1957 // Unclosed definite length array in indefinite length maps
1958 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1959 // Unclosed indefinite length map in definite length arrays
1960 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1961 // Unclosed definite length map in indefinite length arrays
1962 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001963#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001964
1965 // The "argument" for the data item is incomplete
1966 // Positive integer missing 1 byte argument
1967 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1968 // Positive integer missing 2 byte argument
1969 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1970 // Positive integer missing 4 byte argument
1971 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1972 // Positive integer missing 8 byte argument
1973 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1974 // Positive integer missing 1 byte of 2 byte argument
1975 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1976 // Positive integer missing 2 bytes of 4 byte argument
1977 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1978 // Positive integer missing 1 bytes of 7 byte argument
1979 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1980 // Negative integer missing 1 byte argument
1981 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1982 // Binary string missing 1 byte argument
1983 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1984 // Text string missing 1 byte argument
1985 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1986 // Array missing 1 byte argument
1987 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1988 // Map missing 1 byte argument
1989 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1990 // Tag missing 1 byte argument
1991 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1992 // Simple missing 1 byte argument
1993 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001994 // half-precision with 1 byte argument
1995 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1996 // single-precision with 2 byte argument
1997 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1998 // double-precision with 3 byte argument
1999 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
2000
2001
2002 // Tag with no content
2003 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002004
2005
2006 // Breaks must not occur in definite length arrays and maps
2007 // Array of length 1 with sole member replaced by a break
2008 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2009 // Array of length 2 with 2nd member replaced by a break
2010 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2011 // Map of length 1 with sole member label replaced by a break
2012 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2013 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002014 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002015 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
2016 // Array of length 1 with 2nd member value replaced by a break
2017 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2018 // Map of length 2 with 2nd member replaced by a break
2019 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
2020
2021
2022 // Breaks must not occur on their own out of an indefinite length data item
2023 // A bare break is not well formed
2024 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
2025 // A bare break after a zero length definite length array
2026 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002027#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002028 // A bare break after a zero length indefinite length map
2029 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002030 // A break inside a definite length array inside an indefenite length array
2031 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2032 // Complicated mixed nesting with break outside indefinite length array
2033 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002034#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002035
2036
2037 // Forbidden two byte encodings of simple types
2038 // Must use 0xe0 instead
2039 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2040 // Should use 0xe1 instead
2041 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2042 // Should use 0xe2 instead
2043 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2044 // Should use 0xe3 instead
2045 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2046 // Should use 0xe4 instead
2047 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2048 // Should use 0xe5 instead
2049 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2050 // Should use 0xe6 instead
2051 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2052 // Should use 0xe7 instead
2053 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2054 // Should use 0xe8 instead
2055 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2056 // Should use 0xe9 instead
2057 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2058 // Should use 0xea instead
2059 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2060 // Should use 0xeb instead
2061 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2062 // Should use 0xec instead
2063 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2064 // Should use 0xed instead
2065 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2066 // Should use 0xee instead
2067 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2068 // Should use 0xef instead
2069 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2070 // Should use 0xf0 instead
2071 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2072 // Should use 0xf1 instead
2073 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2074 // Should use 0xf2 instead
2075 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2076 // Must use 0xf3 instead
2077 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2078 // Must use 0xf4 instead
2079 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2080 // Must use 0xf5 instead
2081 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2082 // Must use 0xf6 instead
2083 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2084 // Must use 0xf7 instead
2085 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2086 // Must use 0xf8 instead
2087 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002088 // Reserved
2089 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002090
2091 // Integers with additional info indefinite length
2092 // Positive integer with additional info indefinite length
2093 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
2094 // Negative integer with additional info indefinite length
2095 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
2096 // CBOR tag with "argument" an indefinite length
2097 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
2098 // CBOR tag with "argument" an indefinite length alternate vector
2099 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
2100
2101
2102 // Missing bytes from a deterministic length string
2103 // A byte string is of length 1 without the 1 byte
2104 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
2105 // A text string is of length 1 without the 1 byte
2106 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002107
2108#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08002109 // Byte string should have 2^32-15 bytes, but has one
2110 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2111 // Byte string should have 2^32-15 bytes, but has one
2112 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002113 // Byte string should have 2^64 bytes, but has 3
2114 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2115 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2116 // Text string should have 2^64 bytes, but has 3
2117 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2118 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002119#else
2120 // Byte string should have 2^32-15 bytes, but has one
2121 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2122 // Byte string should have 2^32-15 bytes, but has one
2123 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2124 // Byte string should have 2^16 bytes, but has 3
2125 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2126 // Text string should have 2^64 bytes, but has 3
2127 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2128#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002129
2130 // Use of unassigned additional information values
2131 // Major type positive integer with reserved value 28
2132 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2133 // Major type positive integer with reserved value 29
2134 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2135 // Major type positive integer with reserved value 30
2136 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2137 // Major type negative integer with reserved value 28
2138 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2139 // Major type negative integer with reserved value 29
2140 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2141 // Major type negative integer with reserved value 30
2142 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2143 // Major type byte string with reserved value 28 length
2144 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2145 // Major type byte string with reserved value 29 length
2146 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2147 // Major type byte string with reserved value 30 length
2148 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2149 // Major type text string with reserved value 28 length
2150 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2151 // Major type text string with reserved value 29 length
2152 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2153 // Major type text string with reserved value 30 length
2154 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2155 // Major type array with reserved value 28 length
2156 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2157 // Major type array with reserved value 29 length
2158 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2159 // Major type array with reserved value 30 length
2160 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2161 // Major type map with reserved value 28 length
2162 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2163 // Major type map with reserved value 29 length
2164 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2165 // Major type map with reserved value 30 length
2166 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2167 // Major type tag with reserved value 28 length
2168 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2169 // Major type tag with reserved value 29 length
2170 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2171 // Major type tag with reserved value 30 length
2172 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2173 // Major type simple with reserved value 28 length
2174 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2175 // Major type simple with reserved value 29 length
2176 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2177 // Major type simple with reserved value 30 length
2178 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2179
2180
2181 // Maps must have an even number of data items (key & value)
2182 // Map with 1 item when it should have 2
2183 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2184 // Map with 3 item when it should have 4
2185 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002186#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002187 // Map with 1 item when it should have 2
2188 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2189 // Map with 3 item when it should have 4
2190 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002191#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002192
2193
2194 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002195 // Text-based date, with an integer
2196 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2197 // Epoch date, with an byte string
2198 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2199 // tagged as both epoch and string dates
2200 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2201 // big num tagged an int, not a byte string
2202 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002203};
2204
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002205int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002206{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002207 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002208
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002209 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002210 if(nResult) {
2211 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002213
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002214 // Corrupt the UsefulInputBuf and see that
2215 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002216 QCBORDecodeContext DCtx;
2217 QCBORItem Item;
2218 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002219
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002220 QCBORDecode_Init(&DCtx,
2221 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2222 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002223
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002224 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2225 return (int32_t)uQCBORError;
2226 }
2227 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2228 // This wasn't supposed to happen
2229 return -1;
2230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002232 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002233
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002234 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2235 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2236 // Did not get back the error expected
2237 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002239
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002240
Laurence Lundblade98427e92020-09-28 21:33:23 -07002241 /*
2242 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2243 tests here can be performed to see that the max length
2244 error check works correctly. See DecodeBytes(). If the max
2245 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002246
Laurence Lundblade98427e92020-09-28 21:33:23 -07002247 This test will automatocally adapt the all CPU sizes
2248 through the use of SIZE_MAX.
2249 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002250
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002251 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002252 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002253
Laurence Lundblade98427e92020-09-28 21:33:23 -07002254 // This makes a CBOR head with a text string that is very long
2255 // but doesn't fill in the bytes of the text string as that is
2256 // not needed to test this part of QCBOR.
2257 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2258
2259 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2260
2261 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2262 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002263 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002264
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002265 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002266}
2267
2268
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002269/* Try all 256 values of the byte at nLen including recursing for
2270 each of the values to try values at nLen+1 ... up to nLenMax
2271 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002272static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002273{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002274 if(nLen >= nLenMax) {
2275 return;
2276 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002277
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002278 for(int inputByte = 0; inputByte < 256; inputByte++) {
2279 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002280 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002281 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002282
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002283 // Get ready to parse
2284 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002285 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002286
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002287 // Parse by getting the next item until an error occurs
2288 // Just about every possible decoder error can occur here
2289 // The goal of this test is not to check for the correct
2290 // error since that is not really possible. It is to
2291 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002292 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002293 QCBORItem Item;
2294 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002295 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002296 break;
2297 }
2298 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002299
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002300 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002301 }
2302}
2303
2304
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002305int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002306{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002307 // Size 2 tests 64K inputs and runs quickly
2308 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002310 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002311
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002312 return 0;
2313}
2314
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002315
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002316int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002317{
2318 // size 3 tests 16 million inputs and runs OK
2319 // in seconds on fast machines. Size 4 takes
2320 // 10+ minutes and 5 half a day on fast
2321 // machines. This test is kept separate from
2322 // the others so as to no slow down the use
2323 // of them as a very frequent regression.
2324 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002325
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002326 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002327
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002328 return 0;
2329}
2330
2331
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002332static const uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002333 0xc0, // tag for string date
2334 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002335
Laurence Lundbladec7114722020-08-13 05:11:40 -07002336 0xc0, // tag for string date
2337 0x00, // Wrong type for a string date
2338
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002339 0xc1, // tag for epoch date
2340 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2341
Laurence Lundbladec7114722020-08-13 05:11:40 -07002342 0xc1,
2343 0x62, 'h', 'i', // wrong type tagged
2344
Laurence Lundblade99615302020-11-29 11:19:47 -08002345 // CBOR_TAG_ENC_AS_B64
2346 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002347 0x1a, 0x53, 0x72, 0x4E, 0x01,
2348
2349 0xc1, // tag for epoch date
2350 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002351
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002352 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002353 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002355 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002356 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002357
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002358 0xc1, // tag for epoch date
2359 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2360 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2361
2362 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002363 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2364
2365 0xc1, // tag for epoch date
2366 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2367
2368 0xc1,
2369 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2370
2371 0xc1, // tag for epoch date
2372 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002373};
2374
2375
Laurence Lundbladec7114722020-08-13 05:11:40 -07002376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002377// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002378#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002379static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002381 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002382
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002383 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002384
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002385 return diff > 0.0000001;
2386}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002387#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002388
2389
Laurence Lundblade99615302020-11-29 11:19:47 -08002390
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002391int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002392{
2393 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002394 QCBORItem Item;
2395 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002396
Laurence Lundbladeee851742020-01-08 08:37:05 -08002397 QCBORDecode_Init(&DCtx,
2398 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2399 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002400
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002401 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002402 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002403 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002404 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002405 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002406 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002407 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002408 }
2409
Laurence Lundbladec7114722020-08-13 05:11:40 -07002410 // Wrong type for a string date
2411 uError = QCBORDecode_GetNext(&DCtx, &Item);
2412 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002413 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002414 }
2415
2416 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2417 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2418 return -4;
2419 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002420 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2421 Item.val.epochDate.nSeconds != 1400000000 ||
2422 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002423 return -5;
2424 }
2425
2426 // Wrong type for an epoch date
2427 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2428 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002429 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002430
Laurence Lundblade99615302020-11-29 11:19:47 -08002431 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2432 // The date is decoded and the two tags are returned. This is to
2433 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002434 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2435 return -7;
2436 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002437 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2438 Item.val.epochDate.nSeconds != 1400000001 ||
2439 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundblade99615302020-11-29 11:19:47 -08002440 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002441 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002443
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002444 // Epoch date that is too large for our representation
2445 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002446 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002447 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002448
Laurence Lundblade9682a532020-06-06 18:33:04 -07002449#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002450 // Epoch date in float format with fractional seconds
2451 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2452 return -10;
2453 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002454 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2455 Item.val.epochDate.nSeconds != 1 ||
2456 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002457 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002458 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002459
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002460 // Epoch date float that is too large for our representation
2461 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002462 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002464
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002465 // Epoch date double that is just slightly too large
2466 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002467 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002468 }
2469
2470 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002471 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2472 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2473 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2474 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002475 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002476 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002477
2478 // Nan
2479 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2480 return -15;
2481 }
2482
2483 // +Inifinity double-precision
2484 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2485 return -16;
2486 }
2487
2488#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2489 // -Inifinity half-precision
2490 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2491 return -17;
2492 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002493#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002494 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2495 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002496 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002497#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002498
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002499#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002500 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2501 return -19;
2502 }
2503 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2504 return -20;
2505 }
2506 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2507 return -21;
2508 }
2509 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2510 return -22;
2511 }
2512 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2513 return -23;
2514 }
2515 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2516 return -24;
2517 }
2518#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2519 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2520 return -25;
2521 }
2522#else
2523 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2524 return -26;
2525 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002526#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002527
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002528#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002529
2530 return 0;
2531}
2532
Laurence Lundblade4b270642020-08-14 12:53:07 -07002533/*
2534 Test cases covered here. Some items cover more than one of these.
2535 positive integer (zero counts as a positive integer)
2536 negative integer
2537 half-precision float
2538 single-precision float
2539 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002540
Laurence Lundblade4b270642020-08-14 12:53:07 -07002541 float Overflow error
2542 Wrong type error for epoch
2543 Wrong type error for date string
2544 float disabled error
2545 half-precision disabled error
2546 -Infinity
2547 Slightly too large integer
2548 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002549
Laurence Lundblade4b270642020-08-14 12:53:07 -07002550 Get epoch by int
2551 Get string by int
2552 Get epoch by string
2553 Get string by string
2554 Fail to get epoch by wrong int label
2555 Fail to get string by wrong string label
2556 Fail to get epoch by string because it is invalid
2557 Fail to get epoch by int because it is invalid
2558
2559 Untagged values
2560 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002561static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002562 0x86,
2563
2564 0xc1,
2565 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2566
Laurence Lundbladec7114722020-08-13 05:11:40 -07002567 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002568 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2569
2570 0xc1, // tag for epoch date
2571 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2572
2573 0xc1, // tag for epoch date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002574 0x80, // Erroneous empty array as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002575
2576 0xc0, // tag for string date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002577 0xa0, // Erroneous empty map as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002578
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002579 0xa9, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002580
2581 0x00,
2582 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002583 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002584
2585 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002586 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002587 0xc1, // tag for epoch date
2588 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2589
2590 // Untagged integer 0
2591 0x08,
2592 0x00,
2593
2594 // Utagged date string with string label y
2595 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002596 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002597
2598 // Untagged -1000 with label z
2599 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002600 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002601 0x39, 0x03, 0xe7,
2602
Laurence Lundbladec7114722020-08-13 05:11:40 -07002603 0x07,
2604 0xc1, // tag for epoch date
2605 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2606
Laurence Lundblade4b270642020-08-14 12:53:07 -07002607 0x05,
2608 0xc1,
2609 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2610
Laurence Lundbladec7114722020-08-13 05:11:40 -07002611 // Untagged single-precision float with value 3.14 with string label x
2612 0x61, 0x78,
2613 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2614
Laurence Lundbladec7114722020-08-13 05:11:40 -07002615 // Untagged half-precision float with value -2
2616 0x09,
2617 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002618};
2619
2620int32_t SpiffyDateDecodeTest()
2621{
2622 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002623 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002624 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2625 nEpochDate4, nEpochDate6, nEpochDateFail,
2626 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002627 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002628 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002629
2630 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002631 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002632 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002633 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002634
Laurence Lundblade9b334962020-08-27 10:55:53 -07002635 // Too-negative float, -9.2233720368547748E+18
2636 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002637 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002638#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002639 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2640 return 1111;
2641 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002642#else
2643 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2644 return 1112;
2645 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002646#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade4b270642020-08-14 12:53:07 -07002647
2648 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002649 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002650 uError = QCBORDecode_GetAndResetError(&DC);
2651 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002652 return 1;
2653 }
2654
Laurence Lundblade4b270642020-08-14 12:53:07 -07002655 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002656 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002657 uError = QCBORDecode_GetAndResetError(&DC);
2658#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2659#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2660 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2661#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2662 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2663#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2664#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2665 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2666#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2667 if(uError != uExpectedforHalfMinusInfinity) {
2668 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002669 }
2670
Laurence Lundblade4b270642020-08-14 12:53:07 -07002671 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002672 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002673 uError = QCBORDecode_GetAndResetError(&DC);
2674 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2675 return 3;
2676 }
2677
2678 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002679 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002680 uError = QCBORDecode_GetAndResetError(&DC);
2681 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2682 return 4;
2683 }
2684
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002685 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002686
2687 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002688 QCBORDecode_GetEpochDateInMapN(&DC,
2689 5,
2690 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2691 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2692 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002693#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2694 if(nEpochDate2 != -9223372036854773760LL) {
2695 return 101;
2696 }
2697#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2698 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002699 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002700 return 102;
2701 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002702#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002703
Laurence Lundblade4b270642020-08-14 12:53:07 -07002704 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002705 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2706 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002707#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2708 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002709 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002710 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002711#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2712 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002713 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002714 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002715 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002716#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2717
2718 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002719 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2720 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002721#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2722 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002723 return 103;
2724 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002725#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2726 uError = QCBORDecode_GetAndResetError(&DC);
2727 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2728 return 104;
2729 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002730#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2731
Laurence Lundblade9b334962020-08-27 10:55:53 -07002732 // A half-precision date with value -2 FFF
2733 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2734 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002735#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2736 if(nEpochDate4 != -2) {
2737 return 105;
2738 }
2739#else
2740 uError = QCBORDecode_GetAndResetError(&DC);
2741 if(uError == QCBOR_SUCCESS) {
2742 return 106;
2743 }
2744#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002745
Laurence Lundblade4b270642020-08-14 12:53:07 -07002746
2747 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002748 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2749 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2750 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002751 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002752 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002753 return 107;
2754 }
2755
2756 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002757 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2758 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002759 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002760 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002761 return 108;
2762 }
2763
2764 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002765 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2766 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2767 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002768 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002769 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002770 return 109;
2771 }
2772
2773 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002774 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2775 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002776 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002777 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002778 return 110;
2779 }
2780
2781 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002782
Laurence Lundblade4b270642020-08-14 12:53:07 -07002783 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002784 QCBORDecode_GetEpochDateInMapN(&DC,
2785 1,
2786 QCBOR_TAG_REQUIREMENT_TAG |
2787 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2788 &nEpochDate1400000000);
2789 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002790 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002791 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2792 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002793 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002794 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2795 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002796 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002797 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2798 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002799 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002800 QCBORDecode_GetEpochDateInMapSZ(&DC,
2801 "z",
2802 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2803 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2804 &nEpochDate6);
2805 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002806
2807 QCBORDecode_ExitMap(&DC);
2808 QCBORDecode_ExitArray(&DC);
2809 uError = QCBORDecode_Finish(&DC);
2810 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002811 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002812 }
2813
Laurence Lundblade9b334962020-08-27 10:55:53 -07002814 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002815 return 200;
2816 }
2817
Laurence Lundblade9b334962020-08-27 10:55:53 -07002818 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002819 return 201;
2820 }
2821
Laurence Lundblade9b334962020-08-27 10:55:53 -07002822 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002823 return 202;
2824 }
2825
Laurence Lundblade9b334962020-08-27 10:55:53 -07002826 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002827 return 203;
2828 }
2829
Laurence Lundblade9b334962020-08-27 10:55:53 -07002830 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002831 return 204;
2832 }
2833
Laurence Lundblade9b334962020-08-27 10:55:53 -07002834 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2835 return 205;
2836 }
2837
2838 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2839 return 206;
2840 }
2841
Laurence Lundbladec7114722020-08-13 05:11:40 -07002842 return 0;
2843}
2844
2845
2846
Laurence Lundblade9b334962020-08-27 10:55:53 -07002847// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002848static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002849 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002850 0x81, // Array of one
2851 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2852 0x82, // Array of two that is the faction 1/3
2853 0x01,
2854 0x03,
2855
2856 /*
2857 More than 4 tags on an item 225(226(227(228(229([])))))
2858 */
2859 0xd8, 0xe1,
2860 0xd8, 0xe2,
2861 0xd8, 0xe3,
2862 0xd8, 0xe4,
2863 0xd8, 0xe5,
2864 0x80,
2865
2866 /* tag 10489608748473423768(
2867 2442302356(
2868 21590(
2869 240(
2870 []))))
2871 */
2872 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2873 0xda, 0x91, 0x92, 0x93, 0x94,
2874 0xd9, 0x54, 0x56,
2875 0xd8, 0xf0,
2876 0x80,
2877
2878 /* tag 21590(
2879 10489608748473423768(
2880 2442302357(
2881 65534(
2882 []))))
2883 */
2884 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2885 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2886 0xda, 0x91, 0x92, 0x93, 0x95,
2887 0xd9, 0xff, 0xfe,
2888 0x80,
2889
2890 /* Make sure to blow past the limit of tags that must be mapped.
2891 works in conjuntion with entries above.
2892 269488144(269488145(269488146(269488147([]))))
2893 */
2894 0xda, 0x10, 0x10, 0x10, 0x10,
2895 0xda, 0x10, 0x10, 0x10, 0x11,
2896 0xda, 0x10, 0x10, 0x10, 0x12,
2897 0xda, 0x10, 0x10, 0x10, 0x13,
2898 0x80,
2899
2900 /* An invalid decimal fraction with an additional tag */
2901 0xd9, 0xff, 0xfa,
2902 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2903 0x00, // the integer 0; should be a byte string
2904};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002905
Laurence Lundblade59289e52019-12-30 13:44:37 -08002906/*
2907 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002908 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002909 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002910static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002911 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002912
Laurence Lundblade59289e52019-12-30 13:44:37 -08002913/*
2914DB 9192939495969798 # tag(10489608748473423768)
2915 D8 88 # tag(136)
2916 C6 # tag(6)
2917 C7 # tag(7)
2918 80 # array(0)
2919*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002920static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002921 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002922
2923/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002924 55799(55799(55799({
2925 6(7(-23)): 5859837686836516696(7({
2926 7(-20): 11({
2927 17(-18): 17(17(17("Organization"))),
2928 9(-17): 773("SSG"),
2929 -15: 16(17(6(7("Confusion")))),
2930 17(-16): 17("San Diego"),
2931 17(-14): 17("US")
2932 }),
2933 23(-19): 19({
2934 -11: 9({
2935 -9: -7
2936 }),
2937 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2938 })
2939 })),
2940 16(-22): 23({
2941 11(8(7(-5))): 8(-3)
2942 })
2943 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002944 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002945static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002946 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2947 0xc6, 0xc7, 0x36,
2948 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2949 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2950 0xcb, 0xa5,
2951 0xd1, 0x31,
2952 0xd1, 0xd1, 0xd1, 0x6c,
2953 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2954 0xc9, 0x30,
2955 0xd9, 0x03, 0x05, 0x63,
2956 0x53, 0x53, 0x47,
2957 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002958 0xd0, 0xd1, 0xc6, 0xc7,
2959 0x69,
2960 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002961 0xd1, 0x2f,
2962 0xd1, 0x69,
2963 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2964 0xd1, 0x2d,
2965 0xd1, 0x62,
2966 0x55, 0x53,
2967 0xd7, 0x32,
2968 0xd3, 0xa2,
2969 0x2a,
2970 0xc9, 0xa1,
2971 0x28,
2972 0x26,
2973 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2974 0xcc, 0x4a,
2975 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2976 0xd0, 0x35,
2977 0xd7, 0xa1,
2978 0xcb, 0xc8, 0xc7, 0x24,
2979 0xc8, 0x22};
2980
Laurence Lundblade9b334962020-08-27 10:55:53 -07002981
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002982static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002983 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07002984
2985 0xc0, // tag for string date
2986 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2987
2988 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2989
2990 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2991
2992 0xd8, 0x23, // tag for regex
2993 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2994
2995 0xc0, // tag for string date
2996 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002997};
2998
2999
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003000static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003001
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003002
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003003int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003004{
3005 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003006 QCBORItem Item;
3007 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003008
Laurence Lundbladeee851742020-01-08 08:37:05 -08003009 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003010 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003011 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003012
Laurence Lundblade9b334962020-08-27 10:55:53 -07003013 /*
3014 This test matches the magic number tag and the fraction tag
3015 55799([...])
3016 */
3017 uError = QCBORDecode_GetNext(&DCtx, &Item);
3018 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003019 return -2;
3020 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003021 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003022 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3023 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003025
Laurence Lundblade9b334962020-08-27 10:55:53 -07003026 /*
3027 4([1,3])
3028 */
3029 uError = QCBORDecode_GetNext(&DCtx, &Item);
3030#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3031 if(uError != QCBOR_SUCCESS ||
3032 Item.uDataType != QCBOR_TYPE_ARRAY ||
3033 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3034 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3035 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3036 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3037 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3038 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3039 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003040 return -4;
3041 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003042 // consume the items in the array
3043 uError = QCBORDecode_GetNext(&DCtx, &Item);
3044 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003045
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003046#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003047 if(uError != QCBOR_SUCCESS ||
3048 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3049 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3050 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3051 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3052 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3053 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3054 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003055 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003056#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003057
Laurence Lundblade9b334962020-08-27 10:55:53 -07003058 /*
3059 More than 4 tags on an item 225(226(227(228(229([])))))
3060 */
3061 uError = QCBORDecode_GetNext(&DCtx, &Item);
3062 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003063 return -6;
3064 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003065
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003066 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3067 return -106;
3068 }
3069
3070
Laurence Lundblade9b334962020-08-27 10:55:53 -07003071 /* tag 10489608748473423768(
3072 2442302356(
3073 21590(
3074 240(
3075 []))))
3076 */
3077 uError = QCBORDecode_GetNext(&DCtx, &Item);
3078 if(uError != QCBOR_SUCCESS ||
3079 Item.uDataType != QCBOR_TYPE_ARRAY ||
3080 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3081 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3082 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3083 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003084 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003085 }
3086
3087 /* tag 21590(
3088 10489608748473423768(
3089 2442302357(
3090 21591(
3091 []))))
3092 */
3093 uError = QCBORDecode_GetNext(&DCtx, &Item);
3094 if(uError != QCBOR_SUCCESS ||
3095 Item.uDataType != QCBOR_TYPE_ARRAY ||
3096 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3097 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3098 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3099 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3100 return -8;
3101 }
3102
3103 /* Make sure to blow past the limit of tags that must be mapped.
3104 works in conjuntion with entries above.
3105 269488144(269488145(269488146(269488147([]))))
3106 */
3107 uError = QCBORDecode_GetNext(&DCtx, &Item);
3108 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3109 return -9;
3110 }
3111
3112 uError = QCBORDecode_GetNext(&DCtx, &Item);
3113 if(uError == QCBOR_SUCCESS) {
3114 return -10;
3115 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003116
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003117 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003118 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003119 // tage and then matches it. Caller-config lists are no longer
3120 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003121 QCBORDecode_Init(&DCtx,
3122 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3123 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003124 const uint64_t puList[] = {0x9192939495969798, 257};
3125 const QCBORTagListIn TL = {2, puList};
3126 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003127
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003128 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3129 return -8;
3130 }
3131 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3132 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3133 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3134 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3135 Item.val.uCount != 0) {
3136 return -9;
3137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003138
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003139 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003140 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003141 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003142 const uint64_t puLongList[17] = {1,2,1};
3143 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003144 QCBORDecode_Init(&DCtx,
3145 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3146 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003147 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3148 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3149 return -11;
3150 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003151
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003152 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003153 QCBORTagListOut Out = {0, 4, puTags};
3154
3155
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003156 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003157 QCBORDecode_Init(&DCtx,
3158 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3159 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003160 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3161 return -12;
3162 }
3163 if(puTags[0] != 0x9192939495969798 ||
3164 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003165 puTags[2] != 0x06 ||
3166 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003167 return -13;
3168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003169
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003170 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003171 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003172 QCBORDecode_Init(&DCtx,
3173 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3174 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003175 QCBORTagListOut OutSmall = {0, 3, puTags};
3176 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3177 return -14;
3178 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003179
Laurence Lundblade9b334962020-08-27 10:55:53 -07003180
3181
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003182 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003183 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3184 // It is a bit of a messy test and maybe could be improved, but
3185 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003186 QCBORDecode_Init(&DCtx,
3187 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3188 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003189 int n = CheckCSRMaps(&DCtx);
3190 if(n) {
3191 return n-2000;
3192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003193
Laurence Lundblade59289e52019-12-30 13:44:37 -08003194 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003195 QCBORDecode_Init(&DCtx,
3196 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3197 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003198
Laurence Lundblade9b334962020-08-27 10:55:53 -07003199 /* With the spiffy decode revision, this tag list is not used.
3200 It doesn't matter if a tag is in this list or not so some
3201 tests that couldn't process a tag because it isn't in this list
3202 now can process these unlisted tags. The tests have been
3203 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003204 const uint64_t puTagList[] = {773, 1, 90599561};
3205 const QCBORTagListIn TagList = {3, puTagList};
3206 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003207
3208
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003209 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3210 return -100;
3211 }
3212 if(Item.uDataType != QCBOR_TYPE_MAP ||
3213 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3214 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3215 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3216 Item.val.uCount != 2 ||
3217 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3218 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3219 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3220 Out.uNumUsed != 3) {
3221 return -101;
3222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003223
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003224 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3225 return -102;
3226 }
3227 if(Item.uDataType != QCBOR_TYPE_MAP ||
3228 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3229 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003230 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003231 Item.val.uCount != 2 ||
3232 puTags[0] != 5859837686836516696 ||
3233 puTags[1] != 7 ||
3234 Out.uNumUsed != 2) {
3235 return -103;
3236 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003237
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003238 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3239 return -104;
3240 }
3241 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003242 Item.val.uCount != 5 ||
3243 puTags[0] != 0x0b ||
3244 Out.uNumUsed != 1) {
3245 return -105;
3246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003248 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3249 return -106;
3250 }
3251 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3252 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3253 Item.val.string.len != 12 ||
3254 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3255 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3256 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3257 Out.uNumUsed != 3) {
3258 return -105;
3259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003260
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003261 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3262 return -107;
3263 }
3264 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3265 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3266 Item.val.string.len != 3 ||
3267 puTags[0] != 773 ||
3268 Out.uNumUsed != 1) {
3269 return -108;
3270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003271
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003272 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3273 return -109;
3274 }
3275 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003276 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003277 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003278 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003279 puTags[3] != 7 ||
3280 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003281 return -110;
3282 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003283
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003284 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3285 return -111;
3286 }
3287 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3288 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3289 Item.val.string.len != 9 ||
3290 puTags[0] != 17 ||
3291 Out.uNumUsed != 1) {
3292 return -112;
3293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003294
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003295 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3296 return -111;
3297 }
3298 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3299 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3300 Item.val.string.len != 2 ||
3301 puTags[0] != 17 ||
3302 Out.uNumUsed != 1) {
3303 return -112;
3304 }
3305
3306 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3307 return -113;
3308 }
3309 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003310 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003311 Item.val.uCount != 2 ||
3312 puTags[0] != 19 ||
3313 Out.uNumUsed != 1) {
3314 return -114;
3315 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003316
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003317 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3318 return -115;
3319 }
3320 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003321 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003322 Item.val.uCount != 1 ||
3323 puTags[0] != 9 ||
3324 Out.uNumUsed != 1) {
3325 return -116;
3326 }
3327
3328 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3329 return -116;
3330 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003332 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003333 Out.uNumUsed != 0) {
3334 return -117;
3335 }
3336
3337 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3338 return -118;
3339 }
3340 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3341 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003342 puTags[0] != 12 ||
3343 Out.uNumUsed != 1) {
3344 return -119;
3345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003346
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003347 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3348 return -120;
3349 }
3350 if(Item.uDataType != QCBOR_TYPE_MAP ||
3351 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3352 Item.val.uCount != 1 ||
3353 puTags[0] != 0x17 ||
3354 Out.uNumUsed != 1) {
3355 return -121;
3356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003357
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003358 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3359 return -122;
3360 }
3361 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003362 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003363 Item.val.int64 != -3 ||
3364 puTags[0] != 8 ||
3365 Out.uNumUsed != 1) {
3366 return -123;
3367 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003368
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003369 if(QCBORDecode_Finish(&DCtx)) {
3370 return -124;
3371 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003372
3373 UsefulBufC DateString;
3374 QCBORDecode_Init(&DCtx,
3375 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3376 QCBOR_DECODE_MODE_NORMAL);
3377
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003378 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003379 // tagged date string
3380 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3381 // untagged date string
3382 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3383 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3384 return 100;
3385 }
3386 // untagged byte string
3387 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3388 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3389 return 101;
3390 }
3391 // tagged regex
3392 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3393 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3394 return 102;
3395 }
3396 // tagged date string with a byte string
3397 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3398 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3399 return 103;
3400 }
3401 QCBORDecode_ExitArray(&DCtx);
3402 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3403 return 104;
3404 }
3405
3406
3407 QCBORDecode_Init(&DCtx,
3408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3409 QCBOR_DECODE_MODE_NORMAL);
3410
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003411 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003412 // tagged date string
3413 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3414 // untagged date string
3415 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3416 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3417 return 200;
3418 }
3419 // untagged byte string
3420 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3421 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3422 return 201;
3423 }
3424 // tagged regex
3425 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3426 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3427 return 202;
3428 }
3429 // tagged date string with a byte string
3430 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3431 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3432 return 203;
3433 }
3434 QCBORDecode_ExitArray(&DCtx);
3435 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3436 return 204;
3437 }
3438
3439 QCBORDecode_Init(&DCtx,
3440 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3441 QCBOR_DECODE_MODE_NORMAL);
3442
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003443 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003444 // tagged date string
3445 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3446 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3447 return 300;
3448 }
3449 // untagged date string
3450 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3451 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3452 return 301;
3453 }
3454 // untagged byte string
3455 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3456 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3457 return 302;
3458 }
3459 // tagged regex
3460 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3461 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3462 return 303;
3463 }
3464 // tagged date string with a byte string
3465 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3466 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3467 return 304;
3468 }
3469 QCBORDecode_ExitArray(&DCtx);
3470 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3471 return 305;
3472 }
3473
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003474 return 0;
3475}
3476
3477
3478
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003479
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003480static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003481 0x83,
3482 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3483 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3484 0xA4,
3485 0x63, 0x42, 0x4E, 0x2B,
3486 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3487 0x18, 0x40,
3488 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3489 0x63, 0x42, 0x4E, 0x2D,
3490 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3491 0x38, 0x3F,
3492 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3493
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003494/* The expected big num */
3495static const uint8_t spBigNum[] = {
3496 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3497 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003498
3499
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003500int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003501{
3502 QCBORDecodeContext DCtx;
3503 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003504 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003505
Laurence Lundbladeee851742020-01-08 08:37:05 -08003506 QCBORDecode_Init(&DCtx,
3507 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3508 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003509
3510
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003511 //
3512 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3513 return -1;
3514 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003515 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003517
3518 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003519 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003520 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003521 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003522 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003523 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003524 }
3525
3526 //
3527 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003528 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003529 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003530 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003531 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003534 //
3535 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003536 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003537 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003538 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003539 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003540
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003541 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003542 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003543 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3544 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003545 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003546 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003547 }
3548
3549 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003550 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003551 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3552 Item.uLabelType != QCBOR_TYPE_INT64 ||
3553 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003554 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003555 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003556 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003558 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003559 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003560 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3561 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003562 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003563 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003564 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003565
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003566 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003567 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003568 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3569 Item.uLabelType != QCBOR_TYPE_INT64 ||
3570 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003571 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003572 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003573 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003574
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003575 return 0;
3576}
3577
3578
3579
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003580static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003581 uint8_t uDataType,
3582 uint8_t uNestingLevel,
3583 uint8_t uNextNest,
3584 int64_t nLabel,
3585 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003586{
3587 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003588 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003590 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3591 if(Item.uDataType != uDataType) return -1;
3592 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003593 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3594 Item.uLabelType != QCBOR_TYPE_UINT64) {
3595 return -1;
3596 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003597 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3598 if(Item.label.int64 != nLabel) return -1;
3599 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003600 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003601 }
3602 }
3603 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303604 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003606 if(pItem) {
3607 *pItem = Item;
3608 }
3609 return 0;
3610}
3611
3612
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003613// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003614static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003615{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303616 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003617
Laurence Lundblade9b334962020-08-27 10:55:53 -07003618 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003619
Laurence Lundblade9b334962020-08-27 10:55:53 -07003620 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003621
Laurence Lundblade9b334962020-08-27 10:55:53 -07003622 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3623 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3624 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3625 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3626 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003627
Laurence Lundblade9b334962020-08-27 10:55:53 -07003628 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3629 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003630
Laurence Lundblade9b334962020-08-27 10:55:53 -07003631 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3632 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003633
Laurence Lundblade9b334962020-08-27 10:55:53 -07003634 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3635 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003636
Laurence Lundblade9b334962020-08-27 10:55:53 -07003637 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003638
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003639 return 0;
3640}
3641
3642
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003643/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003644{
3645 -23: {
3646 -20: {
3647 -18: "Organization",
3648 -17: "SSG",
3649 -15: "Confusion",
3650 -16: "San Diego",
3651 -14: "US"
3652 },
3653 -19: {
3654 -11: {
3655 -9: -7
3656 },
3657 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3658 }
3659 },
3660 -22: {
3661 -5: -3
3662 }
3663}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003664*/
3665static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003666 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3667 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3668 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3669 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3670 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3671 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3672 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3673 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3674 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3675
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003676// Same map as above, but using indefinite lengths
3677static const uint8_t spCSRInputIndefLen[] = {
3678 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3679 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3680 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3681 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3682 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3683 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3684 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3685 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3686 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3687 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3688
3689
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003690int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003691{
3692 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003693
Laurence Lundbladeee851742020-01-08 08:37:05 -08003694 QCBORDecode_Init(&DCtx,
3695 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3696 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003697
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003698 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003699}
3700
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003701
3702
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003703int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003704{
3705 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003706
Laurence Lundbladeee851742020-01-08 08:37:05 -08003707 QCBORDecode_Init(&DCtx,
3708 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3709 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003710
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003711 QCBORItem Item;
3712 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003713
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003714 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3715 return -1;
3716 }
3717 if(Item.uDataType != QCBOR_TYPE_MAP) {
3718 return -2;
3719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003720
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003721 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3722 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3723 return -3;
3724 }
3725
3726 return 0;
3727}
3728
3729
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003730
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003731int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003732{
3733 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003734
Laurence Lundbladeee851742020-01-08 08:37:05 -08003735 QCBORDecode_Init(&DCtx,
3736 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3737 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003738
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003739 return CheckCSRMaps(&DCtx);
3740}
3741
3742
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003743
Laurence Lundblade17ede402018-10-13 11:43:07 +08003744static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3745{
3746 UsefulOutBuf UOB;
3747 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003748
Laurence Lundblade17ede402018-10-13 11:43:07 +08003749 int i;
3750 for(i = 0; i < n; i++) {
3751 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3752 }
3753
3754 for(i = 0; i < n; i++) {
3755 UsefulOutBuf_AppendByte(&UOB, 0xff);
3756 }
3757 return UsefulOutBuf_OutUBuf(&UOB);
3758}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003759
3760
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003761static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003762{
3763 QCBORDecodeContext DC;
3764 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003765
Laurence Lundblade17ede402018-10-13 11:43:07 +08003766 int j;
3767 for(j = 0; j < nNestLevel; j++) {
3768 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003769 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003770 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3771 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003772 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003773 return -4;
3774 } else {
3775 return 0; // Decoding doesn't recover after an error
3776 }
3777 } else {
3778 // Should be no error
3779 if(nReturn) {
3780 return -9; // Should not have got an error
3781 }
3782 }
3783 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3784 return -7;
3785 }
3786 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003787 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003788 if(nReturn) {
3789 return -3;
3790 }
3791 return 0;
3792}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003793
3794
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003795int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003796{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303797 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003798 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003799 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003800 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003801 int nReturn = parse_indeflen_nested(Nested, i);
3802 if(nReturn) {
3803 return nReturn;
3804 }
3805 }
3806 return 0;
3807}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003808
Laurence Lundbladeee851742020-01-08 08:37:05 -08003809// [1, [2, 3]]
3810static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3811// No closing break
3812static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3813// Not enough closing breaks
3814static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3815// Too many closing breaks
3816static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3817// Unclosed indeflen inside def len
3818static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3819// confused tag
3820static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003821
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003822int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003823{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003824 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003825 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003826 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003827
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003828 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003829 QCBORDecodeContext DC;
3830 QCBORItem Item;
3831 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003832
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003833 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303834
3835 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3836 Item.uNestingLevel != 0 ||
3837 Item.uNextNestLevel != 1) {
3838 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003839 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003840
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003841 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303842 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3843 Item.uNestingLevel != 1 ||
3844 Item.uNextNestLevel != 1) {
3845 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003846 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003847
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003848 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303849 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3850 Item.uNestingLevel != 1 ||
3851 Item.uNextNestLevel != 2) {
3852 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003853 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003854
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003855 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003856 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303857 Item.uNestingLevel != 2 ||
3858 Item.uNextNestLevel != 2) {
3859 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003860 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003861
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003862 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003863 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303864 Item.uNestingLevel != 2 ||
3865 Item.uNextNestLevel != 0) {
3866 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003867 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003868
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003869 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303870 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003871 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003872
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003873 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003874 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003875
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003876 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003877
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003878 nResult = QCBORDecode_GetNext(&DC, &Item);
3879 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303880 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003881 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003882
Laurence Lundblade570fab52018-10-13 18:28:27 +08003883 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003884 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303885 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003886 }
3887
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003888
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003889 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003890 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003891
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003892 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003893
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003894 nResult = QCBORDecode_GetNext(&DC, &Item);
3895 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303896 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003897 }
3898
3899 nResult = QCBORDecode_GetNext(&DC, &Item);
3900 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303901 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003902 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003903
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003904 nResult = QCBORDecode_GetNext(&DC, &Item);
3905 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303906 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003908
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003909 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003910 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303911 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003912 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003913
3914
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003915 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003916 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003917
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003918 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003919
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003920 nResult = QCBORDecode_GetNext(&DC, &Item);
3921 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303922 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003923 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003924
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003925 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003926 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303927 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003928 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303929
Laurence Lundblade642282a2020-06-23 12:00:33 -07003930 nResult = QCBORDecode_GetNext(&DC, &Item);
3931 if(nResult != QCBOR_ERR_BAD_BREAK) {
3932 return -140;
3933 }
3934
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003935
Laurence Lundblade570fab52018-10-13 18:28:27 +08003936 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003937 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003938
Laurence Lundblade570fab52018-10-13 18:28:27 +08003939 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003940
Laurence Lundblade570fab52018-10-13 18:28:27 +08003941 nResult = QCBORDecode_GetNext(&DC, &Item);
3942 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303943 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003944 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003945
Laurence Lundblade570fab52018-10-13 18:28:27 +08003946 nResult = QCBORDecode_GetNext(&DC, &Item);
3947 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303948 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003950
Laurence Lundblade570fab52018-10-13 18:28:27 +08003951 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003952 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303953 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003954 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003955
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303956 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003957 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303959 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003960
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303961 nResult = QCBORDecode_GetNext(&DC, &Item);
3962 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303963 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003965
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303966 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303967 if(nResult != QCBOR_ERR_BAD_BREAK) {
3968 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003970
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003971 return 0;
3972}
3973
Laurence Lundblade17ede402018-10-13 11:43:07 +08003974
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08003975#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
3976
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003977static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003978 0x81, // Array of length one
3979 0x7f, // text string marked with indefinite length
3980 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3981 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3982 0xff // ending break
3983};
3984
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003985static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303986 0x81, // Array of length one
3987 0x7f, // text string marked with indefinite length
3988 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3989 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3990 0xff // ending break
3991};
3992
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003993static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303994 0x81, // Array of length one
3995 0x7f, // text string marked with indefinite length
3996 0x01, 0x02, // Not a string
3997 0xff // ending break
3998};
3999
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004000static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304001 0x81, // Array of length one
4002 0x7f, // text string marked with indefinite length
4003 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4004 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4005 // missing end of string
4006};
4007
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004008static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304009 0xa1, // Array of length one
4010 0x7f, // text string marked with indefinite length
4011 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4012 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4013 0xff, // ending break
4014 0x01 // integer being labeled.
4015};
4016
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004017/**
4018 Make an indefinite length string
4019
4020 @param Storage Storage for string, must be 144 bytes in size
4021 @return The indefinite length string
4022
4023 This makes an array with one indefinite length string that has 7 chunks
4024 from size of 1 byte up to 64 bytes.
4025 */
4026static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304027{
4028 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004029
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304030 UsefulOutBuf_Init(&UOB, Storage);
4031 UsefulOutBuf_AppendByte(&UOB, 0x81);
4032 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004033
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004034 uint8_t uStringByte = 0;
4035 // Use of type int is intentional
4036 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4037 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304038 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004039 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4040 for(int j = 0; j < uChunkSize; j++) {
4041 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4042 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304043 }
4044 }
4045 UsefulOutBuf_AppendByte(&UOB, 0xff);
4046
4047 return UsefulOutBuf_OutUBuf(&UOB);
4048}
4049
4050static int CheckBigString(UsefulBufC BigString)
4051{
4052 if(BigString.len != 255) {
4053 return 1;
4054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004055
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304056 for(uint8_t i = 0; i < 255; i++){
4057 if(((const uint8_t *)BigString.ptr)[i] != i) {
4058 return 1;
4059 }
4060 }
4061 return 0;
4062}
4063
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304064
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004065int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304066{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304067 QCBORDecodeContext DC;
4068 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304069 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004070 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004071
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304072 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004073 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304074 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004075
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304076 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304077 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304078 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004079
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304080 if(QCBORDecode_GetNext(&DC, &Item)) {
4081 return -2;
4082 }
4083 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4084 return -3;
4085 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004086
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304087 if(QCBORDecode_GetNext(&DC, &Item)) {
4088 return -4;
4089 }
4090 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4091 return -5;
4092 }
4093 if(QCBORDecode_Finish(&DC)) {
4094 return -6;
4095 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304096
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304097 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004098 QCBORDecode_Init(&DC,
4099 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4100 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004101
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304102 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4103 return -7;
4104 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004105
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304106 if(QCBORDecode_GetNext(&DC, &Item)) {
4107 return -8;
4108 }
4109 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4110 return -9;
4111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004112
Laurence Lundblade30816f22018-11-10 13:40:22 +07004113 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304114 return -10;
4115 }
4116
4117 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004118 QCBORDecode_Init(&DC,
4119 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4120 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004121
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304122 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4123 return -11;
4124 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004125
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304126 if(QCBORDecode_GetNext(&DC, &Item)) {
4127 return -12;
4128 }
4129 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4130 return -13;
4131 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundblade30816f22018-11-10 13:40:22 +07004133 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304134 return -14;
4135 }
4136
4137 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004138 QCBORDecode_Init(&DC,
4139 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4140 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004141
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304142 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4143 return -15;
4144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004145
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304146 if(QCBORDecode_GetNext(&DC, &Item)) {
4147 return -16;
4148 }
4149 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4150 return -17;
4151 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004152
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304153 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4154 return -18;
4155 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004156
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304157 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304158 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004159
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304160 QCBORDecode_GetNext(&DC, &Item);
4161 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304162 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004164
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304165 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304166 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004168
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304169 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004170 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304171
4172 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4173 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304174 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004176
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304177 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304178 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004179 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004180
Laurence Lundbladeee851742020-01-08 08:37:05 -08004181 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4182 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004183
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304184 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304185 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304186 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004188
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304189 QCBORDecode_GetNext(&DC, &Item);
4190 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304191 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304192 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004193 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304194 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304195 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004196
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304197 // ---- big bstr -----
4198 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004199
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304200 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4201 return -25;
4202 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004203
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304204 if(QCBORDecode_GetNext(&DC, &Item)) {
4205 return -26;
4206 }
4207 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304208 return -26;
4209 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004210
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304211 if(QCBORDecode_GetNext(&DC, &Item)) {
4212 return -27;
4213 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304214 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304215 return -28;
4216 }
4217 if(CheckBigString(Item.val.string)) {
4218 return -3;
4219 }
4220 if(QCBORDecode_Finish(&DC)) {
4221 return -29;
4222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004223
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304224 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004225 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004226
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304227 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4228 return -30;
4229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004230
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304231 QCBORDecode_GetNext(&DC, &Item);
4232 if(Item.uDataType != QCBOR_TYPE_MAP) {
4233 return -31;
4234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004235
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304236 if(QCBORDecode_GetNext(&DC, &Item)){
4237 return -32;
4238 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004239 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4240 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304241 Item.uDataAlloc || !Item.uLabelAlloc ||
4242 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4243 return -33;
4244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004245
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304246 if(QCBORDecode_Finish(&DC)) {
4247 return -34;
4248 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004249
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004250 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004251}
4252
4253
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004254int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304255{
4256 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004257 QCBORError nCBORError;
4258
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004259
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304260 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004261 QCBORDecode_Init(&DC,
4262 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4263 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004264
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004265 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004266
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004267 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4268 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304269 return -1;
4270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004271
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004272 if(CheckCSRMaps(&DC)) {
4273 return -2;
4274 }
4275
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004276 // Next parse, save pointers to a few strings, destroy original and
4277 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004278 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004279 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004280
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304281 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004282 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304283 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004284
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304285 QCBORItem Item1, Item2, Item3, Item4;
4286 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004287 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304288 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4289 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004290 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304291 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004292 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304293 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004294 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304295 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004296 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304297 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004298 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004299
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304300 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004301
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304302 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304303 Item1.uDataType != QCBOR_TYPE_INT64 ||
4304 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004305 Item1.uDataAlloc != 0 ||
4306 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004307 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004308 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004310
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304311
4312 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004313 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304314 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004315 Item2.uDataAlloc != 0 ||
4316 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304317 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004318 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004319
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304320 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004321 Item3.uDataAlloc == 0 ||
4322 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004323 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004324 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004326
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304327 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004328 Item4.uDataAlloc == 0 ||
4329 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004330 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004331 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004333
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304334 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004335 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004336 QCBORDecode_Init(&DC,
4337 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4338 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304339 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4340 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004341 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304342 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004343 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004344 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004345 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304346 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4347 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4348 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4349 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4350 }
4351 }
4352 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004353 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004354 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304355 }
4356
4357 return 0;
4358}
4359
Laurence Lundbladef6531662018-12-04 10:42:22 +09004360
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004361int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004362{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004363 // Set up the decoder with a tiny bit of CBOR to parse because
4364 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004365 QCBORDecodeContext DC;
4366 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4367 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004368
Laurence Lundbladef6531662018-12-04 10:42:22 +09004369 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004370 // Then fish into the internals of the decode context
4371 // to get the allocator function so it can be called directly.
4372 // Also figure out how much pool is available for use
4373 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004374 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004375 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4376 if(nError) {
4377 return -9;
4378 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004379 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4380 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4381 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004382
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004383 // First test -- ask for one more byte than available and see failure
4384 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004385 if(!UsefulBuf_IsNULL(Allocated)) {
4386 return -1;
4387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004388
Laurence Lundbladef6531662018-12-04 10:42:22 +09004389 // Re do the set up for the next test that will do a successful alloc,
4390 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004391 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004392 pAlloc = DC.StringAllocator.pfAllocator;
4393 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4394 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004395
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004396 // Allocate one byte less than available and see success
4397 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004398 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4399 return -2;
4400 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004401 // Ask for some more and see failure
4402 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004403 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4404 return -3;
4405 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004406 // Free the first allocate, retry the second and see success
4407 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4408 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004409 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4410 return -4;
4411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004412
Laurence Lundbladef6531662018-12-04 10:42:22 +09004413 // Re do set up for next test that involves a successful alloc,
4414 // and a successful realloc and a failed realloc
4415 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004416 pAlloc = DC.StringAllocator.pfAllocator;
4417 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004418
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004419 // Allocate half the pool and see success
4420 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004421 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4422 return -5;
4423 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004424 // Reallocate to take up the whole pool and see success
4425 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004426 if(UsefulBuf_IsNULL(Allocated2)) {
4427 return -6;
4428 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004429 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004430 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4431 return -7;
4432 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004433 // Try to allocate more to be sure there is failure after a realloc
4434 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4435 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004436 return -8;
4437 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004438
Laurence Lundbladef6531662018-12-04 10:42:22 +09004439 return 0;
4440}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004441
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004442
4443/* Just enough of an allocator to test configuration of one */
4444static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4445{
4446 (void)pOldMem; // unused variable
4447
4448 if(uNewSize) {
4449 // Assumes the context pointer is the buffer and
4450 // nothing too big will ever be asked for.
4451 // This is only good for this basic test!
4452 return (UsefulBuf) {pCtx, uNewSize};
4453 } else {
4454 return NULLUsefulBuf;
4455 }
4456}
4457
4458
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004459int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004460{
4461 // Set up the decoder with a tiny bit of CBOR to parse because
4462 // nothing can be done with it unless that is set up.
4463 QCBORDecodeContext DC;
4464 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4465 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4466
4467 uint8_t pAllocatorBuffer[50];
4468
4469 // This is really just to test that this call works.
4470 // The full functionality of string allocators is tested
4471 // elsewhere with the MemPool internal allocator.
4472 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4473
4474 QCBORItem Item;
4475 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4476 return -1;
4477 }
4478
4479 if(Item.uDataAlloc == 0 ||
4480 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4481 Item.val.string.ptr != pAllocatorBuffer) {
4482 return -2;
4483 }
4484
4485 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4486 return -3;
4487 }
4488
4489 return 0;
4490}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004491#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4492
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004493
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004494#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004495
Laurence Lundbladea826c502020-05-10 21:07:00 -07004496/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004497 [
4498 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004499 4([-20, 4759477275222530853136]),
4500 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004501 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004502 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004503 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004504 5([ 9223372036854775806, -4759477275222530853137])
4505 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004506 ]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004507 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004508static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004509 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004510 0xC4, 0x82, 0x20,
4511 0x03,
4512 0xC4, 0x82, 0x33,
4513 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4514 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4515 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4516 0xC5, 0x82, 0x19, 0x01, 0x2C,
4517 0x18, 0x64,
4518 0xC5, 0x82, 0x33,
4519 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4520 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4521 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004522 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4523 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004524 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4525 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4526};
4527
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004528
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004529int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004530{
4531 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004532 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004533 QCBORItem item;
4534
Laurence Lundblade17af4902020-01-07 19:11:55 -08004535 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4536 0x06, 0x07, 0x08, 0x09, 0x010};
4537 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004538
4539
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004540 QCBORDecode_Init(&DC,
4541 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4542 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004543
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004544 uErr = QCBORDecode_GetNext(&DC, &item);
4545 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004546 return 1;
4547 }
4548
4549 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4550 return 2;
4551 }
4552
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004553 uErr = QCBORDecode_GetNext(&DC, &item);
4554 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004555 return 3;
4556 }
4557
4558 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4559 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4560 item.val.expAndMantissa.nExponent != -1) {
4561 return 4;
4562 }
4563
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004564 uErr = QCBORDecode_GetNext(&DC, &item);
4565 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004566 return 5;
4567 }
4568
4569 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4570 item.val.expAndMantissa.nExponent != -20 ||
4571 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4572 return 6;
4573 }
4574
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004575 uErr = QCBORDecode_GetNext(&DC, &item);
4576 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004577 return 7;
4578 }
4579
4580 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4581 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4582 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4583 return 8;
4584 }
4585
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004586 uErr = QCBORDecode_GetNext(&DC, &item);
4587 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004588 return 9;
4589 }
4590
4591 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4592 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4593 item.val.expAndMantissa.nExponent != 300) {
4594 return 10;
4595 }
4596
Laurence Lundbladea826c502020-05-10 21:07:00 -07004597 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004598 uErr = QCBORDecode_GetNext(&DC, &item);
4599 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004600 return 11;
4601 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004602 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4603 item.val.expAndMantissa.nExponent != -20 ||
4604 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4605 return 12;
4606 }
4607
Laurence Lundbladea826c502020-05-10 21:07:00 -07004608 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004609 uErr = QCBORDecode_GetNext(&DC, &item);
4610 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004611 return 13;
4612 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004613 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4614 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4615 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4616 return 14;
4617 }
4618
Laurence Lundbladea826c502020-05-10 21:07:00 -07004619 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004620 uErr = QCBORDecode_GetNext(&DC, &item);
4621 if(uErr != QCBOR_SUCCESS) {
4622 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004623 }
4624 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4625 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4626 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004627 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004628 }
4629
Laurence Lundbladea826c502020-05-10 21:07:00 -07004630 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004631 uErr = QCBORDecode_GetNext(&DC, &item);
4632 if(uErr != QCBOR_SUCCESS) {
4633 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004634 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004635 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4636 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4637 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004638 return 18;
4639 }
4640
4641 uErr = QCBORDecode_Finish(&DC);
4642 if(uErr != QCBOR_SUCCESS) {
4643 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004644 }
4645
4646 /* Now encode some stuff and then decode it */
4647 uint8_t pBuf[40];
4648 QCBOREncodeContext EC;
4649 UsefulBufC Encoded;
4650
4651 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4652 QCBOREncode_OpenArray(&EC);
4653 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4654 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4655 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4656 QCBOREncode_CloseArray(&EC);
4657 QCBOREncode_Finish(&EC, &Encoded);
4658
4659
4660 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004661 uErr = QCBORDecode_GetNext(&DC, &item);
4662 if(uErr != QCBOR_SUCCESS) {
4663 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004664 }
4665
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004666 uErr = QCBORDecode_GetNext(&DC, &item);
4667 if(uErr != QCBOR_SUCCESS) {
4668 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004669 }
4670
4671 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4672 item.val.expAndMantissa.nExponent != 1000 ||
4673 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004674 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004675 }
4676
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004677 uErr = QCBORDecode_GetNext(&DC, &item);
4678 if(uErr != QCBOR_SUCCESS) {
4679 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004680 }
4681
4682 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4683 item.val.expAndMantissa.nExponent != INT32_MIN ||
4684 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004685 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004686 }
4687
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004688 uErr = QCBORDecode_GetNext(&DC, &item);
4689 if(uErr != QCBOR_SUCCESS) {
4690 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004691 }
4692
4693 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4694 item.val.expAndMantissa.nExponent != INT32_MAX ||
4695 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004696 return 106;
4697 }
4698
4699
4700 int64_t nExp, nMant;
4701 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4702 UsefulBufC Mant;
4703 bool bIsNeg;
4704
4705 QCBORDecode_Init(&DC,
4706 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4707 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004708 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004709
4710 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004711 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004712
4713 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004714 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4715 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004716
4717 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004718 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4719 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004720
4721 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004722 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004723
4724 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004725 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4726 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004727
4728 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004729 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4730 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004731
4732 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004733 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4734 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004735
4736 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004737 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4738 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004739
4740 QCBORDecode_ExitArray(&DC);
4741
4742 uErr = QCBORDecode_Finish(&DC);
4743 if(uErr != QCBOR_SUCCESS) {
4744 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004745 }
4746
4747 return 0;
4748}
4749
4750
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004751static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004752 // Exponent > INT64_MAX
4753 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4754 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4755 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4756 // Mantissa > INT64_MAX
4757 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4758 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4759 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4760 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004761 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004762 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004763 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004764 // bad content for big num
4765 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4766 // bad content for big num
4767 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4768 // Bad integer for exponent
4769 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4770 // Bad integer for mantissa
4771 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4772 // 3 items in array
4773 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004774#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004775 // unterminated indefinite length array
4776 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004777#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4778 // unterminated indefinite length array
4779 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4780#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004781 // Empty array
4782 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4783 // Second is not an integer
4784 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4785 // First is not an integer
4786 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4787 // Not an array
4788 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4789};
4790
4791
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004792int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004793{
4794 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004795 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4796 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004797}
4798
4799#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004800
4801
4802
4803/*
4804 Some basic CBOR with map and array used in a lot of tests.
4805 The map labels are all strings
4806
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004807 {
4808 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004809 "an array of two strings": [
4810 "string1", "string2"
4811 ],
4812 "map in a map": {
4813 "bytes 1": h'78787878',
4814 "bytes 2": h'79797979',
4815 "another int": 98,
4816 "text 2": "lies, damn lies and statistics"
4817 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004818 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004819 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004820
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004821int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004822{
4823 QCBORItem Item1, Item2, Item3;
4824 int64_t nDecodedInt1, nDecodedInt2;
4825 UsefulBufC B1, B2, S1, S2, S3;
4826
4827 QCBORDecodeContext DCtx;
4828 QCBORError nCBORError;
4829
4830 QCBORDecode_Init(&DCtx, input, 0);
4831
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004832 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004833
4834 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4835
4836 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4837 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004838 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4839 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4840 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004841 QCBORDecode_ExitMap(&DCtx);
4842
4843 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4844 QCBORDecode_GetNext(&DCtx, &Item1);
4845 QCBORDecode_GetNext(&DCtx, &Item2);
4846 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4847 return -400;
4848 }
4849 QCBORDecode_ExitArray(&DCtx);
4850
4851 // Parse the same array again using GetText() instead of GetItem()
4852 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004853 QCBORDecode_GetTextString(&DCtx, &S2);
4854 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004855 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4856 return 5000;
4857 }
4858 /* QCBORDecode_GetText(&DCtx, &S3);
4859 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4860 return 5001;
4861 } */
4862
4863 QCBORDecode_ExitArray(&DCtx);
4864
4865 QCBORDecode_ExitMap(&DCtx);
4866
4867 nCBORError = QCBORDecode_Finish(&DCtx);
4868
4869 if(nCBORError) {
4870 return (int32_t)nCBORError;
4871 }
4872
4873 if(nDecodedInt1 != 42) {
4874 return 1001;
4875 }
4876
4877 if(nDecodedInt2 != 98) {
4878 return 1002;
4879 }
4880
4881 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004882 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004883 return 1003;
4884 }
4885
4886 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004887 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004888 return 1004;
4889 }
4890
Laurence Lundblade9b334962020-08-27 10:55:53 -07004891 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004892 return 1005;
4893 }
4894
4895 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4896 return 1006;
4897 }
4898
4899 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4900 return 1007;
4901 }
4902
4903 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4904 return 1008;
4905 }
4906
4907 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4908 return 1009;
4909 }
4910
4911 return 0;
4912}
4913
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004914/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004915 {
4916 -75008: h'05083399',
4917 88: [],
4918 100100: {
4919 "sub1": {
4920 10: [
4921 0
4922 ],
4923 -75009: h'A46823990001',
4924 100100: {
4925 "json": "{ \"ueid\", \"xyz\"}",
4926 "subsub": {
4927 100002: h'141813191001'
4928 }
4929 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004930 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004931 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004932 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004933 */
4934
4935static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004936 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
4937 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
4938 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
4939 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
4940 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
4941 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
4942 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
4943 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
4944 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
4945 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
4946 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
4947 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004948};
4949
4950/* Get item in multi-level nesting in spNestedCBOR */
4951static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4952{
4953 UsefulBufC String;
4954
4955 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4956 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4957
4958 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4959 QCBORDecode_EnterMap(pDCtx, NULL);
4960 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4961 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4962 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4963 if(QCBORDecode_GetError(pDCtx)) {
4964 return 4001;
4965 }
4966 if(UsefulBuf_Compare(String, test_oemid)) {
4967 return 4002;
4968 }
4969 QCBORDecode_ExitMap(pDCtx);
4970 QCBORDecode_ExitMap(pDCtx);
4971 QCBORDecode_ExitMap(pDCtx);
4972 QCBORDecode_ExitMap(pDCtx);
4973
4974 return 0;
4975}
4976
4977/* Iterations on the zero-length array in spNestedCBOR */
4978static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4979{
4980 QCBORItem Item;
4981 QCBORError uErr;
4982
4983 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4984 for(int x = 0; x < 20; x++) {
4985 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4986 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4987 return 4100;
4988
4989 }
4990 }
4991 QCBORDecode_ExitArray(pDCtx);
4992 if(QCBORDecode_GetError(pDCtx)) {
4993 return 4101;
4994 }
4995
4996 return 0;
4997}
4998
4999/* Various iterations on the array that contains a zero in spNestedCBOR */
5000static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5001{
5002 QCBORError uErr;
5003
5004 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5005 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5006 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5007 int64_t nInt = 99;
5008 QCBORDecode_GetInt64(pDCtx, &nInt);
5009 if(nInt != 0) {
5010 return 4200;
5011 }
5012 for(int x = 0; x < 20; x++) {
5013 QCBORItem Item;
5014 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5015 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5016 return 4201;
5017
5018 }
5019 }
5020 QCBORDecode_ExitArray(pDCtx);
5021 if(QCBORDecode_GetAndResetError(pDCtx)) {
5022 return 4202;
5023 }
5024 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5025 UsefulBufC dD;
5026 QCBORDecode_GetByteString(pDCtx, &dD);
5027 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5028 return 4203;
5029 }
5030 for(int x = 0; x < 20; x++) {
5031 QCBORDecode_GetByteString(pDCtx, &dD);
5032 uErr = QCBORDecode_GetAndResetError(pDCtx);
5033 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5034 return 4204;
5035 }
5036 }
5037 QCBORDecode_ExitArray(pDCtx);
5038 QCBORDecode_ExitMap(pDCtx);
5039 QCBORDecode_ExitMap(pDCtx);
5040
5041 return 0;
5042}
5043
5044/* Repeatedly enter and exit maps and arrays, go off the end of maps
5045 and arrays and such. */
5046static int32_t DecodeNestedIterate()
5047{
5048 QCBORDecodeContext DCtx;
5049 int32_t nReturn;
5050 QCBORError uErr;
5051
5052 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5053 QCBORDecode_EnterMap(&DCtx, NULL);
5054
5055 for(int j = 0; j < 5; j++) {
5056 for(int i = 0; i < 20; i++) {
5057 nReturn = DecodeNestedGetSubSub(&DCtx);
5058 if(nReturn) {
5059 return nReturn;
5060 }
5061 }
5062
5063 for(int i = 0; i < 20; i++) {
5064 nReturn = DecodeNestedGetEmpty(&DCtx);
5065 if(nReturn ) {
5066 return nReturn;
5067 }
5068 }
5069
5070 for(int i = 0; i < 20; i++) {
5071 nReturn = DecodeNestedGetZero(&DCtx);
5072 if(nReturn ) {
5073 return nReturn;
5074 }
5075 }
5076 }
5077
5078 QCBORDecode_ExitMap(&DCtx);
5079 uErr = QCBORDecode_Finish(&DCtx);
5080 if(uErr) {
5081 return (int32_t)uErr + 4100;
5082 }
5083
5084 return 0;
5085}
5086
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005087
5088/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005089 [
5090 23,
5091 6000,
5092 h'67616C6163746963',
5093 h'686176656E20746F6B656E'
5094 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005095 */
5096static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005097 0x84,
5098 0x17,
5099 0x19, 0x17, 0x70,
5100 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5101 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005102
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005103/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005104static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5105
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005106/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005107static const uint8_t spEmptyMap[] = {0xa0};
5108
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005109#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005110/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005111static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005112
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005113
Laurence Lundbladef0499502020-08-01 11:55:57 -07005114/*
5115 {
5116 0: [],
5117 9: [
5118 [],
5119 []
5120 ],
5121 8: {
5122 1: [],
5123 2: {},
5124 3: []
5125 },
5126 4: {},
5127 5: [],
5128 6: [
5129 [],
5130 []
5131 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005132 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005133 */
5134static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005135 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5136 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5137 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5138 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005139
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005140#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5141
5142
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005143/*
5144 Too many tags
5145 Invalid tag content
5146 Duplicate label
5147 Integer overflow
5148 Date overflow
5149
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005150 {
5151 1: 224(225(226(227(4(0))))),
5152 2: 1(h''),
5153 3: -18446744073709551616,
5154 4: 1(1.0e+300),
5155 5: 0, 8: 8
5156 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005157 */
5158static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005159 0xa7,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005160 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5161 0x02, 0xc1, 0x40,
5162 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5163 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5164 0x05, 0x00,
5165 0x05, 0x00,
5166 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005167};
5168
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005169/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005170static const uint8_t spUnRecoverableMapError1[] = {
5171 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5172};
5173
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005174#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005175/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005176static const uint8_t spUnRecoverableMapError2[] = {
5177 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5178};
5179
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005180/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005181static const uint8_t spUnRecoverableMapError3[] = {
5182 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5183};
5184
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005185/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005186static const uint8_t spUnRecoverableMapError4[] = {
5187 0xbf,
5188 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5189 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5191 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5192 0xff
5193};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005194#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005195
5196
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005197int32_t EnterMapTest()
5198{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005199 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005200 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005201 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005202 int32_t nReturn;
5203 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005204
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005205#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005206 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005207 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005208
Laurence Lundbladef0499502020-08-01 11:55:57 -07005209
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005210 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005211 QCBORDecode_ExitArray(&DCtx);
5212
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005213 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5214 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005215 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005216 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005217 QCBORDecode_ExitArray(&DCtx);
5218 QCBORDecode_ExitArray(&DCtx);
5219
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005220 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5221 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005222 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005223 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005224 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005225 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005226 QCBORDecode_ExitArray(&DCtx);
5227 QCBORDecode_ExitMap(&DCtx);
5228
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005229 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005230 QCBORDecode_ExitMap(&DCtx);
5231
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005232 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005233 QCBORDecode_ExitArray(&DCtx);
5234
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005235 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5236 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005237 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005238 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005239 QCBORDecode_ExitArray(&DCtx);
5240 QCBORDecode_ExitArray(&DCtx);
5241
5242 QCBORDecode_ExitMap(&DCtx);
5243
5244 uErr = QCBORDecode_Finish(&DCtx);
5245 if(uErr != QCBOR_SUCCESS){
5246 return 3011;
5247 }
5248
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005249 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005250 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005251 if(nReturn) {
5252 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005253 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005254#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5255
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005256
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005257 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005258 if(nReturn) {
5259 return nReturn;
5260 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005261
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005262
Laurence Lundblade937ea812020-05-08 11:38:23 -07005263
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005264 // These tests confirm the cursor is at the right place after entering
5265 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005266 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005267
5268 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005269 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005270 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005271 QCBORDecode_GetNext(&DCtx, &Item1);
5272 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5273 return 2001;
5274 }
5275
5276
Laurence Lundblade9b334962020-08-27 10:55:53 -07005277 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005278 QCBORDecode_VGetNext(&DCtx, &Item1);
5279 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005280 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5281 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5282 UsefulBuf_Compare(ArrayItem.label.string,
5283 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5284 return 2051;
5285 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005286 QCBORDecode_GetNext(&DCtx, &Item1);
5287 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5288 return 2002;
5289 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005290 QCBORDecode_ExitArray(&DCtx);
5291 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5292 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5293 UsefulBuf_Compare(ArrayItem.label.string,
5294 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5295 return 2052;
5296 }
5297
Laurence Lundblade937ea812020-05-08 11:38:23 -07005298
Laurence Lundblade9b334962020-08-27 10:55:53 -07005299 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005300 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005301 QCBORDecode_GetNext(&DCtx, &Item1);
5302 QCBORDecode_GetNext(&DCtx, &Item1);
5303 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005304 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5305 QCBORDecode_GetNext(&DCtx, &Item1);
5306 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5307 return 2003;
5308 }
5309
Laurence Lundblade9b334962020-08-27 10:55:53 -07005310 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005311 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005312 QCBORDecode_GetNext(&DCtx, &Item1);
5313 QCBORDecode_GetNext(&DCtx, &Item1);
5314 QCBORDecode_GetNext(&DCtx, &Item1);
5315 QCBORDecode_GetNext(&DCtx, &Item1);
5316 QCBORDecode_GetNext(&DCtx, &Item1);
5317 QCBORDecode_GetNext(&DCtx, &Item1);
5318 QCBORDecode_GetNext(&DCtx, &Item1);
5319 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5320 QCBORDecode_GetNext(&DCtx, &Item1);
5321 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005322 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005323 }
5324
Laurence Lundblade9b334962020-08-27 10:55:53 -07005325 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005326 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005327 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5328 QCBORDecode_ExitArray(&DCtx);
5329 QCBORDecode_GetNext(&DCtx, &Item1);
5330 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5331 return 2006;
5332 }
5333 QCBORDecode_ExitMap(&DCtx);
5334 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5335 return 2007;
5336 }
5337
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005338 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005339 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005340 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005341 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5342 uErr = QCBORDecode_GetAndResetError(&DCtx);
5343 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005344 return 2008;
5345 }
5346 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005347 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005348 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005349 return 2009;
5350 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005351
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005352
5353 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005354 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005355 // This will fail because the map is empty.
5356 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5357 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005358 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005359 return 2010;
5360 }
5361 QCBORDecode_ExitMap(&DCtx);
5362 uErr = QCBORDecode_Finish(&DCtx);
5363 if(uErr != QCBOR_SUCCESS){
5364 return 2011;
5365 }
5366
5367
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005368#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005369 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005370 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005371 // This will fail because the map is empty.
5372 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5373 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005374 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005375 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005376 }
5377 QCBORDecode_ExitMap(&DCtx);
5378 uErr = QCBORDecode_Finish(&DCtx);
5379 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005380 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005381 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005382#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005383
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005384
5385 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005386 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005387 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005388 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005389 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005390 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005391 QCBORDecode_ExitArray(&DCtx);
5392 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5393 QCBORDecode_ExitArray(&DCtx);
5394 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005395 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005396 return 2014;
5397 }
5398
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005399 int64_t nInt;
5400 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005401 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005402 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005403 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005404 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5405 return 2021;
5406 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005407 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5408 return 2121;
5409 }
5410 (void)QCBORDecode_GetAndResetError(&DCtx);
5411
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005412
5413 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5414 uErr = QCBORDecode_GetAndResetError(&DCtx);
5415 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5416 return 2022;
5417 }
5418
5419 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5420 uErr = QCBORDecode_GetAndResetError(&DCtx);
5421 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5422 return 2023;
5423 }
5424
5425 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5426 uErr = QCBORDecode_GetAndResetError(&DCtx);
5427#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5428 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5429 return 2024;
5430 }
5431#else
5432 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5433 return 2027;
5434 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005435#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005436
5437 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5438 uErr = QCBORDecode_GetAndResetError(&DCtx);
5439 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5440 return 2025;
5441 }
5442
5443 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5444
5445 QCBORDecode_ExitMap(&DCtx);
5446 uErr = QCBORDecode_Finish(&DCtx);
5447 if(uErr != QCBOR_SUCCESS) {
5448 return 2026;
5449 }
5450
5451 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005452 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005453 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5454 uErr = QCBORDecode_GetAndResetError(&DCtx);
5455 if(uErr != QCBOR_ERR_BAD_BREAK) {
5456 return 2030;
5457 }
5458
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005459#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005460 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005461 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005462 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5463 uErr = QCBORDecode_GetAndResetError(&DCtx);
5464 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5465 return 2031;
5466 }
5467
5468 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005469 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005470 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5471 uErr = QCBORDecode_GetAndResetError(&DCtx);
5472 if(uErr != QCBOR_ERR_HIT_END) {
5473 return 2032;
5474 }
5475
5476 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005477 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005478 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5479 uErr = QCBORDecode_GetAndResetError(&DCtx);
5480 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5481 return 2033;
5482 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005483#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5484
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005485
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005486 nReturn = DecodeNestedIterate();
5487
5488 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005489}
5490
5491
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005492struct NumberConversion {
5493 char *szDescription;
5494 UsefulBufC CBOR;
5495 int64_t nConvertedToInt64;
5496 QCBORError uErrorInt64;
5497 uint64_t uConvertToUInt64;
5498 QCBORError uErrorUint64;
5499 double dConvertToDouble;
5500 QCBORError uErrorDouble;
5501};
5502
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005503static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005504 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005505 "too large to fit into int64_t",
5506 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5507 0,
5508 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5509 0,
5510 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5511 ((double)INT64_MIN) + 1 ,
5512 QCBOR_SUCCESS
5513 },
5514 {
5515 "largest negative int that fits in int64_t",
5516 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5517 INT64_MIN,
5518 QCBOR_SUCCESS,
5519 0,
5520 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5521 (double)INT64_MIN,
5522 QCBOR_SUCCESS
5523 },
5524 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005525 "negative bignum -1",
5526 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5527 -1,
5528 QCBOR_SUCCESS,
5529 0,
5530 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5531 -1.0,
5532 QCBOR_SUCCESS
5533 },
5534 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005535 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005536 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5537 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005538#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005539 257000,
5540 QCBOR_SUCCESS,
5541 257000,
5542 QCBOR_SUCCESS,
5543 257000.0,
5544 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005545#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5546 0,
5547 QCBOR_ERR_UNEXPECTED_TYPE,
5548 0,
5549 QCBOR_ERR_UNEXPECTED_TYPE,
5550 0.0,
5551 QCBOR_ERR_UNEXPECTED_TYPE
5552#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005553 },
5554 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005555 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005556 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5557 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005558#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005559 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005560 QCBOR_SUCCESS,
5561 0,
5562 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005563 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005564 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005565#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5566 0,
5567 QCBOR_ERR_UNEXPECTED_TYPE,
5568 0,
5569 QCBOR_ERR_UNEXPECTED_TYPE,
5570 0.0,
5571 QCBOR_ERR_UNEXPECTED_TYPE
5572#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005573 },
5574 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005575 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005576 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5577 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005578#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005579 2056,
5580 QCBOR_SUCCESS,
5581 2056,
5582 QCBOR_SUCCESS,
5583 2056.0,
5584 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005585#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5586 0,
5587 QCBOR_ERR_UNEXPECTED_TYPE,
5588 0,
5589 QCBOR_ERR_UNEXPECTED_TYPE,
5590 0.0,
5591 QCBOR_ERR_UNEXPECTED_TYPE
5592#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005593 },
5594 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005595 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005596 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5597 0,
5598 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5599 0,
5600 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5601 -18446744073709551617.0,
5602 QCBOR_SUCCESS
5603 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005604#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005605 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005606 "Positive bignum 0x01020304 indefinite length string",
5607 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5608 0x01020304,
5609 QCBOR_SUCCESS,
5610 0x01020304,
5611 QCBOR_SUCCESS,
5612 16909060.0,
5613 QCBOR_SUCCESS
5614 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005615#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005616 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005617 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005618 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5619 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005620#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005621 0,
5622 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5623 0,
5624 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5625 -INFINITY,
5626 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005627#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5628 0,
5629 QCBOR_ERR_UNEXPECTED_TYPE,
5630 0,
5631 QCBOR_ERR_UNEXPECTED_TYPE,
5632 0.0,
5633 QCBOR_ERR_UNEXPECTED_TYPE
5634#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005635 },
5636 {
5637 "big float [9223372036854775806, 9223372036854775806]",
5638 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5639 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005640#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005641 0,
5642 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5643 0,
5644 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5645 INFINITY,
5646 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005647#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5648 0,
5649 QCBOR_ERR_UNEXPECTED_TYPE,
5650 0,
5651 QCBOR_ERR_UNEXPECTED_TYPE,
5652 0.0,
5653 QCBOR_ERR_UNEXPECTED_TYPE
5654#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005655 },
5656 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005657 "Big float 3 * 2^^2",
5658 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005659#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005660 12,
5661 QCBOR_SUCCESS,
5662 12,
5663 QCBOR_SUCCESS,
5664 12.0,
5665 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005666#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5667 0,
5668 QCBOR_ERR_UNEXPECTED_TYPE,
5669 0,
5670 QCBOR_ERR_UNEXPECTED_TYPE,
5671 0.0,
5672 QCBOR_ERR_UNEXPECTED_TYPE
5673#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005674 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005675 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005676 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005677 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5678 0,
5679 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5680 18446744073709551615ULL,
5681 QCBOR_SUCCESS,
5682 18446744073709551615.0,
5683 QCBOR_SUCCESS
5684 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005685 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005686 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005687 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5688 65536-1,
5689 QCBOR_SUCCESS,
5690 0xffff,
5691 QCBOR_SUCCESS,
5692 65535.0,
5693 QCBOR_SUCCESS
5694 },
5695 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005696 "Postive integer 0",
5697 {(uint8_t[]){0x0}, 1},
5698 0LL,
5699 QCBOR_SUCCESS,
5700 0ULL,
5701 QCBOR_SUCCESS,
5702 0.0,
5703 QCBOR_SUCCESS
5704 },
5705 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005706 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005707 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5708 -9223372036854775807-1, // INT64_MIN
5709 QCBOR_SUCCESS,
5710 0ULL,
5711 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5712 -9223372036854775808.0,
5713 QCBOR_SUCCESS
5714 },
5715 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005716 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005717 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005718#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005719 100L,
5720 QCBOR_SUCCESS,
5721 100ULL,
5722 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005723#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5724 0,
5725 QCBOR_ERR_HW_FLOAT_DISABLED,
5726 0,
5727 QCBOR_ERR_HW_FLOAT_DISABLED,
5728#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005729 100.3,
5730 QCBOR_SUCCESS
5731 },
5732 {
5733 "Floating point value NaN 0xfa7fc00000",
5734 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005735#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005736 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005737 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005738 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005739 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005740#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5741 0,
5742 QCBOR_ERR_HW_FLOAT_DISABLED,
5743 0,
5744 QCBOR_ERR_HW_FLOAT_DISABLED,
5745#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005746 NAN,
5747 QCBOR_SUCCESS
5748 },
5749 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005750 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005751 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005752#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5753#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5754 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005755 -4,
5756 QCBOR_SUCCESS,
5757 0,
5758 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5759 -4.0,
5760 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005761#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5762 // Float HW disabled
5763 -4,
5764 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5765 0,
5766 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5767 -4.0,
5768 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5769#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005770#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005771 // Half-precision disabled
5772 -4,
5773 QCBOR_ERR_HALF_PRECISION_DISABLED,
5774 0,
5775 QCBOR_ERR_HALF_PRECISION_DISABLED,
5776 -4.0,
5777 QCBOR_ERR_HALF_PRECISION_DISABLED
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005778#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005779 },
5780 {
5781 "Decimal fraction 3/10",
5782 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005783#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005784 0,
5785 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5786 0,
5787 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5788 0.30000000000000004,
5789 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005790#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5791 0,
5792 QCBOR_ERR_UNEXPECTED_TYPE,
5793 0,
5794 QCBOR_ERR_UNEXPECTED_TYPE,
5795 0.0,
5796 QCBOR_ERR_UNEXPECTED_TYPE
5797#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005798 },
5799 {
5800 "+inifinity",
5801 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5802#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5803 0,
5804 QCBOR_ERR_FLOAT_EXCEPTION,
5805 0,
5806 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5807#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5808 0,
5809 QCBOR_ERR_HW_FLOAT_DISABLED,
5810 0,
5811 QCBOR_ERR_HW_FLOAT_DISABLED,
5812#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5813 INFINITY,
5814 QCBOR_SUCCESS
5815 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005816
5817 {
5818 "extreme pos bignum",
5819 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5820 // 50 rows of 8 is 400 digits.
5821 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5822 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5823 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5824 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5825 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5826 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5827 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5828 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5829 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5830 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5831 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5832 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5833 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5834 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5835 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5836 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5837 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5838 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5839 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5840 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5841 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5842 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5843 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5844 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5845 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5846 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5847 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5848 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5849 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5850 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5851 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5852 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5853 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5854 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5855 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5856 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5857 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5858 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5859 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5860 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5861 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5862 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5863 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5864 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5865 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5866 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5867 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5868 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5869 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5870 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5871 404},
5872 0,
5873 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5874 0,
5875 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5876#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5877 INFINITY,
5878 QCBOR_SUCCESS
5879#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5880 0,
5881 QCBOR_ERR_HW_FLOAT_DISABLED,
5882#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5883 },
5884
5885 {
5886 "extreme neg bignum",
5887 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5888 // 50 rows of 8 is 400 digits.
5889 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5890 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5891 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5892 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5893 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5894 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5895 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5896 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5897 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5898 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5899 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5900 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5901 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5902 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5903 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5904 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5905 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5906 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5907 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5908 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5909 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5910 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5911 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5912 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5913 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5914 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5915 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5916 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5917 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5918 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5919 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5920 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5921 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5922 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5923 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5924 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5925 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5926 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5927 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5928 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5929 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5930 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5931 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5932 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5933 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5934 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5935 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5936 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5937 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5938 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5939 404},
5940 0,
5941 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5942 0,
5943 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5944#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5945 -INFINITY,
5946 QCBOR_SUCCESS
5947#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5948 0,
5949 QCBOR_ERR_HW_FLOAT_DISABLED,
5950#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5951 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005952
5953 {
5954 "big float underflow [9223372036854775806, -9223372036854775806]",
5955 {(uint8_t[]){
5956 0xC5, 0x82,
5957 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5958 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5959#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5960 0,
5961 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5962 0,
5963 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5964 0,
5965 QCBOR_SUCCESS
5966#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5967 0,
5968 QCBOR_ERR_UNEXPECTED_TYPE,
5969 0,
5970 QCBOR_ERR_UNEXPECTED_TYPE,
5971 0.0,
5972 QCBOR_ERR_UNEXPECTED_TYPE
5973#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5974 },
5975
5976 {
5977 "bigfloat that evaluates to -INFINITY",
5978 {(uint8_t[]){
5979 0xC5, 0x82,
5980 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5981 0xC3, 0x42, 0x01, 0x01}, 15},
5982#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5983 0,
5984 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5985 0,
5986 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5987 -INFINITY,
5988 QCBOR_SUCCESS
5989#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5990 0,
5991 QCBOR_ERR_UNEXPECTED_TYPE,
5992 0,
5993 QCBOR_ERR_UNEXPECTED_TYPE,
5994 0.0,
5995 QCBOR_ERR_UNEXPECTED_TYPE
5996#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5997 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005998};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005999
6000
6001
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006002
6003static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6004{
6005 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6006#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6007 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6008 return 1;
6009 }
6010#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6011 (void)Pool;
6012#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6013 return 0;
6014}
6015
6016
Laurence Lundblade313b2862020-05-16 01:23:06 -07006017int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006018{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006019 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6020 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006021
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006022 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6023 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006024
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006025 // Set up the decoding context including a memory pool so that
6026 // indefinite length items can be checked
6027 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006028 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006029
6030 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006031 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6032 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006033 }
6034
6035 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006036 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006037 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006038 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006039 }
6040 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006041 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006042 }
6043
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006044 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006045 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6046 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006047 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006048
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006049 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006050 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006051 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006052 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006053 }
6054 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006055 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006056 }
6057
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006058 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006059 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6060 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006061 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006062#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006063 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006064 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006065 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006066 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006067 }
6068 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6069 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006070 // NaN's can't be compared for equality. A NaN is
6071 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006072 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006073 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006074 }
6075 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006076 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006077 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006078 }
6079 }
6080 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006081#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006082 }
6083
6084 return 0;
6085}
6086
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006087
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006088
6089
Laurence Lundbladee3553422020-05-02 11:11:17 -07006090int32_t CBORSequenceDecodeTests(void)
6091{
6092 QCBORDecodeContext DCtx;
6093 QCBORItem Item;
6094 QCBORError uCBORError;
6095
6096 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006097
Laurence Lundbladee3553422020-05-02 11:11:17 -07006098 // The input for the date test happens to be a sequence so it
6099 // is reused. It is a sequence because it doesn't start as
6100 // an array or map.
6101 QCBORDecode_Init(&DCtx,
6102 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
6103 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006104
Laurence Lundbladee3553422020-05-02 11:11:17 -07006105 // Get the first item
6106 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6107 if(uCBORError != QCBOR_SUCCESS) {
6108 return 1;
6109 }
6110 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
6111 return 2;
6112 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006113
Laurence Lundbladee3553422020-05-02 11:11:17 -07006114 // Get a second item
6115 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07006116 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
6117 return 66;
6118 }
6119
6120 // Get a third item
6121 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006122 if(uCBORError != QCBOR_SUCCESS) {
6123 return 2;
6124 }
6125 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6126 return 3;
6127 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006128
Laurence Lundbladee3553422020-05-02 11:11:17 -07006129 // A sequence can have stuff at the end that may
6130 // or may not be valid CBOR. The protocol decoder knows
6131 // when to stop by definition of the protocol, not
6132 // when the top-level map or array is ended.
6133 // Finish still has to be called to know that
6134 // maps and arrays (if there were any) were closed
6135 // off correctly. When called like this it
6136 // must return the error QCBOR_ERR_EXTRA_BYTES.
6137 uCBORError = QCBORDecode_Finish(&DCtx);
6138 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6139 return 4;
6140 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006141
6142
Laurence Lundbladee3553422020-05-02 11:11:17 -07006143 // --- Test an empty input ----
6144 uint8_t empty[1];
6145 UsefulBufC Empty = {empty, 0};
6146 QCBORDecode_Init(&DCtx,
6147 Empty,
6148 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006149
Laurence Lundbladee3553422020-05-02 11:11:17 -07006150 uCBORError = QCBORDecode_Finish(&DCtx);
6151 if(uCBORError != QCBOR_SUCCESS) {
6152 return 5;
6153 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006154
6155
Laurence Lundbladee3553422020-05-02 11:11:17 -07006156 // --- Sequence with unclosed indefinite length array ---
6157 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006158
Laurence Lundbladee3553422020-05-02 11:11:17 -07006159 QCBORDecode_Init(&DCtx,
6160 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6161 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006162
Laurence Lundbladee3553422020-05-02 11:11:17 -07006163 // Get the first item
6164 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6165 if(uCBORError != QCBOR_SUCCESS) {
6166 return 7;
6167 }
6168 if(Item.uDataType != QCBOR_TYPE_INT64) {
6169 return 8;
6170 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006171
Laurence Lundbladee3553422020-05-02 11:11:17 -07006172 // Get a second item
6173 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006174#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006175 if(uCBORError != QCBOR_SUCCESS) {
6176 return 9;
6177 }
6178 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6179 return 10;
6180 }
6181
6182 // Try to finish before consuming all bytes to confirm
6183 // that the still-open error is returned.
6184 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006185 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006186 return 11;
6187 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006188#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6189 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6190 return 20;
6191 }
6192#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006193
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006194
Laurence Lundbladee3553422020-05-02 11:11:17 -07006195 // --- Sequence with a closed indefinite length array ---
6196 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006197
Laurence Lundbladee3553422020-05-02 11:11:17 -07006198 QCBORDecode_Init(&DCtx,
6199 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6200 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006201
Laurence Lundbladee3553422020-05-02 11:11:17 -07006202 // Get the first item
6203 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6204 if(uCBORError != QCBOR_SUCCESS) {
6205 return 12;
6206 }
6207 if(Item.uDataType != QCBOR_TYPE_INT64) {
6208 return 13;
6209 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006210
Laurence Lundbladee3553422020-05-02 11:11:17 -07006211 // Get a second item
6212 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006213#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6214
Laurence Lundbladee3553422020-05-02 11:11:17 -07006215 if(uCBORError != QCBOR_SUCCESS) {
6216 return 14;
6217 }
6218 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6219 return 15;
6220 }
6221
6222 // Try to finish before consuming all bytes to confirm
6223 // that the still-open error is returned.
6224 uCBORError = QCBORDecode_Finish(&DCtx);
6225 if(uCBORError != QCBOR_SUCCESS) {
6226 return 16;
6227 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006228#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6229 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6230 return 20;
6231 }
6232#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006233
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006234
Laurence Lundbladee3553422020-05-02 11:11:17 -07006235 return 0;
6236}
6237
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006238
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006239
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006240int32_t IntToTests()
6241{
6242 int nErrCode;
6243 int32_t n32;
6244 int16_t n16;
6245 int8_t n8;
6246 uint32_t u32;
6247 uint16_t u16;
6248 uint8_t u8;
6249 uint64_t u64;
6250
6251 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6252 if(nErrCode == -1 || n32 != 1) {
6253 return 1;
6254 }
6255
6256 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6257 if(nErrCode == -1 || n32 != INT32_MAX) {
6258 return 2;
6259 }
6260
6261 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6262 if(nErrCode == -1 || n32 != INT32_MIN) {
6263 return 3;
6264 }
6265
6266 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6267 if(nErrCode != -1) {
6268 return 4;
6269 }
6270
6271 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6272 if(nErrCode != -1) {
6273 return 5;
6274 }
6275
6276
6277 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6278 if(nErrCode == -1 || n16 != INT16_MAX) {
6279 return 6;
6280 }
6281
6282 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6283 if(nErrCode == -1 || n16 != INT16_MIN) {
6284 return 7;
6285 }
6286
6287 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6288 if(nErrCode == -1 || n16 != 1) {
6289 return 8;
6290 }
6291
6292 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6293 if(nErrCode != -1) {
6294 return 9;
6295 }
6296
6297 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6298 if(nErrCode != -1) {
6299 return 10;
6300 }
6301
6302
6303 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6304 if(nErrCode == -1 || n8 != 1) {
6305 return 11;
6306 }
6307
6308 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6309 if(nErrCode == -1 || n8 != INT8_MAX) {
6310 return 12;
6311 }
6312
6313 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6314 if(nErrCode == -1 || n8 != INT8_MIN) {
6315 return 13;
6316 }
6317
6318 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6319 if(nErrCode != -1) {
6320 return 14;
6321 }
6322
6323 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6324 if(nErrCode != -1) {
6325 return 15;
6326 }
6327
6328
6329 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6330 if(nErrCode == -1 || u32 != 1) {
6331 return 16;
6332 }
6333
6334 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6335 if(nErrCode == -1 || u32 != UINT32_MAX) {
6336 return 17;
6337 }
6338
6339 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6340 if(nErrCode == -1 || u32 != 0) {
6341 return 18;
6342 }
6343
6344 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6345 if(nErrCode != -1) {
6346 return 19;
6347 }
6348
6349 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6350 if(nErrCode != -1) {
6351 return 20;
6352 }
6353
6354
6355 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6356 if(nErrCode == -1 || u16 != UINT16_MAX) {
6357 return 21;
6358 }
6359
6360 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6361 if(nErrCode == -1 || u16 != 0) {
6362 return 22;
6363 }
6364
6365 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6366 if(nErrCode == -1 || u16 != 1) {
6367 return 23;
6368 }
6369
6370 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6371 if(nErrCode != -1) {
6372 return 24;
6373 }
6374
6375 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6376 if(nErrCode != -1) {
6377 return 25;
6378 }
6379
6380
6381 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6382 if(nErrCode == -1 || u8 != UINT8_MAX) {
6383 return 26;
6384 }
6385
6386 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6387 if(nErrCode == -1 || u8 != 0) {
6388 return 27;
6389 }
6390
6391 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6392 if(nErrCode == -1 || u8 != 1) {
6393 return 28;
6394 }
6395
6396 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6397 if(nErrCode != -1) {
6398 return 29;
6399 }
6400
6401 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6402 if(nErrCode != -1) {
6403 return 30;
6404 }
6405
6406
6407 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6408 if(nErrCode == -1 || u64 != 1) {
6409 return 31;
6410 }
6411
6412 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6413 if(nErrCode == -1 || u64 != INT64_MAX) {
6414 return 32;
6415 }
6416
6417 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6418 if(nErrCode == -1 || u64 != 0) {
6419 return 33;
6420 }
6421
6422 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6423 if(nErrCode != -1) {
6424 return 34;
6425 }
6426
6427 return 0;
6428}
6429
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006430
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006431
6432
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006433/*
6434A sequence with
6435 A wrapping bstr
6436 containing a map
6437 1
6438 2
6439 A wrapping bstr
6440 containing an array
6441 3
6442 wrapping bstr
6443 4
6444 5
6445 6
6446 array
6447 7
6448 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006449 */
6450
Laurence Lundblade55013642020-09-23 05:39:22 -07006451static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006452{
Laurence Lundblade55013642020-09-23 05:39:22 -07006453 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006454 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006455 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006456
Laurence Lundblade55013642020-09-23 05:39:22 -07006457 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006458
6459 QCBOREncode_BstrWrap(&EC);
6460 QCBOREncode_OpenMap(&EC);
6461 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6462 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6463 QCBOREncode_CloseMap(&EC);
6464 QCBOREncode_BstrWrap(&EC);
6465 QCBOREncode_OpenArray(&EC);
6466 QCBOREncode_AddInt64(&EC, 3);
6467 QCBOREncode_BstrWrap(&EC);
6468 QCBOREncode_AddInt64(&EC, 4);
6469 QCBOREncode_CloseBstrWrap(&EC, NULL);
6470 QCBOREncode_AddInt64(&EC, 5);
6471 QCBOREncode_CloseArray(&EC);
6472 QCBOREncode_CloseBstrWrap(&EC, NULL);
6473 QCBOREncode_AddInt64(&EC, 6);
6474 QCBOREncode_CloseBstrWrap(&EC, NULL);
6475 QCBOREncode_OpenArray(&EC);
6476 QCBOREncode_AddInt64(&EC, 7);
6477 QCBOREncode_AddInt64(&EC, 8);
6478 QCBOREncode_CloseArray(&EC);
6479
6480 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006481 if(uErr) {
6482 Encoded = NULLUsefulBufC;
6483 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006484
6485 return Encoded;
6486}
6487
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006488/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006489static const uint8_t spBreakInByteString[] = {
6490 0x41, 0xff
6491};
6492
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006493
6494int32_t EnterBstrTest()
6495{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006496 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006497
6498 QCBORDecodeContext DC;
6499
Laurence Lundblade55013642020-09-23 05:39:22 -07006500 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006501
Laurence Lundblade55013642020-09-23 05:39:22 -07006502 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006503
6504
Laurence Lundblade9b334962020-08-27 10:55:53 -07006505 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006506 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006507 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6508 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006509 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006510 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006511 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006512 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006513 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006514 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006515 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006516 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006517 QCBORDecode_ExitArray(&DC);
6518 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006519 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006520 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006521 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006522 QCBORDecode_GetInt64(&DC, &n7);
6523 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006524 QCBORDecode_ExitArray(&DC);
6525
6526 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006527 if(uErr) {
6528 return (int32_t)uErr;
6529 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006530
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006531
6532 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6533 * Successful because no items are fetched from byte string.
6534 */
6535 QCBORDecode_Init(&DC,
6536 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6537 0);
6538 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6539 uErr = QCBORDecode_GetError(&DC);
6540 if(uErr) {
6541 return 100 + (int32_t)uErr;
6542 }
6543
6544 QCBORDecode_ExitBstrWrapped(&DC);
6545 uErr = QCBORDecode_GetError(&DC);
6546 if(uErr) {
6547 return 200 + (int32_t)uErr;
6548 }
6549
6550 /* Try to get item that is a break out of a byte string wrapped CBOR.
6551 * It fails because there should be no break.
6552 */
6553 QCBORDecode_Init(&DC,
6554 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6555 0);
6556 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6557 QCBORItem Item;
6558 uErr = QCBORDecode_GetNext(&DC, &Item);
6559 if(uErr != QCBOR_ERR_BAD_BREAK) {
6560 return 300 + (int32_t)uErr;
6561 }
6562
6563 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006564}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006565
6566
6567
6568
6569static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006570 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006571
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006572 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006573 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006574 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6575 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6576 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006577
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006578 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006579 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6580 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6581 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006582
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006583 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006584 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006585 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6586 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006587
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006588 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006589 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6590 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006591
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006592 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006593 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006594 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6595 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006596
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006597 21,
6598 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6599 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6600
6601 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006602 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006603 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6604 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006605
6606 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006607 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6608 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006609
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006610 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006611 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006612 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6613 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006614
6615 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006616 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6617 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006618
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006619 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006620 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006621 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6622 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006623
6624 0x18, 0x33,
6625 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6626
6627 // MIME
6628 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006629 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6630 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6631 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006632
6633 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006634 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6635 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006636
6637 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006638 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6639 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6640 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006641
6642 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006643 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6644 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006645
6646 // UUID
6647 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006648 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6649 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006650
6651 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006652 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6653 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006654};
6655
6656int32_t DecodeTaggedTypeTests()
6657{
6658 QCBORDecodeContext DC;
6659 QCBORError uErr;
6660
6661 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6662
6663 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006664 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006665
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006666 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006667 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006668 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006669 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6670 return 1;
6671 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006672 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006673 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6674 return 2;
6675 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006676 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006677 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6678 return 3;
6679 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006680 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006681 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006682 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6683 return 4;
6684 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006685 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006686 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006687 return 5;
6688 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006689
Laurence Lundblade9b334962020-08-27 10:55:53 -07006690 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006691 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6692 bNeg != false) {
6693 return 10;
6694 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006695 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006696 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6697 bNeg != true) {
6698 return 11;
6699 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006700 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006701 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6702 return 12;
6703 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006704 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006705 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006706 return 13;
6707 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006708 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006709 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006710 return 14;
6711 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006712
Laurence Lundblade9b334962020-08-27 10:55:53 -07006713 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006714 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6715 return 20;
6716 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006717 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006718 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6719 return 21;
6720 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006721 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006722 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006723 return 22;
6724 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006725 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006726 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006727 return 23;
6728 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006729
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006730#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006731 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006732 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6733 return 30;
6734 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006735#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006736 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006737 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6738 return 31;
6739 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006740 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006741 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006742 return 32;
6743 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006744 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006745 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006746 return 33;
6747 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006748
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006749#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006750 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006751 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6752 return 40;
6753 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006754#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006755 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006756 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6757 return 41;
6758 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006759 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006760 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006761 return 42;
6762 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006763 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006764 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006765 return 43;
6766 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006767
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006768#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006769 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006770 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6771 return 50;
6772 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006773#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006774 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006775 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6776 return 51;
6777 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006778 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006779 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006780 return 52;
6781 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006782 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006783 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006784 return 53;
6785 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006786
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006787#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006788 // MIME
6789 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006790 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006791 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6792 bIsNot7Bit == true) {
6793 return 60;
6794 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006795 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006796 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6797 bIsNot7Bit == true) {
6798 return 61;
6799 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006800 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006801 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6802 bIsNot7Bit == false) {
6803 return 62;
6804 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006805 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006806 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6807 bIsNot7Bit == false) {
6808 return 63;
6809 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006810 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006811 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006812 return 64;
6813 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006814 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006815 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006816 return 65;
6817 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006818
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006819
Laurence Lundblade9b334962020-08-27 10:55:53 -07006820 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006821 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6822 return 70;
6823 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006824#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
6825
Laurence Lundblade9b334962020-08-27 10:55:53 -07006826 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006827 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6828 return 71;
6829 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006830 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006831 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006832 return 72;
6833 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006834 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006835 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006836 return 73;
6837 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006838
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006839 // Improvement: add some more error test cases
6840
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006841 QCBORDecode_ExitMap(&DC);
6842
6843 uErr = QCBORDecode_Finish(&DC);
6844 if(uErr != QCBOR_SUCCESS) {
6845 return 100;
6846 }
6847
6848 return 0;
6849}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006850
6851
6852
6853
6854/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006855 [
6856 "aaaaaaaaaa",
6857 {}
6858 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006859 */
6860static const uint8_t spTooLarge1[] = {
6861 0x9f,
6862 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6863 0xa0,
6864 0xff
6865};
6866
6867/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006868 [
6869 {
6870 0: "aaaaaaaaaa"
6871 }
6872 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006873 */
6874static const uint8_t spTooLarge2[] = {
6875 0x9f,
6876 0xa1,
6877 0x00,
6878 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6879 0xff
6880};
6881
6882/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006883 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006884
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006885 {
6886 0: "aaaaaaaaaa"
6887 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006888 */
6889static const uint8_t spTooLarge3[] = {
6890 0x4d,
6891 0xa1,
6892 0x00,
6893 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6894};
6895
6896int32_t TooLargeInputTest(void)
6897{
6898 QCBORDecodeContext DC;
6899 QCBORError uErr;
6900 UsefulBufC String;
6901
6902 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6903 // to 10 There's not really any way to test this error
6904 // condition. The error condition is not complex, so setting
6905 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6906
6907 // The input CBOR is only too large because the
6908 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6909 //
6910 // This test is disabled for the normal test runs because of the
6911 // special build requirement.
6912
6913
6914 // Tests the start of a map being too large
6915 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006916 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006917 QCBORDecode_GetTextString(&DC, &String);
6918 uErr = QCBORDecode_GetError(&DC);
6919 if(uErr != QCBOR_SUCCESS) {
6920 return 1;
6921 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006922 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006923 uErr = QCBORDecode_GetError(&DC);
6924 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6925 return 2;
6926 }
6927
6928 // Tests the end of a map being too large
6929 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006930 QCBORDecode_EnterArray(&DC, NULL);
6931 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006932 uErr = QCBORDecode_GetError(&DC);
6933 if(uErr != QCBOR_SUCCESS) {
6934 return 3;
6935 }
6936 QCBORDecode_ExitMap(&DC);
6937 uErr = QCBORDecode_GetError(&DC);
6938 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6939 return 4;
6940 }
6941
6942 // Tests the entire input CBOR being too large when processing bstr wrapping
6943 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6944 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6945 uErr = QCBORDecode_GetError(&DC);
6946 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6947 return 5;
6948 }
6949
6950 return 0;
6951}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006952
6953
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006954#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6955
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006956static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006957 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006958 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6959 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6960 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6961 0x03,
6962 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6963 0xc3,
6964 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006965};
6966
6967int32_t SpiffyIndefiniteLengthStringsTests()
6968{
6969 QCBORDecodeContext DCtx;
6970
6971 QCBORDecode_Init(&DCtx,
6972 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6973 QCBOR_DECODE_MODE_NORMAL);
6974
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006975 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006976 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6977
6978 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006979 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006980 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6981 if(QCBORDecode_GetAndResetError(&DCtx)) {
6982 return 1;
6983 }
6984
6985 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6986 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6987 return 2;
6988 }
6989
6990 uint64_t uInt;
6991 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6992 if(QCBORDecode_GetAndResetError(&DCtx)) {
6993 return 3;
6994 }
6995 if(uInt != 3) {
6996 return 4;
6997 }
6998
6999 double uDouble;
7000 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7001 "label2",
7002 0xff,
7003 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007004#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007005 if(QCBORDecode_GetAndResetError(&DCtx)) {
7006 return 5;
7007 }
7008 if(uDouble != -16777474) {
7009 return 6;
7010 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007011#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007012 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7013 return 7;
7014 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007015#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007016
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007017
7018 QCBORDecode_ExitMap(&DCtx);
7019
7020 if(QCBORDecode_Finish(&DCtx)) {
7021 return 99;
7022 }
7023
7024 return 0;
7025}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007026#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007027
7028
7029
7030int32_t PeekTest()
7031{
7032 QCBORItem Item;
7033 QCBORError nCBORError;
7034 QCBORDecodeContext DCtx;
7035
7036 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7037
7038 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7039 return 100+(int32_t)nCBORError;
7040 }
7041 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7042 return 200;
7043 }
7044
7045 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7046 return (int32_t)nCBORError;
7047 }
7048 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7049 return 300;
7050 }
7051
7052 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7053 return 400 + (int32_t)nCBORError;
7054 }
7055 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7056 return 500;
7057 }
7058
7059 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7060 return (int32_t)nCBORError;
7061 }
7062 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7063 return 600;
7064 }
7065
7066 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7067 return 900 + (int32_t)nCBORError;
7068 }
7069 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7070 Item.uDataType != QCBOR_TYPE_INT64 ||
7071 Item.val.int64 != 42 ||
7072 Item.uDataAlloc ||
7073 Item.uLabelAlloc ||
7074 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7075 return 1000;
7076 }
7077
7078 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7079 return 1100 + (int32_t)nCBORError;
7080 }
7081
7082 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7083 Item.uDataType != QCBOR_TYPE_INT64 ||
7084 Item.val.int64 != 42 ||
7085 Item.uDataAlloc ||
7086 Item.uLabelAlloc ||
7087 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7088 return 1200;
7089 }
7090
7091
7092 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7093 return 1300 + (int32_t)nCBORError;
7094 }
7095 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7096 Item.uDataAlloc ||
7097 Item.uLabelAlloc ||
7098 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7099 Item.uDataType != QCBOR_TYPE_ARRAY ||
7100 Item.val.uCount != 2)
7101 return 1400;
7102
7103 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7104 return 1500 + (int32_t)nCBORError;
7105 }
7106 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7107 Item.uDataAlloc ||
7108 Item.uLabelAlloc ||
7109 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7110 return 1600;
7111 }
7112
7113 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7114 return 1700 + (int32_t)nCBORError;
7115 }
7116 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7117 Item.uDataAlloc ||
7118 Item.uLabelAlloc ||
7119 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7120 return 1800;
7121 }
7122
7123 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7124 return (int32_t)nCBORError;
7125 }
7126 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7127 Item.uDataAlloc ||
7128 Item.uLabelAlloc ||
7129 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7130 return 1900;
7131 }
7132
7133 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7134 return (int32_t)nCBORError;
7135 }
7136 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7137 Item.uDataAlloc ||
7138 Item.uLabelAlloc ||
7139 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7140 return 2000;
7141 }
7142
7143
7144 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7145 return 2100 + (int32_t)nCBORError;
7146 }
7147 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7148 Item.uDataAlloc ||
7149 Item.uLabelAlloc ||
7150 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7151 Item.uDataType != QCBOR_TYPE_MAP ||
7152 Item.val.uCount != 4) {
7153 return 2100;
7154 }
7155
7156 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7157 return 2200 + (int32_t)nCBORError;
7158 }
7159 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7160 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7161 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7162 Item.uDataAlloc ||
7163 Item.uLabelAlloc ||
7164 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7165 return 2300;
7166 }
7167
7168 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7169 return 2400 + (int32_t)nCBORError;
7170 }
7171 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7172 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7173 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7174 Item.uDataAlloc ||
7175 Item.uLabelAlloc ||
7176 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7177 return 2500;
7178 }
7179
7180 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7181 return 2600 + (int32_t)nCBORError;
7182 }
7183 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7184 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7185 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7186 Item.uDataAlloc ||
7187 Item.uLabelAlloc ||
7188 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7189 return 2700;
7190 }
7191
7192 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7193 return 2800 + (int32_t)nCBORError;
7194 }
7195 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7196 Item.uDataAlloc ||
7197 Item.uLabelAlloc ||
7198 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7199 Item.uDataType != QCBOR_TYPE_INT64 ||
7200 Item.val.int64 != 98)
7201 return 2900;
7202
7203 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7204 return 3000 + (int32_t)nCBORError;
7205 }
7206 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7207 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7208 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7209 Item.uDataAlloc ||
7210 Item.uLabelAlloc ||
7211 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7212 return 3100;
7213 }
7214
7215 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7216 return 3200 + (int32_t)nCBORError;
7217 }
7218 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7219 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7220 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7221 Item.uDataAlloc ||
7222 Item.uLabelAlloc ||
7223 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7224 return 3300;
7225 }
7226
7227 return 0;
7228}