blob: acde3ca54543e97f691283944948f0ce56a820c1 [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/* [[[[[[[[[[]]]]]]]]]] */
922static const uint8_t spDeepArrays[] = {
923 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
924 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800925
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800926int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800927{
928 QCBORDecodeContext DCtx;
929 int nReturn = 0;
930 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800931
Laurence Lundbladeee851742020-01-08 08:37:05 -0800932 QCBORDecode_Init(&DCtx,
933 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
934 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800935
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800936 for(i = 0; i < 10; i++) {
937 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800938
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800939 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
940 Item.uDataType != QCBOR_TYPE_ARRAY ||
941 Item.uNestingLevel != i) {
942 nReturn = -1;
943 break;
944 }
945 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800946
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800947 return(nReturn);
948}
949
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800950/* Big enough to test nesting to the depth of 24
951 [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]
952 */
953static const uint8_t spTooDeepArrays[] = {
954 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
955 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
956 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
957 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800959int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960{
961 QCBORDecodeContext DCtx;
962 int nReturn = 0;
963 int i;
964 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800965
966
Laurence Lundbladeee851742020-01-08 08:37:05 -0800967 QCBORDecode_Init(&DCtx,
968 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
969 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800970
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700971 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800972
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800973 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
974 Item.uDataType != QCBOR_TYPE_ARRAY ||
975 Item.uNestingLevel != i) {
976 nReturn = -1;
977 break;
978 }
979 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800980
Laurence Lundbladea9489f82020-09-12 13:50:56 -0700981 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800982 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800983
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800984 return(nReturn);
985}
986
987
988
989
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800990int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800991{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700992 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800993
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800994 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700995 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800996
Laurence Lundbladeee851742020-01-08 08:37:05 -0800997 QCBORDecode_Init(&DCtx,
998 (UsefulBufC){spExpectedEncodedInts, nNum},
999 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001000
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001001 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001002
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001003 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001004 nResult = -1;
1005 goto Done;
1006 }
1007 }
1008Done:
1009 return nResult;
1010}
1011
1012
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001013
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001014int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001015{
1016 uint8_t *pEncoded;
1017 int nReturn;
1018 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001019
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001020 int64_t i1, i2;
1021 size_t i3, i4;
1022 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001023
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001024 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001025
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001026 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
1027 return(-1);
1028 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001029
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001030 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07001031 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
1032 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001033 if(nResult == 0) {
1034 nReturn = -1;
1035 }
1036 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001037
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038 return(nReturn);
1039}
1040
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301041/*
1042 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001043 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1044 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301045 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001046static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001047{
1048 QCBORDecodeContext DCtx;
1049 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001050 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001051
Laurence Lundbladeee851742020-01-08 08:37:05 -08001052 QCBORDecode_Init(&DCtx,
1053 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1054 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001055
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001056 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001057 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001058 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001059 if(Item.uDataType != QCBOR_TYPE_MAP ||
1060 Item.val.uCount != 3)
1061 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001062
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001063 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001064 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001065 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001066
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001067 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001068 Item.uDataType != QCBOR_TYPE_INT64 ||
1069 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301070 Item.uDataAlloc ||
1071 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001072 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001073 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001074 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001075
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001077 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001078 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301080 Item.uDataAlloc ||
1081 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001082 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001083 Item.uDataType != QCBOR_TYPE_ARRAY ||
1084 Item.val.uCount != 2)
1085 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001086
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001087 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001088 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001089 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001090 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301091 Item.uDataAlloc ||
1092 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001093 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001094 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001096
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001097 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001098 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001099 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001100 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301101 Item.uDataAlloc ||
1102 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001103 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001104 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001105 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001106
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001107 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001108 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001109 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001110 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301111 Item.uDataAlloc ||
1112 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001113 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001114 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001115 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001116 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001118
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001120 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001121 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001122 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001123 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001124 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301125 Item.uDataAlloc ||
1126 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001127 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001128 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001129 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001130
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001131 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001132 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001134 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001135 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001136 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301137 Item.uDataAlloc ||
1138 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001139 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001140 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001142
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001143 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001144 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001145 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001146 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301147 Item.uDataAlloc ||
1148 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001149 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001150 Item.uDataType != QCBOR_TYPE_INT64 ||
1151 Item.val.int64 != 98)
1152 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001153
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001154 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001155 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001156 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001157 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001158 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001159 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301160 Item.uDataAlloc ||
1161 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001162 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001163 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001164 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001165
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001166 return 0;
1167}
1168
1169
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001170/*
1171 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001172 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001173 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001174int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175{
1176 QCBORDecodeContext DCtx;
1177 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001178 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001179
Laurence Lundbladeee851742020-01-08 08:37:05 -08001180 QCBORDecode_Init(&DCtx,
1181 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1182 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001183
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001184 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001185 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001186 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001187 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1188 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 return -1;
1190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001191
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001192 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001193 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001194 }
1195 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1196 Item.uDataAlloc ||
1197 Item.uLabelAlloc ||
1198 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001199 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 return -2;
1201 }
1202
1203 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001204 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001205 }
1206 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1207 Item.uDataType != QCBOR_TYPE_INT64 ||
1208 Item.val.int64 != 42 ||
1209 Item.uDataAlloc ||
1210 Item.uLabelAlloc) {
1211 return -3;
1212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001213
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001215 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 }
1217 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1218 Item.uDataAlloc ||
1219 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001220 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001221 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1222 return -4;
1223 }
1224
1225 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001226 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001227 }
1228 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1229 Item.uDataAlloc ||
1230 Item.uLabelAlloc ||
1231 Item.uDataType != QCBOR_TYPE_ARRAY ||
1232 Item.val.uCount != 2) {
1233 return -5;
1234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001235
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001236 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001237 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001238 }
1239 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1240 Item.val.string.len != 7 ||
1241 Item.uDataAlloc ||
1242 Item.uLabelAlloc ||
1243 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1244 return -6;
1245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001246
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001247 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001248 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001249 }
1250 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1251 Item.uDataAlloc ||
1252 Item.uLabelAlloc ||
1253 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1254 return -7;
1255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001256
1257
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001259 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 }
1261 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1262 Item.uDataAlloc ||
1263 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001264 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001265 return -8;
1266 }
1267
1268 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001269 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001270 }
1271 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1272 Item.uDataAlloc ||
1273 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001274 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1275 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001276 return -9;
1277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001278
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001279 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001280 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001281 }
1282 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001283 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001284 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1285 Item.uDataAlloc ||
1286 Item.uLabelAlloc) {
1287 return -10;
1288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001289
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001290 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001291 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001292 }
1293 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1294 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1295 Item.uDataAlloc ||
1296 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001297 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001298 return -11;
1299 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001300
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001301 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001302 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001303 }
1304 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001305 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001306 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1307 Item.uDataAlloc ||
1308 Item.uLabelAlloc) {
1309 return -12;
1310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001311
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001312 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001313 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001314 }
1315 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1316 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1317 Item.uDataAlloc ||
1318 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001319 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001320 return -13;
1321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001323 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001324 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001325 }
1326 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1327 Item.uDataAlloc ||
1328 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001329 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001330 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1331 return -14;
1332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001333
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001334 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001335 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001336 }
1337 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1338 Item.uDataAlloc ||
1339 Item.uLabelAlloc ||
1340 Item.uDataType != QCBOR_TYPE_INT64 ||
1341 Item.val.int64 != 98) {
1342 return -15;
1343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001344
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001345 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001346 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 }
1348 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001349 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001350 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1351 Item.uDataAlloc ||
1352 Item.uLabelAlloc) {
1353 return -16;
1354 }
1355
1356 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001357 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001358 }
1359 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1360 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1361 Item.uDataAlloc ||
1362 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001363 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001364 return -17;
1365 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001366
1367
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001368 /*
1369 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1370 map that when interpreted as an array will be too many. Test
1371 data just has the start of the map, not all the items in the map.
1372 */
1373 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001374
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001375 QCBORDecode_Init(&DCtx,
1376 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1377 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001378
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001379 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001380 return -50;
1381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001382
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001383 // TODO: test decoding of labels that are arrays or such
1384 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1385
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 return 0;
1387}
1388
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001389
1390/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301391 Fully or partially decode pValidMapEncoded. When
1392 partially decoding check for the right error code.
1393 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001394
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301395 The partial decodes test error conditions of
1396 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001397
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301398 This could be combined with the above test
1399 and made prettier and maybe a little more
1400 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001402static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403{
1404 QCBORDecodeContext DCtx;
1405 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001406 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001407
Laurence Lundbladeee851742020-01-08 08:37:05 -08001408 QCBORDecode_Init(&DCtx,
1409 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1410 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001411
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001412 if(nLevel < 1) {
1413 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1414 return -1;
1415 } else {
1416 return 0;
1417 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301419
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001420
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001421 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001422 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001423 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001424 if(Item.uDataType != QCBOR_TYPE_MAP ||
1425 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001426 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001427
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001428 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001429 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1430 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001431 } else {
1432 return 0;
1433 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001434 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001435
1436
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001437 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001438 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001439 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001441 Item.uDataType != QCBOR_TYPE_INT64 ||
1442 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001443 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001444 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001446
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001447 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001448 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1449 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001450 } else {
1451 return 0;
1452 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001453 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001454
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001455 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001456 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001457 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001458 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001459 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001460 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001461 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001462 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001464
1465
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001466 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001467 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1468 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001469 } else {
1470 return 0;
1471 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001472 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001473
1474
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001475 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001476 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001477 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001478 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001479 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001480 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001481 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001482
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001483 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001484 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1485 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001486 } else {
1487 return 0;
1488 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001489 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001490
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001491 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001492 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001493 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001494 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001495 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001496 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001497 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001498
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001499 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001500 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1501 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001502 } else {
1503 return 0;
1504 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001505 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001506
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001507 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001508 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001509 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001511 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001512 Item.uDataType != QCBOR_TYPE_MAP ||
1513 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001514 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001515
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001516 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001517 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1518 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001519 } else {
1520 return 0;
1521 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001522 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001523
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001524 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001525 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001526 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001527 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001528 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001529 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001530 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001531 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001533
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001534 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001535 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1536 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001537 } else {
1538 return 0;
1539 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001540 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001541
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001542 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001543 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001544 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001545 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001546 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001547 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001548 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001549 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001550 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001551
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001552 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001553 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1554 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001555 } else {
1556 return 0;
1557 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001558 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001559
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001560 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001561 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001562 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001563 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001564 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001565 Item.uDataType != QCBOR_TYPE_INT64 ||
1566 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001567 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001568
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001569 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001570 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1571 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001572 } else {
1573 return 0;
1574 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001576
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001577 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001578 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001579 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001580 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001581 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001583 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001584 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001585 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001586
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301587 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001588 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001591 return 0;
1592}
1593
1594
1595
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001596
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001597int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001598{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001599 // Parse a moderatly complex map structure very thoroughly
1600 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1601 if(nResult) {
1602 return nResult;
1603 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001604
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001605 // Again, but in strings-only mode. It should succeed since the input
1606 // map has only string labels.
1607 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1608 if(nResult) {
1609 return nResult;
1610 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001611
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001612 // Again, but try to finish the decoding before the end of the
1613 // input at 10 different place and see that the right error code
1614 // is returned.
1615 for(int i = 0; i < 10; i++) {
1616 nResult = ExtraBytesTest(i);
1617 if(nResult) {
1618 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001619 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001620 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001621
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001622 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001623}
1624
1625
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001626/* The simple-values including some not well formed */
1627static const uint8_t spSimpleValues[] = {
1628 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
1629 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
1630 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001631
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001632int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001633{
1634 QCBORDecodeContext DCtx;
1635 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001636 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001637
1638
Laurence Lundbladeee851742020-01-08 08:37:05 -08001639 QCBORDecode_Init(&DCtx,
1640 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1641 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001642
1643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001644 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001645 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001646 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1647 Item.val.uCount != 10)
1648 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001649
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001650 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001651 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001652 if(Item.uDataType != QCBOR_TYPE_FALSE)
1653 return -1;
1654
1655 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001656 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001657 if(Item.uDataType != QCBOR_TYPE_TRUE)
1658 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001659
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001660 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001661 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001662 if(Item.uDataType != QCBOR_TYPE_NULL)
1663 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001664
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001665 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001666 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001667 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1668 return -1;
1669
1670 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001671 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001672 return -1;
1673
1674 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001675 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001676 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1677 return -1;
1678
1679 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001680 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001681 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1682 return -1;
1683
Laurence Lundblade077475f2019-04-26 09:06:33 -07001684 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001685 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001686
Laurence Lundblade077475f2019-04-26 09:06:33 -07001687 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001688 return -1;
1689
Laurence Lundblade077475f2019-04-26 09:06:33 -07001690 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001691 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001693 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001694 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001695 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1696 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001697
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001698 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001699 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001700 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1701 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001702
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001703 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001704
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001705}
1706
1707
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001708int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001709{
1710 // Loop over all the not-well-formed instance of CBOR
1711 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001712 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
1713 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001714 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1715 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1716 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1717
Laurence Lundbladeee851742020-01-08 08:37:05 -08001718 // Set up decoder context. String allocator needed for indefinite
1719 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001720 QCBORDecodeContext DCtx;
1721 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001722#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001723 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1724 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001725#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001726
1727 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001728 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001729 do {
1730 QCBORItem Item;
1731
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001732 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1733 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001734
1735 // Every test vector must fail with
1736 // a not-well-formed error. If not
1737 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001738 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001739 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001740 /* Return index of failure and QCBOR error in the result */
1741 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001742 }
1743 }
1744 return 0;
1745}
1746
1747
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001748// 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 +08001749struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001750 UsefulBufC Input;
1751 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001752};
1753
Laurence Lundblade59289e52019-12-30 13:44:37 -08001754
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001755static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001756{
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001757 for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001758 QCBORDecodeContext DCtx;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001759 QCBORError uCBORError;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001760
Laurence Lundblade59289e52019-12-30 13:44:37 -08001761 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001762
1763#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001764 // Set up the decoding context including a memory pool so that
1765 // indefinite length items can be checked
Laurence Lundblade59289e52019-12-30 13:44:37 -08001766 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001767
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001768 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1769 if(uCBORError) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001770 return -9;
1771 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001772#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1773
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001774
Laurence Lundblade59289e52019-12-30 13:44:37 -08001775 // Iterate until there is an error of some sort error
1776 QCBORItem Item;
1777 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001778 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001779 memset(&Item, 0x33, sizeof(Item));
1780
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001781 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1782 } while(uCBORError == QCBOR_SUCCESS);
1783
1784
Laurence Lundblade59289e52019-12-30 13:44:37 -08001785
1786 // Must get the expected error or the this test fails
1787 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001788 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001789 Item.uDataType != QCBOR_TYPE_NONE ||
1790 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001791 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001792 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001793 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001794 }
1795 }
1796
1797 return 0;
1798}
1799
1800
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001801static const struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001802 // Most of this is copied from not_well_formed.h. Here the error code
1803 // returned is also checked.
1804
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001805#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001806 // Indefinite length strings must be closed off
1807 // An indefinite length byte string not closed off
1808 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1809 // An indefinite length text string not closed off
1810 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1811
1812
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001813 // All the chunks in an indefinite length string must be of the type of
1814 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001815 // indefinite length byte string with text string chunk
1816 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1817 // indefinite length text string with a byte string chunk
1818 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1819 // indefinite length byte string with an positive integer chunk
1820 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1821 // indefinite length byte string with an negative integer chunk
1822 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1823 // indefinite length byte string with an array chunk
1824 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1825 // indefinite length byte string with an map chunk
1826 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1827 // indefinite length byte string with tagged integer chunk
1828 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1829 // indefinite length byte string with an simple type chunk
1830 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1831 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1832 // indefinite length text string with indefinite string inside
1833 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1834
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001835#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1836
1837 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1838 // An indefinite length text string not closed off
1839 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1840
1841
1842 // All the chunks in an indefinite length string must be of the type of
1843 // indefinite length string
1844 // indefinite length byte string with text string chunk
1845 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1846 // indefinite length text string with a byte string chunk
1847 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1848 // indefinite length byte string with an positive integer chunk
1849 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1850 // indefinite length byte string with an negative integer chunk
1851 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1852 // indefinite length byte string with an array chunk
1853 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1854 // indefinite length byte string with an map chunk
1855 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1856 // indefinite length byte string with tagged integer chunk
1857 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1858 // indefinite length byte string with an simple type chunk
1859 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1860 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1861 // indefinite length text string with indefinite string inside
1862 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1863#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1864
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001865
1866 // Definte length maps and arrays must be closed by having the right number of items
1867 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001868 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001869 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001870 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001871 // A definte length array that is supposed to have 511 items, but has only 1
1872 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1873 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001874 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001875 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001876 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001877
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001878#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001879 // Indefinte length maps and arrays must be ended by a break
1880 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001881 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001882 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001883 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001884 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001885 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001886 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001887 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001888
1889
1890 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001891 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001892 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001893 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001894 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001895 // Unclosed indefinite map containing a closed definite length array
1896 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1897 // Definite length map containing an unclosed indefinite length array
1898 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001899 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001900 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001901 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001902 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001903 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001904 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001905 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001906 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001907 // Unclosed indefinite length map in definite length maps
1908 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1909 QCBOR_ERR_NO_MORE_ITEMS},
1910 // Unclosed definite length map in indefinite length maps
1911 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1912 // Unclosed indefinite length array in definite length maps
1913 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1914 QCBOR_ERR_NO_MORE_ITEMS},
1915 // Unclosed definite length array in indefinite length maps
1916 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1917 // Unclosed indefinite length map in definite length arrays
1918 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1919 // Unclosed definite length map in indefinite length arrays
1920 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001921#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001922
1923 // The "argument" for the data item is incomplete
1924 // Positive integer missing 1 byte argument
1925 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1926 // Positive integer missing 2 byte argument
1927 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1928 // Positive integer missing 4 byte argument
1929 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1930 // Positive integer missing 8 byte argument
1931 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1932 // Positive integer missing 1 byte of 2 byte argument
1933 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1934 // Positive integer missing 2 bytes of 4 byte argument
1935 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1936 // Positive integer missing 1 bytes of 7 byte argument
1937 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1938 // Negative integer missing 1 byte argument
1939 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1940 // Binary string missing 1 byte argument
1941 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1942 // Text string missing 1 byte argument
1943 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1944 // Array missing 1 byte argument
1945 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1946 // Map missing 1 byte argument
1947 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1948 // Tag missing 1 byte argument
1949 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1950 // Simple missing 1 byte argument
1951 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001952 // half-precision with 1 byte argument
1953 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1954 // single-precision with 2 byte argument
1955 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1956 // double-precision with 3 byte argument
1957 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1958
1959
1960 // Tag with no content
1961 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001962
1963
1964 // Breaks must not occur in definite length arrays and maps
1965 // Array of length 1 with sole member replaced by a break
1966 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1967 // Array of length 2 with 2nd member replaced by a break
1968 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1969 // Map of length 1 with sole member label replaced by a break
1970 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1971 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001972 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001973 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1974 // Array of length 1 with 2nd member value replaced by a break
1975 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1976 // Map of length 2 with 2nd member replaced by a break
1977 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1978
1979
1980 // Breaks must not occur on their own out of an indefinite length data item
1981 // A bare break is not well formed
1982 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1983 // A bare break after a zero length definite length array
1984 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001985#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001986 // A bare break after a zero length indefinite length map
1987 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001988 // A break inside a definite length array inside an indefenite length array
1989 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1990 // Complicated mixed nesting with break outside indefinite length array
1991 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001992#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001993
1994
1995 // Forbidden two byte encodings of simple types
1996 // Must use 0xe0 instead
1997 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1998 // Should use 0xe1 instead
1999 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2000 // Should use 0xe2 instead
2001 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2002 // Should use 0xe3 instead
2003 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2004 // Should use 0xe4 instead
2005 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2006 // Should use 0xe5 instead
2007 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2008 // Should use 0xe6 instead
2009 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2010 // Should use 0xe7 instead
2011 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2012 // Should use 0xe8 instead
2013 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2014 // Should use 0xe9 instead
2015 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2016 // Should use 0xea instead
2017 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2018 // Should use 0xeb instead
2019 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2020 // Should use 0xec instead
2021 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2022 // Should use 0xed instead
2023 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2024 // Should use 0xee instead
2025 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2026 // Should use 0xef instead
2027 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2028 // Should use 0xf0 instead
2029 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2030 // Should use 0xf1 instead
2031 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2032 // Should use 0xf2 instead
2033 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2034 // Must use 0xf3 instead
2035 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2036 // Must use 0xf4 instead
2037 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2038 // Must use 0xf5 instead
2039 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2040 // Must use 0xf6 instead
2041 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2042 // Must use 0xf7 instead
2043 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2044 // Must use 0xf8 instead
2045 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002046 // Reserved
2047 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002048
2049 // Integers with additional info indefinite length
2050 // Positive integer with additional info indefinite length
2051 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
2052 // Negative integer with additional info indefinite length
2053 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
2054 // CBOR tag with "argument" an indefinite length
2055 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
2056 // CBOR tag with "argument" an indefinite length alternate vector
2057 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
2058
2059
2060 // Missing bytes from a deterministic length string
2061 // A byte string is of length 1 without the 1 byte
2062 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
2063 // A text string is of length 1 without the 1 byte
2064 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002065
2066#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08002067 // Byte string should have 2^32-15 bytes, but has one
2068 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2069 // Byte string should have 2^32-15 bytes, but has one
2070 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002071 // Byte string should have 2^64 bytes, but has 3
2072 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2073 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2074 // Text string should have 2^64 bytes, but has 3
2075 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2076 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002077#else
2078 // Byte string should have 2^32-15 bytes, but has one
2079 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2080 // Byte string should have 2^32-15 bytes, but has one
2081 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2082 // Byte string should have 2^16 bytes, but has 3
2083 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2084 // Text string should have 2^64 bytes, but has 3
2085 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2086#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002087
2088 // Use of unassigned additional information values
2089 // Major type positive integer with reserved value 28
2090 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2091 // Major type positive integer with reserved value 29
2092 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2093 // Major type positive integer with reserved value 30
2094 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2095 // Major type negative integer with reserved value 28
2096 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2097 // Major type negative integer with reserved value 29
2098 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2099 // Major type negative integer with reserved value 30
2100 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2101 // Major type byte string with reserved value 28 length
2102 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2103 // Major type byte string with reserved value 29 length
2104 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2105 // Major type byte string with reserved value 30 length
2106 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2107 // Major type text string with reserved value 28 length
2108 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2109 // Major type text string with reserved value 29 length
2110 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2111 // Major type text string with reserved value 30 length
2112 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2113 // Major type array with reserved value 28 length
2114 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2115 // Major type array with reserved value 29 length
2116 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2117 // Major type array with reserved value 30 length
2118 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2119 // Major type map with reserved value 28 length
2120 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2121 // Major type map with reserved value 29 length
2122 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2123 // Major type map with reserved value 30 length
2124 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2125 // Major type tag with reserved value 28 length
2126 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2127 // Major type tag with reserved value 29 length
2128 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2129 // Major type tag with reserved value 30 length
2130 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2131 // Major type simple with reserved value 28 length
2132 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2133 // Major type simple with reserved value 29 length
2134 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2135 // Major type simple with reserved value 30 length
2136 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2137
2138
2139 // Maps must have an even number of data items (key & value)
2140 // Map with 1 item when it should have 2
2141 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2142 // Map with 3 item when it should have 4
2143 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002144#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002145 // Map with 1 item when it should have 2
2146 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2147 // Map with 3 item when it should have 4
2148 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002149#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002150
2151
2152 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002153 // Text-based date, with an integer
2154 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2155 // Epoch date, with an byte string
2156 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2157 // tagged as both epoch and string dates
2158 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2159 // big num tagged an int, not a byte string
2160 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161};
2162
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002163int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002165 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002166
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002167 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002168 if(nResult) {
2169 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002171
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002172 // Corrupt the UsefulInputBuf and see that
2173 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002174 QCBORDecodeContext DCtx;
2175 QCBORItem Item;
2176 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002177
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002178 QCBORDecode_Init(&DCtx,
2179 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2180 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002181
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002182 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2183 return (int32_t)uQCBORError;
2184 }
2185 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2186 // This wasn't supposed to happen
2187 return -1;
2188 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002189
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002190 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002191
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002192 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2193 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2194 // Did not get back the error expected
2195 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002196 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002197
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002198
Laurence Lundblade98427e92020-09-28 21:33:23 -07002199 /*
2200 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2201 tests here can be performed to see that the max length
2202 error check works correctly. See DecodeBytes(). If the max
2203 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002204
Laurence Lundblade98427e92020-09-28 21:33:23 -07002205 This test will automatocally adapt the all CPU sizes
2206 through the use of SIZE_MAX.
2207 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002208
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002209 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002210 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002211
Laurence Lundblade98427e92020-09-28 21:33:23 -07002212 // This makes a CBOR head with a text string that is very long
2213 // but doesn't fill in the bytes of the text string as that is
2214 // not needed to test this part of QCBOR.
2215 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2216
2217 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2218
2219 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2220 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002221 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002222
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002223 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002224}
2225
2226
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002227/* Try all 256 values of the byte at nLen including recursing for
2228 each of the values to try values at nLen+1 ... up to nLenMax
2229 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002230static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002231{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002232 if(nLen >= nLenMax) {
2233 return;
2234 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002235
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002236 for(int inputByte = 0; inputByte < 256; inputByte++) {
2237 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002238 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002239 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002240
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002241 // Get ready to parse
2242 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002243 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002244
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002245 // Parse by getting the next item until an error occurs
2246 // Just about every possible decoder error can occur here
2247 // The goal of this test is not to check for the correct
2248 // error since that is not really possible. It is to
2249 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002250 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002251 QCBORItem Item;
2252 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002253 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002254 break;
2255 }
2256 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002257
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002258 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002259 }
2260}
2261
2262
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002263int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002264{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002265 // Size 2 tests 64K inputs and runs quickly
2266 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002267
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002268 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002269
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002270 return 0;
2271}
2272
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002273
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002274int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002275{
2276 // size 3 tests 16 million inputs and runs OK
2277 // in seconds on fast machines. Size 4 takes
2278 // 10+ minutes and 5 half a day on fast
2279 // machines. This test is kept separate from
2280 // the others so as to no slow down the use
2281 // of them as a very frequent regression.
2282 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002283
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002284 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002285
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002286 return 0;
2287}
2288
2289
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002290static const uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002291 0xc0, // tag for string date
2292 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002293
Laurence Lundbladec7114722020-08-13 05:11:40 -07002294 0xc0, // tag for string date
2295 0x00, // Wrong type for a string date
2296
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002297 0xc1, // tag for epoch date
2298 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2299
Laurence Lundbladec7114722020-08-13 05:11:40 -07002300 0xc1,
2301 0x62, 'h', 'i', // wrong type tagged
2302
Laurence Lundblade99615302020-11-29 11:19:47 -08002303 // CBOR_TAG_ENC_AS_B64
2304 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002305 0x1a, 0x53, 0x72, 0x4E, 0x01,
2306
2307 0xc1, // tag for epoch date
2308 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002310 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002311 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002312
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002313 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002314 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002315
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002316 0xc1, // tag for epoch date
2317 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2318 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2319
2320 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002321 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2322
2323 0xc1, // tag for epoch date
2324 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2325
2326 0xc1,
2327 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2328
2329 0xc1, // tag for epoch date
2330 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002331};
2332
2333
Laurence Lundbladec7114722020-08-13 05:11:40 -07002334
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002335// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002336#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002337static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002338
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002339 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002341 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002343 return diff > 0.0000001;
2344}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002345#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002346
2347
Laurence Lundblade99615302020-11-29 11:19:47 -08002348
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002349int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002350{
2351 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002352 QCBORItem Item;
2353 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002354
Laurence Lundbladeee851742020-01-08 08:37:05 -08002355 QCBORDecode_Init(&DCtx,
2356 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2357 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002359 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002360 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002361 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002362 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002363 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002364 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002365 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002366 }
2367
Laurence Lundbladec7114722020-08-13 05:11:40 -07002368 // Wrong type for a string date
2369 uError = QCBORDecode_GetNext(&DCtx, &Item);
2370 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002371 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002372 }
2373
2374 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2375 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2376 return -4;
2377 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002378 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2379 Item.val.epochDate.nSeconds != 1400000000 ||
2380 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002381 return -5;
2382 }
2383
2384 // Wrong type for an epoch date
2385 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2386 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002388
Laurence Lundblade99615302020-11-29 11:19:47 -08002389 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2390 // The date is decoded and the two tags are returned. This is to
2391 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002392 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2393 return -7;
2394 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002395 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2396 Item.val.epochDate.nSeconds != 1400000001 ||
2397 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundblade99615302020-11-29 11:19:47 -08002398 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002399 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002401
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002402 // Epoch date that is too large for our representation
2403 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002404 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002405 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002406
Laurence Lundblade9682a532020-06-06 18:33:04 -07002407#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002408 // Epoch date in float format with fractional seconds
2409 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2410 return -10;
2411 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002412 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2413 Item.val.epochDate.nSeconds != 1 ||
2414 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002415 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002418 // Epoch date float that is too large for our representation
2419 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002420 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002422
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002423 // Epoch date double that is just slightly too large
2424 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002425 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002426 }
2427
2428 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002429 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2430 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2431 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2432 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002433 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002434 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002435
2436 // Nan
2437 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2438 return -15;
2439 }
2440
2441 // +Inifinity double-precision
2442 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2443 return -16;
2444 }
2445
2446#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2447 // -Inifinity half-precision
2448 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2449 return -17;
2450 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002451#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002452 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2453 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002454 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002455#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002456
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002457#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002458 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2459 return -19;
2460 }
2461 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2462 return -20;
2463 }
2464 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2465 return -21;
2466 }
2467 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2468 return -22;
2469 }
2470 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2471 return -23;
2472 }
2473 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2474 return -24;
2475 }
2476#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2477 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2478 return -25;
2479 }
2480#else
2481 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2482 return -26;
2483 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002484#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002485
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002486#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002487
2488 return 0;
2489}
2490
Laurence Lundblade4b270642020-08-14 12:53:07 -07002491/*
2492 Test cases covered here. Some items cover more than one of these.
2493 positive integer (zero counts as a positive integer)
2494 negative integer
2495 half-precision float
2496 single-precision float
2497 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002498
Laurence Lundblade4b270642020-08-14 12:53:07 -07002499 float Overflow error
2500 Wrong type error for epoch
2501 Wrong type error for date string
2502 float disabled error
2503 half-precision disabled error
2504 -Infinity
2505 Slightly too large integer
2506 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002507
Laurence Lundblade4b270642020-08-14 12:53:07 -07002508 Get epoch by int
2509 Get string by int
2510 Get epoch by string
2511 Get string by string
2512 Fail to get epoch by wrong int label
2513 Fail to get string by wrong string label
2514 Fail to get epoch by string because it is invalid
2515 Fail to get epoch by int because it is invalid
2516
2517 Untagged values
2518 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002519static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002520 0x86,
2521
2522 0xc1,
2523 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2524
Laurence Lundbladec7114722020-08-13 05:11:40 -07002525 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002526 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2527
2528 0xc1, // tag for epoch date
2529 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2530
2531 0xc1, // tag for epoch date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002532 0x80, // Erroneous empty array as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002533
2534 0xc0, // tag for string date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002535 0xa0, // Erroneous empty map as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002536
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002537 0xa9, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002538
2539 0x00,
2540 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002541 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002542
2543 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002544 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002545 0xc1, // tag for epoch date
2546 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2547
2548 // Untagged integer 0
2549 0x08,
2550 0x00,
2551
2552 // Utagged date string with string label y
2553 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002554 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002555
2556 // Untagged -1000 with label z
2557 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002558 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002559 0x39, 0x03, 0xe7,
2560
Laurence Lundbladec7114722020-08-13 05:11:40 -07002561 0x07,
2562 0xc1, // tag for epoch date
2563 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2564
Laurence Lundblade4b270642020-08-14 12:53:07 -07002565 0x05,
2566 0xc1,
2567 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2568
Laurence Lundbladec7114722020-08-13 05:11:40 -07002569 // Untagged single-precision float with value 3.14 with string label x
2570 0x61, 0x78,
2571 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2572
Laurence Lundbladec7114722020-08-13 05:11:40 -07002573 // Untagged half-precision float with value -2
2574 0x09,
2575 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002576};
2577
2578int32_t SpiffyDateDecodeTest()
2579{
2580 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002581 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002582 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2583 nEpochDate4, nEpochDate6, nEpochDateFail,
2584 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002585 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002586 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002587
2588 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002589 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002590 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002591 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002592
Laurence Lundblade9b334962020-08-27 10:55:53 -07002593 // Too-negative float, -9.2233720368547748E+18
2594 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002595 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002596#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002597 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2598 return 1111;
2599 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002600#else
2601 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2602 return 1112;
2603 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002604#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade4b270642020-08-14 12:53:07 -07002605
2606 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002607 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002608 uError = QCBORDecode_GetAndResetError(&DC);
2609 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002610 return 1;
2611 }
2612
Laurence Lundblade4b270642020-08-14 12:53:07 -07002613 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002614 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002615 uError = QCBORDecode_GetAndResetError(&DC);
2616#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2617#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2618 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2619#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2620 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2621#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2622#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2623 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2624#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2625 if(uError != uExpectedforHalfMinusInfinity) {
2626 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002627 }
2628
Laurence Lundblade4b270642020-08-14 12:53:07 -07002629 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002630 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002631 uError = QCBORDecode_GetAndResetError(&DC);
2632 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2633 return 3;
2634 }
2635
2636 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002637 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002638 uError = QCBORDecode_GetAndResetError(&DC);
2639 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2640 return 4;
2641 }
2642
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002643 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002644
2645 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002646 QCBORDecode_GetEpochDateInMapN(&DC,
2647 5,
2648 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2649 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2650 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002651#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2652 if(nEpochDate2 != -9223372036854773760LL) {
2653 return 101;
2654 }
2655#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2656 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002657 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002658 return 102;
2659 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002660#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002661
Laurence Lundblade4b270642020-08-14 12:53:07 -07002662 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002663 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2664 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002665#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2666 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002667 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002668 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002669#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2670 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002671 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002672 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002673 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002674#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2675
2676 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002677 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2678 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002679#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2680 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002681 return 103;
2682 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002683#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2684 uError = QCBORDecode_GetAndResetError(&DC);
2685 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2686 return 104;
2687 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002688#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2689
Laurence Lundblade9b334962020-08-27 10:55:53 -07002690 // A half-precision date with value -2 FFF
2691 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2692 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002693#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2694 if(nEpochDate4 != -2) {
2695 return 105;
2696 }
2697#else
2698 uError = QCBORDecode_GetAndResetError(&DC);
2699 if(uError == QCBOR_SUCCESS) {
2700 return 106;
2701 }
2702#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002703
Laurence Lundblade4b270642020-08-14 12:53:07 -07002704
2705 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002706 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2707 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2708 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002709 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002710 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002711 return 107;
2712 }
2713
2714 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002715 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2716 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002717 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002718 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002719 return 108;
2720 }
2721
2722 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002723 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2724 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2725 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002726 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002727 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002728 return 109;
2729 }
2730
2731 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002732 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2733 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002734 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002735 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002736 return 110;
2737 }
2738
2739 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002740
Laurence Lundblade4b270642020-08-14 12:53:07 -07002741 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002742 QCBORDecode_GetEpochDateInMapN(&DC,
2743 1,
2744 QCBOR_TAG_REQUIREMENT_TAG |
2745 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2746 &nEpochDate1400000000);
2747 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002748 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002749 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2750 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002751 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002752 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2753 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002754 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002755 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2756 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002757 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002758 QCBORDecode_GetEpochDateInMapSZ(&DC,
2759 "z",
2760 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2761 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2762 &nEpochDate6);
2763 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002764
2765 QCBORDecode_ExitMap(&DC);
2766 QCBORDecode_ExitArray(&DC);
2767 uError = QCBORDecode_Finish(&DC);
2768 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002769 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002770 }
2771
Laurence Lundblade9b334962020-08-27 10:55:53 -07002772 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002773 return 200;
2774 }
2775
Laurence Lundblade9b334962020-08-27 10:55:53 -07002776 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002777 return 201;
2778 }
2779
Laurence Lundblade9b334962020-08-27 10:55:53 -07002780 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002781 return 202;
2782 }
2783
Laurence Lundblade9b334962020-08-27 10:55:53 -07002784 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002785 return 203;
2786 }
2787
Laurence Lundblade9b334962020-08-27 10:55:53 -07002788 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002789 return 204;
2790 }
2791
Laurence Lundblade9b334962020-08-27 10:55:53 -07002792 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2793 return 205;
2794 }
2795
2796 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2797 return 206;
2798 }
2799
Laurence Lundbladec7114722020-08-13 05:11:40 -07002800 return 0;
2801}
2802
2803
2804
Laurence Lundblade9b334962020-08-27 10:55:53 -07002805// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002806static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002807 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002808 0x81, // Array of one
2809 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2810 0x82, // Array of two that is the faction 1/3
2811 0x01,
2812 0x03,
2813
2814 /*
2815 More than 4 tags on an item 225(226(227(228(229([])))))
2816 */
2817 0xd8, 0xe1,
2818 0xd8, 0xe2,
2819 0xd8, 0xe3,
2820 0xd8, 0xe4,
2821 0xd8, 0xe5,
2822 0x80,
2823
2824 /* tag 10489608748473423768(
2825 2442302356(
2826 21590(
2827 240(
2828 []))))
2829 */
2830 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2831 0xda, 0x91, 0x92, 0x93, 0x94,
2832 0xd9, 0x54, 0x56,
2833 0xd8, 0xf0,
2834 0x80,
2835
2836 /* tag 21590(
2837 10489608748473423768(
2838 2442302357(
2839 65534(
2840 []))))
2841 */
2842 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2843 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2844 0xda, 0x91, 0x92, 0x93, 0x95,
2845 0xd9, 0xff, 0xfe,
2846 0x80,
2847
2848 /* Make sure to blow past the limit of tags that must be mapped.
2849 works in conjuntion with entries above.
2850 269488144(269488145(269488146(269488147([]))))
2851 */
2852 0xda, 0x10, 0x10, 0x10, 0x10,
2853 0xda, 0x10, 0x10, 0x10, 0x11,
2854 0xda, 0x10, 0x10, 0x10, 0x12,
2855 0xda, 0x10, 0x10, 0x10, 0x13,
2856 0x80,
2857
2858 /* An invalid decimal fraction with an additional tag */
2859 0xd9, 0xff, 0xfa,
2860 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2861 0x00, // the integer 0; should be a byte string
2862};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002863
Laurence Lundblade59289e52019-12-30 13:44:37 -08002864/*
2865 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002866 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002867 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002868static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002869 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002870
Laurence Lundblade59289e52019-12-30 13:44:37 -08002871/*
2872DB 9192939495969798 # tag(10489608748473423768)
2873 D8 88 # tag(136)
2874 C6 # tag(6)
2875 C7 # tag(7)
2876 80 # array(0)
2877*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002878static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002879 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002880
2881/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002882 55799(55799(55799({
2883 6(7(-23)): 5859837686836516696(7({
2884 7(-20): 11({
2885 17(-18): 17(17(17("Organization"))),
2886 9(-17): 773("SSG"),
2887 -15: 16(17(6(7("Confusion")))),
2888 17(-16): 17("San Diego"),
2889 17(-14): 17("US")
2890 }),
2891 23(-19): 19({
2892 -11: 9({
2893 -9: -7
2894 }),
2895 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2896 })
2897 })),
2898 16(-22): 23({
2899 11(8(7(-5))): 8(-3)
2900 })
2901 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002902 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002903static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002904 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2905 0xc6, 0xc7, 0x36,
2906 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2907 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2908 0xcb, 0xa5,
2909 0xd1, 0x31,
2910 0xd1, 0xd1, 0xd1, 0x6c,
2911 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2912 0xc9, 0x30,
2913 0xd9, 0x03, 0x05, 0x63,
2914 0x53, 0x53, 0x47,
2915 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002916 0xd0, 0xd1, 0xc6, 0xc7,
2917 0x69,
2918 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002919 0xd1, 0x2f,
2920 0xd1, 0x69,
2921 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2922 0xd1, 0x2d,
2923 0xd1, 0x62,
2924 0x55, 0x53,
2925 0xd7, 0x32,
2926 0xd3, 0xa2,
2927 0x2a,
2928 0xc9, 0xa1,
2929 0x28,
2930 0x26,
2931 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2932 0xcc, 0x4a,
2933 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2934 0xd0, 0x35,
2935 0xd7, 0xa1,
2936 0xcb, 0xc8, 0xc7, 0x24,
2937 0xc8, 0x22};
2938
Laurence Lundblade9b334962020-08-27 10:55:53 -07002939
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002940static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002941 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07002942
2943 0xc0, // tag for string date
2944 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2945
2946 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2947
2948 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2949
2950 0xd8, 0x23, // tag for regex
2951 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2952
2953 0xc0, // tag for string date
2954 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002955};
2956
2957
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002958static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002959
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002960
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002961int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002962{
2963 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002964 QCBORItem Item;
2965 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002966
Laurence Lundbladeee851742020-01-08 08:37:05 -08002967 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002968 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002969 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002970
Laurence Lundblade9b334962020-08-27 10:55:53 -07002971 /*
2972 This test matches the magic number tag and the fraction tag
2973 55799([...])
2974 */
2975 uError = QCBORDecode_GetNext(&DCtx, &Item);
2976 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002977 return -2;
2978 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002979 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002980 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2981 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002982 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002983
Laurence Lundblade9b334962020-08-27 10:55:53 -07002984 /*
2985 4([1,3])
2986 */
2987 uError = QCBORDecode_GetNext(&DCtx, &Item);
2988#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2989 if(uError != QCBOR_SUCCESS ||
2990 Item.uDataType != QCBOR_TYPE_ARRAY ||
2991 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2992 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2993 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2994 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2995 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2996 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2997 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002998 return -4;
2999 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003000 // consume the items in the array
3001 uError = QCBORDecode_GetNext(&DCtx, &Item);
3002 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003003
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003004#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003005 if(uError != QCBOR_SUCCESS ||
3006 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3007 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3008 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3009 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3010 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3011 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3012 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003013 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003014#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003015
Laurence Lundblade9b334962020-08-27 10:55:53 -07003016 /*
3017 More than 4 tags on an item 225(226(227(228(229([])))))
3018 */
3019 uError = QCBORDecode_GetNext(&DCtx, &Item);
3020 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003021 return -6;
3022 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003023
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003024 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3025 return -106;
3026 }
3027
3028
Laurence Lundblade9b334962020-08-27 10:55:53 -07003029 /* tag 10489608748473423768(
3030 2442302356(
3031 21590(
3032 240(
3033 []))))
3034 */
3035 uError = QCBORDecode_GetNext(&DCtx, &Item);
3036 if(uError != QCBOR_SUCCESS ||
3037 Item.uDataType != QCBOR_TYPE_ARRAY ||
3038 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3039 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3040 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3041 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003042 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003043 }
3044
3045 /* tag 21590(
3046 10489608748473423768(
3047 2442302357(
3048 21591(
3049 []))))
3050 */
3051 uError = QCBORDecode_GetNext(&DCtx, &Item);
3052 if(uError != QCBOR_SUCCESS ||
3053 Item.uDataType != QCBOR_TYPE_ARRAY ||
3054 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3055 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3056 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3057 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3058 return -8;
3059 }
3060
3061 /* Make sure to blow past the limit of tags that must be mapped.
3062 works in conjuntion with entries above.
3063 269488144(269488145(269488146(269488147([]))))
3064 */
3065 uError = QCBORDecode_GetNext(&DCtx, &Item);
3066 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3067 return -9;
3068 }
3069
3070 uError = QCBORDecode_GetNext(&DCtx, &Item);
3071 if(uError == QCBOR_SUCCESS) {
3072 return -10;
3073 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003074
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003075 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003076 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003077 // tage and then matches it. Caller-config lists are no longer
3078 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003079 QCBORDecode_Init(&DCtx,
3080 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3081 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003082 const uint64_t puList[] = {0x9192939495969798, 257};
3083 const QCBORTagListIn TL = {2, puList};
3084 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003085
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003086 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3087 return -8;
3088 }
3089 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3090 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3091 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3092 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3093 Item.val.uCount != 0) {
3094 return -9;
3095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003096
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003097 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003098 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003099 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003100 const uint64_t puLongList[17] = {1,2,1};
3101 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003102 QCBORDecode_Init(&DCtx,
3103 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3104 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003105 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3106 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3107 return -11;
3108 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003109
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003110 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003111 QCBORTagListOut Out = {0, 4, puTags};
3112
3113
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003114 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003115 QCBORDecode_Init(&DCtx,
3116 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3117 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003118 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3119 return -12;
3120 }
3121 if(puTags[0] != 0x9192939495969798 ||
3122 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003123 puTags[2] != 0x06 ||
3124 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003125 return -13;
3126 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003127
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003128 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003129 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003130 QCBORDecode_Init(&DCtx,
3131 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3132 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003133 QCBORTagListOut OutSmall = {0, 3, puTags};
3134 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3135 return -14;
3136 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003137
Laurence Lundblade9b334962020-08-27 10:55:53 -07003138
3139
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003140 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003141 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3142 // It is a bit of a messy test and maybe could be improved, but
3143 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003144 QCBORDecode_Init(&DCtx,
3145 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3146 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003147 int n = CheckCSRMaps(&DCtx);
3148 if(n) {
3149 return n-2000;
3150 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003151
Laurence Lundblade59289e52019-12-30 13:44:37 -08003152 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003153 QCBORDecode_Init(&DCtx,
3154 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3155 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003156
Laurence Lundblade9b334962020-08-27 10:55:53 -07003157 /* With the spiffy decode revision, this tag list is not used.
3158 It doesn't matter if a tag is in this list or not so some
3159 tests that couldn't process a tag because it isn't in this list
3160 now can process these unlisted tags. The tests have been
3161 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003162 const uint64_t puTagList[] = {773, 1, 90599561};
3163 const QCBORTagListIn TagList = {3, puTagList};
3164 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003165
3166
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003167 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3168 return -100;
3169 }
3170 if(Item.uDataType != QCBOR_TYPE_MAP ||
3171 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3172 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3173 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3174 Item.val.uCount != 2 ||
3175 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3176 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3177 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3178 Out.uNumUsed != 3) {
3179 return -101;
3180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003181
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003182 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3183 return -102;
3184 }
3185 if(Item.uDataType != QCBOR_TYPE_MAP ||
3186 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3187 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003188 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003189 Item.val.uCount != 2 ||
3190 puTags[0] != 5859837686836516696 ||
3191 puTags[1] != 7 ||
3192 Out.uNumUsed != 2) {
3193 return -103;
3194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003195
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003196 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3197 return -104;
3198 }
3199 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003200 Item.val.uCount != 5 ||
3201 puTags[0] != 0x0b ||
3202 Out.uNumUsed != 1) {
3203 return -105;
3204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003205
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003206 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3207 return -106;
3208 }
3209 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3210 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3211 Item.val.string.len != 12 ||
3212 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3213 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3214 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3215 Out.uNumUsed != 3) {
3216 return -105;
3217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003218
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003219 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3220 return -107;
3221 }
3222 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3223 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3224 Item.val.string.len != 3 ||
3225 puTags[0] != 773 ||
3226 Out.uNumUsed != 1) {
3227 return -108;
3228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003229
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003230 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3231 return -109;
3232 }
3233 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003234 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003235 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003236 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003237 puTags[3] != 7 ||
3238 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003239 return -110;
3240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003241
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003242 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3243 return -111;
3244 }
3245 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3246 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3247 Item.val.string.len != 9 ||
3248 puTags[0] != 17 ||
3249 Out.uNumUsed != 1) {
3250 return -112;
3251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003252
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003253 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3254 return -111;
3255 }
3256 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3257 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3258 Item.val.string.len != 2 ||
3259 puTags[0] != 17 ||
3260 Out.uNumUsed != 1) {
3261 return -112;
3262 }
3263
3264 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3265 return -113;
3266 }
3267 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003268 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003269 Item.val.uCount != 2 ||
3270 puTags[0] != 19 ||
3271 Out.uNumUsed != 1) {
3272 return -114;
3273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003274
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003275 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3276 return -115;
3277 }
3278 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003279 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003280 Item.val.uCount != 1 ||
3281 puTags[0] != 9 ||
3282 Out.uNumUsed != 1) {
3283 return -116;
3284 }
3285
3286 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3287 return -116;
3288 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003289 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003290 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003291 Out.uNumUsed != 0) {
3292 return -117;
3293 }
3294
3295 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3296 return -118;
3297 }
3298 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3299 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003300 puTags[0] != 12 ||
3301 Out.uNumUsed != 1) {
3302 return -119;
3303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003304
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003305 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3306 return -120;
3307 }
3308 if(Item.uDataType != QCBOR_TYPE_MAP ||
3309 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3310 Item.val.uCount != 1 ||
3311 puTags[0] != 0x17 ||
3312 Out.uNumUsed != 1) {
3313 return -121;
3314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003315
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003316 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3317 return -122;
3318 }
3319 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003320 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003321 Item.val.int64 != -3 ||
3322 puTags[0] != 8 ||
3323 Out.uNumUsed != 1) {
3324 return -123;
3325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003326
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003327 if(QCBORDecode_Finish(&DCtx)) {
3328 return -124;
3329 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003330
3331 UsefulBufC DateString;
3332 QCBORDecode_Init(&DCtx,
3333 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3334 QCBOR_DECODE_MODE_NORMAL);
3335
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003336 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003337 // tagged date string
3338 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3339 // untagged date string
3340 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3341 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3342 return 100;
3343 }
3344 // untagged byte string
3345 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3346 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3347 return 101;
3348 }
3349 // tagged regex
3350 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3351 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3352 return 102;
3353 }
3354 // tagged date string with a byte string
3355 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3356 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3357 return 103;
3358 }
3359 QCBORDecode_ExitArray(&DCtx);
3360 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3361 return 104;
3362 }
3363
3364
3365 QCBORDecode_Init(&DCtx,
3366 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3367 QCBOR_DECODE_MODE_NORMAL);
3368
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003369 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003370 // tagged date string
3371 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3372 // untagged date string
3373 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3374 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3375 return 200;
3376 }
3377 // untagged byte string
3378 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3379 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3380 return 201;
3381 }
3382 // tagged regex
3383 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3384 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3385 return 202;
3386 }
3387 // tagged date string with a byte string
3388 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3389 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3390 return 203;
3391 }
3392 QCBORDecode_ExitArray(&DCtx);
3393 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3394 return 204;
3395 }
3396
3397 QCBORDecode_Init(&DCtx,
3398 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3399 QCBOR_DECODE_MODE_NORMAL);
3400
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003401 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003402 // tagged date string
3403 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3404 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3405 return 300;
3406 }
3407 // untagged date string
3408 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3409 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3410 return 301;
3411 }
3412 // untagged byte string
3413 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3414 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3415 return 302;
3416 }
3417 // tagged regex
3418 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3419 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3420 return 303;
3421 }
3422 // tagged date string with a byte string
3423 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3424 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3425 return 304;
3426 }
3427 QCBORDecode_ExitArray(&DCtx);
3428 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3429 return 305;
3430 }
3431
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003432 return 0;
3433}
3434
3435
3436
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003437
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003438static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003439 0x83,
3440 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3441 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3442 0xA4,
3443 0x63, 0x42, 0x4E, 0x2B,
3444 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3445 0x18, 0x40,
3446 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3447 0x63, 0x42, 0x4E, 0x2D,
3448 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3449 0x38, 0x3F,
3450 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3451
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003452/* The expected big num */
3453static const uint8_t spBigNum[] = {
3454 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3455 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003456
3457
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003458int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003459{
3460 QCBORDecodeContext DCtx;
3461 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003462 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003463
Laurence Lundbladeee851742020-01-08 08:37:05 -08003464 QCBORDecode_Init(&DCtx,
3465 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3466 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003467
3468
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003469 //
3470 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3471 return -1;
3472 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003473 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003474 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003475
3476 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003477 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003478 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003479 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003480 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003481 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003482 }
3483
3484 //
3485 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003486 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003487 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003488 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003489 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003490 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003491
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003492 //
3493 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003494 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003495 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003496 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003497 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003498
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003499 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003500 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003501 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3502 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003503 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003504 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003505 }
3506
3507 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003508 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003509 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3510 Item.uLabelType != QCBOR_TYPE_INT64 ||
3511 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003512 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003513 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003514 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003515
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003516 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003517 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003518 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3519 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
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 -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003522 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003523
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003524 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003525 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003526 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3527 Item.uLabelType != QCBOR_TYPE_INT64 ||
3528 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003529 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003530 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003531 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003532
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003533 return 0;
3534}
3535
3536
3537
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003538static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003539 uint8_t uDataType,
3540 uint8_t uNestingLevel,
3541 uint8_t uNextNest,
3542 int64_t nLabel,
3543 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003544{
3545 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003546 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003547
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003548 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3549 if(Item.uDataType != uDataType) return -1;
3550 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003551 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3552 Item.uLabelType != QCBOR_TYPE_UINT64) {
3553 return -1;
3554 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003555 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3556 if(Item.label.int64 != nLabel) return -1;
3557 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003558 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003559 }
3560 }
3561 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303562 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003564 if(pItem) {
3565 *pItem = Item;
3566 }
3567 return 0;
3568}
3569
3570
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003571// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003572static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003573{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303574 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003575
Laurence Lundblade9b334962020-08-27 10:55:53 -07003576 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003577
Laurence Lundblade9b334962020-08-27 10:55:53 -07003578 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003579
Laurence Lundblade9b334962020-08-27 10:55:53 -07003580 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3581 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3582 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3583 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3584 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003585
Laurence Lundblade9b334962020-08-27 10:55:53 -07003586 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3587 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003588
Laurence Lundblade9b334962020-08-27 10:55:53 -07003589 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3590 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003591
Laurence Lundblade9b334962020-08-27 10:55:53 -07003592 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3593 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003594
Laurence Lundblade9b334962020-08-27 10:55:53 -07003595 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003596
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003597 return 0;
3598}
3599
3600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003601/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003602{
3603 -23: {
3604 -20: {
3605 -18: "Organization",
3606 -17: "SSG",
3607 -15: "Confusion",
3608 -16: "San Diego",
3609 -14: "US"
3610 },
3611 -19: {
3612 -11: {
3613 -9: -7
3614 },
3615 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3616 }
3617 },
3618 -22: {
3619 -5: -3
3620 }
3621}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003622*/
3623static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003624 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3625 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3626 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3627 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3628 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3629 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3630 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3631 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3632 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3633
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003634// Same map as above, but using indefinite lengths
3635static const uint8_t spCSRInputIndefLen[] = {
3636 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3637 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3638 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3639 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3640 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3641 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3642 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3643 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3644 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3645 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3646
3647
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003648int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003649{
3650 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003651
Laurence Lundbladeee851742020-01-08 08:37:05 -08003652 QCBORDecode_Init(&DCtx,
3653 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3654 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003655
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003656 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003657}
3658
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003659
3660
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003661int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003662{
3663 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003664
Laurence Lundbladeee851742020-01-08 08:37:05 -08003665 QCBORDecode_Init(&DCtx,
3666 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3667 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003668
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003669 QCBORItem Item;
3670 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003671
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003672 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3673 return -1;
3674 }
3675 if(Item.uDataType != QCBOR_TYPE_MAP) {
3676 return -2;
3677 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003678
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003679 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3680 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3681 return -3;
3682 }
3683
3684 return 0;
3685}
3686
3687
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003688
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003689int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003690{
3691 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003692
Laurence Lundbladeee851742020-01-08 08:37:05 -08003693 QCBORDecode_Init(&DCtx,
3694 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3695 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003696
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003697 return CheckCSRMaps(&DCtx);
3698}
3699
3700
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003701
Laurence Lundblade17ede402018-10-13 11:43:07 +08003702static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3703{
3704 UsefulOutBuf UOB;
3705 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003706
Laurence Lundblade17ede402018-10-13 11:43:07 +08003707 int i;
3708 for(i = 0; i < n; i++) {
3709 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3710 }
3711
3712 for(i = 0; i < n; i++) {
3713 UsefulOutBuf_AppendByte(&UOB, 0xff);
3714 }
3715 return UsefulOutBuf_OutUBuf(&UOB);
3716}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003717
3718
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003719static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003720{
3721 QCBORDecodeContext DC;
3722 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003723
Laurence Lundblade17ede402018-10-13 11:43:07 +08003724 int j;
3725 for(j = 0; j < nNestLevel; j++) {
3726 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003727 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003728 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3729 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003730 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003731 return -4;
3732 } else {
3733 return 0; // Decoding doesn't recover after an error
3734 }
3735 } else {
3736 // Should be no error
3737 if(nReturn) {
3738 return -9; // Should not have got an error
3739 }
3740 }
3741 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3742 return -7;
3743 }
3744 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003745 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003746 if(nReturn) {
3747 return -3;
3748 }
3749 return 0;
3750}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003751
3752
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003753int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003754{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303755 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003756 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003757 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003758 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003759 int nReturn = parse_indeflen_nested(Nested, i);
3760 if(nReturn) {
3761 return nReturn;
3762 }
3763 }
3764 return 0;
3765}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003766
Laurence Lundbladeee851742020-01-08 08:37:05 -08003767// [1, [2, 3]]
3768static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3769// No closing break
3770static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3771// Not enough closing breaks
3772static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3773// Too many closing breaks
3774static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3775// Unclosed indeflen inside def len
3776static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3777// confused tag
3778static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003779
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003780int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003781{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003782 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003783 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003784 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003785
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003786 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003787 QCBORDecodeContext DC;
3788 QCBORItem Item;
3789 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003790
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003791 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303792
3793 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3794 Item.uNestingLevel != 0 ||
3795 Item.uNextNestLevel != 1) {
3796 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003797 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003798
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003799 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303800 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3801 Item.uNestingLevel != 1 ||
3802 Item.uNextNestLevel != 1) {
3803 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003804 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003805
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003806 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303807 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3808 Item.uNestingLevel != 1 ||
3809 Item.uNextNestLevel != 2) {
3810 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003811 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003812
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003813 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003814 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303815 Item.uNestingLevel != 2 ||
3816 Item.uNextNestLevel != 2) {
3817 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003818 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003819
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003820 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003821 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303822 Item.uNestingLevel != 2 ||
3823 Item.uNextNestLevel != 0) {
3824 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003825 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003826
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003827 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303828 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003829 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003830
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003831 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003832 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003833
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003834 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003835
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003836 nResult = QCBORDecode_GetNext(&DC, &Item);
3837 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303838 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003839 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003840
Laurence Lundblade570fab52018-10-13 18:28:27 +08003841 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003842 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303843 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003844 }
3845
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003846
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003847 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003848 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003849
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003850 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003851
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003852 nResult = QCBORDecode_GetNext(&DC, &Item);
3853 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303854 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003855 }
3856
3857 nResult = QCBORDecode_GetNext(&DC, &Item);
3858 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303859 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003860 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003861
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003862 nResult = QCBORDecode_GetNext(&DC, &Item);
3863 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303864 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003865 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003866
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003867 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003868 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303869 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003870 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003871
3872
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003873 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003874 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003875
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003876 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003877
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003878 nResult = QCBORDecode_GetNext(&DC, &Item);
3879 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303880 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003881 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003882
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003883 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003884 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303885 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003886 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303887
Laurence Lundblade642282a2020-06-23 12:00:33 -07003888 nResult = QCBORDecode_GetNext(&DC, &Item);
3889 if(nResult != QCBOR_ERR_BAD_BREAK) {
3890 return -140;
3891 }
3892
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003893
Laurence Lundblade570fab52018-10-13 18:28:27 +08003894 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003895 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003896
Laurence Lundblade570fab52018-10-13 18:28:27 +08003897 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003898
Laurence Lundblade570fab52018-10-13 18:28:27 +08003899 nResult = QCBORDecode_GetNext(&DC, &Item);
3900 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303901 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003902 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003903
Laurence Lundblade570fab52018-10-13 18:28:27 +08003904 nResult = QCBORDecode_GetNext(&DC, &Item);
3905 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303906 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003908
Laurence Lundblade570fab52018-10-13 18:28:27 +08003909 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003910 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303911 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003912 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003913
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303914 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003915 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003916
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303917 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003918
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303919 nResult = QCBORDecode_GetNext(&DC, &Item);
3920 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303921 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303922 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003923
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303924 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303925 if(nResult != QCBOR_ERR_BAD_BREAK) {
3926 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303927 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003928
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003929 return 0;
3930}
3931
Laurence Lundblade17ede402018-10-13 11:43:07 +08003932
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08003933#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
3934
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003935static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003936 0x81, // Array of length one
3937 0x7f, // text string marked with indefinite length
3938 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3939 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3940 0xff // ending break
3941};
3942
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003943static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303944 0x81, // Array of length one
3945 0x7f, // text string marked with indefinite length
3946 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3947 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3948 0xff // ending break
3949};
3950
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003951static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303952 0x81, // Array of length one
3953 0x7f, // text string marked with indefinite length
3954 0x01, 0x02, // Not a string
3955 0xff // ending break
3956};
3957
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003958static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303959 0x81, // Array of length one
3960 0x7f, // text string marked with indefinite length
3961 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3962 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3963 // missing end of string
3964};
3965
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003966static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303967 0xa1, // Array of length one
3968 0x7f, // text string marked with indefinite length
3969 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3970 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3971 0xff, // ending break
3972 0x01 // integer being labeled.
3973};
3974
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003975/**
3976 Make an indefinite length string
3977
3978 @param Storage Storage for string, must be 144 bytes in size
3979 @return The indefinite length string
3980
3981 This makes an array with one indefinite length string that has 7 chunks
3982 from size of 1 byte up to 64 bytes.
3983 */
3984static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303985{
3986 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003987
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303988 UsefulOutBuf_Init(&UOB, Storage);
3989 UsefulOutBuf_AppendByte(&UOB, 0x81);
3990 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003991
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003992 uint8_t uStringByte = 0;
3993 // Use of type int is intentional
3994 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3995 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303996 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003997 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3998 for(int j = 0; j < uChunkSize; j++) {
3999 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4000 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304001 }
4002 }
4003 UsefulOutBuf_AppendByte(&UOB, 0xff);
4004
4005 return UsefulOutBuf_OutUBuf(&UOB);
4006}
4007
4008static int CheckBigString(UsefulBufC BigString)
4009{
4010 if(BigString.len != 255) {
4011 return 1;
4012 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004013
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304014 for(uint8_t i = 0; i < 255; i++){
4015 if(((const uint8_t *)BigString.ptr)[i] != i) {
4016 return 1;
4017 }
4018 }
4019 return 0;
4020}
4021
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304022
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004023int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304024{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304025 QCBORDecodeContext DC;
4026 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304027 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004028 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004029
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304030 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004031 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304032 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004033
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304034 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304035 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304036 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004037
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304038 if(QCBORDecode_GetNext(&DC, &Item)) {
4039 return -2;
4040 }
4041 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4042 return -3;
4043 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004044
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304045 if(QCBORDecode_GetNext(&DC, &Item)) {
4046 return -4;
4047 }
4048 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4049 return -5;
4050 }
4051 if(QCBORDecode_Finish(&DC)) {
4052 return -6;
4053 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304054
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304055 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004056 QCBORDecode_Init(&DC,
4057 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4058 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004059
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304060 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4061 return -7;
4062 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004063
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304064 if(QCBORDecode_GetNext(&DC, &Item)) {
4065 return -8;
4066 }
4067 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4068 return -9;
4069 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004070
Laurence Lundblade30816f22018-11-10 13:40:22 +07004071 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304072 return -10;
4073 }
4074
4075 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004076 QCBORDecode_Init(&DC,
4077 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4078 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004079
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304080 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4081 return -11;
4082 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004083
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304084 if(QCBORDecode_GetNext(&DC, &Item)) {
4085 return -12;
4086 }
4087 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4088 return -13;
4089 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004090
Laurence Lundblade30816f22018-11-10 13:40:22 +07004091 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304092 return -14;
4093 }
4094
4095 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004096 QCBORDecode_Init(&DC,
4097 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
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 -15;
4102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004103
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304104 if(QCBORDecode_GetNext(&DC, &Item)) {
4105 return -16;
4106 }
4107 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4108 return -17;
4109 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004110
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304111 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4112 return -18;
4113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004114
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304115 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304116 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004117
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304118 QCBORDecode_GetNext(&DC, &Item);
4119 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304120 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304121 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004122
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304123 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304124 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004126
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304127 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004128 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304129
4130 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4131 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304132 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304133 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004134
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304135 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304136 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004137 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004138
Laurence Lundbladeee851742020-01-08 08:37:05 -08004139 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4140 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004141
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304142 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304143 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304144 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304145 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004146
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304147 QCBORDecode_GetNext(&DC, &Item);
4148 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304149 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304150 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004151 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304152 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004154
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304155 // ---- big bstr -----
4156 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004157
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304158 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4159 return -25;
4160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004161
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304162 if(QCBORDecode_GetNext(&DC, &Item)) {
4163 return -26;
4164 }
4165 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304166 return -26;
4167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004168
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304169 if(QCBORDecode_GetNext(&DC, &Item)) {
4170 return -27;
4171 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304172 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304173 return -28;
4174 }
4175 if(CheckBigString(Item.val.string)) {
4176 return -3;
4177 }
4178 if(QCBORDecode_Finish(&DC)) {
4179 return -29;
4180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004181
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304182 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004183 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004184
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304185 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4186 return -30;
4187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004188
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304189 QCBORDecode_GetNext(&DC, &Item);
4190 if(Item.uDataType != QCBOR_TYPE_MAP) {
4191 return -31;
4192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004193
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304194 if(QCBORDecode_GetNext(&DC, &Item)){
4195 return -32;
4196 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004197 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4198 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304199 Item.uDataAlloc || !Item.uLabelAlloc ||
4200 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4201 return -33;
4202 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004203
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304204 if(QCBORDecode_Finish(&DC)) {
4205 return -34;
4206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004207
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004208 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004209}
4210
4211
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004212int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304213{
4214 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004215 QCBORError nCBORError;
4216
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004217
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304218 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004219 QCBORDecode_Init(&DC,
4220 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4221 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004222
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004223 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004224
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004225 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4226 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304227 return -1;
4228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004229
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004230 if(CheckCSRMaps(&DC)) {
4231 return -2;
4232 }
4233
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004234 // Next parse, save pointers to a few strings, destroy original and
4235 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004236 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004237 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004238
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304239 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004240 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304241 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004242
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304243 QCBORItem Item1, Item2, Item3, Item4;
4244 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004245 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304246 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4247 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004248 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304249 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004250 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304251 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004252 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304253 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004254 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304255 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004256 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004257
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304258 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004259
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304260 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304261 Item1.uDataType != QCBOR_TYPE_INT64 ||
4262 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004263 Item1.uDataAlloc != 0 ||
4264 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004265 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004266 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004268
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304269
4270 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004271 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304272 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004273 Item2.uDataAlloc != 0 ||
4274 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304275 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004276 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004277
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304278 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004279 Item3.uDataAlloc == 0 ||
4280 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004281 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004282 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004283 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004284
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304285 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004286 Item4.uDataAlloc == 0 ||
4287 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004288 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004289 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004291
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304292 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004293 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004294 QCBORDecode_Init(&DC,
4295 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4296 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304297 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4298 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004299 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304300 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004301 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004302 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004303 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304304 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4305 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4306 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4307 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4308 }
4309 }
4310 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004311 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004312 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304313 }
4314
4315 return 0;
4316}
4317
Laurence Lundbladef6531662018-12-04 10:42:22 +09004318
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004319int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004320{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004321 // Set up the decoder with a tiny bit of CBOR to parse because
4322 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004323 QCBORDecodeContext DC;
4324 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4325 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004326
Laurence Lundbladef6531662018-12-04 10:42:22 +09004327 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004328 // Then fish into the internals of the decode context
4329 // to get the allocator function so it can be called directly.
4330 // Also figure out how much pool is available for use
4331 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004332 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004333 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4334 if(nError) {
4335 return -9;
4336 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004337 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4338 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4339 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004340
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004341 // First test -- ask for one more byte than available and see failure
4342 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004343 if(!UsefulBuf_IsNULL(Allocated)) {
4344 return -1;
4345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004346
Laurence Lundbladef6531662018-12-04 10:42:22 +09004347 // Re do the set up for the next test that will do a successful alloc,
4348 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004349 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004350 pAlloc = DC.StringAllocator.pfAllocator;
4351 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4352 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004353
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004354 // Allocate one byte less than available and see success
4355 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004356 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4357 return -2;
4358 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004359 // Ask for some more and see failure
4360 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004361 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4362 return -3;
4363 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004364 // Free the first allocate, retry the second and see success
4365 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4366 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004367 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4368 return -4;
4369 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004370
Laurence Lundbladef6531662018-12-04 10:42:22 +09004371 // Re do set up for next test that involves a successful alloc,
4372 // and a successful realloc and a failed realloc
4373 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004374 pAlloc = DC.StringAllocator.pfAllocator;
4375 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004376
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004377 // Allocate half the pool and see success
4378 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004379 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4380 return -5;
4381 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004382 // Reallocate to take up the whole pool and see success
4383 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004384 if(UsefulBuf_IsNULL(Allocated2)) {
4385 return -6;
4386 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004387 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004388 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4389 return -7;
4390 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004391 // Try to allocate more to be sure there is failure after a realloc
4392 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4393 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004394 return -8;
4395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004396
Laurence Lundbladef6531662018-12-04 10:42:22 +09004397 return 0;
4398}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004399
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004400
4401/* Just enough of an allocator to test configuration of one */
4402static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4403{
4404 (void)pOldMem; // unused variable
4405
4406 if(uNewSize) {
4407 // Assumes the context pointer is the buffer and
4408 // nothing too big will ever be asked for.
4409 // This is only good for this basic test!
4410 return (UsefulBuf) {pCtx, uNewSize};
4411 } else {
4412 return NULLUsefulBuf;
4413 }
4414}
4415
4416
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004417int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004418{
4419 // Set up the decoder with a tiny bit of CBOR to parse because
4420 // nothing can be done with it unless that is set up.
4421 QCBORDecodeContext DC;
4422 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4423 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4424
4425 uint8_t pAllocatorBuffer[50];
4426
4427 // This is really just to test that this call works.
4428 // The full functionality of string allocators is tested
4429 // elsewhere with the MemPool internal allocator.
4430 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4431
4432 QCBORItem Item;
4433 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4434 return -1;
4435 }
4436
4437 if(Item.uDataAlloc == 0 ||
4438 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4439 Item.val.string.ptr != pAllocatorBuffer) {
4440 return -2;
4441 }
4442
4443 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4444 return -3;
4445 }
4446
4447 return 0;
4448}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004449#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4450
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004451
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004452#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004453
Laurence Lundbladea826c502020-05-10 21:07:00 -07004454/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004455 [
4456 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004457 4([-20, 4759477275222530853136]),
4458 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004459 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004460 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004461 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004462 5([ 9223372036854775806, -4759477275222530853137])
4463 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004464 ]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004465 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004466static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004467 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004468 0xC4, 0x82, 0x20,
4469 0x03,
4470 0xC4, 0x82, 0x33,
4471 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4472 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4473 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4474 0xC5, 0x82, 0x19, 0x01, 0x2C,
4475 0x18, 0x64,
4476 0xC5, 0x82, 0x33,
4477 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4478 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4479 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004480 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4481 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004482 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4483 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4484};
4485
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004486
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004487int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004488{
4489 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004490 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004491 QCBORItem item;
4492
Laurence Lundblade17af4902020-01-07 19:11:55 -08004493 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4494 0x06, 0x07, 0x08, 0x09, 0x010};
4495 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004496
4497
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004498 QCBORDecode_Init(&DC,
4499 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4500 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004501
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004502 uErr = QCBORDecode_GetNext(&DC, &item);
4503 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004504 return 1;
4505 }
4506
4507 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4508 return 2;
4509 }
4510
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004511 uErr = QCBORDecode_GetNext(&DC, &item);
4512 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004513 return 3;
4514 }
4515
4516 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4517 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4518 item.val.expAndMantissa.nExponent != -1) {
4519 return 4;
4520 }
4521
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004522 uErr = QCBORDecode_GetNext(&DC, &item);
4523 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004524 return 5;
4525 }
4526
4527 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4528 item.val.expAndMantissa.nExponent != -20 ||
4529 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4530 return 6;
4531 }
4532
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004533 uErr = QCBORDecode_GetNext(&DC, &item);
4534 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004535 return 7;
4536 }
4537
4538 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4539 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4540 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4541 return 8;
4542 }
4543
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004544 uErr = QCBORDecode_GetNext(&DC, &item);
4545 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004546 return 9;
4547 }
4548
4549 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4550 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4551 item.val.expAndMantissa.nExponent != 300) {
4552 return 10;
4553 }
4554
Laurence Lundbladea826c502020-05-10 21:07:00 -07004555 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004556 uErr = QCBORDecode_GetNext(&DC, &item);
4557 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004558 return 11;
4559 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004560 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4561 item.val.expAndMantissa.nExponent != -20 ||
4562 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4563 return 12;
4564 }
4565
Laurence Lundbladea826c502020-05-10 21:07:00 -07004566 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004567 uErr = QCBORDecode_GetNext(&DC, &item);
4568 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004569 return 13;
4570 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004571 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4572 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4573 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4574 return 14;
4575 }
4576
Laurence Lundbladea826c502020-05-10 21:07:00 -07004577 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004578 uErr = QCBORDecode_GetNext(&DC, &item);
4579 if(uErr != QCBOR_SUCCESS) {
4580 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004581 }
4582 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4583 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4584 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004585 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004586 }
4587
Laurence Lundbladea826c502020-05-10 21:07:00 -07004588 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004589 uErr = QCBORDecode_GetNext(&DC, &item);
4590 if(uErr != QCBOR_SUCCESS) {
4591 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004592 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004593 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4594 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4595 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004596 return 18;
4597 }
4598
4599 uErr = QCBORDecode_Finish(&DC);
4600 if(uErr != QCBOR_SUCCESS) {
4601 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004602 }
4603
4604 /* Now encode some stuff and then decode it */
4605 uint8_t pBuf[40];
4606 QCBOREncodeContext EC;
4607 UsefulBufC Encoded;
4608
4609 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4610 QCBOREncode_OpenArray(&EC);
4611 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4612 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4613 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4614 QCBOREncode_CloseArray(&EC);
4615 QCBOREncode_Finish(&EC, &Encoded);
4616
4617
4618 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004619 uErr = QCBORDecode_GetNext(&DC, &item);
4620 if(uErr != QCBOR_SUCCESS) {
4621 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004622 }
4623
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004624 uErr = QCBORDecode_GetNext(&DC, &item);
4625 if(uErr != QCBOR_SUCCESS) {
4626 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004627 }
4628
4629 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4630 item.val.expAndMantissa.nExponent != 1000 ||
4631 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004632 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004633 }
4634
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004635 uErr = QCBORDecode_GetNext(&DC, &item);
4636 if(uErr != QCBOR_SUCCESS) {
4637 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004638 }
4639
4640 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4641 item.val.expAndMantissa.nExponent != INT32_MIN ||
4642 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004643 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004644 }
4645
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004646 uErr = QCBORDecode_GetNext(&DC, &item);
4647 if(uErr != QCBOR_SUCCESS) {
4648 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004649 }
4650
4651 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4652 item.val.expAndMantissa.nExponent != INT32_MAX ||
4653 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004654 return 106;
4655 }
4656
4657
4658 int64_t nExp, nMant;
4659 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4660 UsefulBufC Mant;
4661 bool bIsNeg;
4662
4663 QCBORDecode_Init(&DC,
4664 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4665 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004666 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004667
4668 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004669 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004670
4671 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004672 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4673 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004674
4675 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004676 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4677 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004678
4679 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004680 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004681
4682 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004683 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4684 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004685
4686 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004687 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4688 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004689
4690 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004691 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4692 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004693
4694 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004695 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4696 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004697
4698 QCBORDecode_ExitArray(&DC);
4699
4700 uErr = QCBORDecode_Finish(&DC);
4701 if(uErr != QCBOR_SUCCESS) {
4702 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004703 }
4704
4705 return 0;
4706}
4707
4708
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004709static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004710 // Exponent > INT64_MAX
4711 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4712 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4713 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4714 // Mantissa > INT64_MAX
4715 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4716 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4717 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4718 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004719 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004720 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004721 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004722 // bad content for big num
4723 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4724 // bad content for big num
4725 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4726 // Bad integer for exponent
4727 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4728 // Bad integer for mantissa
4729 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4730 // 3 items in array
4731 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004732#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004733 // unterminated indefinite length array
4734 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004735#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4736 // unterminated indefinite length array
4737 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4738#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004739 // Empty array
4740 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4741 // Second is not an integer
4742 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4743 // First is not an integer
4744 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4745 // Not an array
4746 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4747};
4748
4749
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004750int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004751{
4752 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004753 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4754 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004755}
4756
4757#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004758
4759
4760
4761/*
4762 Some basic CBOR with map and array used in a lot of tests.
4763 The map labels are all strings
4764
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004765 {
4766 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004767 "an array of two strings": [
4768 "string1", "string2"
4769 ],
4770 "map in a map": {
4771 "bytes 1": h'78787878',
4772 "bytes 2": h'79797979',
4773 "another int": 98,
4774 "text 2": "lies, damn lies and statistics"
4775 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004776 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004777 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004778
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004779int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004780{
4781 QCBORItem Item1, Item2, Item3;
4782 int64_t nDecodedInt1, nDecodedInt2;
4783 UsefulBufC B1, B2, S1, S2, S3;
4784
4785 QCBORDecodeContext DCtx;
4786 QCBORError nCBORError;
4787
4788 QCBORDecode_Init(&DCtx, input, 0);
4789
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004790 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004791
4792 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4793
4794 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4795 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004796 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4797 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4798 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004799 QCBORDecode_ExitMap(&DCtx);
4800
4801 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4802 QCBORDecode_GetNext(&DCtx, &Item1);
4803 QCBORDecode_GetNext(&DCtx, &Item2);
4804 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4805 return -400;
4806 }
4807 QCBORDecode_ExitArray(&DCtx);
4808
4809 // Parse the same array again using GetText() instead of GetItem()
4810 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004811 QCBORDecode_GetTextString(&DCtx, &S2);
4812 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004813 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4814 return 5000;
4815 }
4816 /* QCBORDecode_GetText(&DCtx, &S3);
4817 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4818 return 5001;
4819 } */
4820
4821 QCBORDecode_ExitArray(&DCtx);
4822
4823 QCBORDecode_ExitMap(&DCtx);
4824
4825 nCBORError = QCBORDecode_Finish(&DCtx);
4826
4827 if(nCBORError) {
4828 return (int32_t)nCBORError;
4829 }
4830
4831 if(nDecodedInt1 != 42) {
4832 return 1001;
4833 }
4834
4835 if(nDecodedInt2 != 98) {
4836 return 1002;
4837 }
4838
4839 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004840 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004841 return 1003;
4842 }
4843
4844 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004845 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004846 return 1004;
4847 }
4848
Laurence Lundblade9b334962020-08-27 10:55:53 -07004849 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004850 return 1005;
4851 }
4852
4853 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4854 return 1006;
4855 }
4856
4857 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4858 return 1007;
4859 }
4860
4861 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4862 return 1008;
4863 }
4864
4865 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4866 return 1009;
4867 }
4868
4869 return 0;
4870}
4871
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004872/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004873 {
4874 -75008: h'05083399',
4875 88: [],
4876 100100: {
4877 "sub1": {
4878 10: [
4879 0
4880 ],
4881 -75009: h'A46823990001',
4882 100100: {
4883 "json": "{ \"ueid\", \"xyz\"}",
4884 "subsub": {
4885 100002: h'141813191001'
4886 }
4887 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004888 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004889 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004890 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004891 */
4892
4893static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004894 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
4895 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
4896 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
4897 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
4898 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
4899 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
4900 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
4901 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
4902 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
4903 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
4904 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
4905 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004906};
4907
4908/* Get item in multi-level nesting in spNestedCBOR */
4909static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4910{
4911 UsefulBufC String;
4912
4913 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4914 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4915
4916 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4917 QCBORDecode_EnterMap(pDCtx, NULL);
4918 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4919 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4920 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4921 if(QCBORDecode_GetError(pDCtx)) {
4922 return 4001;
4923 }
4924 if(UsefulBuf_Compare(String, test_oemid)) {
4925 return 4002;
4926 }
4927 QCBORDecode_ExitMap(pDCtx);
4928 QCBORDecode_ExitMap(pDCtx);
4929 QCBORDecode_ExitMap(pDCtx);
4930 QCBORDecode_ExitMap(pDCtx);
4931
4932 return 0;
4933}
4934
4935/* Iterations on the zero-length array in spNestedCBOR */
4936static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4937{
4938 QCBORItem Item;
4939 QCBORError uErr;
4940
4941 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4942 for(int x = 0; x < 20; x++) {
4943 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4944 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4945 return 4100;
4946
4947 }
4948 }
4949 QCBORDecode_ExitArray(pDCtx);
4950 if(QCBORDecode_GetError(pDCtx)) {
4951 return 4101;
4952 }
4953
4954 return 0;
4955}
4956
4957/* Various iterations on the array that contains a zero in spNestedCBOR */
4958static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
4959{
4960 QCBORError uErr;
4961
4962 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4963 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
4964 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4965 int64_t nInt = 99;
4966 QCBORDecode_GetInt64(pDCtx, &nInt);
4967 if(nInt != 0) {
4968 return 4200;
4969 }
4970 for(int x = 0; x < 20; x++) {
4971 QCBORItem Item;
4972 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4973 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4974 return 4201;
4975
4976 }
4977 }
4978 QCBORDecode_ExitArray(pDCtx);
4979 if(QCBORDecode_GetAndResetError(pDCtx)) {
4980 return 4202;
4981 }
4982 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4983 UsefulBufC dD;
4984 QCBORDecode_GetByteString(pDCtx, &dD);
4985 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4986 return 4203;
4987 }
4988 for(int x = 0; x < 20; x++) {
4989 QCBORDecode_GetByteString(pDCtx, &dD);
4990 uErr = QCBORDecode_GetAndResetError(pDCtx);
4991 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4992 return 4204;
4993 }
4994 }
4995 QCBORDecode_ExitArray(pDCtx);
4996 QCBORDecode_ExitMap(pDCtx);
4997 QCBORDecode_ExitMap(pDCtx);
4998
4999 return 0;
5000}
5001
5002/* Repeatedly enter and exit maps and arrays, go off the end of maps
5003 and arrays and such. */
5004static int32_t DecodeNestedIterate()
5005{
5006 QCBORDecodeContext DCtx;
5007 int32_t nReturn;
5008 QCBORError uErr;
5009
5010 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5011 QCBORDecode_EnterMap(&DCtx, NULL);
5012
5013 for(int j = 0; j < 5; j++) {
5014 for(int i = 0; i < 20; i++) {
5015 nReturn = DecodeNestedGetSubSub(&DCtx);
5016 if(nReturn) {
5017 return nReturn;
5018 }
5019 }
5020
5021 for(int i = 0; i < 20; i++) {
5022 nReturn = DecodeNestedGetEmpty(&DCtx);
5023 if(nReturn ) {
5024 return nReturn;
5025 }
5026 }
5027
5028 for(int i = 0; i < 20; i++) {
5029 nReturn = DecodeNestedGetZero(&DCtx);
5030 if(nReturn ) {
5031 return nReturn;
5032 }
5033 }
5034 }
5035
5036 QCBORDecode_ExitMap(&DCtx);
5037 uErr = QCBORDecode_Finish(&DCtx);
5038 if(uErr) {
5039 return (int32_t)uErr + 4100;
5040 }
5041
5042 return 0;
5043}
5044
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005045
5046/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005047 [
5048 23,
5049 6000,
5050 h'67616C6163746963',
5051 h'686176656E20746F6B656E'
5052 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005053 */
5054static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005055 0x84,
5056 0x17,
5057 0x19, 0x17, 0x70,
5058 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5059 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005060
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005061/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005062static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5063
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005064/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005065static const uint8_t spEmptyMap[] = {0xa0};
5066
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005067#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005068/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005069static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005070
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005071
Laurence Lundbladef0499502020-08-01 11:55:57 -07005072/*
5073 {
5074 0: [],
5075 9: [
5076 [],
5077 []
5078 ],
5079 8: {
5080 1: [],
5081 2: {},
5082 3: []
5083 },
5084 4: {},
5085 5: [],
5086 6: [
5087 [],
5088 []
5089 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005090 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005091 */
5092static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005093 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5094 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5095 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5096 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005097
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005098#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5099
5100
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005101/*
5102 Too many tags
5103 Invalid tag content
5104 Duplicate label
5105 Integer overflow
5106 Date overflow
5107
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005108 {
5109 1: 224(225(226(227(4(0))))),
5110 2: 1(h''),
5111 3: -18446744073709551616,
5112 4: 1(1.0e+300),
5113 5: 0, 8: 8
5114 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005115 */
5116static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005117 0xa7,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005118 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5119 0x02, 0xc1, 0x40,
5120 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5121 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5122 0x05, 0x00,
5123 0x05, 0x00,
5124 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005125};
5126
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005127/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005128static const uint8_t spUnRecoverableMapError1[] = {
5129 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5130};
5131
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005132#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005133/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005134static const uint8_t spUnRecoverableMapError2[] = {
5135 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5136};
5137
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005138/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005139static const uint8_t spUnRecoverableMapError3[] = {
5140 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5141};
5142
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005143/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005144static const uint8_t spUnRecoverableMapError4[] = {
5145 0xbf,
5146 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5147 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5150 0xff
5151};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005152#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005153
5154
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005155int32_t EnterMapTest()
5156{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005157 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005158 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005159 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005160 int32_t nReturn;
5161 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005162
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005163#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005164 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005165 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005166
Laurence Lundbladef0499502020-08-01 11:55:57 -07005167
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005168 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005169 QCBORDecode_ExitArray(&DCtx);
5170
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005171 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5172 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005173 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005174 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005175 QCBORDecode_ExitArray(&DCtx);
5176 QCBORDecode_ExitArray(&DCtx);
5177
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005178 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5179 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005180 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005181 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005182 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005183 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005184 QCBORDecode_ExitArray(&DCtx);
5185 QCBORDecode_ExitMap(&DCtx);
5186
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005187 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005188 QCBORDecode_ExitMap(&DCtx);
5189
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005190 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005191 QCBORDecode_ExitArray(&DCtx);
5192
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005193 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5194 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005195 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005196 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005197 QCBORDecode_ExitArray(&DCtx);
5198 QCBORDecode_ExitArray(&DCtx);
5199
5200 QCBORDecode_ExitMap(&DCtx);
5201
5202 uErr = QCBORDecode_Finish(&DCtx);
5203 if(uErr != QCBOR_SUCCESS){
5204 return 3011;
5205 }
5206
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005207 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005208 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005209 if(nReturn) {
5210 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005211 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005212#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5213
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005214
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005215 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005216 if(nReturn) {
5217 return nReturn;
5218 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005219
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005220
Laurence Lundblade937ea812020-05-08 11:38:23 -07005221
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005222 // These tests confirm the cursor is at the right place after entering
5223 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005224 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005225
5226 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005227 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005228 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005229 QCBORDecode_GetNext(&DCtx, &Item1);
5230 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5231 return 2001;
5232 }
5233
5234
Laurence Lundblade9b334962020-08-27 10:55:53 -07005235 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005236 QCBORDecode_VGetNext(&DCtx, &Item1);
5237 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005238 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5239 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5240 UsefulBuf_Compare(ArrayItem.label.string,
5241 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5242 return 2051;
5243 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005244 QCBORDecode_GetNext(&DCtx, &Item1);
5245 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5246 return 2002;
5247 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005248 QCBORDecode_ExitArray(&DCtx);
5249 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5250 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5251 UsefulBuf_Compare(ArrayItem.label.string,
5252 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5253 return 2052;
5254 }
5255
Laurence Lundblade937ea812020-05-08 11:38:23 -07005256
Laurence Lundblade9b334962020-08-27 10:55:53 -07005257 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005258 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005259 QCBORDecode_GetNext(&DCtx, &Item1);
5260 QCBORDecode_GetNext(&DCtx, &Item1);
5261 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005262 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5263 QCBORDecode_GetNext(&DCtx, &Item1);
5264 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5265 return 2003;
5266 }
5267
Laurence Lundblade9b334962020-08-27 10:55:53 -07005268 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005269 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005270 QCBORDecode_GetNext(&DCtx, &Item1);
5271 QCBORDecode_GetNext(&DCtx, &Item1);
5272 QCBORDecode_GetNext(&DCtx, &Item1);
5273 QCBORDecode_GetNext(&DCtx, &Item1);
5274 QCBORDecode_GetNext(&DCtx, &Item1);
5275 QCBORDecode_GetNext(&DCtx, &Item1);
5276 QCBORDecode_GetNext(&DCtx, &Item1);
5277 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5278 QCBORDecode_GetNext(&DCtx, &Item1);
5279 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005280 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005281 }
5282
Laurence Lundblade9b334962020-08-27 10:55:53 -07005283 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005284 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005285 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5286 QCBORDecode_ExitArray(&DCtx);
5287 QCBORDecode_GetNext(&DCtx, &Item1);
5288 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5289 return 2006;
5290 }
5291 QCBORDecode_ExitMap(&DCtx);
5292 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5293 return 2007;
5294 }
5295
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005296 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005297 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005298 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005299 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5300 uErr = QCBORDecode_GetAndResetError(&DCtx);
5301 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005302 return 2008;
5303 }
5304 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005305 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005306 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005307 return 2009;
5308 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005309
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005310
5311 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005312 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005313 // This will fail because the map is empty.
5314 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5315 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005316 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005317 return 2010;
5318 }
5319 QCBORDecode_ExitMap(&DCtx);
5320 uErr = QCBORDecode_Finish(&DCtx);
5321 if(uErr != QCBOR_SUCCESS){
5322 return 2011;
5323 }
5324
5325
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005326#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005327 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005328 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005329 // This will fail because the map is empty.
5330 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5331 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005332 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005333 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005334 }
5335 QCBORDecode_ExitMap(&DCtx);
5336 uErr = QCBORDecode_Finish(&DCtx);
5337 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005338 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005339 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005340#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005341
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005342
5343 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005344 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005345 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005346 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005347 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005348 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005349 QCBORDecode_ExitArray(&DCtx);
5350 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5351 QCBORDecode_ExitArray(&DCtx);
5352 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005353 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005354 return 2014;
5355 }
5356
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005357 int64_t nInt;
5358 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005359 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005360 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005361 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005362 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5363 return 2021;
5364 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005365 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5366 return 2121;
5367 }
5368 (void)QCBORDecode_GetAndResetError(&DCtx);
5369
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005370
5371 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5372 uErr = QCBORDecode_GetAndResetError(&DCtx);
5373 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5374 return 2022;
5375 }
5376
5377 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5378 uErr = QCBORDecode_GetAndResetError(&DCtx);
5379 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5380 return 2023;
5381 }
5382
5383 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5384 uErr = QCBORDecode_GetAndResetError(&DCtx);
5385#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5386 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5387 return 2024;
5388 }
5389#else
5390 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5391 return 2027;
5392 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005393#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005394
5395 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5396 uErr = QCBORDecode_GetAndResetError(&DCtx);
5397 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5398 return 2025;
5399 }
5400
5401 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5402
5403 QCBORDecode_ExitMap(&DCtx);
5404 uErr = QCBORDecode_Finish(&DCtx);
5405 if(uErr != QCBOR_SUCCESS) {
5406 return 2026;
5407 }
5408
5409 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005410 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005411 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5412 uErr = QCBORDecode_GetAndResetError(&DCtx);
5413 if(uErr != QCBOR_ERR_BAD_BREAK) {
5414 return 2030;
5415 }
5416
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005417#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005418 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005419 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005420 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5421 uErr = QCBORDecode_GetAndResetError(&DCtx);
5422 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5423 return 2031;
5424 }
5425
5426 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005427 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005428 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5429 uErr = QCBORDecode_GetAndResetError(&DCtx);
5430 if(uErr != QCBOR_ERR_HIT_END) {
5431 return 2032;
5432 }
5433
5434 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005435 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005436 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5437 uErr = QCBORDecode_GetAndResetError(&DCtx);
5438 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5439 return 2033;
5440 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005441#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5442
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005443
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005444 nReturn = DecodeNestedIterate();
5445
5446 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005447}
5448
5449
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005450struct NumberConversion {
5451 char *szDescription;
5452 UsefulBufC CBOR;
5453 int64_t nConvertedToInt64;
5454 QCBORError uErrorInt64;
5455 uint64_t uConvertToUInt64;
5456 QCBORError uErrorUint64;
5457 double dConvertToDouble;
5458 QCBORError uErrorDouble;
5459};
5460
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005461static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005462 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005463 "too large to fit into int64_t",
5464 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5465 0,
5466 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5467 0,
5468 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5469 ((double)INT64_MIN) + 1 ,
5470 QCBOR_SUCCESS
5471 },
5472 {
5473 "largest negative int that fits in int64_t",
5474 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5475 INT64_MIN,
5476 QCBOR_SUCCESS,
5477 0,
5478 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5479 (double)INT64_MIN,
5480 QCBOR_SUCCESS
5481 },
5482 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005483 "negative bignum -1",
5484 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5485 -1,
5486 QCBOR_SUCCESS,
5487 0,
5488 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5489 -1.0,
5490 QCBOR_SUCCESS
5491 },
5492 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005493 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005494 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5495 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005496#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005497 257000,
5498 QCBOR_SUCCESS,
5499 257000,
5500 QCBOR_SUCCESS,
5501 257000.0,
5502 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005503#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5504 0,
5505 QCBOR_ERR_UNEXPECTED_TYPE,
5506 0,
5507 QCBOR_ERR_UNEXPECTED_TYPE,
5508 0.0,
5509 QCBOR_ERR_UNEXPECTED_TYPE
5510#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005511 },
5512 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005513 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005514 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5515 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005516#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005517 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005518 QCBOR_SUCCESS,
5519 0,
5520 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005521 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005522 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005523#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5524 0,
5525 QCBOR_ERR_UNEXPECTED_TYPE,
5526 0,
5527 QCBOR_ERR_UNEXPECTED_TYPE,
5528 0.0,
5529 QCBOR_ERR_UNEXPECTED_TYPE
5530#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005531 },
5532 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005533 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005534 {(uint8_t[]){0xC5, 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 2056,
5538 QCBOR_SUCCESS,
5539 2056,
5540 QCBOR_SUCCESS,
5541 2056.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 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005554 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5555 0,
5556 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5557 0,
5558 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5559 -18446744073709551617.0,
5560 QCBOR_SUCCESS
5561 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005562#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005563 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005564 "Positive bignum 0x01020304 indefinite length string",
5565 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5566 0x01020304,
5567 QCBOR_SUCCESS,
5568 0x01020304,
5569 QCBOR_SUCCESS,
5570 16909060.0,
5571 QCBOR_SUCCESS
5572 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005573#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005574 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005575 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005576 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5577 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005578#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005579 0,
5580 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5581 0,
5582 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5583 -INFINITY,
5584 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005585#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5586 0,
5587 QCBOR_ERR_UNEXPECTED_TYPE,
5588 0,
5589 QCBOR_ERR_UNEXPECTED_TYPE,
5590 0.0,
5591 QCBOR_ERR_UNEXPECTED_TYPE
5592#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005593 },
5594 {
5595 "big float [9223372036854775806, 9223372036854775806]",
5596 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5597 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005598#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005599 0,
5600 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5601 0,
5602 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5603 INFINITY,
5604 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005605#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5606 0,
5607 QCBOR_ERR_UNEXPECTED_TYPE,
5608 0,
5609 QCBOR_ERR_UNEXPECTED_TYPE,
5610 0.0,
5611 QCBOR_ERR_UNEXPECTED_TYPE
5612#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005613 },
5614 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005615 "Big float 3 * 2^^2",
5616 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005617#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005618 12,
5619 QCBOR_SUCCESS,
5620 12,
5621 QCBOR_SUCCESS,
5622 12.0,
5623 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005624#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5625 0,
5626 QCBOR_ERR_UNEXPECTED_TYPE,
5627 0,
5628 QCBOR_ERR_UNEXPECTED_TYPE,
5629 0.0,
5630 QCBOR_ERR_UNEXPECTED_TYPE
5631#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005632 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005633 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005634 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005635 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5636 0,
5637 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5638 18446744073709551615ULL,
5639 QCBOR_SUCCESS,
5640 18446744073709551615.0,
5641 QCBOR_SUCCESS
5642 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005643 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005644 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005645 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5646 65536-1,
5647 QCBOR_SUCCESS,
5648 0xffff,
5649 QCBOR_SUCCESS,
5650 65535.0,
5651 QCBOR_SUCCESS
5652 },
5653 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005654 "Postive integer 0",
5655 {(uint8_t[]){0x0}, 1},
5656 0LL,
5657 QCBOR_SUCCESS,
5658 0ULL,
5659 QCBOR_SUCCESS,
5660 0.0,
5661 QCBOR_SUCCESS
5662 },
5663 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005664 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005665 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5666 -9223372036854775807-1, // INT64_MIN
5667 QCBOR_SUCCESS,
5668 0ULL,
5669 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5670 -9223372036854775808.0,
5671 QCBOR_SUCCESS
5672 },
5673 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005674 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005675 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005676#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005677 100L,
5678 QCBOR_SUCCESS,
5679 100ULL,
5680 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005681#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5682 0,
5683 QCBOR_ERR_HW_FLOAT_DISABLED,
5684 0,
5685 QCBOR_ERR_HW_FLOAT_DISABLED,
5686#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005687 100.3,
5688 QCBOR_SUCCESS
5689 },
5690 {
5691 "Floating point value NaN 0xfa7fc00000",
5692 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005693#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005694 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005695 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005696 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005697 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005698#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5699 0,
5700 QCBOR_ERR_HW_FLOAT_DISABLED,
5701 0,
5702 QCBOR_ERR_HW_FLOAT_DISABLED,
5703#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005704 NAN,
5705 QCBOR_SUCCESS
5706 },
5707 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005708 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005709 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005710#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5711#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5712 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005713 -4,
5714 QCBOR_SUCCESS,
5715 0,
5716 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5717 -4.0,
5718 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005719#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5720 // Float HW disabled
5721 -4,
5722 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5723 0,
5724 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5725 -4.0,
5726 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5727#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005728#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005729 // Half-precision disabled
5730 -4,
5731 QCBOR_ERR_HALF_PRECISION_DISABLED,
5732 0,
5733 QCBOR_ERR_HALF_PRECISION_DISABLED,
5734 -4.0,
5735 QCBOR_ERR_HALF_PRECISION_DISABLED
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005736#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005737 },
5738 {
5739 "Decimal fraction 3/10",
5740 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005741#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005742 0,
5743 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5744 0,
5745 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5746 0.30000000000000004,
5747 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005748#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5749 0,
5750 QCBOR_ERR_UNEXPECTED_TYPE,
5751 0,
5752 QCBOR_ERR_UNEXPECTED_TYPE,
5753 0.0,
5754 QCBOR_ERR_UNEXPECTED_TYPE
5755#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005756 },
5757 {
5758 "+inifinity",
5759 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5760#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5761 0,
5762 QCBOR_ERR_FLOAT_EXCEPTION,
5763 0,
5764 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5765#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5766 0,
5767 QCBOR_ERR_HW_FLOAT_DISABLED,
5768 0,
5769 QCBOR_ERR_HW_FLOAT_DISABLED,
5770#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5771 INFINITY,
5772 QCBOR_SUCCESS
5773 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005774
5775 {
5776 "extreme pos bignum",
5777 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5778 // 50 rows of 8 is 400 digits.
5779 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5780 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5781 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5782 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5783 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5784 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5785 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5786 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5787 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5788 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5789 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5790 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5791 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5792 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5793 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5794 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5795 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5796 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5797 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5798 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5799 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5800 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5801 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5802 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5803 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5804 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5805 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5806 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5807 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5808 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5809 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5810 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5811 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5812 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5813 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5814 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5815 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5816 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5817 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5818 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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},
5829 404},
5830 0,
5831 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5832 0,
5833 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5834#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5835 INFINITY,
5836 QCBOR_SUCCESS
5837#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5838 0,
5839 QCBOR_ERR_HW_FLOAT_DISABLED,
5840#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5841 },
5842
5843 {
5844 "extreme neg bignum",
5845 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5846 // 50 rows of 8 is 400 digits.
5847 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5848 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5849 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5850 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5851 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5852 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5853 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5854 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5855 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5856 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5857 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5858 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5859 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5860 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5861 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5862 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5863 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5864 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5865 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5866 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5867 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5868 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5869 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5870 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5871 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5872 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5873 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5874 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5875 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5876 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5877 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5878 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5879 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5880 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5881 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5882 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5883 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5884 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5885 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5886 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5887 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5888 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5889 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5890 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5891 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5892 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5893 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5894 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5895 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5896 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5897 404},
5898 0,
5899 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5900 0,
5901 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5902#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5903 -INFINITY,
5904 QCBOR_SUCCESS
5905#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5906 0,
5907 QCBOR_ERR_HW_FLOAT_DISABLED,
5908#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5909 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005910
5911 {
5912 "big float underflow [9223372036854775806, -9223372036854775806]",
5913 {(uint8_t[]){
5914 0xC5, 0x82,
5915 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5916 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5917#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5918 0,
5919 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5920 0,
5921 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5922 0,
5923 QCBOR_SUCCESS
5924#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5925 0,
5926 QCBOR_ERR_UNEXPECTED_TYPE,
5927 0,
5928 QCBOR_ERR_UNEXPECTED_TYPE,
5929 0.0,
5930 QCBOR_ERR_UNEXPECTED_TYPE
5931#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5932 },
5933
5934 {
5935 "bigfloat that evaluates to -INFINITY",
5936 {(uint8_t[]){
5937 0xC5, 0x82,
5938 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5939 0xC3, 0x42, 0x01, 0x01}, 15},
5940#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5941 0,
5942 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5943 0,
5944 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5945 -INFINITY,
5946 QCBOR_SUCCESS
5947#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5948 0,
5949 QCBOR_ERR_UNEXPECTED_TYPE,
5950 0,
5951 QCBOR_ERR_UNEXPECTED_TYPE,
5952 0.0,
5953 QCBOR_ERR_UNEXPECTED_TYPE
5954#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5955 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005956};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005957
5958
5959
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005960
5961static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
5962{
5963 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
5964#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5965 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
5966 return 1;
5967 }
5968#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5969 (void)Pool;
5970#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5971 return 0;
5972}
5973
5974
Laurence Lundblade313b2862020-05-16 01:23:06 -07005975int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005976{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005977 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
5978 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005979
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005980 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5981 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005982
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005983 // Set up the decoding context including a memory pool so that
5984 // indefinite length items can be checked
5985 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005986 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005987
5988 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005989 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5990 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005991 }
5992
5993 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005994 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005995 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005996 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005997 }
5998 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005999 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006000 }
6001
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006002 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006003 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6004 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006005 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006006
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006007 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006008 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006009 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006010 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006011 }
6012 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006013 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006014 }
6015
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006016 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006017 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6018 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006019 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006020#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006021 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006022 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006023 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006024 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006025 }
6026 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6027 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006028 // NaN's can't be compared for equality. A NaN is
6029 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006030 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006031 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006032 }
6033 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006034 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006035 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006036 }
6037 }
6038 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07006039#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006040 }
6041
6042 return 0;
6043}
6044
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006045
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006046
6047
Laurence Lundbladee3553422020-05-02 11:11:17 -07006048int32_t CBORSequenceDecodeTests(void)
6049{
6050 QCBORDecodeContext DCtx;
6051 QCBORItem Item;
6052 QCBORError uCBORError;
6053
6054 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006055
Laurence Lundbladee3553422020-05-02 11:11:17 -07006056 // The input for the date test happens to be a sequence so it
6057 // is reused. It is a sequence because it doesn't start as
6058 // an array or map.
6059 QCBORDecode_Init(&DCtx,
6060 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
6061 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006062
Laurence Lundbladee3553422020-05-02 11:11:17 -07006063 // Get the first item
6064 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6065 if(uCBORError != QCBOR_SUCCESS) {
6066 return 1;
6067 }
6068 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
6069 return 2;
6070 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006071
Laurence Lundbladee3553422020-05-02 11:11:17 -07006072 // Get a second item
6073 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07006074 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
6075 return 66;
6076 }
6077
6078 // Get a third item
6079 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006080 if(uCBORError != QCBOR_SUCCESS) {
6081 return 2;
6082 }
6083 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6084 return 3;
6085 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006086
Laurence Lundbladee3553422020-05-02 11:11:17 -07006087 // A sequence can have stuff at the end that may
6088 // or may not be valid CBOR. The protocol decoder knows
6089 // when to stop by definition of the protocol, not
6090 // when the top-level map or array is ended.
6091 // Finish still has to be called to know that
6092 // maps and arrays (if there were any) were closed
6093 // off correctly. When called like this it
6094 // must return the error QCBOR_ERR_EXTRA_BYTES.
6095 uCBORError = QCBORDecode_Finish(&DCtx);
6096 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6097 return 4;
6098 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006099
6100
Laurence Lundbladee3553422020-05-02 11:11:17 -07006101 // --- Test an empty input ----
6102 uint8_t empty[1];
6103 UsefulBufC Empty = {empty, 0};
6104 QCBORDecode_Init(&DCtx,
6105 Empty,
6106 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006107
Laurence Lundbladee3553422020-05-02 11:11:17 -07006108 uCBORError = QCBORDecode_Finish(&DCtx);
6109 if(uCBORError != QCBOR_SUCCESS) {
6110 return 5;
6111 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006112
6113
Laurence Lundbladee3553422020-05-02 11:11:17 -07006114 // --- Sequence with unclosed indefinite length array ---
6115 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006116
Laurence Lundbladee3553422020-05-02 11:11:17 -07006117 QCBORDecode_Init(&DCtx,
6118 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6119 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006120
Laurence Lundbladee3553422020-05-02 11:11:17 -07006121 // Get the first item
6122 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6123 if(uCBORError != QCBOR_SUCCESS) {
6124 return 7;
6125 }
6126 if(Item.uDataType != QCBOR_TYPE_INT64) {
6127 return 8;
6128 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006129
Laurence Lundbladee3553422020-05-02 11:11:17 -07006130 // Get a second item
6131 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006132#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006133 if(uCBORError != QCBOR_SUCCESS) {
6134 return 9;
6135 }
6136 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6137 return 10;
6138 }
6139
6140 // Try to finish before consuming all bytes to confirm
6141 // that the still-open error is returned.
6142 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006143 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006144 return 11;
6145 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006146#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6147 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6148 return 20;
6149 }
6150#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006151
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006152
Laurence Lundbladee3553422020-05-02 11:11:17 -07006153 // --- Sequence with a closed indefinite length array ---
6154 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006155
Laurence Lundbladee3553422020-05-02 11:11:17 -07006156 QCBORDecode_Init(&DCtx,
6157 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6158 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006159
Laurence Lundbladee3553422020-05-02 11:11:17 -07006160 // Get the first item
6161 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6162 if(uCBORError != QCBOR_SUCCESS) {
6163 return 12;
6164 }
6165 if(Item.uDataType != QCBOR_TYPE_INT64) {
6166 return 13;
6167 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006168
Laurence Lundbladee3553422020-05-02 11:11:17 -07006169 // Get a second item
6170 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006171#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6172
Laurence Lundbladee3553422020-05-02 11:11:17 -07006173 if(uCBORError != QCBOR_SUCCESS) {
6174 return 14;
6175 }
6176 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6177 return 15;
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);
6183 if(uCBORError != QCBOR_SUCCESS) {
6184 return 16;
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 return 0;
6194}
6195
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006196
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006197
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006198int32_t IntToTests()
6199{
6200 int nErrCode;
6201 int32_t n32;
6202 int16_t n16;
6203 int8_t n8;
6204 uint32_t u32;
6205 uint16_t u16;
6206 uint8_t u8;
6207 uint64_t u64;
6208
6209 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6210 if(nErrCode == -1 || n32 != 1) {
6211 return 1;
6212 }
6213
6214 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6215 if(nErrCode == -1 || n32 != INT32_MAX) {
6216 return 2;
6217 }
6218
6219 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6220 if(nErrCode == -1 || n32 != INT32_MIN) {
6221 return 3;
6222 }
6223
6224 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6225 if(nErrCode != -1) {
6226 return 4;
6227 }
6228
6229 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6230 if(nErrCode != -1) {
6231 return 5;
6232 }
6233
6234
6235 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6236 if(nErrCode == -1 || n16 != INT16_MAX) {
6237 return 6;
6238 }
6239
6240 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6241 if(nErrCode == -1 || n16 != INT16_MIN) {
6242 return 7;
6243 }
6244
6245 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6246 if(nErrCode == -1 || n16 != 1) {
6247 return 8;
6248 }
6249
6250 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6251 if(nErrCode != -1) {
6252 return 9;
6253 }
6254
6255 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6256 if(nErrCode != -1) {
6257 return 10;
6258 }
6259
6260
6261 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6262 if(nErrCode == -1 || n8 != 1) {
6263 return 11;
6264 }
6265
6266 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6267 if(nErrCode == -1 || n8 != INT8_MAX) {
6268 return 12;
6269 }
6270
6271 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6272 if(nErrCode == -1 || n8 != INT8_MIN) {
6273 return 13;
6274 }
6275
6276 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6277 if(nErrCode != -1) {
6278 return 14;
6279 }
6280
6281 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6282 if(nErrCode != -1) {
6283 return 15;
6284 }
6285
6286
6287 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6288 if(nErrCode == -1 || u32 != 1) {
6289 return 16;
6290 }
6291
6292 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6293 if(nErrCode == -1 || u32 != UINT32_MAX) {
6294 return 17;
6295 }
6296
6297 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6298 if(nErrCode == -1 || u32 != 0) {
6299 return 18;
6300 }
6301
6302 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6303 if(nErrCode != -1) {
6304 return 19;
6305 }
6306
6307 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6308 if(nErrCode != -1) {
6309 return 20;
6310 }
6311
6312
6313 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6314 if(nErrCode == -1 || u16 != UINT16_MAX) {
6315 return 21;
6316 }
6317
6318 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6319 if(nErrCode == -1 || u16 != 0) {
6320 return 22;
6321 }
6322
6323 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6324 if(nErrCode == -1 || u16 != 1) {
6325 return 23;
6326 }
6327
6328 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6329 if(nErrCode != -1) {
6330 return 24;
6331 }
6332
6333 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6334 if(nErrCode != -1) {
6335 return 25;
6336 }
6337
6338
6339 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6340 if(nErrCode == -1 || u8 != UINT8_MAX) {
6341 return 26;
6342 }
6343
6344 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6345 if(nErrCode == -1 || u8 != 0) {
6346 return 27;
6347 }
6348
6349 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6350 if(nErrCode == -1 || u8 != 1) {
6351 return 28;
6352 }
6353
6354 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6355 if(nErrCode != -1) {
6356 return 29;
6357 }
6358
6359 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6360 if(nErrCode != -1) {
6361 return 30;
6362 }
6363
6364
6365 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6366 if(nErrCode == -1 || u64 != 1) {
6367 return 31;
6368 }
6369
6370 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6371 if(nErrCode == -1 || u64 != INT64_MAX) {
6372 return 32;
6373 }
6374
6375 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6376 if(nErrCode == -1 || u64 != 0) {
6377 return 33;
6378 }
6379
6380 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6381 if(nErrCode != -1) {
6382 return 34;
6383 }
6384
6385 return 0;
6386}
6387
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006388
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006389
6390
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006391/*
6392A sequence with
6393 A wrapping bstr
6394 containing a map
6395 1
6396 2
6397 A wrapping bstr
6398 containing an array
6399 3
6400 wrapping bstr
6401 4
6402 5
6403 6
6404 array
6405 7
6406 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006407 */
6408
Laurence Lundblade55013642020-09-23 05:39:22 -07006409static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006410{
Laurence Lundblade55013642020-09-23 05:39:22 -07006411 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006412 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006413 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006414
Laurence Lundblade55013642020-09-23 05:39:22 -07006415 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006416
6417 QCBOREncode_BstrWrap(&EC);
6418 QCBOREncode_OpenMap(&EC);
6419 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6420 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6421 QCBOREncode_CloseMap(&EC);
6422 QCBOREncode_BstrWrap(&EC);
6423 QCBOREncode_OpenArray(&EC);
6424 QCBOREncode_AddInt64(&EC, 3);
6425 QCBOREncode_BstrWrap(&EC);
6426 QCBOREncode_AddInt64(&EC, 4);
6427 QCBOREncode_CloseBstrWrap(&EC, NULL);
6428 QCBOREncode_AddInt64(&EC, 5);
6429 QCBOREncode_CloseArray(&EC);
6430 QCBOREncode_CloseBstrWrap(&EC, NULL);
6431 QCBOREncode_AddInt64(&EC, 6);
6432 QCBOREncode_CloseBstrWrap(&EC, NULL);
6433 QCBOREncode_OpenArray(&EC);
6434 QCBOREncode_AddInt64(&EC, 7);
6435 QCBOREncode_AddInt64(&EC, 8);
6436 QCBOREncode_CloseArray(&EC);
6437
6438 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006439 if(uErr) {
6440 Encoded = NULLUsefulBufC;
6441 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006442
6443 return Encoded;
6444}
6445
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006446/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006447static const uint8_t spBreakInByteString[] = {
6448 0x41, 0xff
6449};
6450
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006451
6452int32_t EnterBstrTest()
6453{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006454 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006455
6456 QCBORDecodeContext DC;
6457
Laurence Lundblade55013642020-09-23 05:39:22 -07006458 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006459
Laurence Lundblade55013642020-09-23 05:39:22 -07006460 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006461
6462
Laurence Lundblade9b334962020-08-27 10:55:53 -07006463 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006464 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006465 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6466 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006467 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006468 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006469 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006470 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006471 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006472 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006473 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006474 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006475 QCBORDecode_ExitArray(&DC);
6476 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006477 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006478 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006479 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006480 QCBORDecode_GetInt64(&DC, &n7);
6481 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006482 QCBORDecode_ExitArray(&DC);
6483
6484 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006485 if(uErr) {
6486 return (int32_t)uErr;
6487 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006488
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006489
6490 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6491 * Successful because no items are fetched from byte string.
6492 */
6493 QCBORDecode_Init(&DC,
6494 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6495 0);
6496 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6497 uErr = QCBORDecode_GetError(&DC);
6498 if(uErr) {
6499 return 100 + (int32_t)uErr;
6500 }
6501
6502 QCBORDecode_ExitBstrWrapped(&DC);
6503 uErr = QCBORDecode_GetError(&DC);
6504 if(uErr) {
6505 return 200 + (int32_t)uErr;
6506 }
6507
6508 /* Try to get item that is a break out of a byte string wrapped CBOR.
6509 * It fails because there should be no break.
6510 */
6511 QCBORDecode_Init(&DC,
6512 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6513 0);
6514 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6515 QCBORItem Item;
6516 uErr = QCBORDecode_GetNext(&DC, &Item);
6517 if(uErr != QCBOR_ERR_BAD_BREAK) {
6518 return 300 + (int32_t)uErr;
6519 }
6520
6521 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006522}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006523
6524
6525
6526
6527static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006528 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006529
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006530 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006531 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006532 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6533 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6534 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006535
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006536 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006537 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6538 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6539 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006540
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006541 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006542 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006543 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6544 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006545
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006546 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006547 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6548 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006549
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006550 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006551 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006552 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6553 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006554
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006555 21,
6556 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6557 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6558
6559 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006560 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006561 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6562 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006563
6564 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006565 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6566 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006567
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006568 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006569 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006570 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6571 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006572
6573 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006574 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6575 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006576
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006577 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006578 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006579 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6580 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006581
6582 0x18, 0x33,
6583 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6584
6585 // MIME
6586 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006587 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6588 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6589 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006590
6591 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006592 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6593 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006594
6595 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006596 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6597 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6598 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006599
6600 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006601 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6602 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006603
6604 // UUID
6605 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006606 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6607 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006608
6609 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006610 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6611 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006612};
6613
6614int32_t DecodeTaggedTypeTests()
6615{
6616 QCBORDecodeContext DC;
6617 QCBORError uErr;
6618
6619 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6620
6621 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006622 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006623
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006624 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006625 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006626 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006627 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6628 return 1;
6629 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006630 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006631 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6632 return 2;
6633 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006634 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006635 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6636 return 3;
6637 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006638 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006639 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006640 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6641 return 4;
6642 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006643 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006644 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006645 return 5;
6646 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006647
Laurence Lundblade9b334962020-08-27 10:55:53 -07006648 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006649 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6650 bNeg != false) {
6651 return 10;
6652 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006653 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006654 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6655 bNeg != true) {
6656 return 11;
6657 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006658 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006659 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6660 return 12;
6661 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006662 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006663 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006664 return 13;
6665 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006666 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006667 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006668 return 14;
6669 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006670
Laurence Lundblade9b334962020-08-27 10:55:53 -07006671 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006672 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6673 return 20;
6674 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006675 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006676 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6677 return 21;
6678 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006679 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006680 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006681 return 22;
6682 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006683 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_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 23;
6686 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006687
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006688#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006689 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006690 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6691 return 30;
6692 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006693#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006694 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006695 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6696 return 31;
6697 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006698 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006699 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006700 return 32;
6701 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006702 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
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 33;
6705 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006706
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006707#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006708 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006709 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6710 return 40;
6711 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006712#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006713 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006714 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6715 return 41;
6716 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006717 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006718 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006719 return 42;
6720 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006721 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006722 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006723 return 43;
6724 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006725
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006726#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006727 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006728 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6729 return 50;
6730 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006731#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006732 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006733 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6734 return 51;
6735 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006736 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006737 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006738 return 52;
6739 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006740 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006741 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006742 return 53;
6743 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006744
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006745#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006746 // MIME
6747 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006748 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006749 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6750 bIsNot7Bit == true) {
6751 return 60;
6752 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006753 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006754 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6755 bIsNot7Bit == true) {
6756 return 61;
6757 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006758 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006759 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6760 bIsNot7Bit == false) {
6761 return 62;
6762 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006763 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006764 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6765 bIsNot7Bit == false) {
6766 return 63;
6767 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006768 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006769 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006770 return 64;
6771 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006772 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006773 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006774 return 65;
6775 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006776
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006777
Laurence Lundblade9b334962020-08-27 10:55:53 -07006778 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006779 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6780 return 70;
6781 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006782#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
6783
Laurence Lundblade9b334962020-08-27 10:55:53 -07006784 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006785 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6786 return 71;
6787 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006788 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006789 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006790 return 72;
6791 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006792 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006793 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006794 return 73;
6795 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006796
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006797 // Improvement: add some more error test cases
6798
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006799 QCBORDecode_ExitMap(&DC);
6800
6801 uErr = QCBORDecode_Finish(&DC);
6802 if(uErr != QCBOR_SUCCESS) {
6803 return 100;
6804 }
6805
6806 return 0;
6807}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006808
6809
6810
6811
6812/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006813 [
6814 "aaaaaaaaaa",
6815 {}
6816 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006817 */
6818static const uint8_t spTooLarge1[] = {
6819 0x9f,
6820 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6821 0xa0,
6822 0xff
6823};
6824
6825/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006826 [
6827 {
6828 0: "aaaaaaaaaa"
6829 }
6830 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006831 */
6832static const uint8_t spTooLarge2[] = {
6833 0x9f,
6834 0xa1,
6835 0x00,
6836 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6837 0xff
6838};
6839
6840/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006841 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006842
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006843 {
6844 0: "aaaaaaaaaa"
6845 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006846 */
6847static const uint8_t spTooLarge3[] = {
6848 0x4d,
6849 0xa1,
6850 0x00,
6851 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6852};
6853
6854int32_t TooLargeInputTest(void)
6855{
6856 QCBORDecodeContext DC;
6857 QCBORError uErr;
6858 UsefulBufC String;
6859
6860 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6861 // to 10 There's not really any way to test this error
6862 // condition. The error condition is not complex, so setting
6863 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6864
6865 // The input CBOR is only too large because the
6866 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6867 //
6868 // This test is disabled for the normal test runs because of the
6869 // special build requirement.
6870
6871
6872 // Tests the start of a map being too large
6873 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006874 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006875 QCBORDecode_GetTextString(&DC, &String);
6876 uErr = QCBORDecode_GetError(&DC);
6877 if(uErr != QCBOR_SUCCESS) {
6878 return 1;
6879 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006880 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006881 uErr = QCBORDecode_GetError(&DC);
6882 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6883 return 2;
6884 }
6885
6886 // Tests the end of a map being too large
6887 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006888 QCBORDecode_EnterArray(&DC, NULL);
6889 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006890 uErr = QCBORDecode_GetError(&DC);
6891 if(uErr != QCBOR_SUCCESS) {
6892 return 3;
6893 }
6894 QCBORDecode_ExitMap(&DC);
6895 uErr = QCBORDecode_GetError(&DC);
6896 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6897 return 4;
6898 }
6899
6900 // Tests the entire input CBOR being too large when processing bstr wrapping
6901 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6902 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6903 uErr = QCBORDecode_GetError(&DC);
6904 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6905 return 5;
6906 }
6907
6908 return 0;
6909}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006910
6911
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006912#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6913
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006914static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006915 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006916 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6917 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6918 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6919 0x03,
6920 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6921 0xc3,
6922 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006923};
6924
6925int32_t SpiffyIndefiniteLengthStringsTests()
6926{
6927 QCBORDecodeContext DCtx;
6928
6929 QCBORDecode_Init(&DCtx,
6930 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6931 QCBOR_DECODE_MODE_NORMAL);
6932
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006933 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006934 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6935
6936 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006937 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006938 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6939 if(QCBORDecode_GetAndResetError(&DCtx)) {
6940 return 1;
6941 }
6942
6943 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6944 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6945 return 2;
6946 }
6947
6948 uint64_t uInt;
6949 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6950 if(QCBORDecode_GetAndResetError(&DCtx)) {
6951 return 3;
6952 }
6953 if(uInt != 3) {
6954 return 4;
6955 }
6956
6957 double uDouble;
6958 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6959 "label2",
6960 0xff,
6961 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006962#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006963 if(QCBORDecode_GetAndResetError(&DCtx)) {
6964 return 5;
6965 }
6966 if(uDouble != -16777474) {
6967 return 6;
6968 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006969#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006970 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6971 return 7;
6972 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006973#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006974
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006975
6976 QCBORDecode_ExitMap(&DCtx);
6977
6978 if(QCBORDecode_Finish(&DCtx)) {
6979 return 99;
6980 }
6981
6982 return 0;
6983}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006984#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08006985
6986
6987
6988int32_t PeekTest()
6989{
6990 QCBORItem Item;
6991 QCBORError nCBORError;
6992 QCBORDecodeContext DCtx;
6993
6994 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6995
6996 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6997 return 100+(int32_t)nCBORError;
6998 }
6999 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7000 return 200;
7001 }
7002
7003 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7004 return (int32_t)nCBORError;
7005 }
7006 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7007 return 300;
7008 }
7009
7010 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7011 return 400 + (int32_t)nCBORError;
7012 }
7013 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7014 return 500;
7015 }
7016
7017 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7018 return (int32_t)nCBORError;
7019 }
7020 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7021 return 600;
7022 }
7023
7024 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7025 return 900 + (int32_t)nCBORError;
7026 }
7027 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7028 Item.uDataType != QCBOR_TYPE_INT64 ||
7029 Item.val.int64 != 42 ||
7030 Item.uDataAlloc ||
7031 Item.uLabelAlloc ||
7032 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7033 return 1000;
7034 }
7035
7036 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7037 return 1100 + (int32_t)nCBORError;
7038 }
7039
7040 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7041 Item.uDataType != QCBOR_TYPE_INT64 ||
7042 Item.val.int64 != 42 ||
7043 Item.uDataAlloc ||
7044 Item.uLabelAlloc ||
7045 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7046 return 1200;
7047 }
7048
7049
7050 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7051 return 1300 + (int32_t)nCBORError;
7052 }
7053 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7054 Item.uDataAlloc ||
7055 Item.uLabelAlloc ||
7056 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7057 Item.uDataType != QCBOR_TYPE_ARRAY ||
7058 Item.val.uCount != 2)
7059 return 1400;
7060
7061 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7062 return 1500 + (int32_t)nCBORError;
7063 }
7064 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7065 Item.uDataAlloc ||
7066 Item.uLabelAlloc ||
7067 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7068 return 1600;
7069 }
7070
7071 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7072 return 1700 + (int32_t)nCBORError;
7073 }
7074 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7075 Item.uDataAlloc ||
7076 Item.uLabelAlloc ||
7077 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7078 return 1800;
7079 }
7080
7081 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7082 return (int32_t)nCBORError;
7083 }
7084 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7085 Item.uDataAlloc ||
7086 Item.uLabelAlloc ||
7087 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7088 return 1900;
7089 }
7090
7091 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7092 return (int32_t)nCBORError;
7093 }
7094 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7095 Item.uDataAlloc ||
7096 Item.uLabelAlloc ||
7097 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7098 return 2000;
7099 }
7100
7101
7102 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7103 return 2100 + (int32_t)nCBORError;
7104 }
7105 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7106 Item.uDataAlloc ||
7107 Item.uLabelAlloc ||
7108 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7109 Item.uDataType != QCBOR_TYPE_MAP ||
7110 Item.val.uCount != 4) {
7111 return 2100;
7112 }
7113
7114 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7115 return 2200 + (int32_t)nCBORError;
7116 }
7117 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7118 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7119 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7120 Item.uDataAlloc ||
7121 Item.uLabelAlloc ||
7122 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7123 return 2300;
7124 }
7125
7126 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7127 return 2400 + (int32_t)nCBORError;
7128 }
7129 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7130 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7131 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7132 Item.uDataAlloc ||
7133 Item.uLabelAlloc ||
7134 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7135 return 2500;
7136 }
7137
7138 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7139 return 2600 + (int32_t)nCBORError;
7140 }
7141 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7142 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7143 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7144 Item.uDataAlloc ||
7145 Item.uLabelAlloc ||
7146 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7147 return 2700;
7148 }
7149
7150 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7151 return 2800 + (int32_t)nCBORError;
7152 }
7153 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7154 Item.uDataAlloc ||
7155 Item.uLabelAlloc ||
7156 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7157 Item.uDataType != QCBOR_TYPE_INT64 ||
7158 Item.val.int64 != 98)
7159 return 2900;
7160
7161 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7162 return 3000 + (int32_t)nCBORError;
7163 }
7164 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7165 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7166 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7167 Item.uDataAlloc ||
7168 Item.uLabelAlloc ||
7169 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7170 return 3100;
7171 }
7172
7173 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7174 return 3200 + (int32_t)nCBORError;
7175 }
7176 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7177 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7178 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7179 Item.uDataAlloc ||
7180 Item.uLabelAlloc ||
7181 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7182 return 3300;
7183 }
7184
7185 return 0;
7186}