blob: 88203ff2d6edd73f819fdce4ac8989141bfbb677 [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 Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, 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 Lundbladecc7da412020-12-27 00:09:07 -0800921/* [[[[[[[[[[]]]]]]]]]] */
Michael Richardson87de9af2021-02-18 23:13:31 -0500922static uint8_t sEmptyMap[] = {
923 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;
933
934 QCBORDecode_Init(&DCtx,
935 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap),
936 QCBOR_DECODE_MODE_NORMAL);
937
938 /* now open the first Map */
939 nReturn = QCBORDecode_GetNext(&DCtx, &Item);
940 if(nReturn != QCBOR_SUCCESS ||
941 Item.uDataType != QCBOR_TYPE_MAP) {
942 nReturn = -3;
943 goto done;
944 }
945
946 if(QCBORDecode_GetNext(&DCtx, &Item) != 0) {
947 nReturn = -1;
948 goto done;
949 }
950 if(Item.uDataType != QCBOR_TYPE_MAP ||
951 Item.uNestingLevel != 1 ||
952 Item.label.int64 != 2) {
953 nReturn = -2;
954 goto done;
955 }
956
957 done:
958 return(nReturn);
959}
960
961/* [[[[[[[[[[]]]]]]]]]] */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800962static const uint8_t spDeepArrays[] = {
963 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
964 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800965
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800966int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800967{
968 QCBORDecodeContext DCtx;
969 int nReturn = 0;
970 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800971
Laurence Lundbladeee851742020-01-08 08:37:05 -0800972 QCBORDecode_Init(&DCtx,
973 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
974 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800975
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800976 for(i = 0; i < 10; i++) {
977 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800978
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800979 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
980 Item.uDataType != QCBOR_TYPE_ARRAY ||
981 Item.uNestingLevel != i) {
982 nReturn = -1;
983 break;
984 }
985 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800986
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800987 return(nReturn);
988}
989
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800990/* Big enough to test nesting to the depth of 24
991 [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]
992 */
993static const uint8_t spTooDeepArrays[] = {
994 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
995 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
996 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
997 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800998
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800999int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001000{
1001 QCBORDecodeContext DCtx;
1002 int nReturn = 0;
1003 int i;
1004 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001005
1006
Laurence Lundbladeee851742020-01-08 08:37:05 -08001007 QCBORDecode_Init(&DCtx,
1008 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
1009 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001010
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001011 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001012
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001013 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
1014 Item.uDataType != QCBOR_TYPE_ARRAY ||
1015 Item.uNestingLevel != i) {
1016 nReturn = -1;
1017 break;
1018 }
1019 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001020
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001021 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001022 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001023
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001024 return(nReturn);
1025}
1026
1027
1028
1029
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001030int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001031{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001032 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001033
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001034 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001035 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001036
Laurence Lundbladeee851742020-01-08 08:37:05 -08001037 QCBORDecode_Init(&DCtx,
1038 (UsefulBufC){spExpectedEncodedInts, nNum},
1039 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001040
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001041 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001042
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001043 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044 nResult = -1;
1045 goto Done;
1046 }
1047 }
1048Done:
1049 return nResult;
1050}
1051
1052
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001053
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001054int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001055{
1056 uint8_t *pEncoded;
1057 int nReturn;
1058 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001059
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001060 int64_t i1, i2;
1061 size_t i3, i4;
1062 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001063
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001064 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001065
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001066 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
1067 return(-1);
1068 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001069
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07001071 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
1072 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001073 if(nResult == 0) {
1074 nReturn = -1;
1075 }
1076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001077
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001078 return(nReturn);
1079}
1080
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301081/*
1082 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001083 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1084 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301085 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001086static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001087{
1088 QCBORDecodeContext DCtx;
1089 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001090 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001091
Laurence Lundbladeee851742020-01-08 08:37:05 -08001092 QCBORDecode_Init(&DCtx,
1093 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1094 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001095
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001096 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001097 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001099 if(Item.uDataType != QCBOR_TYPE_MAP ||
1100 Item.val.uCount != 3)
1101 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001102
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001103 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001104 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001105 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001106
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001107 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001108 Item.uDataType != QCBOR_TYPE_INT64 ||
1109 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301110 Item.uDataAlloc ||
1111 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001112 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001113 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001115
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001116 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001117 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001119 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301120 Item.uDataAlloc ||
1121 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001122 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001123 Item.uDataType != QCBOR_TYPE_ARRAY ||
1124 Item.val.uCount != 2)
1125 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001126
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001127 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001128 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001129 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001130 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301131 Item.uDataAlloc ||
1132 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001133 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001134 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001135 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001136
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001137 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001138 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001139 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001140 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301141 Item.uDataAlloc ||
1142 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001143 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001144 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001145 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001146
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001147 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001148 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001149 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001150 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301151 Item.uDataAlloc ||
1152 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001153 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001154 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001156 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001158
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001159 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001160 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001162 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001164 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301165 Item.uDataAlloc ||
1166 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001167 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001168 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001169 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001170
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001171 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001172 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001173 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001174 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001175 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001176 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301177 Item.uDataAlloc ||
1178 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001179 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001180 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001182
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001184 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001185 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001186 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301187 Item.uDataAlloc ||
1188 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001189 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001190 Item.uDataType != QCBOR_TYPE_INT64 ||
1191 Item.val.int64 != 98)
1192 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001193
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001194 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001195 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001196 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001197 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001198 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001199 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301200 Item.uDataAlloc ||
1201 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001202 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001203 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206 return 0;
1207}
1208
1209
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001210/*
1211 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001212 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001213 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001214int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001215{
1216 QCBORDecodeContext DCtx;
1217 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001218 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001219
Laurence Lundbladeee851742020-01-08 08:37:05 -08001220 QCBORDecode_Init(&DCtx,
1221 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1222 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001223
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001224 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001225 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001227 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1228 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001229 return -1;
1230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001231
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001232 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001233 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001234 }
1235 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1236 Item.uDataAlloc ||
1237 Item.uLabelAlloc ||
1238 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001239 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001240 return -2;
1241 }
1242
1243 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001244 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001245 }
1246 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1247 Item.uDataType != QCBOR_TYPE_INT64 ||
1248 Item.val.int64 != 42 ||
1249 Item.uDataAlloc ||
1250 Item.uLabelAlloc) {
1251 return -3;
1252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001253
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001254 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001255 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001256 }
1257 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1258 Item.uDataAlloc ||
1259 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001260 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001261 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1262 return -4;
1263 }
1264
1265 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001266 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001267 }
1268 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1269 Item.uDataAlloc ||
1270 Item.uLabelAlloc ||
1271 Item.uDataType != QCBOR_TYPE_ARRAY ||
1272 Item.val.uCount != 2) {
1273 return -5;
1274 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001275
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001277 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001278 }
1279 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1280 Item.val.string.len != 7 ||
1281 Item.uDataAlloc ||
1282 Item.uLabelAlloc ||
1283 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1284 return -6;
1285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001286
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001287 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001288 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001289 }
1290 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1291 Item.uDataAlloc ||
1292 Item.uLabelAlloc ||
1293 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1294 return -7;
1295 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001296
1297
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001298 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001299 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001300 }
1301 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1302 Item.uDataAlloc ||
1303 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001304 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001305 return -8;
1306 }
1307
1308 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001309 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001310 }
1311 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1312 Item.uDataAlloc ||
1313 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001314 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1315 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001316 return -9;
1317 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001318
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001319 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001320 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001321 }
1322 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001323 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001324 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1325 Item.uDataAlloc ||
1326 Item.uLabelAlloc) {
1327 return -10;
1328 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001329
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001331 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001332 }
1333 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1334 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1335 Item.uDataAlloc ||
1336 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001337 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001338 return -11;
1339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001340
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001341 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001342 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001343 }
1344 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001345 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001346 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1347 Item.uDataAlloc ||
1348 Item.uLabelAlloc) {
1349 return -12;
1350 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001351
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001352 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001353 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001354 }
1355 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1356 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1357 Item.uDataAlloc ||
1358 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001359 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001360 return -13;
1361 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001362
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001363 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001364 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 }
1366 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1367 Item.uDataAlloc ||
1368 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001369 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001370 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1371 return -14;
1372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001373
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001374 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001375 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001376 }
1377 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1378 Item.uDataAlloc ||
1379 Item.uLabelAlloc ||
1380 Item.uDataType != QCBOR_TYPE_INT64 ||
1381 Item.val.int64 != 98) {
1382 return -15;
1383 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001384
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001385 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001386 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001387 }
1388 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001389 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001390 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1391 Item.uDataAlloc ||
1392 Item.uLabelAlloc) {
1393 return -16;
1394 }
1395
1396 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001397 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001398 }
1399 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1400 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1401 Item.uDataAlloc ||
1402 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001403 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 return -17;
1405 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001406
1407
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001408 /*
1409 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1410 map that when interpreted as an array will be too many. Test
1411 data just has the start of the map, not all the items in the map.
1412 */
1413 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001414
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001415 QCBORDecode_Init(&DCtx,
1416 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1417 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001418
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001419 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001420 return -50;
1421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001422
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001423 // TODO: test decoding of labels that are arrays or such
1424 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1425
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001426 return 0;
1427}
1428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429
1430/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301431 Fully or partially decode pValidMapEncoded. When
1432 partially decoding check for the right error code.
1433 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001434
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301435 The partial decodes test error conditions of
1436 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001437
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301438 This could be combined with the above test
1439 and made prettier and maybe a little more
1440 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001441 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001442static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001443{
1444 QCBORDecodeContext DCtx;
1445 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001446 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001447
Laurence Lundbladeee851742020-01-08 08:37:05 -08001448 QCBORDecode_Init(&DCtx,
1449 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1450 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001451
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001452 if(nLevel < 1) {
1453 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1454 return -1;
1455 } else {
1456 return 0;
1457 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001458 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301459
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001460
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001461 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001462 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001463 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001464 if(Item.uDataType != QCBOR_TYPE_MAP ||
1465 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001466 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001468 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001469 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1470 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001471 } else {
1472 return 0;
1473 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001474 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001475
1476
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001477 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001478 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001479 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001480 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001481 Item.uDataType != QCBOR_TYPE_INT64 ||
1482 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001483 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001484 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001485 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001486
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001487 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001488 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1489 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001490 } else {
1491 return 0;
1492 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001493 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001494
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001495 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001496 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001497 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001499 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001500 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001501 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001502 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001504
1505
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001506 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001507 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1508 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001509 } else {
1510 return 0;
1511 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001512 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001513
1514
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001515 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001516 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001517 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001518 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001519 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001520 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001521 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001522
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001523 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001524 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1525 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001526 } else {
1527 return 0;
1528 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001529 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001530
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001531 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001532 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001533 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001534 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001535 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001536 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001537 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001538
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001539 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001540 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1541 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001542 } else {
1543 return 0;
1544 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001545 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001546
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001547 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001548 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001549 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001550 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001551 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001552 Item.uDataType != QCBOR_TYPE_MAP ||
1553 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001554 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001555
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001556 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001557 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1558 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001559 } else {
1560 return 0;
1561 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001562 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001563
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001564 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001565 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001566 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001567 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001568 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001570 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001571 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001572 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001573
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001574 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001575 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1576 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001577 } else {
1578 return 0;
1579 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001580 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001581
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001582 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001583 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001584 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001586 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001588 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001589 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001591
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001592 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001593 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1594 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001595 } else {
1596 return 0;
1597 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001598 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001599
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001600 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001601 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001602 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001603 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001604 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001605 Item.uDataType != QCBOR_TYPE_INT64 ||
1606 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001607 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001608
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001609 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001610 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1611 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001612 } else {
1613 return 0;
1614 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001615 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001616
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001617 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001618 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001619 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001620 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001621 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001622 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001623 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001624 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001625 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001626
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301627 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001628 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001629 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001630
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001631 return 0;
1632}
1633
1634
1635
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001636
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001637int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001638{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001639 // Parse a moderatly complex map structure very thoroughly
1640 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1641 if(nResult) {
1642 return nResult;
1643 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001644
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001645 // Again, but in strings-only mode. It should succeed since the input
1646 // map has only string labels.
1647 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1648 if(nResult) {
1649 return nResult;
1650 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001651
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001652 // Again, but try to finish the decoding before the end of the
1653 // input at 10 different place and see that the right error code
1654 // is returned.
1655 for(int i = 0; i < 10; i++) {
1656 nResult = ExtraBytesTest(i);
1657 if(nResult) {
1658 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001659 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001660 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001661
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001662 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001663}
1664
1665
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001666/* The simple-values including some not well formed */
1667static const uint8_t spSimpleValues[] = {
1668 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
1669 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
1670 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001671
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001672int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001673{
1674 QCBORDecodeContext DCtx;
1675 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001676 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001677
1678
Laurence Lundbladeee851742020-01-08 08:37:05 -08001679 QCBORDecode_Init(&DCtx,
1680 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1681 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001682
1683
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001684 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001685 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001686 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1687 Item.val.uCount != 10)
1688 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001689
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001690 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001691 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001692 if(Item.uDataType != QCBOR_TYPE_FALSE)
1693 return -1;
1694
1695 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001696 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001697 if(Item.uDataType != QCBOR_TYPE_TRUE)
1698 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001699
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001700 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001701 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001702 if(Item.uDataType != QCBOR_TYPE_NULL)
1703 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001704
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001705 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001706 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001707 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1708 return -1;
1709
1710 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001711 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001712 return -1;
1713
1714 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001715 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001716 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1717 return -1;
1718
1719 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001720 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001721 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1722 return -1;
1723
Laurence Lundblade077475f2019-04-26 09:06:33 -07001724 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001725 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001726
Laurence Lundblade077475f2019-04-26 09:06:33 -07001727 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001728 return -1;
1729
Laurence Lundblade077475f2019-04-26 09:06:33 -07001730 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001731 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001732
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001733 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001734 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001735 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1736 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001737
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001738 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001739 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001740 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1741 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001742
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001743 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001744
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001745}
1746
1747
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001748int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001749{
1750 // Loop over all the not-well-formed instance of CBOR
1751 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001752 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
1753 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001754 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1755 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1756 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1757
Laurence Lundbladeee851742020-01-08 08:37:05 -08001758 // Set up decoder context. String allocator needed for indefinite
1759 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001760 QCBORDecodeContext DCtx;
1761 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001762#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1764 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001765#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001766
1767 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001768 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001769 do {
1770 QCBORItem Item;
1771
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001772 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1773 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001774
1775 // Every test vector must fail with
1776 // a not-well-formed error. If not
1777 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001778 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001779 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001780 /* Return index of failure and QCBOR error in the result */
1781 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001782 }
1783 }
1784 return 0;
1785}
1786
1787
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001788// 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 +08001789struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001790 UsefulBufC Input;
1791 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001792};
1793
Laurence Lundblade59289e52019-12-30 13:44:37 -08001794
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001795static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001796{
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001797 for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001798 QCBORDecodeContext DCtx;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001799 QCBORError uCBORError;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001800
Laurence Lundblade59289e52019-12-30 13:44:37 -08001801 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001802
1803#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001804 // Set up the decoding context including a memory pool so that
1805 // indefinite length items can be checked
Laurence Lundblade59289e52019-12-30 13:44:37 -08001806 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001807
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001808 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1809 if(uCBORError) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001810 return -9;
1811 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001812#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1813
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001814
Laurence Lundblade59289e52019-12-30 13:44:37 -08001815 // Iterate until there is an error of some sort error
1816 QCBORItem Item;
1817 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001818 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001819 memset(&Item, 0x33, sizeof(Item));
1820
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001821 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1822 } while(uCBORError == QCBOR_SUCCESS);
1823
1824
Laurence Lundblade59289e52019-12-30 13:44:37 -08001825
1826 // Must get the expected error or the this test fails
1827 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001828 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001829 Item.uDataType != QCBOR_TYPE_NONE ||
1830 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001831 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001832 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001833 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001834 }
1835 }
1836
1837 return 0;
1838}
1839
1840
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001841static const struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001842 // Most of this is copied from not_well_formed.h. Here the error code
1843 // returned is also checked.
1844
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001845#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001846 // Indefinite length strings must be closed off
1847 // An indefinite length byte string not closed off
1848 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1849 // An indefinite length text string not closed off
1850 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1851
1852
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001853 // All the chunks in an indefinite length string must be of the type of
1854 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001855 // indefinite length byte string with text string chunk
1856 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1857 // indefinite length text string with a byte string chunk
1858 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1859 // indefinite length byte string with an positive integer chunk
1860 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1861 // indefinite length byte string with an negative integer chunk
1862 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1863 // indefinite length byte string with an array chunk
1864 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1865 // indefinite length byte string with an map chunk
1866 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1867 // indefinite length byte string with tagged integer chunk
1868 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1869 // indefinite length byte string with an simple type chunk
1870 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1871 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1872 // indefinite length text string with indefinite string inside
1873 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1874
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001875#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1876
1877 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1878 // An indefinite length text string not closed off
1879 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1880
1881
1882 // All the chunks in an indefinite length string must be of the type of
1883 // indefinite length string
1884 // indefinite length byte string with text string chunk
1885 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1886 // indefinite length text string with a byte string chunk
1887 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1888 // indefinite length byte string with an positive integer chunk
1889 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1890 // indefinite length byte string with an negative integer chunk
1891 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1892 // indefinite length byte string with an array chunk
1893 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1894 // indefinite length byte string with an map chunk
1895 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1896 // indefinite length byte string with tagged integer chunk
1897 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1898 // indefinite length byte string with an simple type chunk
1899 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1900 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1901 // indefinite length text string with indefinite string inside
1902 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1903#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1904
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001905
1906 // Definte length maps and arrays must be closed by having the right number of items
1907 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001908 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001909 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001910 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001911 // A definte length array that is supposed to have 511 items, but has only 1
1912 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1913 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001914 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001915 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001916 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001917
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001918#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001919 // Indefinte length maps and arrays must be ended by a break
1920 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001921 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001922 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001923 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001924 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001925 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001926 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001927 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001928
1929
1930 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001931 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001932 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001933 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001934 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001935 // Unclosed indefinite map containing a closed definite length array
1936 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1937 // Definite length map containing an unclosed indefinite length array
1938 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001939 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001940 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001941 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001942 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001943 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001944 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001945 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001946 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001947 // Unclosed indefinite length map in definite length maps
1948 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1949 QCBOR_ERR_NO_MORE_ITEMS},
1950 // Unclosed definite length map in indefinite length maps
1951 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1952 // Unclosed indefinite length array in definite length maps
1953 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1954 QCBOR_ERR_NO_MORE_ITEMS},
1955 // Unclosed definite length array in indefinite length maps
1956 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1957 // Unclosed indefinite length map in definite length arrays
1958 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1959 // Unclosed definite length map in indefinite length arrays
1960 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001961#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001962
1963 // The "argument" for the data item is incomplete
1964 // Positive integer missing 1 byte argument
1965 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1966 // Positive integer missing 2 byte argument
1967 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1968 // Positive integer missing 4 byte argument
1969 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1970 // Positive integer missing 8 byte argument
1971 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1972 // Positive integer missing 1 byte of 2 byte argument
1973 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1974 // Positive integer missing 2 bytes of 4 byte argument
1975 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1976 // Positive integer missing 1 bytes of 7 byte argument
1977 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1978 // Negative integer missing 1 byte argument
1979 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1980 // Binary string missing 1 byte argument
1981 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1982 // Text string missing 1 byte argument
1983 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1984 // Array missing 1 byte argument
1985 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1986 // Map missing 1 byte argument
1987 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1988 // Tag missing 1 byte argument
1989 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1990 // Simple missing 1 byte argument
1991 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001992 // half-precision with 1 byte argument
1993 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1994 // single-precision with 2 byte argument
1995 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1996 // double-precision with 3 byte argument
1997 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1998
1999
2000 // Tag with no content
2001 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002002
2003
2004 // Breaks must not occur in definite length arrays and maps
2005 // Array of length 1 with sole member replaced by a break
2006 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2007 // Array of length 2 with 2nd member replaced by a break
2008 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2009 // Map of length 1 with sole member label replaced by a break
2010 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2011 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002012 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002013 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
2014 // Array of length 1 with 2nd member value replaced by a break
2015 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2016 // Map of length 2 with 2nd member replaced by a break
2017 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
2018
2019
2020 // Breaks must not occur on their own out of an indefinite length data item
2021 // A bare break is not well formed
2022 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
2023 // A bare break after a zero length definite length array
2024 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002025#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002026 // A bare break after a zero length indefinite length map
2027 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002028 // A break inside a definite length array inside an indefenite length array
2029 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2030 // Complicated mixed nesting with break outside indefinite length array
2031 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002032#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002033
2034
2035 // Forbidden two byte encodings of simple types
2036 // Must use 0xe0 instead
2037 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2038 // Should use 0xe1 instead
2039 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2040 // Should use 0xe2 instead
2041 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2042 // Should use 0xe3 instead
2043 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2044 // Should use 0xe4 instead
2045 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2046 // Should use 0xe5 instead
2047 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2048 // Should use 0xe6 instead
2049 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2050 // Should use 0xe7 instead
2051 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2052 // Should use 0xe8 instead
2053 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2054 // Should use 0xe9 instead
2055 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2056 // Should use 0xea instead
2057 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2058 // Should use 0xeb instead
2059 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2060 // Should use 0xec instead
2061 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2062 // Should use 0xed instead
2063 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2064 // Should use 0xee instead
2065 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2066 // Should use 0xef instead
2067 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2068 // Should use 0xf0 instead
2069 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2070 // Should use 0xf1 instead
2071 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2072 // Should use 0xf2 instead
2073 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2074 // Must use 0xf3 instead
2075 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2076 // Must use 0xf4 instead
2077 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2078 // Must use 0xf5 instead
2079 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2080 // Must use 0xf6 instead
2081 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2082 // Must use 0xf7 instead
2083 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2084 // Must use 0xf8 instead
2085 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002086 // Reserved
2087 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002088
2089 // Integers with additional info indefinite length
2090 // Positive integer with additional info indefinite length
2091 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
2092 // Negative integer with additional info indefinite length
2093 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
2094 // CBOR tag with "argument" an indefinite length
2095 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
2096 // CBOR tag with "argument" an indefinite length alternate vector
2097 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
2098
2099
2100 // Missing bytes from a deterministic length string
2101 // A byte string is of length 1 without the 1 byte
2102 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
2103 // A text string is of length 1 without the 1 byte
2104 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002105
2106#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08002107 // Byte string should have 2^32-15 bytes, but has one
2108 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2109 // Byte string should have 2^32-15 bytes, but has one
2110 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002111 // Byte string should have 2^64 bytes, but has 3
2112 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2113 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2114 // Text string should have 2^64 bytes, but has 3
2115 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2116 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002117#else
2118 // Byte string should have 2^32-15 bytes, but has one
2119 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2120 // Byte string should have 2^32-15 bytes, but has one
2121 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2122 // Byte string should have 2^16 bytes, but has 3
2123 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2124 // Text string should have 2^64 bytes, but has 3
2125 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2126#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002127
2128 // Use of unassigned additional information values
2129 // Major type positive integer with reserved value 28
2130 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2131 // Major type positive integer with reserved value 29
2132 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2133 // Major type positive integer with reserved value 30
2134 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2135 // Major type negative integer with reserved value 28
2136 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2137 // Major type negative integer with reserved value 29
2138 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2139 // Major type negative integer with reserved value 30
2140 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2141 // Major type byte string with reserved value 28 length
2142 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2143 // Major type byte string with reserved value 29 length
2144 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2145 // Major type byte string with reserved value 30 length
2146 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2147 // Major type text string with reserved value 28 length
2148 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2149 // Major type text string with reserved value 29 length
2150 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2151 // Major type text string with reserved value 30 length
2152 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2153 // Major type array with reserved value 28 length
2154 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2155 // Major type array with reserved value 29 length
2156 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2157 // Major type array with reserved value 30 length
2158 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2159 // Major type map with reserved value 28 length
2160 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2161 // Major type map with reserved value 29 length
2162 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2163 // Major type map with reserved value 30 length
2164 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2165 // Major type tag with reserved value 28 length
2166 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2167 // Major type tag with reserved value 29 length
2168 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2169 // Major type tag with reserved value 30 length
2170 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2171 // Major type simple with reserved value 28 length
2172 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2173 // Major type simple with reserved value 29 length
2174 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2175 // Major type simple with reserved value 30 length
2176 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2177
2178
2179 // Maps must have an even number of data items (key & value)
2180 // Map with 1 item when it should have 2
2181 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2182 // Map with 3 item when it should have 4
2183 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002184#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002185 // Map with 1 item when it should have 2
2186 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2187 // Map with 3 item when it should have 4
2188 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002189#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002190
2191
2192 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002193 // Text-based date, with an integer
2194 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2195 // Epoch date, with an byte string
2196 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2197 // tagged as both epoch and string dates
2198 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2199 // big num tagged an int, not a byte string
2200 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002201};
2202
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002203int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002204{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002205 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002206
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002207 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002208 if(nResult) {
2209 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002210 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002211
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002212 // Corrupt the UsefulInputBuf and see that
2213 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002214 QCBORDecodeContext DCtx;
2215 QCBORItem Item;
2216 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002217
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002218 QCBORDecode_Init(&DCtx,
2219 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2220 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002221
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002222 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2223 return (int32_t)uQCBORError;
2224 }
2225 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2226 // This wasn't supposed to happen
2227 return -1;
2228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002229
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002230 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002232 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2233 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2234 // Did not get back the error expected
2235 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002236 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002237
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002238
Laurence Lundblade98427e92020-09-28 21:33:23 -07002239 /*
2240 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2241 tests here can be performed to see that the max length
2242 error check works correctly. See DecodeBytes(). If the max
2243 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002244
Laurence Lundblade98427e92020-09-28 21:33:23 -07002245 This test will automatocally adapt the all CPU sizes
2246 through the use of SIZE_MAX.
2247 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002248
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002249 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002250 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002251
Laurence Lundblade98427e92020-09-28 21:33:23 -07002252 // This makes a CBOR head with a text string that is very long
2253 // but doesn't fill in the bytes of the text string as that is
2254 // not needed to test this part of QCBOR.
2255 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2256
2257 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2258
2259 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2260 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002261 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002262
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002263 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002264}
2265
2266
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002267/* Try all 256 values of the byte at nLen including recursing for
2268 each of the values to try values at nLen+1 ... up to nLenMax
2269 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002270static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002271{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002272 if(nLen >= nLenMax) {
2273 return;
2274 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002275
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002276 for(int inputByte = 0; inputByte < 256; inputByte++) {
2277 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002278 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002279 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002280
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002281 // Get ready to parse
2282 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002283 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002284
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002285 // Parse by getting the next item until an error occurs
2286 // Just about every possible decoder error can occur here
2287 // The goal of this test is not to check for the correct
2288 // error since that is not really possible. It is to
2289 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002290 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002291 QCBORItem Item;
2292 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002293 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002294 break;
2295 }
2296 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002297
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002298 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002299 }
2300}
2301
2302
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002303int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002304{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002305 // Size 2 tests 64K inputs and runs quickly
2306 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002307
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002308 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002310 return 0;
2311}
2312
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002313
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002314int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002315{
2316 // size 3 tests 16 million inputs and runs OK
2317 // in seconds on fast machines. Size 4 takes
2318 // 10+ minutes and 5 half a day on fast
2319 // machines. This test is kept separate from
2320 // the others so as to no slow down the use
2321 // of them as a very frequent regression.
2322 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002323
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002324 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002325
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002326 return 0;
2327}
2328
2329
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002330static const uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002331 0xc0, // tag for string date
2332 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002333
Laurence Lundbladec7114722020-08-13 05:11:40 -07002334 0xc0, // tag for string date
2335 0x00, // Wrong type for a string date
2336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002337 0xc1, // tag for epoch date
2338 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2339
Laurence Lundbladec7114722020-08-13 05:11:40 -07002340 0xc1,
2341 0x62, 'h', 'i', // wrong type tagged
2342
Laurence Lundblade99615302020-11-29 11:19:47 -08002343 // CBOR_TAG_ENC_AS_B64
2344 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002345 0x1a, 0x53, 0x72, 0x4E, 0x01,
2346
2347 0xc1, // tag for epoch date
2348 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002349
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002350 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002351 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002352
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002353 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002354 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002355
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002356 0xc1, // tag for epoch date
2357 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2358 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2359
2360 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002361 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2362
2363 0xc1, // tag for epoch date
2364 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2365
2366 0xc1,
2367 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2368
2369 0xc1, // tag for epoch date
2370 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002371};
2372
2373
Laurence Lundbladec7114722020-08-13 05:11:40 -07002374
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002375// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002376#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002377static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002378
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002379 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002381 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002382
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002383 return diff > 0.0000001;
2384}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002385#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002386
2387
Laurence Lundblade99615302020-11-29 11:19:47 -08002388
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002389int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002390{
2391 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002392 QCBORItem Item;
2393 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
Laurence Lundbladeee851742020-01-08 08:37:05 -08002395 QCBORDecode_Init(&DCtx,
2396 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2397 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002399 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002400 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002401 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002402 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002403 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002404 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002405 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002406 }
2407
Laurence Lundbladec7114722020-08-13 05:11:40 -07002408 // Wrong type for a string date
2409 uError = QCBORDecode_GetNext(&DCtx, &Item);
2410 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002411 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002412 }
2413
2414 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2415 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2416 return -4;
2417 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002418 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2419 Item.val.epochDate.nSeconds != 1400000000 ||
2420 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002421 return -5;
2422 }
2423
2424 // Wrong type for an epoch date
2425 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2426 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002428
Laurence Lundblade99615302020-11-29 11:19:47 -08002429 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2430 // The date is decoded and the two tags are returned. This is to
2431 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002432 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2433 return -7;
2434 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002435 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2436 Item.val.epochDate.nSeconds != 1400000001 ||
2437 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundblade99615302020-11-29 11:19:47 -08002438 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002439 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002440 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002441
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002442 // Epoch date that is too large for our representation
2443 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002444 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002446
Laurence Lundblade9682a532020-06-06 18:33:04 -07002447#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002448 // Epoch date in float format with fractional seconds
2449 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2450 return -10;
2451 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002452 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2453 Item.val.epochDate.nSeconds != 1 ||
2454 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002455 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002456 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002457
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002458 // Epoch date float that is too large for our representation
2459 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002460 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002461 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002462
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002463 // Epoch date double that is just slightly too large
2464 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002465 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002466 }
2467
2468 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002469 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2470 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2471 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2472 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002473 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002474 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002475
2476 // Nan
2477 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2478 return -15;
2479 }
2480
2481 // +Inifinity double-precision
2482 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2483 return -16;
2484 }
2485
2486#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2487 // -Inifinity half-precision
2488 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2489 return -17;
2490 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002491#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002492 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2493 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002494 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002495#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002496
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002497#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002498 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2499 return -19;
2500 }
2501 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2502 return -20;
2503 }
2504 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2505 return -21;
2506 }
2507 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2508 return -22;
2509 }
2510 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2511 return -23;
2512 }
2513 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2514 return -24;
2515 }
2516#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2517 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2518 return -25;
2519 }
2520#else
2521 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2522 return -26;
2523 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002524#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002525
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002526#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002527
2528 return 0;
2529}
2530
Laurence Lundblade4b270642020-08-14 12:53:07 -07002531/*
2532 Test cases covered here. Some items cover more than one of these.
2533 positive integer (zero counts as a positive integer)
2534 negative integer
2535 half-precision float
2536 single-precision float
2537 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002538
Laurence Lundblade4b270642020-08-14 12:53:07 -07002539 float Overflow error
2540 Wrong type error for epoch
2541 Wrong type error for date string
2542 float disabled error
2543 half-precision disabled error
2544 -Infinity
2545 Slightly too large integer
2546 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002547
Laurence Lundblade4b270642020-08-14 12:53:07 -07002548 Get epoch by int
2549 Get string by int
2550 Get epoch by string
2551 Get string by string
2552 Fail to get epoch by wrong int label
2553 Fail to get string by wrong string label
2554 Fail to get epoch by string because it is invalid
2555 Fail to get epoch by int because it is invalid
2556
2557 Untagged values
2558 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002559static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002560 0x86,
2561
2562 0xc1,
2563 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2564
Laurence Lundbladec7114722020-08-13 05:11:40 -07002565 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002566 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2567
2568 0xc1, // tag for epoch date
2569 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2570
2571 0xc1, // tag for epoch date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002572 0x80, // Erroneous empty array as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002573
2574 0xc0, // tag for string date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002575 0xa0, // Erroneous empty map as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002576
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002577 0xa9, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002578
2579 0x00,
2580 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002581 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002582
2583 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002584 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002585 0xc1, // tag for epoch date
2586 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2587
2588 // Untagged integer 0
2589 0x08,
2590 0x00,
2591
2592 // Utagged date string with string label y
2593 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002594 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002595
2596 // Untagged -1000 with label z
2597 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002598 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002599 0x39, 0x03, 0xe7,
2600
Laurence Lundbladec7114722020-08-13 05:11:40 -07002601 0x07,
2602 0xc1, // tag for epoch date
2603 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2604
Laurence Lundblade4b270642020-08-14 12:53:07 -07002605 0x05,
2606 0xc1,
2607 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2608
Laurence Lundbladec7114722020-08-13 05:11:40 -07002609 // Untagged single-precision float with value 3.14 with string label x
2610 0x61, 0x78,
2611 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2612
Laurence Lundbladec7114722020-08-13 05:11:40 -07002613 // Untagged half-precision float with value -2
2614 0x09,
2615 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002616};
2617
2618int32_t SpiffyDateDecodeTest()
2619{
2620 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002621 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002622 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2623 nEpochDate4, nEpochDate6, nEpochDateFail,
2624 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002625 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002626 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002627
2628 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002629 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002630 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002631 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002632
Laurence Lundblade9b334962020-08-27 10:55:53 -07002633 // Too-negative float, -9.2233720368547748E+18
2634 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002635 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002636#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002637 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2638 return 1111;
2639 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002640#else
2641 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2642 return 1112;
2643 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002644#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade4b270642020-08-14 12:53:07 -07002645
2646 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002647 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002648 uError = QCBORDecode_GetAndResetError(&DC);
2649 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002650 return 1;
2651 }
2652
Laurence Lundblade4b270642020-08-14 12:53:07 -07002653 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002654 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002655 uError = QCBORDecode_GetAndResetError(&DC);
2656#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2657#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2658 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2659#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2660 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2661#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2662#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2663 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2664#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2665 if(uError != uExpectedforHalfMinusInfinity) {
2666 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002667 }
2668
Laurence Lundblade4b270642020-08-14 12:53:07 -07002669 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002670 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002671 uError = QCBORDecode_GetAndResetError(&DC);
2672 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2673 return 3;
2674 }
2675
2676 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002677 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002678 uError = QCBORDecode_GetAndResetError(&DC);
2679 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2680 return 4;
2681 }
2682
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002683 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002684
2685 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002686 QCBORDecode_GetEpochDateInMapN(&DC,
2687 5,
2688 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2689 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2690 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002691#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2692 if(nEpochDate2 != -9223372036854773760LL) {
2693 return 101;
2694 }
2695#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2696 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002697 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002698 return 102;
2699 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002700#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002701
Laurence Lundblade4b270642020-08-14 12:53:07 -07002702 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002703 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2704 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002705#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2706 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002707 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002708 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002709#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2710 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002711 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002712 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002713 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002714#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2715
2716 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002717 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2718 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002719#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2720 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002721 return 103;
2722 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002723#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2724 uError = QCBORDecode_GetAndResetError(&DC);
2725 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2726 return 104;
2727 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002728#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2729
Laurence Lundblade9b334962020-08-27 10:55:53 -07002730 // A half-precision date with value -2 FFF
2731 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2732 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002733#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2734 if(nEpochDate4 != -2) {
2735 return 105;
2736 }
2737#else
2738 uError = QCBORDecode_GetAndResetError(&DC);
2739 if(uError == QCBOR_SUCCESS) {
2740 return 106;
2741 }
2742#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002743
Laurence Lundblade4b270642020-08-14 12:53:07 -07002744
2745 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002746 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2747 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2748 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002749 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002750 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002751 return 107;
2752 }
2753
2754 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002755 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2756 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002757 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002758 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002759 return 108;
2760 }
2761
2762 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002763 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2764 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2765 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002766 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002767 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002768 return 109;
2769 }
2770
2771 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002772 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2773 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002774 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002775 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002776 return 110;
2777 }
2778
2779 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002780
Laurence Lundblade4b270642020-08-14 12:53:07 -07002781 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002782 QCBORDecode_GetEpochDateInMapN(&DC,
2783 1,
2784 QCBOR_TAG_REQUIREMENT_TAG |
2785 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2786 &nEpochDate1400000000);
2787 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002788 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002789 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2790 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002791 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002792 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2793 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002794 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002795 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2796 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002797 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002798 QCBORDecode_GetEpochDateInMapSZ(&DC,
2799 "z",
2800 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2801 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2802 &nEpochDate6);
2803 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002804
2805 QCBORDecode_ExitMap(&DC);
2806 QCBORDecode_ExitArray(&DC);
2807 uError = QCBORDecode_Finish(&DC);
2808 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002809 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002810 }
2811
Laurence Lundblade9b334962020-08-27 10:55:53 -07002812 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002813 return 200;
2814 }
2815
Laurence Lundblade9b334962020-08-27 10:55:53 -07002816 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002817 return 201;
2818 }
2819
Laurence Lundblade9b334962020-08-27 10:55:53 -07002820 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002821 return 202;
2822 }
2823
Laurence Lundblade9b334962020-08-27 10:55:53 -07002824 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002825 return 203;
2826 }
2827
Laurence Lundblade9b334962020-08-27 10:55:53 -07002828 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002829 return 204;
2830 }
2831
Laurence Lundblade9b334962020-08-27 10:55:53 -07002832 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2833 return 205;
2834 }
2835
2836 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2837 return 206;
2838 }
2839
Laurence Lundbladec7114722020-08-13 05:11:40 -07002840 return 0;
2841}
2842
2843
2844
Laurence Lundblade9b334962020-08-27 10:55:53 -07002845// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002846static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002847 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002848 0x81, // Array of one
2849 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2850 0x82, // Array of two that is the faction 1/3
2851 0x01,
2852 0x03,
2853
2854 /*
2855 More than 4 tags on an item 225(226(227(228(229([])))))
2856 */
2857 0xd8, 0xe1,
2858 0xd8, 0xe2,
2859 0xd8, 0xe3,
2860 0xd8, 0xe4,
2861 0xd8, 0xe5,
2862 0x80,
2863
2864 /* tag 10489608748473423768(
2865 2442302356(
2866 21590(
2867 240(
2868 []))))
2869 */
2870 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2871 0xda, 0x91, 0x92, 0x93, 0x94,
2872 0xd9, 0x54, 0x56,
2873 0xd8, 0xf0,
2874 0x80,
2875
2876 /* tag 21590(
2877 10489608748473423768(
2878 2442302357(
2879 65534(
2880 []))))
2881 */
2882 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2883 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2884 0xda, 0x91, 0x92, 0x93, 0x95,
2885 0xd9, 0xff, 0xfe,
2886 0x80,
2887
2888 /* Make sure to blow past the limit of tags that must be mapped.
2889 works in conjuntion with entries above.
2890 269488144(269488145(269488146(269488147([]))))
2891 */
2892 0xda, 0x10, 0x10, 0x10, 0x10,
2893 0xda, 0x10, 0x10, 0x10, 0x11,
2894 0xda, 0x10, 0x10, 0x10, 0x12,
2895 0xda, 0x10, 0x10, 0x10, 0x13,
2896 0x80,
2897
2898 /* An invalid decimal fraction with an additional tag */
2899 0xd9, 0xff, 0xfa,
2900 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2901 0x00, // the integer 0; should be a byte string
2902};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002903
Laurence Lundblade59289e52019-12-30 13:44:37 -08002904/*
2905 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002906 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002907 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002908static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002909 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002910
Laurence Lundblade59289e52019-12-30 13:44:37 -08002911/*
2912DB 9192939495969798 # tag(10489608748473423768)
2913 D8 88 # tag(136)
2914 C6 # tag(6)
2915 C7 # tag(7)
2916 80 # array(0)
2917*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002918static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002919 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002920
2921/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002922 55799(55799(55799({
2923 6(7(-23)): 5859837686836516696(7({
2924 7(-20): 11({
2925 17(-18): 17(17(17("Organization"))),
2926 9(-17): 773("SSG"),
2927 -15: 16(17(6(7("Confusion")))),
2928 17(-16): 17("San Diego"),
2929 17(-14): 17("US")
2930 }),
2931 23(-19): 19({
2932 -11: 9({
2933 -9: -7
2934 }),
2935 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2936 })
2937 })),
2938 16(-22): 23({
2939 11(8(7(-5))): 8(-3)
2940 })
2941 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002942 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002943static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002944 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2945 0xc6, 0xc7, 0x36,
2946 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2947 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2948 0xcb, 0xa5,
2949 0xd1, 0x31,
2950 0xd1, 0xd1, 0xd1, 0x6c,
2951 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2952 0xc9, 0x30,
2953 0xd9, 0x03, 0x05, 0x63,
2954 0x53, 0x53, 0x47,
2955 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002956 0xd0, 0xd1, 0xc6, 0xc7,
2957 0x69,
2958 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002959 0xd1, 0x2f,
2960 0xd1, 0x69,
2961 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2962 0xd1, 0x2d,
2963 0xd1, 0x62,
2964 0x55, 0x53,
2965 0xd7, 0x32,
2966 0xd3, 0xa2,
2967 0x2a,
2968 0xc9, 0xa1,
2969 0x28,
2970 0x26,
2971 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2972 0xcc, 0x4a,
2973 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2974 0xd0, 0x35,
2975 0xd7, 0xa1,
2976 0xcb, 0xc8, 0xc7, 0x24,
2977 0xc8, 0x22};
2978
Laurence Lundblade9b334962020-08-27 10:55:53 -07002979
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002980static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002981 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07002982
2983 0xc0, // tag for string date
2984 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2985
2986 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2987
2988 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2989
2990 0xd8, 0x23, // tag for regex
2991 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2992
2993 0xc0, // tag for string date
2994 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002995};
2996
2997
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002998static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002999
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003000
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003001int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003002{
3003 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003004 QCBORItem Item;
3005 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundbladeee851742020-01-08 08:37:05 -08003007 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003008 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003009 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003010
Laurence Lundblade9b334962020-08-27 10:55:53 -07003011 /*
3012 This test matches the magic number tag and the fraction tag
3013 55799([...])
3014 */
3015 uError = QCBORDecode_GetNext(&DCtx, &Item);
3016 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003017 return -2;
3018 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003019 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003020 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3021 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003022 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003023
Laurence Lundblade9b334962020-08-27 10:55:53 -07003024 /*
3025 4([1,3])
3026 */
3027 uError = QCBORDecode_GetNext(&DCtx, &Item);
3028#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3029 if(uError != QCBOR_SUCCESS ||
3030 Item.uDataType != QCBOR_TYPE_ARRAY ||
3031 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3032 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3033 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3034 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3035 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3036 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3037 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003038 return -4;
3039 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003040 // consume the items in the array
3041 uError = QCBORDecode_GetNext(&DCtx, &Item);
3042 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003043
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003044#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003045 if(uError != QCBOR_SUCCESS ||
3046 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3047 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3048 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3049 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3050 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3051 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3052 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003053 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003054#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003055
Laurence Lundblade9b334962020-08-27 10:55:53 -07003056 /*
3057 More than 4 tags on an item 225(226(227(228(229([])))))
3058 */
3059 uError = QCBORDecode_GetNext(&DCtx, &Item);
3060 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003061 return -6;
3062 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003063
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003064 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3065 return -106;
3066 }
3067
3068
Laurence Lundblade9b334962020-08-27 10:55:53 -07003069 /* tag 10489608748473423768(
3070 2442302356(
3071 21590(
3072 240(
3073 []))))
3074 */
3075 uError = QCBORDecode_GetNext(&DCtx, &Item);
3076 if(uError != QCBOR_SUCCESS ||
3077 Item.uDataType != QCBOR_TYPE_ARRAY ||
3078 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3079 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3080 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3081 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003082 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003083 }
3084
3085 /* tag 21590(
3086 10489608748473423768(
3087 2442302357(
3088 21591(
3089 []))))
3090 */
3091 uError = QCBORDecode_GetNext(&DCtx, &Item);
3092 if(uError != QCBOR_SUCCESS ||
3093 Item.uDataType != QCBOR_TYPE_ARRAY ||
3094 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3095 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3096 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3097 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3098 return -8;
3099 }
3100
3101 /* Make sure to blow past the limit of tags that must be mapped.
3102 works in conjuntion with entries above.
3103 269488144(269488145(269488146(269488147([]))))
3104 */
3105 uError = QCBORDecode_GetNext(&DCtx, &Item);
3106 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3107 return -9;
3108 }
3109
3110 uError = QCBORDecode_GetNext(&DCtx, &Item);
3111 if(uError == QCBOR_SUCCESS) {
3112 return -10;
3113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003114
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003115 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003116 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003117 // tage and then matches it. Caller-config lists are no longer
3118 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003119 QCBORDecode_Init(&DCtx,
3120 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3121 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003122 const uint64_t puList[] = {0x9192939495969798, 257};
3123 const QCBORTagListIn TL = {2, puList};
3124 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003125
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003126 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3127 return -8;
3128 }
3129 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3130 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3131 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3132 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3133 Item.val.uCount != 0) {
3134 return -9;
3135 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003136
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003137 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003138 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003139 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003140 const uint64_t puLongList[17] = {1,2,1};
3141 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003142 QCBORDecode_Init(&DCtx,
3143 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3144 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003145 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3146 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3147 return -11;
3148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003149
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003150 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003151 QCBORTagListOut Out = {0, 4, puTags};
3152
3153
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003154 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003155 QCBORDecode_Init(&DCtx,
3156 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3157 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003158 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3159 return -12;
3160 }
3161 if(puTags[0] != 0x9192939495969798 ||
3162 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003163 puTags[2] != 0x06 ||
3164 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003165 return -13;
3166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003167
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003168 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003169 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003170 QCBORDecode_Init(&DCtx,
3171 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3172 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003173 QCBORTagListOut OutSmall = {0, 3, puTags};
3174 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3175 return -14;
3176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003177
Laurence Lundblade9b334962020-08-27 10:55:53 -07003178
3179
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003180 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003181 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3182 // It is a bit of a messy test and maybe could be improved, but
3183 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003184 QCBORDecode_Init(&DCtx,
3185 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3186 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003187 int n = CheckCSRMaps(&DCtx);
3188 if(n) {
3189 return n-2000;
3190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003191
Laurence Lundblade59289e52019-12-30 13:44:37 -08003192 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003193 QCBORDecode_Init(&DCtx,
3194 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3195 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003196
Laurence Lundblade9b334962020-08-27 10:55:53 -07003197 /* With the spiffy decode revision, this tag list is not used.
3198 It doesn't matter if a tag is in this list or not so some
3199 tests that couldn't process a tag because it isn't in this list
3200 now can process these unlisted tags. The tests have been
3201 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003202 const uint64_t puTagList[] = {773, 1, 90599561};
3203 const QCBORTagListIn TagList = {3, puTagList};
3204 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003205
3206
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003207 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3208 return -100;
3209 }
3210 if(Item.uDataType != QCBOR_TYPE_MAP ||
3211 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3212 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3213 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3214 Item.val.uCount != 2 ||
3215 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3216 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3217 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3218 Out.uNumUsed != 3) {
3219 return -101;
3220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003221
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003222 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3223 return -102;
3224 }
3225 if(Item.uDataType != QCBOR_TYPE_MAP ||
3226 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3227 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003228 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003229 Item.val.uCount != 2 ||
3230 puTags[0] != 5859837686836516696 ||
3231 puTags[1] != 7 ||
3232 Out.uNumUsed != 2) {
3233 return -103;
3234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003235
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003236 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3237 return -104;
3238 }
3239 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003240 Item.val.uCount != 5 ||
3241 puTags[0] != 0x0b ||
3242 Out.uNumUsed != 1) {
3243 return -105;
3244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003245
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003246 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3247 return -106;
3248 }
3249 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3250 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3251 Item.val.string.len != 12 ||
3252 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3253 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3254 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3255 Out.uNumUsed != 3) {
3256 return -105;
3257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003258
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003259 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3260 return -107;
3261 }
3262 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3263 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3264 Item.val.string.len != 3 ||
3265 puTags[0] != 773 ||
3266 Out.uNumUsed != 1) {
3267 return -108;
3268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003269
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003270 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3271 return -109;
3272 }
3273 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003274 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003275 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003276 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003277 puTags[3] != 7 ||
3278 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003279 return -110;
3280 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003281
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003282 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3283 return -111;
3284 }
3285 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3286 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3287 Item.val.string.len != 9 ||
3288 puTags[0] != 17 ||
3289 Out.uNumUsed != 1) {
3290 return -112;
3291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003292
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003293 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3294 return -111;
3295 }
3296 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3297 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3298 Item.val.string.len != 2 ||
3299 puTags[0] != 17 ||
3300 Out.uNumUsed != 1) {
3301 return -112;
3302 }
3303
3304 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3305 return -113;
3306 }
3307 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003308 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003309 Item.val.uCount != 2 ||
3310 puTags[0] != 19 ||
3311 Out.uNumUsed != 1) {
3312 return -114;
3313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003314
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003315 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3316 return -115;
3317 }
3318 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003319 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003320 Item.val.uCount != 1 ||
3321 puTags[0] != 9 ||
3322 Out.uNumUsed != 1) {
3323 return -116;
3324 }
3325
3326 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3327 return -116;
3328 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003329 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003330 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003331 Out.uNumUsed != 0) {
3332 return -117;
3333 }
3334
3335 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3336 return -118;
3337 }
3338 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3339 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003340 puTags[0] != 12 ||
3341 Out.uNumUsed != 1) {
3342 return -119;
3343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003344
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003345 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3346 return -120;
3347 }
3348 if(Item.uDataType != QCBOR_TYPE_MAP ||
3349 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3350 Item.val.uCount != 1 ||
3351 puTags[0] != 0x17 ||
3352 Out.uNumUsed != 1) {
3353 return -121;
3354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003355
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003356 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3357 return -122;
3358 }
3359 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003360 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003361 Item.val.int64 != -3 ||
3362 puTags[0] != 8 ||
3363 Out.uNumUsed != 1) {
3364 return -123;
3365 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003366
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003367 if(QCBORDecode_Finish(&DCtx)) {
3368 return -124;
3369 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003370
3371 UsefulBufC DateString;
3372 QCBORDecode_Init(&DCtx,
3373 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3374 QCBOR_DECODE_MODE_NORMAL);
3375
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003376 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003377 // tagged date string
3378 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3379 // untagged date string
3380 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3381 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3382 return 100;
3383 }
3384 // untagged byte string
3385 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3386 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3387 return 101;
3388 }
3389 // tagged regex
3390 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3391 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3392 return 102;
3393 }
3394 // tagged date string with a byte string
3395 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3396 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3397 return 103;
3398 }
3399 QCBORDecode_ExitArray(&DCtx);
3400 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3401 return 104;
3402 }
3403
3404
3405 QCBORDecode_Init(&DCtx,
3406 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3407 QCBOR_DECODE_MODE_NORMAL);
3408
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003409 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003410 // tagged date string
3411 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3412 // untagged date string
3413 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3414 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3415 return 200;
3416 }
3417 // untagged byte string
3418 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3419 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3420 return 201;
3421 }
3422 // tagged regex
3423 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3424 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3425 return 202;
3426 }
3427 // tagged date string with a byte string
3428 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3429 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3430 return 203;
3431 }
3432 QCBORDecode_ExitArray(&DCtx);
3433 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3434 return 204;
3435 }
3436
3437 QCBORDecode_Init(&DCtx,
3438 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3439 QCBOR_DECODE_MODE_NORMAL);
3440
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003441 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003442 // tagged date string
3443 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3444 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3445 return 300;
3446 }
3447 // untagged date string
3448 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3449 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3450 return 301;
3451 }
3452 // untagged byte string
3453 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3454 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3455 return 302;
3456 }
3457 // tagged regex
3458 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3459 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3460 return 303;
3461 }
3462 // tagged date string with a byte string
3463 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3464 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3465 return 304;
3466 }
3467 QCBORDecode_ExitArray(&DCtx);
3468 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3469 return 305;
3470 }
3471
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003472 return 0;
3473}
3474
3475
3476
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003477
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003478static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003479 0x83,
3480 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3481 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3482 0xA4,
3483 0x63, 0x42, 0x4E, 0x2B,
3484 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3485 0x18, 0x40,
3486 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3487 0x63, 0x42, 0x4E, 0x2D,
3488 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3489 0x38, 0x3F,
3490 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3491
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003492/* The expected big num */
3493static const uint8_t spBigNum[] = {
3494 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3495 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003496
3497
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003498int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003499{
3500 QCBORDecodeContext DCtx;
3501 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003502 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003503
Laurence Lundbladeee851742020-01-08 08:37:05 -08003504 QCBORDecode_Init(&DCtx,
3505 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3506 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003507
3508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003509 //
3510 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3511 return -1;
3512 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003513 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003514 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003515
3516 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003517 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003518 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003519 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003520 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003521 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003522 }
3523
3524 //
3525 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003526 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003527 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003528 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003529 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003530 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003531
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003532 //
3533 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003534 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003535 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003536 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003537 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003538
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003539 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003540 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003541 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3542 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003543 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003544 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003545 }
3546
3547 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003548 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003549 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3550 Item.uLabelType != QCBOR_TYPE_INT64 ||
3551 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003552 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003553 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003554 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003555
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003556 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003557 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003558 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3559 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003560 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003561 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003562 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003564 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003565 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003566 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3567 Item.uLabelType != QCBOR_TYPE_INT64 ||
3568 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003569 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003570 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003571 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003573 return 0;
3574}
3575
3576
3577
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003578static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003579 uint8_t uDataType,
3580 uint8_t uNestingLevel,
3581 uint8_t uNextNest,
3582 int64_t nLabel,
3583 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003584{
3585 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003586 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003588 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3589 if(Item.uDataType != uDataType) return -1;
3590 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003591 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3592 Item.uLabelType != QCBOR_TYPE_UINT64) {
3593 return -1;
3594 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003595 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3596 if(Item.label.int64 != nLabel) return -1;
3597 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003598 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003599 }
3600 }
3601 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303602 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003603
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003604 if(pItem) {
3605 *pItem = Item;
3606 }
3607 return 0;
3608}
3609
3610
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003611// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003612static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003613{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303614 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003615
Laurence Lundblade9b334962020-08-27 10:55:53 -07003616 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003617
Laurence Lundblade9b334962020-08-27 10:55:53 -07003618 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003619
Laurence Lundblade9b334962020-08-27 10:55:53 -07003620 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3621 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3622 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3623 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3624 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003625
Laurence Lundblade9b334962020-08-27 10:55:53 -07003626 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3627 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003628
Laurence Lundblade9b334962020-08-27 10:55:53 -07003629 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3630 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003631
Laurence Lundblade9b334962020-08-27 10:55:53 -07003632 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3633 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003634
Laurence Lundblade9b334962020-08-27 10:55:53 -07003635 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003636
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003637 return 0;
3638}
3639
3640
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003641/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003642{
3643 -23: {
3644 -20: {
3645 -18: "Organization",
3646 -17: "SSG",
3647 -15: "Confusion",
3648 -16: "San Diego",
3649 -14: "US"
3650 },
3651 -19: {
3652 -11: {
3653 -9: -7
3654 },
3655 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3656 }
3657 },
3658 -22: {
3659 -5: -3
3660 }
3661}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003662*/
3663static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003664 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3665 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3666 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3667 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3668 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3669 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3670 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3671 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3672 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3673
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003674// Same map as above, but using indefinite lengths
3675static const uint8_t spCSRInputIndefLen[] = {
3676 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3677 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3678 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3679 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3680 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3681 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3682 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3683 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3684 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3685 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3686
3687
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003688int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003689{
3690 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003691
Laurence Lundbladeee851742020-01-08 08:37:05 -08003692 QCBORDecode_Init(&DCtx,
3693 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3694 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003695
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003696 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003697}
3698
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003699
3700
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003701int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003702{
3703 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003704
Laurence Lundbladeee851742020-01-08 08:37:05 -08003705 QCBORDecode_Init(&DCtx,
3706 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3707 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003708
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003709 QCBORItem Item;
3710 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003711
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003712 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3713 return -1;
3714 }
3715 if(Item.uDataType != QCBOR_TYPE_MAP) {
3716 return -2;
3717 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003718
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003719 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3720 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3721 return -3;
3722 }
3723
3724 return 0;
3725}
3726
3727
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003728
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003729int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003730{
3731 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003732
Laurence Lundbladeee851742020-01-08 08:37:05 -08003733 QCBORDecode_Init(&DCtx,
3734 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3735 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003736
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003737 return CheckCSRMaps(&DCtx);
3738}
3739
3740
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003741
Laurence Lundblade17ede402018-10-13 11:43:07 +08003742static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3743{
3744 UsefulOutBuf UOB;
3745 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003746
Laurence Lundblade17ede402018-10-13 11:43:07 +08003747 int i;
3748 for(i = 0; i < n; i++) {
3749 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3750 }
3751
3752 for(i = 0; i < n; i++) {
3753 UsefulOutBuf_AppendByte(&UOB, 0xff);
3754 }
3755 return UsefulOutBuf_OutUBuf(&UOB);
3756}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003757
3758
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003759static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003760{
3761 QCBORDecodeContext DC;
3762 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003763
Laurence Lundblade17ede402018-10-13 11:43:07 +08003764 int j;
3765 for(j = 0; j < nNestLevel; j++) {
3766 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003767 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003768 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3769 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003770 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003771 return -4;
3772 } else {
3773 return 0; // Decoding doesn't recover after an error
3774 }
3775 } else {
3776 // Should be no error
3777 if(nReturn) {
3778 return -9; // Should not have got an error
3779 }
3780 }
3781 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3782 return -7;
3783 }
3784 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003785 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003786 if(nReturn) {
3787 return -3;
3788 }
3789 return 0;
3790}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003791
3792
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003793int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003794{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303795 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003796 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003797 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003798 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003799 int nReturn = parse_indeflen_nested(Nested, i);
3800 if(nReturn) {
3801 return nReturn;
3802 }
3803 }
3804 return 0;
3805}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003806
Laurence Lundbladeee851742020-01-08 08:37:05 -08003807// [1, [2, 3]]
3808static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3809// No closing break
3810static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3811// Not enough closing breaks
3812static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3813// Too many closing breaks
3814static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3815// Unclosed indeflen inside def len
3816static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3817// confused tag
3818static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003819
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003820int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003821{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003822 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003823 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003824 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003825
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003826 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003827 QCBORDecodeContext DC;
3828 QCBORItem Item;
3829 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003830
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003831 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303832
3833 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3834 Item.uNestingLevel != 0 ||
3835 Item.uNextNestLevel != 1) {
3836 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003837 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003838
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003839 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303840 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3841 Item.uNestingLevel != 1 ||
3842 Item.uNextNestLevel != 1) {
3843 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003844 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003845
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003846 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303847 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3848 Item.uNestingLevel != 1 ||
3849 Item.uNextNestLevel != 2) {
3850 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003851 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003852
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003853 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003854 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303855 Item.uNestingLevel != 2 ||
3856 Item.uNextNestLevel != 2) {
3857 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003858 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003859
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003860 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003861 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303862 Item.uNestingLevel != 2 ||
3863 Item.uNextNestLevel != 0) {
3864 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003865 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003866
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003867 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303868 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003869 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003870
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003871 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003872 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003873
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003874 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003875
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003876 nResult = QCBORDecode_GetNext(&DC, &Item);
3877 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303878 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003879 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003880
Laurence Lundblade570fab52018-10-13 18:28:27 +08003881 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003882 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303883 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003884 }
3885
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003886
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003887 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003888 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003889
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003890 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003891
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003892 nResult = QCBORDecode_GetNext(&DC, &Item);
3893 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303894 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003895 }
3896
3897 nResult = QCBORDecode_GetNext(&DC, &Item);
3898 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303899 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003900 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003901
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003902 nResult = QCBORDecode_GetNext(&DC, &Item);
3903 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303904 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003906
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003907 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003908 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303909 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003910 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003911
3912
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003913 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003914 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003915
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003916 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003917
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003918 nResult = QCBORDecode_GetNext(&DC, &Item);
3919 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303920 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003921 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003922
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003923 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003924 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303925 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003926 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303927
Laurence Lundblade642282a2020-06-23 12:00:33 -07003928 nResult = QCBORDecode_GetNext(&DC, &Item);
3929 if(nResult != QCBOR_ERR_BAD_BREAK) {
3930 return -140;
3931 }
3932
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003933
Laurence Lundblade570fab52018-10-13 18:28:27 +08003934 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003935 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003936
Laurence Lundblade570fab52018-10-13 18:28:27 +08003937 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003938
Laurence Lundblade570fab52018-10-13 18:28:27 +08003939 nResult = QCBORDecode_GetNext(&DC, &Item);
3940 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303941 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003943
Laurence Lundblade570fab52018-10-13 18:28:27 +08003944 nResult = QCBORDecode_GetNext(&DC, &Item);
3945 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303946 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003947 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003948
Laurence Lundblade570fab52018-10-13 18:28:27 +08003949 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003950 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303951 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003952 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003953
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303954 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003955 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003956
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303957 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303959 nResult = QCBORDecode_GetNext(&DC, &Item);
3960 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303961 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003963
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303964 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303965 if(nResult != QCBOR_ERR_BAD_BREAK) {
3966 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303967 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003968
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003969 return 0;
3970}
3971
Laurence Lundblade17ede402018-10-13 11:43:07 +08003972
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08003973#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
3974
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003975static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003976 0x81, // Array of length one
3977 0x7f, // text string marked with indefinite length
3978 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3979 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3980 0xff // ending break
3981};
3982
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003983static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303984 0x81, // Array of length one
3985 0x7f, // text string marked with indefinite length
3986 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3987 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3988 0xff // ending break
3989};
3990
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003991static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303992 0x81, // Array of length one
3993 0x7f, // text string marked with indefinite length
3994 0x01, 0x02, // Not a string
3995 0xff // ending break
3996};
3997
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003998static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303999 0x81, // Array of length one
4000 0x7f, // text string marked with indefinite length
4001 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4002 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4003 // missing end of string
4004};
4005
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004006static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304007 0xa1, // Array of length one
4008 0x7f, // text string marked with indefinite length
4009 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4010 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4011 0xff, // ending break
4012 0x01 // integer being labeled.
4013};
4014
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004015/**
4016 Make an indefinite length string
4017
4018 @param Storage Storage for string, must be 144 bytes in size
4019 @return The indefinite length string
4020
4021 This makes an array with one indefinite length string that has 7 chunks
4022 from size of 1 byte up to 64 bytes.
4023 */
4024static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304025{
4026 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004027
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304028 UsefulOutBuf_Init(&UOB, Storage);
4029 UsefulOutBuf_AppendByte(&UOB, 0x81);
4030 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004031
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004032 uint8_t uStringByte = 0;
4033 // Use of type int is intentional
4034 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4035 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304036 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004037 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4038 for(int j = 0; j < uChunkSize; j++) {
4039 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4040 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304041 }
4042 }
4043 UsefulOutBuf_AppendByte(&UOB, 0xff);
4044
4045 return UsefulOutBuf_OutUBuf(&UOB);
4046}
4047
4048static int CheckBigString(UsefulBufC BigString)
4049{
4050 if(BigString.len != 255) {
4051 return 1;
4052 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004053
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304054 for(uint8_t i = 0; i < 255; i++){
4055 if(((const uint8_t *)BigString.ptr)[i] != i) {
4056 return 1;
4057 }
4058 }
4059 return 0;
4060}
4061
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304062
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004063int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304064{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304065 QCBORDecodeContext DC;
4066 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304067 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004068 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004069
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304070 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004071 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304072 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004073
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304074 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304075 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004077
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304078 if(QCBORDecode_GetNext(&DC, &Item)) {
4079 return -2;
4080 }
4081 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4082 return -3;
4083 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004084
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304085 if(QCBORDecode_GetNext(&DC, &Item)) {
4086 return -4;
4087 }
4088 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4089 return -5;
4090 }
4091 if(QCBORDecode_Finish(&DC)) {
4092 return -6;
4093 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304094
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304095 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004096 QCBORDecode_Init(&DC,
4097 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4098 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004099
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304100 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4101 return -7;
4102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004103
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304104 if(QCBORDecode_GetNext(&DC, &Item)) {
4105 return -8;
4106 }
4107 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4108 return -9;
4109 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004110
Laurence Lundblade30816f22018-11-10 13:40:22 +07004111 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304112 return -10;
4113 }
4114
4115 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004116 QCBORDecode_Init(&DC,
4117 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4118 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004119
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304120 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4121 return -11;
4122 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004123
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304124 if(QCBORDecode_GetNext(&DC, &Item)) {
4125 return -12;
4126 }
4127 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4128 return -13;
4129 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004130
Laurence Lundblade30816f22018-11-10 13:40:22 +07004131 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304132 return -14;
4133 }
4134
4135 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004136 QCBORDecode_Init(&DC,
4137 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4138 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004139
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304140 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4141 return -15;
4142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004143
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304144 if(QCBORDecode_GetNext(&DC, &Item)) {
4145 return -16;
4146 }
4147 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4148 return -17;
4149 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004150
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304151 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4152 return -18;
4153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004154
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304155 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304156 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004157
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304158 QCBORDecode_GetNext(&DC, &Item);
4159 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304160 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304161 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004162
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304163 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304164 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304165 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004166
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304167 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004168 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304169
4170 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4171 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304172 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004174
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304175 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304176 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004177 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004178
Laurence Lundbladeee851742020-01-08 08:37:05 -08004179 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4180 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004181
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304182 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304183 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304184 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304185 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004186
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304187 QCBORDecode_GetNext(&DC, &Item);
4188 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304189 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304190 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004191 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304192 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304193 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004194
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304195 // ---- big bstr -----
4196 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004197
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304198 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4199 return -25;
4200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004201
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304202 if(QCBORDecode_GetNext(&DC, &Item)) {
4203 return -26;
4204 }
4205 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304206 return -26;
4207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004208
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304209 if(QCBORDecode_GetNext(&DC, &Item)) {
4210 return -27;
4211 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304212 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304213 return -28;
4214 }
4215 if(CheckBigString(Item.val.string)) {
4216 return -3;
4217 }
4218 if(QCBORDecode_Finish(&DC)) {
4219 return -29;
4220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004221
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304222 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004223 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004224
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304225 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4226 return -30;
4227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004228
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304229 QCBORDecode_GetNext(&DC, &Item);
4230 if(Item.uDataType != QCBOR_TYPE_MAP) {
4231 return -31;
4232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004233
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304234 if(QCBORDecode_GetNext(&DC, &Item)){
4235 return -32;
4236 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004237 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4238 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304239 Item.uDataAlloc || !Item.uLabelAlloc ||
4240 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4241 return -33;
4242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004243
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304244 if(QCBORDecode_Finish(&DC)) {
4245 return -34;
4246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004247
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004248 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004249}
4250
4251
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004252int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304253{
4254 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004255 QCBORError nCBORError;
4256
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004257
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304258 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004259 QCBORDecode_Init(&DC,
4260 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4261 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004262
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004263 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004264
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004265 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4266 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304267 return -1;
4268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004269
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004270 if(CheckCSRMaps(&DC)) {
4271 return -2;
4272 }
4273
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004274 // Next parse, save pointers to a few strings, destroy original and
4275 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004276 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004277 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004278
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304279 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004280 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304281 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004282
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304283 QCBORItem Item1, Item2, Item3, Item4;
4284 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004285 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304286 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4287 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004288 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304289 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004290 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304291 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
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, &Item3)))
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, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004296 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004297
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304298 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004299
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304300 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304301 Item1.uDataType != QCBOR_TYPE_INT64 ||
4302 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004303 Item1.uDataAlloc != 0 ||
4304 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004305 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004306 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004307 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004308
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304309
4310 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004311 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304312 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004313 Item2.uDataAlloc != 0 ||
4314 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304315 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004316 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004317
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304318 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004319 Item3.uDataAlloc == 0 ||
4320 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004321 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004322 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004323 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004324
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304325 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004326 Item4.uDataAlloc == 0 ||
4327 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004328 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004329 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004331
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304332 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004333 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004334 QCBORDecode_Init(&DC,
4335 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4336 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304337 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4338 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004339 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304340 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004341 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004342 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004343 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304344 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4345 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4346 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4347 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4348 }
4349 }
4350 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004351 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004352 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304353 }
4354
4355 return 0;
4356}
4357
Laurence Lundbladef6531662018-12-04 10:42:22 +09004358
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004359int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004360{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004361 // Set up the decoder with a tiny bit of CBOR to parse because
4362 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004363 QCBORDecodeContext DC;
4364 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4365 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004366
Laurence Lundbladef6531662018-12-04 10:42:22 +09004367 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004368 // Then fish into the internals of the decode context
4369 // to get the allocator function so it can be called directly.
4370 // Also figure out how much pool is available for use
4371 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004372 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004373 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4374 if(nError) {
4375 return -9;
4376 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004377 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4378 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4379 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004380
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004381 // First test -- ask for one more byte than available and see failure
4382 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004383 if(!UsefulBuf_IsNULL(Allocated)) {
4384 return -1;
4385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004386
Laurence Lundbladef6531662018-12-04 10:42:22 +09004387 // Re do the set up for the next test that will do a successful alloc,
4388 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004389 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004390 pAlloc = DC.StringAllocator.pfAllocator;
4391 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4392 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004393
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004394 // Allocate one byte less than available and see success
4395 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004396 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4397 return -2;
4398 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004399 // Ask for some more and see failure
4400 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004401 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4402 return -3;
4403 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004404 // Free the first allocate, retry the second and see success
4405 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4406 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004407 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4408 return -4;
4409 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004410
Laurence Lundbladef6531662018-12-04 10:42:22 +09004411 // Re do set up for next test that involves a successful alloc,
4412 // and a successful realloc and a failed realloc
4413 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004414 pAlloc = DC.StringAllocator.pfAllocator;
4415 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004416
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004417 // Allocate half the pool and see success
4418 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004419 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4420 return -5;
4421 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004422 // Reallocate to take up the whole pool and see success
4423 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004424 if(UsefulBuf_IsNULL(Allocated2)) {
4425 return -6;
4426 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004427 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004428 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4429 return -7;
4430 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004431 // Try to allocate more to be sure there is failure after a realloc
4432 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4433 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004434 return -8;
4435 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004436
Laurence Lundbladef6531662018-12-04 10:42:22 +09004437 return 0;
4438}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004439
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004440
4441/* Just enough of an allocator to test configuration of one */
4442static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4443{
4444 (void)pOldMem; // unused variable
4445
4446 if(uNewSize) {
4447 // Assumes the context pointer is the buffer and
4448 // nothing too big will ever be asked for.
4449 // This is only good for this basic test!
4450 return (UsefulBuf) {pCtx, uNewSize};
4451 } else {
4452 return NULLUsefulBuf;
4453 }
4454}
4455
4456
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004457int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004458{
4459 // Set up the decoder with a tiny bit of CBOR to parse because
4460 // nothing can be done with it unless that is set up.
4461 QCBORDecodeContext DC;
4462 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4463 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4464
4465 uint8_t pAllocatorBuffer[50];
4466
4467 // This is really just to test that this call works.
4468 // The full functionality of string allocators is tested
4469 // elsewhere with the MemPool internal allocator.
4470 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4471
4472 QCBORItem Item;
4473 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4474 return -1;
4475 }
4476
4477 if(Item.uDataAlloc == 0 ||
4478 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4479 Item.val.string.ptr != pAllocatorBuffer) {
4480 return -2;
4481 }
4482
4483 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4484 return -3;
4485 }
4486
4487 return 0;
4488}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004489#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4490
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004491
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004492#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004493
Laurence Lundbladea826c502020-05-10 21:07:00 -07004494/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004495 [
4496 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004497 4([-20, 4759477275222530853136]),
4498 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004499 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004500 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004501 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004502 5([ 9223372036854775806, -4759477275222530853137])
4503 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004504 ]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004505 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004506static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004507 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004508 0xC4, 0x82, 0x20,
4509 0x03,
4510 0xC4, 0x82, 0x33,
4511 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4512 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4513 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4514 0xC5, 0x82, 0x19, 0x01, 0x2C,
4515 0x18, 0x64,
4516 0xC5, 0x82, 0x33,
4517 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4518 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4519 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004520 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4521 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004522 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4523 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4524};
4525
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004526
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004527int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004528{
4529 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004530 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004531 QCBORItem item;
4532
Laurence Lundblade17af4902020-01-07 19:11:55 -08004533 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4534 0x06, 0x07, 0x08, 0x09, 0x010};
4535 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004536
4537
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004538 QCBORDecode_Init(&DC,
4539 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4540 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004541
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004542 uErr = QCBORDecode_GetNext(&DC, &item);
4543 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004544 return 1;
4545 }
4546
4547 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4548 return 2;
4549 }
4550
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004551 uErr = QCBORDecode_GetNext(&DC, &item);
4552 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004553 return 3;
4554 }
4555
4556 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4557 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4558 item.val.expAndMantissa.nExponent != -1) {
4559 return 4;
4560 }
4561
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004562 uErr = QCBORDecode_GetNext(&DC, &item);
4563 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004564 return 5;
4565 }
4566
4567 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4568 item.val.expAndMantissa.nExponent != -20 ||
4569 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4570 return 6;
4571 }
4572
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004573 uErr = QCBORDecode_GetNext(&DC, &item);
4574 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004575 return 7;
4576 }
4577
4578 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4579 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4580 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4581 return 8;
4582 }
4583
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004584 uErr = QCBORDecode_GetNext(&DC, &item);
4585 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004586 return 9;
4587 }
4588
4589 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4590 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4591 item.val.expAndMantissa.nExponent != 300) {
4592 return 10;
4593 }
4594
Laurence Lundbladea826c502020-05-10 21:07:00 -07004595 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004596 uErr = QCBORDecode_GetNext(&DC, &item);
4597 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004598 return 11;
4599 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004600 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4601 item.val.expAndMantissa.nExponent != -20 ||
4602 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4603 return 12;
4604 }
4605
Laurence Lundbladea826c502020-05-10 21:07:00 -07004606 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004607 uErr = QCBORDecode_GetNext(&DC, &item);
4608 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004609 return 13;
4610 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004611 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4612 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4613 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4614 return 14;
4615 }
4616
Laurence Lundbladea826c502020-05-10 21:07:00 -07004617 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004618 uErr = QCBORDecode_GetNext(&DC, &item);
4619 if(uErr != QCBOR_SUCCESS) {
4620 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004621 }
4622 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4623 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4624 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004625 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004626 }
4627
Laurence Lundbladea826c502020-05-10 21:07:00 -07004628 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004629 uErr = QCBORDecode_GetNext(&DC, &item);
4630 if(uErr != QCBOR_SUCCESS) {
4631 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004632 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004633 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4634 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4635 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004636 return 18;
4637 }
4638
4639 uErr = QCBORDecode_Finish(&DC);
4640 if(uErr != QCBOR_SUCCESS) {
4641 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004642 }
4643
4644 /* Now encode some stuff and then decode it */
4645 uint8_t pBuf[40];
4646 QCBOREncodeContext EC;
4647 UsefulBufC Encoded;
4648
4649 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4650 QCBOREncode_OpenArray(&EC);
4651 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4652 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4653 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4654 QCBOREncode_CloseArray(&EC);
4655 QCBOREncode_Finish(&EC, &Encoded);
4656
4657
4658 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004659 uErr = QCBORDecode_GetNext(&DC, &item);
4660 if(uErr != QCBOR_SUCCESS) {
4661 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004662 }
4663
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004664 uErr = QCBORDecode_GetNext(&DC, &item);
4665 if(uErr != QCBOR_SUCCESS) {
4666 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004667 }
4668
4669 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4670 item.val.expAndMantissa.nExponent != 1000 ||
4671 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004672 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004673 }
4674
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004675 uErr = QCBORDecode_GetNext(&DC, &item);
4676 if(uErr != QCBOR_SUCCESS) {
4677 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004678 }
4679
4680 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4681 item.val.expAndMantissa.nExponent != INT32_MIN ||
4682 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004683 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004684 }
4685
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004686 uErr = QCBORDecode_GetNext(&DC, &item);
4687 if(uErr != QCBOR_SUCCESS) {
4688 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004689 }
4690
4691 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4692 item.val.expAndMantissa.nExponent != INT32_MAX ||
4693 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004694 return 106;
4695 }
4696
4697
4698 int64_t nExp, nMant;
4699 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4700 UsefulBufC Mant;
4701 bool bIsNeg;
4702
4703 QCBORDecode_Init(&DC,
4704 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4705 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004706 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004707
4708 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004709 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004710
4711 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004712 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4713 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004714
4715 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004716 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4717 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004718
4719 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004720 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004721
4722 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004723 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4724 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004725
4726 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004727 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4728 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004729
4730 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004731 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4732 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004733
4734 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004735 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4736 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004737
4738 QCBORDecode_ExitArray(&DC);
4739
4740 uErr = QCBORDecode_Finish(&DC);
4741 if(uErr != QCBOR_SUCCESS) {
4742 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004743 }
4744
4745 return 0;
4746}
4747
4748
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004749static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004750 // Exponent > INT64_MAX
4751 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4752 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4753 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4754 // Mantissa > INT64_MAX
4755 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4756 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4757 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4758 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004759 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004760 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004761 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004762 // bad content for big num
4763 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4764 // bad content for big num
4765 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4766 // Bad integer for exponent
4767 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4768 // Bad integer for mantissa
4769 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4770 // 3 items in array
4771 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004772#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004773 // unterminated indefinite length array
4774 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004775#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4776 // unterminated indefinite length array
4777 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4778#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004779 // Empty array
4780 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4781 // Second is not an integer
4782 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4783 // First is not an integer
4784 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4785 // Not an array
4786 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4787};
4788
4789
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004790int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004791{
4792 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004793 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4794 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004795}
4796
4797#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004798
4799
4800
4801/*
4802 Some basic CBOR with map and array used in a lot of tests.
4803 The map labels are all strings
4804
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004805 {
4806 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004807 "an array of two strings": [
4808 "string1", "string2"
4809 ],
4810 "map in a map": {
4811 "bytes 1": h'78787878',
4812 "bytes 2": h'79797979',
4813 "another int": 98,
4814 "text 2": "lies, damn lies and statistics"
4815 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004816 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004817 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004818
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004819int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004820{
4821 QCBORItem Item1, Item2, Item3;
4822 int64_t nDecodedInt1, nDecodedInt2;
4823 UsefulBufC B1, B2, S1, S2, S3;
4824
4825 QCBORDecodeContext DCtx;
4826 QCBORError nCBORError;
4827
4828 QCBORDecode_Init(&DCtx, input, 0);
4829
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004830 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004831
4832 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4833
4834 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4835 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004836 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4837 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4838 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004839 QCBORDecode_ExitMap(&DCtx);
4840
4841 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4842 QCBORDecode_GetNext(&DCtx, &Item1);
4843 QCBORDecode_GetNext(&DCtx, &Item2);
4844 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4845 return -400;
4846 }
4847 QCBORDecode_ExitArray(&DCtx);
4848
4849 // Parse the same array again using GetText() instead of GetItem()
4850 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004851 QCBORDecode_GetTextString(&DCtx, &S2);
4852 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004853 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4854 return 5000;
4855 }
4856 /* QCBORDecode_GetText(&DCtx, &S3);
4857 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4858 return 5001;
4859 } */
4860
4861 QCBORDecode_ExitArray(&DCtx);
4862
4863 QCBORDecode_ExitMap(&DCtx);
4864
4865 nCBORError = QCBORDecode_Finish(&DCtx);
4866
4867 if(nCBORError) {
4868 return (int32_t)nCBORError;
4869 }
4870
4871 if(nDecodedInt1 != 42) {
4872 return 1001;
4873 }
4874
4875 if(nDecodedInt2 != 98) {
4876 return 1002;
4877 }
4878
4879 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004880 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004881 return 1003;
4882 }
4883
4884 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004885 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004886 return 1004;
4887 }
4888
Laurence Lundblade9b334962020-08-27 10:55:53 -07004889 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004890 return 1005;
4891 }
4892
4893 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4894 return 1006;
4895 }
4896
4897 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4898 return 1007;
4899 }
4900
4901 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4902 return 1008;
4903 }
4904
4905 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4906 return 1009;
4907 }
4908
4909 return 0;
4910}
4911
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004912/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004913 {
4914 -75008: h'05083399',
4915 88: [],
4916 100100: {
4917 "sub1": {
4918 10: [
4919 0
4920 ],
4921 -75009: h'A46823990001',
4922 100100: {
4923 "json": "{ \"ueid\", \"xyz\"}",
4924 "subsub": {
4925 100002: h'141813191001'
4926 }
4927 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004928 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004929 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004930 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004931 */
4932
4933static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004934 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
4935 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
4936 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
4937 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
4938 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
4939 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
4940 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
4941 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
4942 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
4943 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
4944 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
4945 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004946};
4947
4948/* Get item in multi-level nesting in spNestedCBOR */
4949static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4950{
4951 UsefulBufC String;
4952
4953 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4954 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4955
4956 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4957 QCBORDecode_EnterMap(pDCtx, NULL);
4958 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4959 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4960 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4961 if(QCBORDecode_GetError(pDCtx)) {
4962 return 4001;
4963 }
4964 if(UsefulBuf_Compare(String, test_oemid)) {
4965 return 4002;
4966 }
4967 QCBORDecode_ExitMap(pDCtx);
4968 QCBORDecode_ExitMap(pDCtx);
4969 QCBORDecode_ExitMap(pDCtx);
4970 QCBORDecode_ExitMap(pDCtx);
4971
4972 return 0;
4973}
4974
4975/* Iterations on the zero-length array in spNestedCBOR */
4976static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4977{
4978 QCBORItem Item;
4979 QCBORError uErr;
4980
4981 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4982 for(int x = 0; x < 20; x++) {
4983 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4984 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4985 return 4100;
4986
4987 }
4988 }
4989 QCBORDecode_ExitArray(pDCtx);
4990 if(QCBORDecode_GetError(pDCtx)) {
4991 return 4101;
4992 }
4993
4994 return 0;
4995}
4996
4997/* Various iterations on the array that contains a zero in spNestedCBOR */
4998static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
4999{
5000 QCBORError uErr;
5001
5002 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5003 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5004 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5005 int64_t nInt = 99;
5006 QCBORDecode_GetInt64(pDCtx, &nInt);
5007 if(nInt != 0) {
5008 return 4200;
5009 }
5010 for(int x = 0; x < 20; x++) {
5011 QCBORItem Item;
5012 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5013 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5014 return 4201;
5015
5016 }
5017 }
5018 QCBORDecode_ExitArray(pDCtx);
5019 if(QCBORDecode_GetAndResetError(pDCtx)) {
5020 return 4202;
5021 }
5022 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5023 UsefulBufC dD;
5024 QCBORDecode_GetByteString(pDCtx, &dD);
5025 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5026 return 4203;
5027 }
5028 for(int x = 0; x < 20; x++) {
5029 QCBORDecode_GetByteString(pDCtx, &dD);
5030 uErr = QCBORDecode_GetAndResetError(pDCtx);
5031 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5032 return 4204;
5033 }
5034 }
5035 QCBORDecode_ExitArray(pDCtx);
5036 QCBORDecode_ExitMap(pDCtx);
5037 QCBORDecode_ExitMap(pDCtx);
5038
5039 return 0;
5040}
5041
5042/* Repeatedly enter and exit maps and arrays, go off the end of maps
5043 and arrays and such. */
5044static int32_t DecodeNestedIterate()
5045{
5046 QCBORDecodeContext DCtx;
5047 int32_t nReturn;
5048 QCBORError uErr;
5049
5050 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5051 QCBORDecode_EnterMap(&DCtx, NULL);
5052
5053 for(int j = 0; j < 5; j++) {
5054 for(int i = 0; i < 20; i++) {
5055 nReturn = DecodeNestedGetSubSub(&DCtx);
5056 if(nReturn) {
5057 return nReturn;
5058 }
5059 }
5060
5061 for(int i = 0; i < 20; i++) {
5062 nReturn = DecodeNestedGetEmpty(&DCtx);
5063 if(nReturn ) {
5064 return nReturn;
5065 }
5066 }
5067
5068 for(int i = 0; i < 20; i++) {
5069 nReturn = DecodeNestedGetZero(&DCtx);
5070 if(nReturn ) {
5071 return nReturn;
5072 }
5073 }
5074 }
5075
5076 QCBORDecode_ExitMap(&DCtx);
5077 uErr = QCBORDecode_Finish(&DCtx);
5078 if(uErr) {
5079 return (int32_t)uErr + 4100;
5080 }
5081
5082 return 0;
5083}
5084
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005085
5086/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005087 [
5088 23,
5089 6000,
5090 h'67616C6163746963',
5091 h'686176656E20746F6B656E'
5092 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005093 */
5094static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005095 0x84,
5096 0x17,
5097 0x19, 0x17, 0x70,
5098 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5099 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005100
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005101/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005102static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5103
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005104/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005105static const uint8_t spEmptyMap[] = {0xa0};
5106
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005107#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005108/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005109static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005110
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005111
Laurence Lundbladef0499502020-08-01 11:55:57 -07005112/*
5113 {
5114 0: [],
5115 9: [
5116 [],
5117 []
5118 ],
5119 8: {
5120 1: [],
5121 2: {},
5122 3: []
5123 },
5124 4: {},
5125 5: [],
5126 6: [
5127 [],
5128 []
5129 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005130 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005131 */
5132static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005133 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5134 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5135 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5136 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005137
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005138#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5139
5140
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005141/*
5142 Too many tags
5143 Invalid tag content
5144 Duplicate label
5145 Integer overflow
5146 Date overflow
5147
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005148 {
5149 1: 224(225(226(227(4(0))))),
5150 2: 1(h''),
5151 3: -18446744073709551616,
5152 4: 1(1.0e+300),
5153 5: 0, 8: 8
5154 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005155 */
5156static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005157 0xa7,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005158 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5159 0x02, 0xc1, 0x40,
5160 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5161 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5162 0x05, 0x00,
5163 0x05, 0x00,
5164 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005165};
5166
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005167/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005168static const uint8_t spUnRecoverableMapError1[] = {
5169 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5170};
5171
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005172#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005173/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005174static const uint8_t spUnRecoverableMapError2[] = {
5175 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5176};
5177
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005178/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005179static const uint8_t spUnRecoverableMapError3[] = {
5180 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5181};
5182
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005183/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005184static const uint8_t spUnRecoverableMapError4[] = {
5185 0xbf,
5186 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5187 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5189 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5190 0xff
5191};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005192#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005193
5194
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005195int32_t EnterMapTest()
5196{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005197 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005198 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005199 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005200 int32_t nReturn;
5201 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005202
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005203#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005204 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005205 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005206
Laurence Lundbladef0499502020-08-01 11:55:57 -07005207
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005208 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005209 QCBORDecode_ExitArray(&DCtx);
5210
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005211 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5212 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005213 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005214 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005215 QCBORDecode_ExitArray(&DCtx);
5216 QCBORDecode_ExitArray(&DCtx);
5217
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005218 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5219 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005220 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005221 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005222 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005223 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005224 QCBORDecode_ExitArray(&DCtx);
5225 QCBORDecode_ExitMap(&DCtx);
5226
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005227 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005228 QCBORDecode_ExitMap(&DCtx);
5229
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005230 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005231 QCBORDecode_ExitArray(&DCtx);
5232
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005233 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5234 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005235 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005236 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005237 QCBORDecode_ExitArray(&DCtx);
5238 QCBORDecode_ExitArray(&DCtx);
5239
5240 QCBORDecode_ExitMap(&DCtx);
5241
5242 uErr = QCBORDecode_Finish(&DCtx);
5243 if(uErr != QCBOR_SUCCESS){
5244 return 3011;
5245 }
5246
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005247 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005248 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005249 if(nReturn) {
5250 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005251 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005252#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5253
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005254
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005255 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005256 if(nReturn) {
5257 return nReturn;
5258 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005259
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005260
Laurence Lundblade937ea812020-05-08 11:38:23 -07005261
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005262 // These tests confirm the cursor is at the right place after entering
5263 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005264 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005265
5266 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005267 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005268 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005269 QCBORDecode_GetNext(&DCtx, &Item1);
5270 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5271 return 2001;
5272 }
5273
5274
Laurence Lundblade9b334962020-08-27 10:55:53 -07005275 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005276 QCBORDecode_VGetNext(&DCtx, &Item1);
5277 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005278 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5279 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5280 UsefulBuf_Compare(ArrayItem.label.string,
5281 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5282 return 2051;
5283 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005284 QCBORDecode_GetNext(&DCtx, &Item1);
5285 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5286 return 2002;
5287 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005288 QCBORDecode_ExitArray(&DCtx);
5289 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5290 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5291 UsefulBuf_Compare(ArrayItem.label.string,
5292 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5293 return 2052;
5294 }
5295
Laurence Lundblade937ea812020-05-08 11:38:23 -07005296
Laurence Lundblade9b334962020-08-27 10:55:53 -07005297 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005298 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005299 QCBORDecode_GetNext(&DCtx, &Item1);
5300 QCBORDecode_GetNext(&DCtx, &Item1);
5301 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005302 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5303 QCBORDecode_GetNext(&DCtx, &Item1);
5304 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5305 return 2003;
5306 }
5307
Laurence Lundblade9b334962020-08-27 10:55:53 -07005308 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005309 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005310 QCBORDecode_GetNext(&DCtx, &Item1);
5311 QCBORDecode_GetNext(&DCtx, &Item1);
5312 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_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5318 QCBORDecode_GetNext(&DCtx, &Item1);
5319 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005320 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005321 }
5322
Laurence Lundblade9b334962020-08-27 10:55:53 -07005323 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005324 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005325 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5326 QCBORDecode_ExitArray(&DCtx);
5327 QCBORDecode_GetNext(&DCtx, &Item1);
5328 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5329 return 2006;
5330 }
5331 QCBORDecode_ExitMap(&DCtx);
5332 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5333 return 2007;
5334 }
5335
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005336 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005337 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005338 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005339 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5340 uErr = QCBORDecode_GetAndResetError(&DCtx);
5341 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005342 return 2008;
5343 }
5344 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005345 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005346 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005347 return 2009;
5348 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005349
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005350
5351 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005352 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005353 // This will fail because the map is empty.
5354 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5355 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005356 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005357 return 2010;
5358 }
5359 QCBORDecode_ExitMap(&DCtx);
5360 uErr = QCBORDecode_Finish(&DCtx);
5361 if(uErr != QCBOR_SUCCESS){
5362 return 2011;
5363 }
5364
5365
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005366#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005367 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005368 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005369 // This will fail because the map is empty.
5370 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5371 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005372 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005373 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005374 }
5375 QCBORDecode_ExitMap(&DCtx);
5376 uErr = QCBORDecode_Finish(&DCtx);
5377 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005378 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005379 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005380#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005381
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005382
5383 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005384 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005385 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005386 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005387 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005388 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005389 QCBORDecode_ExitArray(&DCtx);
5390 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5391 QCBORDecode_ExitArray(&DCtx);
5392 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005393 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005394 return 2014;
5395 }
5396
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005397 int64_t nInt;
5398 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005399 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005400 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005401 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005402 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5403 return 2021;
5404 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005405 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5406 return 2121;
5407 }
5408 (void)QCBORDecode_GetAndResetError(&DCtx);
5409
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005410
5411 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5412 uErr = QCBORDecode_GetAndResetError(&DCtx);
5413 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5414 return 2022;
5415 }
5416
5417 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5418 uErr = QCBORDecode_GetAndResetError(&DCtx);
5419 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5420 return 2023;
5421 }
5422
5423 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5424 uErr = QCBORDecode_GetAndResetError(&DCtx);
5425#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5426 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5427 return 2024;
5428 }
5429#else
5430 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5431 return 2027;
5432 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005433#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005434
5435 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5436 uErr = QCBORDecode_GetAndResetError(&DCtx);
5437 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5438 return 2025;
5439 }
5440
5441 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5442
5443 QCBORDecode_ExitMap(&DCtx);
5444 uErr = QCBORDecode_Finish(&DCtx);
5445 if(uErr != QCBOR_SUCCESS) {
5446 return 2026;
5447 }
5448
5449 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005450 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005451 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5452 uErr = QCBORDecode_GetAndResetError(&DCtx);
5453 if(uErr != QCBOR_ERR_BAD_BREAK) {
5454 return 2030;
5455 }
5456
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005457#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005458 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005459 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005460 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5461 uErr = QCBORDecode_GetAndResetError(&DCtx);
5462 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5463 return 2031;
5464 }
5465
5466 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005467 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005468 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5469 uErr = QCBORDecode_GetAndResetError(&DCtx);
5470 if(uErr != QCBOR_ERR_HIT_END) {
5471 return 2032;
5472 }
5473
5474 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005475 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005476 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5477 uErr = QCBORDecode_GetAndResetError(&DCtx);
5478 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5479 return 2033;
5480 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005481#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5482
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005483
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005484 nReturn = DecodeNestedIterate();
5485
5486 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005487}
5488
5489
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005490struct NumberConversion {
5491 char *szDescription;
5492 UsefulBufC CBOR;
5493 int64_t nConvertedToInt64;
5494 QCBORError uErrorInt64;
5495 uint64_t uConvertToUInt64;
5496 QCBORError uErrorUint64;
5497 double dConvertToDouble;
5498 QCBORError uErrorDouble;
5499};
5500
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005501static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005502 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005503 "too large to fit into int64_t",
5504 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5505 0,
5506 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5507 0,
5508 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5509 ((double)INT64_MIN) + 1 ,
5510 QCBOR_SUCCESS
5511 },
5512 {
5513 "largest negative int that fits in int64_t",
5514 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5515 INT64_MIN,
5516 QCBOR_SUCCESS,
5517 0,
5518 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5519 (double)INT64_MIN,
5520 QCBOR_SUCCESS
5521 },
5522 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005523 "negative bignum -1",
5524 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5525 -1,
5526 QCBOR_SUCCESS,
5527 0,
5528 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5529 -1.0,
5530 QCBOR_SUCCESS
5531 },
5532 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005533 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005534 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5535 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005536#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005537 257000,
5538 QCBOR_SUCCESS,
5539 257000,
5540 QCBOR_SUCCESS,
5541 257000.0,
5542 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005543#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5544 0,
5545 QCBOR_ERR_UNEXPECTED_TYPE,
5546 0,
5547 QCBOR_ERR_UNEXPECTED_TYPE,
5548 0.0,
5549 QCBOR_ERR_UNEXPECTED_TYPE
5550#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005551 },
5552 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005553 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005554 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5555 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005556#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005557 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005558 QCBOR_SUCCESS,
5559 0,
5560 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005561 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005562 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005563#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5564 0,
5565 QCBOR_ERR_UNEXPECTED_TYPE,
5566 0,
5567 QCBOR_ERR_UNEXPECTED_TYPE,
5568 0.0,
5569 QCBOR_ERR_UNEXPECTED_TYPE
5570#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005571 },
5572 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005573 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005574 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5575 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005576#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005577 2056,
5578 QCBOR_SUCCESS,
5579 2056,
5580 QCBOR_SUCCESS,
5581 2056.0,
5582 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005583#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5584 0,
5585 QCBOR_ERR_UNEXPECTED_TYPE,
5586 0,
5587 QCBOR_ERR_UNEXPECTED_TYPE,
5588 0.0,
5589 QCBOR_ERR_UNEXPECTED_TYPE
5590#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005591 },
5592 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005593 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005594 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5595 0,
5596 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5597 0,
5598 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5599 -18446744073709551617.0,
5600 QCBOR_SUCCESS
5601 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005602#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005603 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005604 "Positive bignum 0x01020304 indefinite length string",
5605 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5606 0x01020304,
5607 QCBOR_SUCCESS,
5608 0x01020304,
5609 QCBOR_SUCCESS,
5610 16909060.0,
5611 QCBOR_SUCCESS
5612 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005613#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005614 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005615 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005616 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5617 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005618#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005619 0,
5620 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5621 0,
5622 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5623 -INFINITY,
5624 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005625#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5626 0,
5627 QCBOR_ERR_UNEXPECTED_TYPE,
5628 0,
5629 QCBOR_ERR_UNEXPECTED_TYPE,
5630 0.0,
5631 QCBOR_ERR_UNEXPECTED_TYPE
5632#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005633 },
5634 {
5635 "big float [9223372036854775806, 9223372036854775806]",
5636 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5637 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005638#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005639 0,
5640 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5641 0,
5642 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5643 INFINITY,
5644 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005645#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5646 0,
5647 QCBOR_ERR_UNEXPECTED_TYPE,
5648 0,
5649 QCBOR_ERR_UNEXPECTED_TYPE,
5650 0.0,
5651 QCBOR_ERR_UNEXPECTED_TYPE
5652#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005653 },
5654 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005655 "Big float 3 * 2^^2",
5656 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005657#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005658 12,
5659 QCBOR_SUCCESS,
5660 12,
5661 QCBOR_SUCCESS,
5662 12.0,
5663 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005664#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5665 0,
5666 QCBOR_ERR_UNEXPECTED_TYPE,
5667 0,
5668 QCBOR_ERR_UNEXPECTED_TYPE,
5669 0.0,
5670 QCBOR_ERR_UNEXPECTED_TYPE
5671#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005672 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005673 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005674 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005675 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5676 0,
5677 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5678 18446744073709551615ULL,
5679 QCBOR_SUCCESS,
5680 18446744073709551615.0,
5681 QCBOR_SUCCESS
5682 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005683 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005684 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005685 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5686 65536-1,
5687 QCBOR_SUCCESS,
5688 0xffff,
5689 QCBOR_SUCCESS,
5690 65535.0,
5691 QCBOR_SUCCESS
5692 },
5693 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005694 "Postive integer 0",
5695 {(uint8_t[]){0x0}, 1},
5696 0LL,
5697 QCBOR_SUCCESS,
5698 0ULL,
5699 QCBOR_SUCCESS,
5700 0.0,
5701 QCBOR_SUCCESS
5702 },
5703 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005704 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005705 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5706 -9223372036854775807-1, // INT64_MIN
5707 QCBOR_SUCCESS,
5708 0ULL,
5709 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5710 -9223372036854775808.0,
5711 QCBOR_SUCCESS
5712 },
5713 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005714 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005715 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005716#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005717 100L,
5718 QCBOR_SUCCESS,
5719 100ULL,
5720 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005721#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5722 0,
5723 QCBOR_ERR_HW_FLOAT_DISABLED,
5724 0,
5725 QCBOR_ERR_HW_FLOAT_DISABLED,
5726#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005727 100.3,
5728 QCBOR_SUCCESS
5729 },
5730 {
5731 "Floating point value NaN 0xfa7fc00000",
5732 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005733#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005734 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005735 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005736 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005737 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005738#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5739 0,
5740 QCBOR_ERR_HW_FLOAT_DISABLED,
5741 0,
5742 QCBOR_ERR_HW_FLOAT_DISABLED,
5743#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005744 NAN,
5745 QCBOR_SUCCESS
5746 },
5747 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005748 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005749 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005750#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5751#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5752 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005753 -4,
5754 QCBOR_SUCCESS,
5755 0,
5756 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5757 -4.0,
5758 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005759#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5760 // Float HW disabled
5761 -4,
5762 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5763 0,
5764 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5765 -4.0,
5766 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5767#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005768#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005769 // Half-precision disabled
5770 -4,
5771 QCBOR_ERR_HALF_PRECISION_DISABLED,
5772 0,
5773 QCBOR_ERR_HALF_PRECISION_DISABLED,
5774 -4.0,
5775 QCBOR_ERR_HALF_PRECISION_DISABLED
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005776#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005777 },
5778 {
5779 "Decimal fraction 3/10",
5780 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005781#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005782 0,
5783 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5784 0,
5785 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5786 0.30000000000000004,
5787 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005788#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5789 0,
5790 QCBOR_ERR_UNEXPECTED_TYPE,
5791 0,
5792 QCBOR_ERR_UNEXPECTED_TYPE,
5793 0.0,
5794 QCBOR_ERR_UNEXPECTED_TYPE
5795#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005796 },
5797 {
5798 "+inifinity",
5799 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5800#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5801 0,
5802 QCBOR_ERR_FLOAT_EXCEPTION,
5803 0,
5804 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5805#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5806 0,
5807 QCBOR_ERR_HW_FLOAT_DISABLED,
5808 0,
5809 QCBOR_ERR_HW_FLOAT_DISABLED,
5810#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5811 INFINITY,
5812 QCBOR_SUCCESS
5813 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005814
5815 {
5816 "extreme pos bignum",
5817 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5818 // 50 rows of 8 is 400 digits.
5819 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5820 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5821 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5822 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5823 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5824 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5825 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5826 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5827 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5828 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5829 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5830 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5831 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5832 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5833 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5834 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5835 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5836 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5837 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5838 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5839 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5840 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5841 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5842 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5843 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5844 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5845 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5846 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5847 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5848 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5849 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5850 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5851 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5852 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5853 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5854 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5855 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5856 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5857 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5858 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5859 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5860 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5861 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5862 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5863 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 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},
5869 404},
5870 0,
5871 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5872 0,
5873 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5874#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5875 INFINITY,
5876 QCBOR_SUCCESS
5877#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5878 0,
5879 QCBOR_ERR_HW_FLOAT_DISABLED,
5880#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5881 },
5882
5883 {
5884 "extreme neg bignum",
5885 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5886 // 50 rows of 8 is 400 digits.
5887 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5888 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5889 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5890 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5891 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5892 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5893 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5894 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5895 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5896 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5897 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5898 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5899 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5900 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5901 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5902 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5903 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5904 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5905 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5906 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5907 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5908 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5909 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5910 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5911 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5912 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5913 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5914 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5915 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5916 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5917 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5918 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5919 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5920 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5921 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5922 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5923 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5924 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5925 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5926 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5927 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5928 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5929 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 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},
5937 404},
5938 0,
5939 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5940 0,
5941 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5942#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5943 -INFINITY,
5944 QCBOR_SUCCESS
5945#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5946 0,
5947 QCBOR_ERR_HW_FLOAT_DISABLED,
5948#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5949 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005950
5951 {
5952 "big float underflow [9223372036854775806, -9223372036854775806]",
5953 {(uint8_t[]){
5954 0xC5, 0x82,
5955 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5956 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5957#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5958 0,
5959 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5960 0,
5961 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5962 0,
5963 QCBOR_SUCCESS
5964#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5965 0,
5966 QCBOR_ERR_UNEXPECTED_TYPE,
5967 0,
5968 QCBOR_ERR_UNEXPECTED_TYPE,
5969 0.0,
5970 QCBOR_ERR_UNEXPECTED_TYPE
5971#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5972 },
5973
5974 {
5975 "bigfloat that evaluates to -INFINITY",
5976 {(uint8_t[]){
5977 0xC5, 0x82,
5978 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5979 0xC3, 0x42, 0x01, 0x01}, 15},
5980#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5981 0,
5982 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5983 0,
5984 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5985 -INFINITY,
5986 QCBOR_SUCCESS
5987#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5988 0,
5989 QCBOR_ERR_UNEXPECTED_TYPE,
5990 0,
5991 QCBOR_ERR_UNEXPECTED_TYPE,
5992 0.0,
5993 QCBOR_ERR_UNEXPECTED_TYPE
5994#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5995 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005996};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005997
5998
5999
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006000
6001static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6002{
6003 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6004#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6005 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6006 return 1;
6007 }
6008#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6009 (void)Pool;
6010#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6011 return 0;
6012}
6013
6014
Laurence Lundblade313b2862020-05-16 01:23:06 -07006015int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006016{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006017 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6018 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006019
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006020 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6021 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006022
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006023 // Set up the decoding context including a memory pool so that
6024 // indefinite length items can be checked
6025 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006026 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006027
6028 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006029 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6030 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006031 }
6032
6033 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006034 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006035 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006036 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006037 }
6038 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006039 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006040 }
6041
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006042 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006043 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6044 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006045 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006046
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006047 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006048 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006049 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006050 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006051 }
6052 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006053 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006054 }
6055
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006056 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006057 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6058 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006059 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006060#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006061 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006062 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006063 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006064 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006065 }
6066 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6067 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006068 // NaN's can't be compared for equality. A NaN is
6069 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006070 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006071 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006072 }
6073 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006074 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006075 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006076 }
6077 }
6078 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006079#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006080 }
6081
6082 return 0;
6083}
6084
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006085
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006086
6087
Laurence Lundbladee3553422020-05-02 11:11:17 -07006088int32_t CBORSequenceDecodeTests(void)
6089{
6090 QCBORDecodeContext DCtx;
6091 QCBORItem Item;
6092 QCBORError uCBORError;
6093
6094 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006095
Laurence Lundbladee3553422020-05-02 11:11:17 -07006096 // The input for the date test happens to be a sequence so it
6097 // is reused. It is a sequence because it doesn't start as
6098 // an array or map.
6099 QCBORDecode_Init(&DCtx,
6100 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
6101 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006102
Laurence Lundbladee3553422020-05-02 11:11:17 -07006103 // Get the first item
6104 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6105 if(uCBORError != QCBOR_SUCCESS) {
6106 return 1;
6107 }
6108 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
6109 return 2;
6110 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006111
Laurence Lundbladee3553422020-05-02 11:11:17 -07006112 // Get a second item
6113 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07006114 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
6115 return 66;
6116 }
6117
6118 // Get a third item
6119 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006120 if(uCBORError != QCBOR_SUCCESS) {
6121 return 2;
6122 }
6123 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6124 return 3;
6125 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006126
Laurence Lundbladee3553422020-05-02 11:11:17 -07006127 // A sequence can have stuff at the end that may
6128 // or may not be valid CBOR. The protocol decoder knows
6129 // when to stop by definition of the protocol, not
6130 // when the top-level map or array is ended.
6131 // Finish still has to be called to know that
6132 // maps and arrays (if there were any) were closed
6133 // off correctly. When called like this it
6134 // must return the error QCBOR_ERR_EXTRA_BYTES.
6135 uCBORError = QCBORDecode_Finish(&DCtx);
6136 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6137 return 4;
6138 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006139
6140
Laurence Lundbladee3553422020-05-02 11:11:17 -07006141 // --- Test an empty input ----
6142 uint8_t empty[1];
6143 UsefulBufC Empty = {empty, 0};
6144 QCBORDecode_Init(&DCtx,
6145 Empty,
6146 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006147
Laurence Lundbladee3553422020-05-02 11:11:17 -07006148 uCBORError = QCBORDecode_Finish(&DCtx);
6149 if(uCBORError != QCBOR_SUCCESS) {
6150 return 5;
6151 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006152
6153
Laurence Lundbladee3553422020-05-02 11:11:17 -07006154 // --- Sequence with unclosed indefinite length array ---
6155 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006156
Laurence Lundbladee3553422020-05-02 11:11:17 -07006157 QCBORDecode_Init(&DCtx,
6158 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6159 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006160
Laurence Lundbladee3553422020-05-02 11:11:17 -07006161 // Get the first item
6162 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6163 if(uCBORError != QCBOR_SUCCESS) {
6164 return 7;
6165 }
6166 if(Item.uDataType != QCBOR_TYPE_INT64) {
6167 return 8;
6168 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006169
Laurence Lundbladee3553422020-05-02 11:11:17 -07006170 // Get a second item
6171 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006172#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006173 if(uCBORError != QCBOR_SUCCESS) {
6174 return 9;
6175 }
6176 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6177 return 10;
6178 }
6179
6180 // Try to finish before consuming all bytes to confirm
6181 // that the still-open error is returned.
6182 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006183 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006184 return 11;
6185 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006186#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6187 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6188 return 20;
6189 }
6190#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006191
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006192
Laurence Lundbladee3553422020-05-02 11:11:17 -07006193 // --- Sequence with a closed indefinite length array ---
6194 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006195
Laurence Lundbladee3553422020-05-02 11:11:17 -07006196 QCBORDecode_Init(&DCtx,
6197 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6198 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006199
Laurence Lundbladee3553422020-05-02 11:11:17 -07006200 // Get the first item
6201 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6202 if(uCBORError != QCBOR_SUCCESS) {
6203 return 12;
6204 }
6205 if(Item.uDataType != QCBOR_TYPE_INT64) {
6206 return 13;
6207 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006208
Laurence Lundbladee3553422020-05-02 11:11:17 -07006209 // Get a second item
6210 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006211#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6212
Laurence Lundbladee3553422020-05-02 11:11:17 -07006213 if(uCBORError != QCBOR_SUCCESS) {
6214 return 14;
6215 }
6216 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6217 return 15;
6218 }
6219
6220 // Try to finish before consuming all bytes to confirm
6221 // that the still-open error is returned.
6222 uCBORError = QCBORDecode_Finish(&DCtx);
6223 if(uCBORError != QCBOR_SUCCESS) {
6224 return 16;
6225 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006226#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6227 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6228 return 20;
6229 }
6230#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006231
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006232
Laurence Lundbladee3553422020-05-02 11:11:17 -07006233 return 0;
6234}
6235
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006236
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006237
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006238int32_t IntToTests()
6239{
6240 int nErrCode;
6241 int32_t n32;
6242 int16_t n16;
6243 int8_t n8;
6244 uint32_t u32;
6245 uint16_t u16;
6246 uint8_t u8;
6247 uint64_t u64;
6248
6249 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6250 if(nErrCode == -1 || n32 != 1) {
6251 return 1;
6252 }
6253
6254 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6255 if(nErrCode == -1 || n32 != INT32_MAX) {
6256 return 2;
6257 }
6258
6259 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6260 if(nErrCode == -1 || n32 != INT32_MIN) {
6261 return 3;
6262 }
6263
6264 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6265 if(nErrCode != -1) {
6266 return 4;
6267 }
6268
6269 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6270 if(nErrCode != -1) {
6271 return 5;
6272 }
6273
6274
6275 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6276 if(nErrCode == -1 || n16 != INT16_MAX) {
6277 return 6;
6278 }
6279
6280 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6281 if(nErrCode == -1 || n16 != INT16_MIN) {
6282 return 7;
6283 }
6284
6285 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6286 if(nErrCode == -1 || n16 != 1) {
6287 return 8;
6288 }
6289
6290 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6291 if(nErrCode != -1) {
6292 return 9;
6293 }
6294
6295 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6296 if(nErrCode != -1) {
6297 return 10;
6298 }
6299
6300
6301 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6302 if(nErrCode == -1 || n8 != 1) {
6303 return 11;
6304 }
6305
6306 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6307 if(nErrCode == -1 || n8 != INT8_MAX) {
6308 return 12;
6309 }
6310
6311 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6312 if(nErrCode == -1 || n8 != INT8_MIN) {
6313 return 13;
6314 }
6315
6316 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6317 if(nErrCode != -1) {
6318 return 14;
6319 }
6320
6321 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6322 if(nErrCode != -1) {
6323 return 15;
6324 }
6325
6326
6327 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6328 if(nErrCode == -1 || u32 != 1) {
6329 return 16;
6330 }
6331
6332 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6333 if(nErrCode == -1 || u32 != UINT32_MAX) {
6334 return 17;
6335 }
6336
6337 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6338 if(nErrCode == -1 || u32 != 0) {
6339 return 18;
6340 }
6341
6342 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6343 if(nErrCode != -1) {
6344 return 19;
6345 }
6346
6347 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6348 if(nErrCode != -1) {
6349 return 20;
6350 }
6351
6352
6353 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6354 if(nErrCode == -1 || u16 != UINT16_MAX) {
6355 return 21;
6356 }
6357
6358 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6359 if(nErrCode == -1 || u16 != 0) {
6360 return 22;
6361 }
6362
6363 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6364 if(nErrCode == -1 || u16 != 1) {
6365 return 23;
6366 }
6367
6368 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6369 if(nErrCode != -1) {
6370 return 24;
6371 }
6372
6373 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6374 if(nErrCode != -1) {
6375 return 25;
6376 }
6377
6378
6379 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6380 if(nErrCode == -1 || u8 != UINT8_MAX) {
6381 return 26;
6382 }
6383
6384 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6385 if(nErrCode == -1 || u8 != 0) {
6386 return 27;
6387 }
6388
6389 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6390 if(nErrCode == -1 || u8 != 1) {
6391 return 28;
6392 }
6393
6394 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6395 if(nErrCode != -1) {
6396 return 29;
6397 }
6398
6399 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6400 if(nErrCode != -1) {
6401 return 30;
6402 }
6403
6404
6405 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6406 if(nErrCode == -1 || u64 != 1) {
6407 return 31;
6408 }
6409
6410 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6411 if(nErrCode == -1 || u64 != INT64_MAX) {
6412 return 32;
6413 }
6414
6415 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6416 if(nErrCode == -1 || u64 != 0) {
6417 return 33;
6418 }
6419
6420 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6421 if(nErrCode != -1) {
6422 return 34;
6423 }
6424
6425 return 0;
6426}
6427
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006428
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006429
6430
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006431/*
6432A sequence with
6433 A wrapping bstr
6434 containing a map
6435 1
6436 2
6437 A wrapping bstr
6438 containing an array
6439 3
6440 wrapping bstr
6441 4
6442 5
6443 6
6444 array
6445 7
6446 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006447 */
6448
Laurence Lundblade55013642020-09-23 05:39:22 -07006449static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006450{
Laurence Lundblade55013642020-09-23 05:39:22 -07006451 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006452 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006453 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006454
Laurence Lundblade55013642020-09-23 05:39:22 -07006455 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006456
6457 QCBOREncode_BstrWrap(&EC);
6458 QCBOREncode_OpenMap(&EC);
6459 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6460 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6461 QCBOREncode_CloseMap(&EC);
6462 QCBOREncode_BstrWrap(&EC);
6463 QCBOREncode_OpenArray(&EC);
6464 QCBOREncode_AddInt64(&EC, 3);
6465 QCBOREncode_BstrWrap(&EC);
6466 QCBOREncode_AddInt64(&EC, 4);
6467 QCBOREncode_CloseBstrWrap(&EC, NULL);
6468 QCBOREncode_AddInt64(&EC, 5);
6469 QCBOREncode_CloseArray(&EC);
6470 QCBOREncode_CloseBstrWrap(&EC, NULL);
6471 QCBOREncode_AddInt64(&EC, 6);
6472 QCBOREncode_CloseBstrWrap(&EC, NULL);
6473 QCBOREncode_OpenArray(&EC);
6474 QCBOREncode_AddInt64(&EC, 7);
6475 QCBOREncode_AddInt64(&EC, 8);
6476 QCBOREncode_CloseArray(&EC);
6477
6478 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006479 if(uErr) {
6480 Encoded = NULLUsefulBufC;
6481 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006482
6483 return Encoded;
6484}
6485
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006486/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006487static const uint8_t spBreakInByteString[] = {
6488 0x41, 0xff
6489};
6490
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006491
6492int32_t EnterBstrTest()
6493{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006494 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006495
6496 QCBORDecodeContext DC;
6497
Laurence Lundblade55013642020-09-23 05:39:22 -07006498 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006499
Laurence Lundblade55013642020-09-23 05:39:22 -07006500 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006501
6502
Laurence Lundblade9b334962020-08-27 10:55:53 -07006503 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006504 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006505 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6506 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006507 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006508 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006509 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006510 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006511 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006512 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006513 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006514 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006515 QCBORDecode_ExitArray(&DC);
6516 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006517 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006518 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006519 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006520 QCBORDecode_GetInt64(&DC, &n7);
6521 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006522 QCBORDecode_ExitArray(&DC);
6523
6524 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006525 if(uErr) {
6526 return (int32_t)uErr;
6527 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006528
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006529
6530 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6531 * Successful because no items are fetched from byte string.
6532 */
6533 QCBORDecode_Init(&DC,
6534 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6535 0);
6536 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6537 uErr = QCBORDecode_GetError(&DC);
6538 if(uErr) {
6539 return 100 + (int32_t)uErr;
6540 }
6541
6542 QCBORDecode_ExitBstrWrapped(&DC);
6543 uErr = QCBORDecode_GetError(&DC);
6544 if(uErr) {
6545 return 200 + (int32_t)uErr;
6546 }
6547
6548 /* Try to get item that is a break out of a byte string wrapped CBOR.
6549 * It fails because there should be no break.
6550 */
6551 QCBORDecode_Init(&DC,
6552 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6553 0);
6554 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6555 QCBORItem Item;
6556 uErr = QCBORDecode_GetNext(&DC, &Item);
6557 if(uErr != QCBOR_ERR_BAD_BREAK) {
6558 return 300 + (int32_t)uErr;
6559 }
6560
6561 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006562}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006563
6564
6565
6566
6567static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006568 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006569
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006570 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006571 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006572 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6573 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6574 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006575
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006576 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006577 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6578 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6579 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006580
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006581 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006582 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006583 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6584 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006585
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006586 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006587 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6588 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006589
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006590 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006591 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006592 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6593 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006594
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006595 21,
6596 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6597 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6598
6599 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006600 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006601 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6602 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006603
6604 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006605 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6606 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006607
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006608 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006609 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006610 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6611 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006612
6613 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006614 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6615 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006616
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006617 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006618 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006619 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6620 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006621
6622 0x18, 0x33,
6623 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6624
6625 // MIME
6626 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006627 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6628 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6629 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006630
6631 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006632 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6633 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006634
6635 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006636 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6637 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6638 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006639
6640 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006641 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6642 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006643
6644 // UUID
6645 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006646 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6647 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006648
6649 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006650 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6651 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006652};
6653
6654int32_t DecodeTaggedTypeTests()
6655{
6656 QCBORDecodeContext DC;
6657 QCBORError uErr;
6658
6659 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6660
6661 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006662 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006663
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006664 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006665 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006666 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006667 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6668 return 1;
6669 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006670 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006671 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6672 return 2;
6673 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006674 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006675 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6676 return 3;
6677 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006678 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006679 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006680 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6681 return 4;
6682 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006683 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006684 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006685 return 5;
6686 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006687
Laurence Lundblade9b334962020-08-27 10:55:53 -07006688 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006689 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6690 bNeg != false) {
6691 return 10;
6692 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006693 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006694 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6695 bNeg != true) {
6696 return 11;
6697 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006698 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006699 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6700 return 12;
6701 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006702 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006703 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006704 return 13;
6705 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006706 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006707 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006708 return 14;
6709 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006710
Laurence Lundblade9b334962020-08-27 10:55:53 -07006711 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006712 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6713 return 20;
6714 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006715 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006716 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6717 return 21;
6718 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006719 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006720 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006721 return 22;
6722 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006723 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006724 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006725 return 23;
6726 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006727
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006728#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006729 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006730 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6731 return 30;
6732 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006733#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006734 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006735 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6736 return 31;
6737 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006738 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006739 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006740 return 32;
6741 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006742 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006743 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006744 return 33;
6745 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006746
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006747#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006748 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006749 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6750 return 40;
6751 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006752#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006753 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006754 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6755 return 41;
6756 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006757 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006758 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006759 return 42;
6760 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006761 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006762 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006763 return 43;
6764 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006765
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006766#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006767 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006768 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6769 return 50;
6770 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006771#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006772 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006773 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6774 return 51;
6775 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006776 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006777 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006778 return 52;
6779 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006780 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006781 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006782 return 53;
6783 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006784
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006785#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006786 // MIME
6787 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006788 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006789 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6790 bIsNot7Bit == true) {
6791 return 60;
6792 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006793 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006794 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6795 bIsNot7Bit == true) {
6796 return 61;
6797 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006798 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006799 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6800 bIsNot7Bit == false) {
6801 return 62;
6802 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006803 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006804 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6805 bIsNot7Bit == false) {
6806 return 63;
6807 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006808 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006809 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006810 return 64;
6811 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006812 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006813 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006814 return 65;
6815 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006816
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006817
Laurence Lundblade9b334962020-08-27 10:55:53 -07006818 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006819 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6820 return 70;
6821 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006822#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
6823
Laurence Lundblade9b334962020-08-27 10:55:53 -07006824 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006825 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6826 return 71;
6827 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006828 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006829 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006830 return 72;
6831 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006832 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006833 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006834 return 73;
6835 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006836
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006837 // Improvement: add some more error test cases
6838
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006839 QCBORDecode_ExitMap(&DC);
6840
6841 uErr = QCBORDecode_Finish(&DC);
6842 if(uErr != QCBOR_SUCCESS) {
6843 return 100;
6844 }
6845
6846 return 0;
6847}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006848
6849
6850
6851
6852/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006853 [
6854 "aaaaaaaaaa",
6855 {}
6856 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006857 */
6858static const uint8_t spTooLarge1[] = {
6859 0x9f,
6860 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6861 0xa0,
6862 0xff
6863};
6864
6865/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006866 [
6867 {
6868 0: "aaaaaaaaaa"
6869 }
6870 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006871 */
6872static const uint8_t spTooLarge2[] = {
6873 0x9f,
6874 0xa1,
6875 0x00,
6876 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6877 0xff
6878};
6879
6880/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006881 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006882
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006883 {
6884 0: "aaaaaaaaaa"
6885 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006886 */
6887static const uint8_t spTooLarge3[] = {
6888 0x4d,
6889 0xa1,
6890 0x00,
6891 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6892};
6893
6894int32_t TooLargeInputTest(void)
6895{
6896 QCBORDecodeContext DC;
6897 QCBORError uErr;
6898 UsefulBufC String;
6899
6900 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6901 // to 10 There's not really any way to test this error
6902 // condition. The error condition is not complex, so setting
6903 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6904
6905 // The input CBOR is only too large because the
6906 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6907 //
6908 // This test is disabled for the normal test runs because of the
6909 // special build requirement.
6910
6911
6912 // Tests the start of a map being too large
6913 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006914 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006915 QCBORDecode_GetTextString(&DC, &String);
6916 uErr = QCBORDecode_GetError(&DC);
6917 if(uErr != QCBOR_SUCCESS) {
6918 return 1;
6919 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006920 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006921 uErr = QCBORDecode_GetError(&DC);
6922 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6923 return 2;
6924 }
6925
6926 // Tests the end of a map being too large
6927 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006928 QCBORDecode_EnterArray(&DC, NULL);
6929 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006930 uErr = QCBORDecode_GetError(&DC);
6931 if(uErr != QCBOR_SUCCESS) {
6932 return 3;
6933 }
6934 QCBORDecode_ExitMap(&DC);
6935 uErr = QCBORDecode_GetError(&DC);
6936 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6937 return 4;
6938 }
6939
6940 // Tests the entire input CBOR being too large when processing bstr wrapping
6941 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6942 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6943 uErr = QCBORDecode_GetError(&DC);
6944 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6945 return 5;
6946 }
6947
6948 return 0;
6949}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006950
6951
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006952#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6953
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006954static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006955 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006956 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6957 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6958 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6959 0x03,
6960 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6961 0xc3,
6962 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006963};
6964
6965int32_t SpiffyIndefiniteLengthStringsTests()
6966{
6967 QCBORDecodeContext DCtx;
6968
6969 QCBORDecode_Init(&DCtx,
6970 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6971 QCBOR_DECODE_MODE_NORMAL);
6972
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006973 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006974 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6975
6976 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006977 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006978 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6979 if(QCBORDecode_GetAndResetError(&DCtx)) {
6980 return 1;
6981 }
6982
6983 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6984 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6985 return 2;
6986 }
6987
6988 uint64_t uInt;
6989 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6990 if(QCBORDecode_GetAndResetError(&DCtx)) {
6991 return 3;
6992 }
6993 if(uInt != 3) {
6994 return 4;
6995 }
6996
6997 double uDouble;
6998 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6999 "label2",
7000 0xff,
7001 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007002#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007003 if(QCBORDecode_GetAndResetError(&DCtx)) {
7004 return 5;
7005 }
7006 if(uDouble != -16777474) {
7007 return 6;
7008 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007009#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007010 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7011 return 7;
7012 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007013#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007014
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007015
7016 QCBORDecode_ExitMap(&DCtx);
7017
7018 if(QCBORDecode_Finish(&DCtx)) {
7019 return 99;
7020 }
7021
7022 return 0;
7023}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007024#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007025
7026
7027
7028int32_t PeekTest()
7029{
7030 QCBORItem Item;
7031 QCBORError nCBORError;
7032 QCBORDecodeContext DCtx;
7033
7034 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7035
7036 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7037 return 100+(int32_t)nCBORError;
7038 }
7039 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7040 return 200;
7041 }
7042
7043 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7044 return (int32_t)nCBORError;
7045 }
7046 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7047 return 300;
7048 }
7049
7050 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7051 return 400 + (int32_t)nCBORError;
7052 }
7053 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7054 return 500;
7055 }
7056
7057 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7058 return (int32_t)nCBORError;
7059 }
7060 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7061 return 600;
7062 }
7063
7064 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7065 return 900 + (int32_t)nCBORError;
7066 }
7067 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7068 Item.uDataType != QCBOR_TYPE_INT64 ||
7069 Item.val.int64 != 42 ||
7070 Item.uDataAlloc ||
7071 Item.uLabelAlloc ||
7072 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7073 return 1000;
7074 }
7075
7076 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7077 return 1100 + (int32_t)nCBORError;
7078 }
7079
7080 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7081 Item.uDataType != QCBOR_TYPE_INT64 ||
7082 Item.val.int64 != 42 ||
7083 Item.uDataAlloc ||
7084 Item.uLabelAlloc ||
7085 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7086 return 1200;
7087 }
7088
7089
7090 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7091 return 1300 + (int32_t)nCBORError;
7092 }
7093 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7094 Item.uDataAlloc ||
7095 Item.uLabelAlloc ||
7096 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7097 Item.uDataType != QCBOR_TYPE_ARRAY ||
7098 Item.val.uCount != 2)
7099 return 1400;
7100
7101 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7102 return 1500 + (int32_t)nCBORError;
7103 }
7104 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7105 Item.uDataAlloc ||
7106 Item.uLabelAlloc ||
7107 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7108 return 1600;
7109 }
7110
7111 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7112 return 1700 + (int32_t)nCBORError;
7113 }
7114 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7115 Item.uDataAlloc ||
7116 Item.uLabelAlloc ||
7117 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7118 return 1800;
7119 }
7120
7121 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7122 return (int32_t)nCBORError;
7123 }
7124 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7125 Item.uDataAlloc ||
7126 Item.uLabelAlloc ||
7127 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7128 return 1900;
7129 }
7130
7131 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7132 return (int32_t)nCBORError;
7133 }
7134 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7135 Item.uDataAlloc ||
7136 Item.uLabelAlloc ||
7137 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7138 return 2000;
7139 }
7140
7141
7142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7143 return 2100 + (int32_t)nCBORError;
7144 }
7145 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7146 Item.uDataAlloc ||
7147 Item.uLabelAlloc ||
7148 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7149 Item.uDataType != QCBOR_TYPE_MAP ||
7150 Item.val.uCount != 4) {
7151 return 2100;
7152 }
7153
7154 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7155 return 2200 + (int32_t)nCBORError;
7156 }
7157 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7158 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7159 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7160 Item.uDataAlloc ||
7161 Item.uLabelAlloc ||
7162 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7163 return 2300;
7164 }
7165
7166 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7167 return 2400 + (int32_t)nCBORError;
7168 }
7169 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7170 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7171 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7172 Item.uDataAlloc ||
7173 Item.uLabelAlloc ||
7174 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7175 return 2500;
7176 }
7177
7178 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7179 return 2600 + (int32_t)nCBORError;
7180 }
7181 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7182 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7183 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7184 Item.uDataAlloc ||
7185 Item.uLabelAlloc ||
7186 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7187 return 2700;
7188 }
7189
7190 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7191 return 2800 + (int32_t)nCBORError;
7192 }
7193 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7194 Item.uDataAlloc ||
7195 Item.uLabelAlloc ||
7196 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7197 Item.uDataType != QCBOR_TYPE_INT64 ||
7198 Item.val.int64 != 98)
7199 return 2900;
7200
7201 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7202 return 3000 + (int32_t)nCBORError;
7203 }
7204 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7205 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7206 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7207 Item.uDataAlloc ||
7208 Item.uLabelAlloc ||
7209 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7210 return 3100;
7211 }
7212
7213 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7214 return 3200 + (int32_t)nCBORError;
7215 }
7216 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7217 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7218 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7219 Item.uDataAlloc ||
7220 Item.uLabelAlloc ||
7221 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7222 return 3300;
7223 }
7224
7225 return 0;
7226}