blob: f78ee8d8cb19c09f460ff5c734dec440843dacb3 [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
65
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070066static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080067 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
69 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
70 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
71 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
72 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
73 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
74 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
75 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
76 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
77 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
78 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
79 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
80 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
81 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
82 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
83 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
84 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
85 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
86 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
87 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
88 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 0xff, 0xff};
91
92
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080093// return CBOR error or -1 if type of value doesn't match
94
Laurence Lundbladec5fef682020-01-25 11:38:45 -080095static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080096{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070097 QCBORItem Item;
98 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080099
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800100 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700101 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102 if(Item.uDataType != QCBOR_TYPE_ARRAY)
103 return -1;
104
105 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700106 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800107 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800108 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 return -1;
110
111 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700112 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800114 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800116
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700118 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800120 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700124 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800126 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800128
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700130 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800131 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800132 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800134
135
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800136 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700137 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 if(Item.uDataType != QCBOR_TYPE_INT64 ||
139 Item.val.int64 != -2147483648)
140 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800141
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800142 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700143 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if(Item.uDataType != QCBOR_TYPE_INT64 ||
145 Item.val.int64 != -2147483647)
146 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800147
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700149 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if(Item.uDataType != QCBOR_TYPE_INT64 ||
151 Item.val.int64 != -65538)
152 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800153
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800154 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700155 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if(Item.uDataType != QCBOR_TYPE_INT64 ||
157 Item.val.int64 != -65537)
158 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800159
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800160 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700161 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800162 if(Item.uDataType != QCBOR_TYPE_INT64 ||
163 Item.val.int64 != -65536)
164 return -1;
165
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700168 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if(Item.uDataType != QCBOR_TYPE_INT64 ||
170 Item.val.int64 != -65535)
171 return -1;
172
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 ||
177 Item.val.int64 != -65534)
178 return -1;
179
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800180
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700182 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
184 Item.val.int64 != -257)
185 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800186
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 != -256)
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 != -255)
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 != -254)
203 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800204
205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700207 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
209 Item.val.int64 != -25)
210 return -1;
211
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800212
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800213 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700214 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 if(Item.uDataType != QCBOR_TYPE_INT64 ||
216 Item.val.int64 != -24)
217 return -1;
218
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700221 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if(Item.uDataType != QCBOR_TYPE_INT64 ||
223 Item.val.int64 != -23)
224 return -1;
225
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800227 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700228 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800229 if(Item.uDataType != QCBOR_TYPE_INT64 ||
230 Item.val.int64 != -1)
231 return -1;
232
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800233
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800234 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700235 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 if(Item.uDataType != QCBOR_TYPE_INT64 ||
237 Item.val.int64 != 0)
238 return -1;
239
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800241 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700242 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 if(Item.uDataType != QCBOR_TYPE_INT64 ||
244 Item.val.int64 != 0)
245 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700248 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(Item.uDataType != QCBOR_TYPE_INT64 ||
250 Item.val.int64 != 1)
251 return -1;
252
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800254 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700255 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 if(Item.uDataType != QCBOR_TYPE_INT64 ||
257 Item.val.int64 != 22)
258 return -1;
259
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700262 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
264 Item.val.int64 != 23)
265 return -1;
266
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800268 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700269 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800270 if(Item.uDataType != QCBOR_TYPE_INT64 ||
271 Item.val.int64 != 24)
272 return -1;
273
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800274
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800275 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700276 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800277 if(Item.uDataType != QCBOR_TYPE_INT64 ||
278 Item.val.int64 != 25)
279 return -1;
280
281 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700282 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
284 Item.val.int64 != 26)
285 return -1;
286
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700289 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(Item.uDataType != QCBOR_TYPE_INT64 ||
291 Item.val.int64 != 254)
292 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800293
294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700296 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(Item.uDataType != QCBOR_TYPE_INT64 ||
298 Item.val.int64 != 255)
299 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800300
301
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700303 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(Item.uDataType != QCBOR_TYPE_INT64 ||
305 Item.val.int64 != 256)
306 return -1;
307
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800308
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800309 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700310 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(Item.uDataType != QCBOR_TYPE_INT64 ||
312 Item.val.int64 != 257)
313 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800315 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700316 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(Item.uDataType != QCBOR_TYPE_INT64 ||
318 Item.val.int64 != 65534)
319 return -1;
320
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700323 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(Item.uDataType != QCBOR_TYPE_INT64 ||
325 Item.val.int64 != 65535)
326 return -1;
327
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700330 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
332 Item.val.int64 != 65536)
333 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800334
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800335 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700336 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 if(Item.uDataType != QCBOR_TYPE_INT64 ||
338 Item.val.int64 != 65537)
339 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800341 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700342 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(Item.uDataType != QCBOR_TYPE_INT64 ||
344 Item.val.int64 != 65538)
345 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800347 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700348 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(Item.uDataType != QCBOR_TYPE_INT64 ||
350 Item.val.int64 != 2147483647)
351 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800352
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 != 2147483647)
357 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800359 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700360 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(Item.uDataType != QCBOR_TYPE_INT64 ||
362 Item.val.int64 != 2147483648)
363 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800364
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800365 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700366 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(Item.uDataType != QCBOR_TYPE_INT64 ||
368 Item.val.int64 != 2147483649)
369 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800370
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800371 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700372 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 if(Item.uDataType != QCBOR_TYPE_INT64 ||
374 Item.val.int64 != 4294967294)
375 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800378 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700379 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 if(Item.uDataType != QCBOR_TYPE_INT64 ||
381 Item.val.int64 != 4294967295)
382 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800383
384
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800385 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700386 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(Item.uDataType != QCBOR_TYPE_INT64 ||
388 Item.val.int64 != 4294967296)
389 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800390
391
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 != 4294967297)
396 return -1;
397
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800398
399
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800400 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700401 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800402 if(Item.uDataType != QCBOR_TYPE_INT64 ||
403 Item.val.int64 != 9223372036854775807LL)
404 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800405
406
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800407 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700408 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
410 Item.val.uint64 != 18446744073709551615ULL)
411 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800412
413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
415 return -1;
416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800418 return 0;
419}
420
421
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800422// One less than the smallest negative integer allowed in C. Decoding
423// this should fail.
424static const uint8_t spTooSmallNegative[] = {
425 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000426};
427
428
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800429/*
430 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800431 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800433int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000435 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800436 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000438 QCBORDecode_Init(&DCtx,
439 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
440 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800441
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000442 // The really big test of all successes
443 nReturn = IntegerValuesParseTestInternal(&DCtx);
444 if(nReturn) {
445 return nReturn;
446 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800447
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000448 // The one large negative integer that can be parsed
449 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800450 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000451 QCBOR_DECODE_MODE_NORMAL);
452
453 QCBORItem item;
454 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
455 nReturn = -4000;
456 }
457
458 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459}
460
461
462/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800463 Creates a simple CBOR array and returns it in *pEncoded. The array is
464 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 */
469
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800470static uint8_t spSimpleArrayBuffer[50];
471
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800472static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473{
474 QCBOREncodeContext ECtx;
475 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800476
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 *pEncoded = NULL;
478 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800479
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800481 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 // and last with the buffer to do the actual encoding
483 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700484 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800485 QCBOREncode_OpenArray(&ECtx);
486 QCBOREncode_AddInt64(&ECtx, nInt1);
487 QCBOREncode_AddInt64(&ECtx, nInt2);
488 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
489 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
490 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800491
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800492 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800493 goto Done;
494
495 if(*pEncoded != NULL) {
496 nReturn = 0;
497 goto Done;
498 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800499
500 // Use static buffer to avoid dependency on malloc()
501 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 goto Done;
503 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800504 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800505
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800508Done:
509 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800510}
511
512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800514 Some basic CBOR with map and array used in a lot of tests.
515 The map labels are all strings
516
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900518 "an array of two strings": [
519 "string1", "string2"
520 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521 "map in a map": {
522 "bytes 1": h'78787878',
523 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900524 "another int": 98,
525 "text 2": "lies, damn lies and statistics"
526 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800527 }
528 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800529static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700530 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
531 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
532 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
533 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
534 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
535 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
536 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
537 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
538 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
539 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
540 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
541 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
542 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
543 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
544 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700545 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800546
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800547
548#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700549// Same as above, but with indefinite lengths.
550static const uint8_t pValidMapIndefEncoded[] = {
5510xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5520x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5530x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5540x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5550x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5560x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5570x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5580x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5590x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5600x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5610x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5620x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5630x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5640x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5650x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5660x73, 0xff, 0xff};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800567#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700568
569
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800570static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700571 size_t nLen,
572 int64_t *pInt1,
573 int64_t *pInt2,
574 const uint8_t **pBuf3,
575 size_t *pBuf3Len,
576 const uint8_t **pBuf4,
577 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800578{
579 QCBORDecodeContext DCtx;
580 QCBORItem Item;
581 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800582
Laurence Lundbladeee851742020-01-08 08:37:05 -0800583 QCBORDecode_Init(&DCtx,
584 (UsefulBufC){pEncoded, nLen},
585 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700588 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
589 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800590 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700591 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800592
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800593 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700594 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
595 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800596 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700597 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800598 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800599
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800600 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700601 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
602 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800603 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700604 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800605 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700608 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
609 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800610 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700611 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 *pBuf3 = Item.val.string.ptr;
613 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800614
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800615 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700616 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
617 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700619 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620 *pBuf4 = Item.val.string.ptr;
621 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800622
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800623 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800624
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800625Done:
626 return(nReturn);
627}
628
629
630
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800631
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800632int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800633{
634 uint8_t *pEncoded;
635 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800636
Laurence Lundblade5e390822019-01-06 12:35:01 -0800637 int64_t i1=0, i2=0;
638 size_t i3=0, i4=0;
639 const uint8_t *s3= (uint8_t *)"";
640 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800641
642
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800643 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
644 return(-1);
645 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800646
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800647 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800648
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800649 if(i1 != 23 ||
650 i2 != 6000 ||
651 i3 != 8 ||
652 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530653 memcmp("galactic", s3, 8) !=0 ||
654 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655 return(-1);
656 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800657
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800658 return(0);
659}
660
661
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700662/*
663 [
664 0,
665 [],
666 [
667 [],
668 [
669 0
670 ],
671 {},
672 {
673 1: {},
674 2: {},
675 3: []
676 }
677 ]
678 ]
679 */
680static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
681 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
682
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800683#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700684/* Same as above, but with indefinte lengths */
685static uint8_t sEmptiesIndef[] = {
6860x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700687 0x00,
688 0x9F,
689 0xFF,
690 0x9F,
691 0x9F,
692 0xFF,
693 0x9F,
694 0x00,
695 0xFF,
696 0xBF,
697 0xFF,
698 0xBF,
699 0x01,
700 0xBF,
701 0xFF,
702 0x02,
703 0xBF,
704 0xFF,
705 0x03,
706 0x9F,
707 0xFF,
708 0xFF,
709 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700710 0xFF};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800711#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade02625d42020-06-25 14:41:41 -0700712
713
714static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700715{
716 QCBORDecodeContext DCtx;
717 QCBORItem Item;
718
Laurence Lundbladeee851742020-01-08 08:37:05 -0800719 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700720 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800721 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700722
723 // Array with 3 items
724 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
725 Item.uDataType != QCBOR_TYPE_ARRAY ||
726 Item.uNestingLevel != 0 ||
727 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700728 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700729 return -1;
730 }
731
732 // An integer 0
733 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
734 Item.uDataType != QCBOR_TYPE_INT64 ||
735 Item.uNestingLevel != 1 ||
736 Item.uNextNestLevel != 1 ||
737 Item.val.uint64 != 0) {
738 return -2;
739 }
740
741 // An empty array
742 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
743 Item.uDataType != QCBOR_TYPE_ARRAY ||
744 Item.uNestingLevel != 1 ||
745 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700746 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700747 return -3;
748 }
749
750 // An array with 4 items
751 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
752 Item.uDataType != QCBOR_TYPE_ARRAY ||
753 Item.uNestingLevel != 1 ||
754 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700755 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700756 return -4;
757 }
758
759 // An empty array
760 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
761 Item.uDataType != QCBOR_TYPE_ARRAY ||
762 Item.uNestingLevel != 2 ||
763 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700764 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700765 return -5;
766 }
767
768 // An array with 1 item
769 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
770 Item.uDataType != QCBOR_TYPE_ARRAY ||
771 Item.uNestingLevel != 2 ||
772 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700773 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700774 return -6;
775 }
776
777 // An integer 0
778 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
779 Item.uDataType != QCBOR_TYPE_INT64 ||
780 Item.uNestingLevel != 3 ||
781 Item.uNextNestLevel != 2 ||
782 Item.val.uint64 != 0) {
783 return -7;
784 }
785
786 // An empty map
787 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
788 Item.uDataType != QCBOR_TYPE_MAP ||
789 Item.uNestingLevel != 2 ||
790 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700791 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700792 return -8;
793 }
794
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700795 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700796 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
797 Item.uDataType != QCBOR_TYPE_MAP ||
798 Item.uNestingLevel != 2 ||
799 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700800 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700801 return -9;
802 }
803
804 // An empty map
805 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
806 Item.uDataType != QCBOR_TYPE_MAP ||
807 Item.uNestingLevel != 3 ||
808 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700809 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700810 return -10;
811 }
812
813 // An empty map
814 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
815 Item.uDataType != QCBOR_TYPE_MAP ||
816 Item.uNestingLevel != 3 ||
817 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700818 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700819 return -11;
820 }
821
822 // An empty array
823 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
824 Item.uDataType != QCBOR_TYPE_ARRAY ||
825 Item.uNestingLevel != 3 ||
826 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700827 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700828 return -12;
829 }
830
831 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
832 return -13;
833 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700834 return 0;
835}
836
837
838int32_t EmptyMapsAndArraysTest()
839{
840 int nResult;
841 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
842 true);
843 if(nResult) {
844 return nResult;
845 }
846
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800847#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700848 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
849 false);
850
851 if(nResult) {
852 return nResult -100;
853 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800854#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700855
856 return 0;
857}
858
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800859
Laurence Lundbladeee851742020-01-08 08:37:05 -0800860static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
861 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800862
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800863int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800864{
865 QCBORDecodeContext DCtx;
866 int nReturn = 0;
867 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800868
Laurence Lundbladeee851742020-01-08 08:37:05 -0800869 QCBORDecode_Init(&DCtx,
870 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
871 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800872
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800873 for(i = 0; i < 10; i++) {
874 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800875
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800876 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
877 Item.uDataType != QCBOR_TYPE_ARRAY ||
878 Item.uNestingLevel != i) {
879 nReturn = -1;
880 break;
881 }
882 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800883
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800884 return(nReturn);
885}
886
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700887// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800888static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
889 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
890 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
891 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800892
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800893int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800894{
895 QCBORDecodeContext DCtx;
896 int nReturn = 0;
897 int i;
898 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800899
900
Laurence Lundbladeee851742020-01-08 08:37:05 -0800901 QCBORDecode_Init(&DCtx,
902 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
903 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800904
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700905 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800907 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
908 Item.uDataType != QCBOR_TYPE_ARRAY ||
909 Item.uNestingLevel != i) {
910 nReturn = -1;
911 break;
912 }
913 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800914
Laurence Lundbladea9489f82020-09-12 13:50:56 -0700915 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800916 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800917
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800918 return(nReturn);
919}
920
921
922
923
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800924int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800925{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700926 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800927
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800928 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700929 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundbladeee851742020-01-08 08:37:05 -0800931 QCBORDecode_Init(&DCtx,
932 (UsefulBufC){spExpectedEncodedInts, nNum},
933 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800934
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800935 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800936
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700937 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800938 nResult = -1;
939 goto Done;
940 }
941 }
942Done:
943 return nResult;
944}
945
946
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800947
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800948int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800949{
950 uint8_t *pEncoded;
951 int nReturn;
952 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800953
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 int64_t i1, i2;
955 size_t i3, i4;
956 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800957
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800959
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
961 return(-1);
962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800963
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800964 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -0700965 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
966 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800967 if(nResult == 0) {
968 nReturn = -1;
969 }
970 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800971
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800972 return(nReturn);
973}
974
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530975/*
976 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800977 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
978 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530979 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800980static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800981{
982 QCBORDecodeContext DCtx;
983 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700984 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800985
Laurence Lundbladeee851742020-01-08 08:37:05 -0800986 QCBORDecode_Init(&DCtx,
987 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
988 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800989
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900990 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700991 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900992 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800993 if(Item.uDataType != QCBOR_TYPE_MAP ||
994 Item.val.uCount != 3)
995 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800996
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900997 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700998 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900999 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001000
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002 Item.uDataType != QCBOR_TYPE_INT64 ||
1003 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301004 Item.uDataAlloc ||
1005 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001006 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001007 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001009
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001010 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001011 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001012 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001013 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301014 Item.uDataAlloc ||
1015 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001016 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001017 Item.uDataType != QCBOR_TYPE_ARRAY ||
1018 Item.val.uCount != 2)
1019 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001020
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001021 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001022 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001023 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001024 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301025 Item.uDataAlloc ||
1026 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001027 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001030
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001031 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001032 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001033 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001034 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301035 Item.uDataAlloc ||
1036 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001037 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001039 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001040
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001041 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001042 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001043 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301045 Item.uDataAlloc ||
1046 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001047 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001048 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001049 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001050 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001051 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001052
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001053 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001054 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001055 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001056 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001058 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301059 Item.uDataAlloc ||
1060 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001061 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001062 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001063 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001064
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001065 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001066 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001067 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001068 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001069 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301071 Item.uDataAlloc ||
1072 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001073 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001075 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001076
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001077 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001078 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001079 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001080 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301081 Item.uDataAlloc ||
1082 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001083 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001084 Item.uDataType != QCBOR_TYPE_INT64 ||
1085 Item.val.int64 != 98)
1086 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001087
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001088 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001089 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001090 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301094 Item.uDataAlloc ||
1095 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001096 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001097 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001099
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001100 return 0;
1101}
1102
1103
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001104/*
1105 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001106 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001107 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001108int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001109{
1110 QCBORDecodeContext DCtx;
1111 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001112 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001113
Laurence Lundbladeee851742020-01-08 08:37:05 -08001114 QCBORDecode_Init(&DCtx,
1115 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1116 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001117
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001119 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001120 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001121 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1122 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001123 return -1;
1124 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001125
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001126 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001127 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 }
1129 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1130 Item.uDataAlloc ||
1131 Item.uLabelAlloc ||
1132 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001133 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 return -2;
1135 }
1136
1137 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001138 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001139 }
1140 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1141 Item.uDataType != QCBOR_TYPE_INT64 ||
1142 Item.val.int64 != 42 ||
1143 Item.uDataAlloc ||
1144 Item.uLabelAlloc) {
1145 return -3;
1146 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001147
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001148 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001149 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001150 }
1151 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1152 Item.uDataAlloc ||
1153 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001154 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1156 return -4;
1157 }
1158
1159 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001160 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 }
1162 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1163 Item.uDataAlloc ||
1164 Item.uLabelAlloc ||
1165 Item.uDataType != QCBOR_TYPE_ARRAY ||
1166 Item.val.uCount != 2) {
1167 return -5;
1168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001169
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001170 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001171 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001172 }
1173 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1174 Item.val.string.len != 7 ||
1175 Item.uDataAlloc ||
1176 Item.uLabelAlloc ||
1177 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1178 return -6;
1179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001180
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001182 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 }
1184 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1185 Item.uDataAlloc ||
1186 Item.uLabelAlloc ||
1187 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1188 return -7;
1189 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001190
1191
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.uLabelType != QCBOR_TYPE_NONE ||
1196 Item.uDataAlloc ||
1197 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001198 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001199 return -8;
1200 }
1201
1202 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001203 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001204 }
1205 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1206 Item.uDataAlloc ||
1207 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001208 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1209 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001210 return -9;
1211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001212
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001213 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001214 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001215 }
1216 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001217 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1219 Item.uDataAlloc ||
1220 Item.uLabelAlloc) {
1221 return -10;
1222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001223
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001224 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001225 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 }
1227 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1228 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1229 Item.uDataAlloc ||
1230 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001231 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001232 return -11;
1233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001234
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001235 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001236 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001237 }
1238 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001239 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001240 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1241 Item.uDataAlloc ||
1242 Item.uLabelAlloc) {
1243 return -12;
1244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001245
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001246 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001247 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001248 }
1249 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1250 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1251 Item.uDataAlloc ||
1252 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001253 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001254 return -13;
1255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001256
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001257 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001258 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001259 }
1260 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1261 Item.uDataAlloc ||
1262 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001263 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001264 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1265 return -14;
1266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001267
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001268 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 ||
1274 Item.uDataType != QCBOR_TYPE_INT64 ||
1275 Item.val.int64 != 98) {
1276 return -15;
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, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001284 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1285 Item.uDataAlloc ||
1286 Item.uLabelAlloc) {
1287 return -16;
1288 }
1289
1290 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_TEXT_STRING ||
1295 Item.uDataAlloc ||
1296 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001297 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001298 return -17;
1299 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001300
1301
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001302 /*
1303 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1304 map that when interpreted as an array will be too many. Test
1305 data just has the start of the map, not all the items in the map.
1306 */
1307 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001308
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001309 QCBORDecode_Init(&DCtx,
1310 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1311 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001312
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001313 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001314 return -50;
1315 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001316
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001317 // TODO: test decoding of labels that are arrays or such
1318 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1319
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001320 return 0;
1321}
1322
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001323
1324/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301325 Fully or partially decode pValidMapEncoded. When
1326 partially decoding check for the right error code.
1327 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001328
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301329 The partial decodes test error conditions of
1330 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001331
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301332 This could be combined with the above test
1333 and made prettier and maybe a little more
1334 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001335 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001336static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001337{
1338 QCBORDecodeContext DCtx;
1339 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001340 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001341
Laurence Lundbladeee851742020-01-08 08:37:05 -08001342 QCBORDecode_Init(&DCtx,
1343 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1344 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001345
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001346 if(nLevel < 1) {
1347 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1348 return -1;
1349 } else {
1350 return 0;
1351 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301353
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001354
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001355 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001356 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001357 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001358 if(Item.uDataType != QCBOR_TYPE_MAP ||
1359 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001360 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001362 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001363 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1364 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001365 } else {
1366 return 0;
1367 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001369
1370
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001371 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001372 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001374 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001375 Item.uDataType != QCBOR_TYPE_INT64 ||
1376 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001377 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001378 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001380
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001381 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001382 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1383 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001384 } else {
1385 return 0;
1386 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001388
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001389 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001390 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001391 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001392 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001393 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001394 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001395 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001396 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001397 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001398
1399
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001400 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001401 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1402 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001403 } else {
1404 return 0;
1405 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001407
1408
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001409 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001410 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001411 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001412 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001413 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001414 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001416
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001417 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001418 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1419 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001420 } else {
1421 return 0;
1422 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001423 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001424
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001425 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001426 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001427 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001428 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001429 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001430 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001432
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001433 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001434 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1435 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001436 } else {
1437 return 0;
1438 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001439 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001440
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001441 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001442 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001443 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001444 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001445 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001446 Item.uDataType != QCBOR_TYPE_MAP ||
1447 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001448 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001449
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001450 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001451 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1452 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001453 } else {
1454 return 0;
1455 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001457
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001458 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001459 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001461 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001462 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001464 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001465 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001466 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001467
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001468 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001469 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1470 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001471 } else {
1472 return 0;
1473 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001474 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001475
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001476 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001477 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001478 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001479 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001480 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001481 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001482 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001483 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001484 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001485
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001486 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001487 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1488 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001489 } else {
1490 return 0;
1491 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001493
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001494 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001495 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001496 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001497 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001498 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001499 Item.uDataType != QCBOR_TYPE_INT64 ||
1500 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001501 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001502
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001503 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001504 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1505 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001506 } else {
1507 return 0;
1508 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001509 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001510
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001511 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001512 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001513 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001515 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001516 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001517 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001518 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001519 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001520
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301521 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001522 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001525 return 0;
1526}
1527
1528
1529
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001530
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001531int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001532{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001533 // Parse a moderatly complex map structure very thoroughly
1534 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1535 if(nResult) {
1536 return nResult;
1537 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001538
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001539 // Again, but in strings-only mode. It should succeed since the input
1540 // map has only string labels.
1541 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1542 if(nResult) {
1543 return nResult;
1544 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001545
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001546 // Again, but try to finish the decoding before the end of the
1547 // input at 10 different place and see that the right error code
1548 // is returned.
1549 for(int i = 0; i < 10; i++) {
1550 nResult = ExtraBytesTest(i);
1551 if(nResult) {
1552 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001553 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001555
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001556 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557}
1558
1559
Laurence Lundbladeee851742020-01-08 08:37:05 -08001560static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1561 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1562 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001563
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001564int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001565{
1566 QCBORDecodeContext DCtx;
1567 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001568 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001569
1570
Laurence Lundbladeee851742020-01-08 08:37:05 -08001571 QCBORDecode_Init(&DCtx,
1572 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1573 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001574
1575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001577 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001578 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1579 Item.val.uCount != 10)
1580 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001581
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001583 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001584 if(Item.uDataType != QCBOR_TYPE_FALSE)
1585 return -1;
1586
1587 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001588 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589 if(Item.uDataType != QCBOR_TYPE_TRUE)
1590 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001591
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001592 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001593 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001594 if(Item.uDataType != QCBOR_TYPE_NULL)
1595 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001596
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001597 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001598 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001599 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1600 return -1;
1601
1602 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001603 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001604 return -1;
1605
1606 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001607 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001608 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1609 return -1;
1610
1611 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001612 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001613 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1614 return -1;
1615
Laurence Lundblade077475f2019-04-26 09:06:33 -07001616 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001617 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001618
Laurence Lundblade077475f2019-04-26 09:06:33 -07001619 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001620 return -1;
1621
Laurence Lundblade077475f2019-04-26 09:06:33 -07001622 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001623 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001624
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001625 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001626 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001627 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1628 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001629
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001630 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001631 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001632 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1633 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001634
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001635 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001636
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001637}
1638
1639
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001640int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001641{
1642 // Loop over all the not-well-formed instance of CBOR
1643 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001644 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
1645 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001646 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1647 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1648 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1649
Laurence Lundbladeee851742020-01-08 08:37:05 -08001650 // Set up decoder context. String allocator needed for indefinite
1651 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001652 QCBORDecodeContext DCtx;
1653 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001654#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001655 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1656 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001657#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001658
1659 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001660 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001661 do {
1662 QCBORItem Item;
1663
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001664 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1665 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001666
1667 // Every test vector must fail with
1668 // a not-well-formed error. If not
1669 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001670 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001671 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001672 /* Return index of failure and QCBOR error in the result */
1673 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001674 }
1675 }
1676 return 0;
1677}
1678
1679
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001680// 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 +08001681struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001682 UsefulBufC Input;
1683 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001684};
1685
Laurence Lundblade59289e52019-12-30 13:44:37 -08001686
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001687static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001688{
1689 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001690 QCBORDecodeContext DCtx;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001691 QCBORError uCBORError;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001692
Laurence Lundblade59289e52019-12-30 13:44:37 -08001693 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001694
1695#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001696 // Set up the decoding context including a memory pool so that
1697 // indefinite length items can be checked
Laurence Lundblade59289e52019-12-30 13:44:37 -08001698 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001699
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001700 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1701 if(uCBORError) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001702 return -9;
1703 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001704#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1705
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001706
Laurence Lundblade59289e52019-12-30 13:44:37 -08001707 // Iterate until there is an error of some sort error
1708 QCBORItem Item;
1709 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001710 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001711 memset(&Item, 0x33, sizeof(Item));
1712
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001713 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1714 } while(uCBORError == QCBOR_SUCCESS);
1715
1716
Laurence Lundblade59289e52019-12-30 13:44:37 -08001717
1718 // Must get the expected error or the this test fails
1719 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001720 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001721 Item.uDataType != QCBOR_TYPE_NONE ||
1722 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001723 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001724 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001725 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001726 }
1727 }
1728
1729 return 0;
1730}
1731
1732
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001733struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001734 // Most of this is copied from not_well_formed.h. Here the error code
1735 // returned is also checked.
1736
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001737#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001738 // Indefinite length strings must be closed off
1739 // An indefinite length byte string not closed off
1740 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1741 // An indefinite length text string not closed off
1742 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1743
1744
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001745 // All the chunks in an indefinite length string must be of the type of
1746 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001747 // indefinite length byte string with text string chunk
1748 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1749 // indefinite length text string with a byte string chunk
1750 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1751 // indefinite length byte string with an positive integer chunk
1752 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1753 // indefinite length byte string with an negative integer chunk
1754 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1755 // indefinite length byte string with an array chunk
1756 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1757 // indefinite length byte string with an map chunk
1758 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1759 // indefinite length byte string with tagged integer chunk
1760 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1761 // indefinite length byte string with an simple type chunk
1762 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1763 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1764 // indefinite length text string with indefinite string inside
1765 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1766
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001767#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1768
1769 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1770 // An indefinite length text string not closed off
1771 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1772
1773
1774 // All the chunks in an indefinite length string must be of the type of
1775 // indefinite length string
1776 // indefinite length byte string with text string chunk
1777 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1778 // indefinite length text string with a byte string chunk
1779 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1780 // indefinite length byte string with an positive integer chunk
1781 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1782 // indefinite length byte string with an negative integer chunk
1783 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1784 // indefinite length byte string with an array chunk
1785 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1786 // indefinite length byte string with an map chunk
1787 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1788 // indefinite length byte string with tagged integer chunk
1789 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1790 // indefinite length byte string with an simple type chunk
1791 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1792 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1793 // indefinite length text string with indefinite string inside
1794 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1795#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1796
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001797
1798 // Definte length maps and arrays must be closed by having the right number of items
1799 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001800 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001801 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001802 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001803 // A definte length array that is supposed to have 511 items, but has only 1
1804 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1805 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001806 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001807 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001808 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001809
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001810#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001811 // Indefinte length maps and arrays must be ended by a break
1812 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001813 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001814 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001815 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001816 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001817 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001818 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001819 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001820
1821
1822 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001823 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001824 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001825 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001826 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001827 // Unclosed indefinite map containing a closed definite length array
1828 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1829 // Definite length map containing an unclosed indefinite length array
1830 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001831 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001832 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001833 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001834 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001835 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001836 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001837 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001838 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001839 // Unclosed indefinite length map in definite length maps
1840 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1841 QCBOR_ERR_NO_MORE_ITEMS},
1842 // Unclosed definite length map in indefinite length maps
1843 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1844 // Unclosed indefinite length array in definite length maps
1845 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1846 QCBOR_ERR_NO_MORE_ITEMS},
1847 // Unclosed definite length array in indefinite length maps
1848 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1849 // Unclosed indefinite length map in definite length arrays
1850 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1851 // Unclosed definite length map in indefinite length arrays
1852 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001853#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001854
1855 // The "argument" for the data item is incomplete
1856 // Positive integer missing 1 byte argument
1857 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1858 // Positive integer missing 2 byte argument
1859 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1860 // Positive integer missing 4 byte argument
1861 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1862 // Positive integer missing 8 byte argument
1863 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1864 // Positive integer missing 1 byte of 2 byte argument
1865 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1866 // Positive integer missing 2 bytes of 4 byte argument
1867 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1868 // Positive integer missing 1 bytes of 7 byte argument
1869 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1870 // Negative integer missing 1 byte argument
1871 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1872 // Binary string missing 1 byte argument
1873 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1874 // Text string missing 1 byte argument
1875 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1876 // Array missing 1 byte argument
1877 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1878 // Map missing 1 byte argument
1879 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1880 // Tag missing 1 byte argument
1881 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1882 // Simple missing 1 byte argument
1883 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001884 // half-precision with 1 byte argument
1885 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1886 // single-precision with 2 byte argument
1887 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1888 // double-precision with 3 byte argument
1889 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1890
1891
1892 // Tag with no content
1893 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001894
1895
1896 // Breaks must not occur in definite length arrays and maps
1897 // Array of length 1 with sole member replaced by a break
1898 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1899 // Array of length 2 with 2nd member replaced by a break
1900 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1901 // Map of length 1 with sole member label replaced by a break
1902 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1903 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001904 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001905 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1906 // Array of length 1 with 2nd member value replaced by a break
1907 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1908 // Map of length 2 with 2nd member replaced by a break
1909 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1910
1911
1912 // Breaks must not occur on their own out of an indefinite length data item
1913 // A bare break is not well formed
1914 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1915 // A bare break after a zero length definite length array
1916 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001917#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001918 // A bare break after a zero length indefinite length map
1919 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001920 // A break inside a definite length array inside an indefenite length array
1921 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1922 // Complicated mixed nesting with break outside indefinite length array
1923 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001924#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001925
1926
1927 // Forbidden two byte encodings of simple types
1928 // Must use 0xe0 instead
1929 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1930 // Should use 0xe1 instead
1931 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1932 // Should use 0xe2 instead
1933 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1934 // Should use 0xe3 instead
1935 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1936 // Should use 0xe4 instead
1937 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1938 // Should use 0xe5 instead
1939 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1940 // Should use 0xe6 instead
1941 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1942 // Should use 0xe7 instead
1943 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1944 // Should use 0xe8 instead
1945 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1946 // Should use 0xe9 instead
1947 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1948 // Should use 0xea instead
1949 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1950 // Should use 0xeb instead
1951 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1952 // Should use 0xec instead
1953 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1954 // Should use 0xed instead
1955 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1956 // Should use 0xee instead
1957 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1958 // Should use 0xef instead
1959 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1960 // Should use 0xf0 instead
1961 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1962 // Should use 0xf1 instead
1963 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1964 // Should use 0xf2 instead
1965 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1966 // Must use 0xf3 instead
1967 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1968 // Must use 0xf4 instead
1969 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1970 // Must use 0xf5 instead
1971 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1972 // Must use 0xf6 instead
1973 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1974 // Must use 0xf7 instead
1975 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1976 // Must use 0xf8 instead
1977 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001978 // Reserved
1979 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001980
1981 // Integers with additional info indefinite length
1982 // Positive integer with additional info indefinite length
1983 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1984 // Negative integer with additional info indefinite length
1985 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1986 // CBOR tag with "argument" an indefinite length
1987 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1988 // CBOR tag with "argument" an indefinite length alternate vector
1989 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1990
1991
1992 // Missing bytes from a deterministic length string
1993 // A byte string is of length 1 without the 1 byte
1994 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1995 // A text string is of length 1 without the 1 byte
1996 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001997
1998#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08001999 // Byte string should have 2^32-15 bytes, but has one
2000 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2001 // Byte string should have 2^32-15 bytes, but has one
2002 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002003 // Byte string should have 2^64 bytes, but has 3
2004 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2005 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2006 // Text string should have 2^64 bytes, but has 3
2007 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2008 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002009#else
2010 // Byte string should have 2^32-15 bytes, but has one
2011 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2012 // Byte string should have 2^32-15 bytes, but has one
2013 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2014 // Byte string should have 2^16 bytes, but has 3
2015 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2016 // Text string should have 2^64 bytes, but has 3
2017 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2018#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002019
2020 // Use of unassigned additional information values
2021 // Major type positive integer with reserved value 28
2022 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2023 // Major type positive integer with reserved value 29
2024 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2025 // Major type positive integer with reserved value 30
2026 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2027 // Major type negative integer with reserved value 28
2028 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2029 // Major type negative integer with reserved value 29
2030 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2031 // Major type negative integer with reserved value 30
2032 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2033 // Major type byte string with reserved value 28 length
2034 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2035 // Major type byte string with reserved value 29 length
2036 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2037 // Major type byte string with reserved value 30 length
2038 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2039 // Major type text string with reserved value 28 length
2040 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2041 // Major type text string with reserved value 29 length
2042 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2043 // Major type text string with reserved value 30 length
2044 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2045 // Major type array with reserved value 28 length
2046 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2047 // Major type array with reserved value 29 length
2048 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2049 // Major type array with reserved value 30 length
2050 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2051 // Major type map with reserved value 28 length
2052 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2053 // Major type map with reserved value 29 length
2054 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2055 // Major type map with reserved value 30 length
2056 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2057 // Major type tag with reserved value 28 length
2058 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2059 // Major type tag with reserved value 29 length
2060 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2061 // Major type tag with reserved value 30 length
2062 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2063 // Major type simple with reserved value 28 length
2064 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2065 // Major type simple with reserved value 29 length
2066 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2067 // Major type simple with reserved value 30 length
2068 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2069
2070
2071 // Maps must have an even number of data items (key & value)
2072 // Map with 1 item when it should have 2
2073 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2074 // Map with 3 item when it should have 4
2075 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002076#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002077 // Map with 1 item when it should have 2
2078 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2079 // Map with 3 item when it should have 4
2080 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002081#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002082
2083
2084 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002085 // Text-based date, with an integer
2086 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2087 // Epoch date, with an byte string
2088 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2089 // tagged as both epoch and string dates
2090 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2091 // big num tagged an int, not a byte string
2092 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002093};
2094
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002095int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002096{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002097 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002098
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002099 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002100 if(nResult) {
2101 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002103
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002104 // Corrupt the UsefulInputBuf and see that
2105 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002106 QCBORDecodeContext DCtx;
2107 QCBORItem Item;
2108 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002109
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002110 QCBORDecode_Init(&DCtx,
2111 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2112 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002113
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002114 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2115 return (int32_t)uQCBORError;
2116 }
2117 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2118 // This wasn't supposed to happen
2119 return -1;
2120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002121
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002122 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002123
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002124 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2125 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2126 // Did not get back the error expected
2127 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002129
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002130
Laurence Lundblade98427e92020-09-28 21:33:23 -07002131 /*
2132 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2133 tests here can be performed to see that the max length
2134 error check works correctly. See DecodeBytes(). If the max
2135 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002136
Laurence Lundblade98427e92020-09-28 21:33:23 -07002137 This test will automatocally adapt the all CPU sizes
2138 through the use of SIZE_MAX.
2139 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002140
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002141 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002142 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002143
Laurence Lundblade98427e92020-09-28 21:33:23 -07002144 // This makes a CBOR head with a text string that is very long
2145 // but doesn't fill in the bytes of the text string as that is
2146 // not needed to test this part of QCBOR.
2147 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2148
2149 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2150
2151 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2152 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002153 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002154
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002155 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002156}
2157
2158
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002159/* Try all 256 values of the byte at nLen including recursing for
2160 each of the values to try values at nLen+1 ... up to nLenMax
2161 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002162static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002163{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164 if(nLen >= nLenMax) {
2165 return;
2166 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002167
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002168 for(int inputByte = 0; inputByte < 256; inputByte++) {
2169 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002170 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002171 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002172
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002173 // Get ready to parse
2174 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002175 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002177 // Parse by getting the next item until an error occurs
2178 // Just about every possible decoder error can occur here
2179 // The goal of this test is not to check for the correct
2180 // error since that is not really possible. It is to
2181 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002182 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002183 QCBORItem Item;
2184 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002185 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002186 break;
2187 }
2188 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002189
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002190 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002191 }
2192}
2193
2194
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002195int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002196{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002197 // Size 2 tests 64K inputs and runs quickly
2198 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002199
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002200 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002201
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002202 return 0;
2203}
2204
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002205
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002206int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002207{
2208 // size 3 tests 16 million inputs and runs OK
2209 // in seconds on fast machines. Size 4 takes
2210 // 10+ minutes and 5 half a day on fast
2211 // machines. This test is kept separate from
2212 // the others so as to no slow down the use
2213 // of them as a very frequent regression.
2214 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002215
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002216 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002217
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002218 return 0;
2219}
2220
2221
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002222static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002223 0xc0, // tag for string date
2224 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002225
Laurence Lundbladec7114722020-08-13 05:11:40 -07002226 0xc0, // tag for string date
2227 0x00, // Wrong type for a string date
2228
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002229 0xc1, // tag for epoch date
2230 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2231
Laurence Lundbladec7114722020-08-13 05:11:40 -07002232 0xc1,
2233 0x62, 'h', 'i', // wrong type tagged
2234
Laurence Lundblade99615302020-11-29 11:19:47 -08002235 // CBOR_TAG_ENC_AS_B64
2236 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002237 0x1a, 0x53, 0x72, 0x4E, 0x01,
2238
2239 0xc1, // tag for epoch date
2240 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002242 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002243 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002244
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002245 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002246 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002247
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002248 0xc1, // tag for epoch date
2249 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2250 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2251
2252 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002253 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2254
2255 0xc1, // tag for epoch date
2256 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2257
2258 0xc1,
2259 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2260
2261 0xc1, // tag for epoch date
2262 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002263};
2264
2265
Laurence Lundbladec7114722020-08-13 05:11:40 -07002266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002267// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002268#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002269static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002270
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002271 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002272
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002273 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002274
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002275 return diff > 0.0000001;
2276}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002277#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002278
2279
Laurence Lundblade99615302020-11-29 11:19:47 -08002280
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002281int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002282{
2283 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002284 QCBORItem Item;
2285 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002286
Laurence Lundbladeee851742020-01-08 08:37:05 -08002287 QCBORDecode_Init(&DCtx,
2288 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2289 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002291 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002292 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002293 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002294 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002295 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002296 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002297 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002298 }
2299
Laurence Lundbladec7114722020-08-13 05:11:40 -07002300 // Wrong type for a string date
2301 uError = QCBORDecode_GetNext(&DCtx, &Item);
2302 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002303 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002304 }
2305
2306 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2307 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2308 return -4;
2309 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002310 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2311 Item.val.epochDate.nSeconds != 1400000000 ||
2312 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002313 return -5;
2314 }
2315
2316 // Wrong type for an epoch date
2317 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2318 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002319 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002320
Laurence Lundblade99615302020-11-29 11:19:47 -08002321 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2322 // The date is decoded and the two tags are returned. This is to
2323 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002324 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2325 return -7;
2326 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002327 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2328 Item.val.epochDate.nSeconds != 1400000001 ||
2329 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundblade99615302020-11-29 11:19:47 -08002330 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002331 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002333
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002334 // Epoch date that is too large for our representation
2335 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002336 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002338
Laurence Lundblade9682a532020-06-06 18:33:04 -07002339#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002340 // Epoch date in float format with fractional seconds
2341 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2342 return -10;
2343 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002344 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2345 Item.val.epochDate.nSeconds != 1 ||
2346 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002347 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002349
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002350 // Epoch date float that is too large for our representation
2351 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002352 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002353 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002354
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002355 // Epoch date double that is just slightly too large
2356 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002357 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002358 }
2359
2360 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002361 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2362 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2363 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2364 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002365 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002366 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002367
2368 // Nan
2369 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2370 return -15;
2371 }
2372
2373 // +Inifinity double-precision
2374 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2375 return -16;
2376 }
2377
2378#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2379 // -Inifinity half-precision
2380 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2381 return -17;
2382 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002383#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002384 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2385 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002386 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002387#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002388
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002389#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002390 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2391 return -19;
2392 }
2393 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2394 return -20;
2395 }
2396 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2397 return -21;
2398 }
2399 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2400 return -22;
2401 }
2402 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2403 return -23;
2404 }
2405 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2406 return -24;
2407 }
2408#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2409 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2410 return -25;
2411 }
2412#else
2413 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2414 return -26;
2415 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002416#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002417
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002418#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002419
2420 return 0;
2421}
2422
Laurence Lundblade4b270642020-08-14 12:53:07 -07002423/*
2424 Test cases covered here. Some items cover more than one of these.
2425 positive integer (zero counts as a positive integer)
2426 negative integer
2427 half-precision float
2428 single-precision float
2429 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002430
Laurence Lundblade4b270642020-08-14 12:53:07 -07002431 float Overflow error
2432 Wrong type error for epoch
2433 Wrong type error for date string
2434 float disabled error
2435 half-precision disabled error
2436 -Infinity
2437 Slightly too large integer
2438 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002439
Laurence Lundblade4b270642020-08-14 12:53:07 -07002440 Get epoch by int
2441 Get string by int
2442 Get epoch by string
2443 Get string by string
2444 Fail to get epoch by wrong int label
2445 Fail to get string by wrong string label
2446 Fail to get epoch by string because it is invalid
2447 Fail to get epoch by int because it is invalid
2448
2449 Untagged values
2450 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002451static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002452 0x86,
2453
2454 0xc1,
2455 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2456
Laurence Lundbladec7114722020-08-13 05:11:40 -07002457 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002458 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2459
2460 0xc1, // tag for epoch date
2461 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2462
2463 0xc1, // tag for epoch date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002464 0x80, // Erroneous empty array as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002465
2466 0xc0, // tag for string date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002467 0xa0, // Erroneous empty map as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002468
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002469 0xa9, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002470
2471 0x00,
2472 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002473 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002474
2475 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002476 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002477 0xc1, // tag for epoch date
2478 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2479
2480 // Untagged integer 0
2481 0x08,
2482 0x00,
2483
2484 // Utagged date string with string label y
2485 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002486 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002487
2488 // Untagged -1000 with label z
2489 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002490 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002491 0x39, 0x03, 0xe7,
2492
Laurence Lundbladec7114722020-08-13 05:11:40 -07002493 0x07,
2494 0xc1, // tag for epoch date
2495 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2496
Laurence Lundblade4b270642020-08-14 12:53:07 -07002497 0x05,
2498 0xc1,
2499 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2500
Laurence Lundbladec7114722020-08-13 05:11:40 -07002501 // Untagged single-precision float with value 3.14 with string label x
2502 0x61, 0x78,
2503 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2504
Laurence Lundbladec7114722020-08-13 05:11:40 -07002505 // Untagged half-precision float with value -2
2506 0x09,
2507 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002508};
2509
2510int32_t SpiffyDateDecodeTest()
2511{
2512 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002513 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002514 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2515 nEpochDate4, nEpochDate6, nEpochDateFail,
2516 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002517 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002518 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002519
2520 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002521 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002522 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002523 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002524
Laurence Lundblade9b334962020-08-27 10:55:53 -07002525 // Too-negative float, -9.2233720368547748E+18
2526 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002527 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002528#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002529 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2530 return 1111;
2531 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002532#else
2533 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2534 return 1112;
2535 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002536#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade4b270642020-08-14 12:53:07 -07002537
2538 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002539 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002540 uError = QCBORDecode_GetAndResetError(&DC);
2541 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002542 return 1;
2543 }
2544
Laurence Lundblade4b270642020-08-14 12:53:07 -07002545 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002546 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002547 uError = QCBORDecode_GetAndResetError(&DC);
2548#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2549#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2550 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2551#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2552 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2553#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2554#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2555 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2556#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2557 if(uError != uExpectedforHalfMinusInfinity) {
2558 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002559 }
2560
Laurence Lundblade4b270642020-08-14 12:53:07 -07002561 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002562 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002563 uError = QCBORDecode_GetAndResetError(&DC);
2564 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2565 return 3;
2566 }
2567
2568 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002569 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002570 uError = QCBORDecode_GetAndResetError(&DC);
2571 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2572 return 4;
2573 }
2574
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002575 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002576
2577 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002578 QCBORDecode_GetEpochDateInMapN(&DC,
2579 5,
2580 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2581 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2582 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002583#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2584 if(nEpochDate2 != -9223372036854773760LL) {
2585 return 101;
2586 }
2587#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2588 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002589 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002590 return 102;
2591 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002592#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002593
Laurence Lundblade4b270642020-08-14 12:53:07 -07002594 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002595 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2596 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002597#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2598 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002599 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002600 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002601#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2602 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002603 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002604 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002605 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002606#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2607
2608 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002609 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2610 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002611#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2612 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002613 return 103;
2614 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002615#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2616 uError = QCBORDecode_GetAndResetError(&DC);
2617 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2618 return 104;
2619 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002620#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2621
Laurence Lundblade9b334962020-08-27 10:55:53 -07002622 // A half-precision date with value -2 FFF
2623 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2624 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002625#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2626 if(nEpochDate4 != -2) {
2627 return 105;
2628 }
2629#else
2630 uError = QCBORDecode_GetAndResetError(&DC);
2631 if(uError == QCBOR_SUCCESS) {
2632 return 106;
2633 }
2634#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002635
Laurence Lundblade4b270642020-08-14 12:53:07 -07002636
2637 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002638 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2639 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2640 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002641 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002642 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002643 return 107;
2644 }
2645
2646 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002647 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2648 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002649 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002650 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002651 return 108;
2652 }
2653
2654 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002655 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2656 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2657 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002658 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002659 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002660 return 109;
2661 }
2662
2663 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002664 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2665 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002666 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002667 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002668 return 110;
2669 }
2670
2671 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002672
Laurence Lundblade4b270642020-08-14 12:53:07 -07002673 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002674 QCBORDecode_GetEpochDateInMapN(&DC,
2675 1,
2676 QCBOR_TAG_REQUIREMENT_TAG |
2677 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2678 &nEpochDate1400000000);
2679 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002680 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002681 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2682 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002683 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002684 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2685 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002686 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002687 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2688 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002689 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002690 QCBORDecode_GetEpochDateInMapSZ(&DC,
2691 "z",
2692 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2693 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2694 &nEpochDate6);
2695 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002696
2697 QCBORDecode_ExitMap(&DC);
2698 QCBORDecode_ExitArray(&DC);
2699 uError = QCBORDecode_Finish(&DC);
2700 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002701 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002702 }
2703
Laurence Lundblade9b334962020-08-27 10:55:53 -07002704 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002705 return 200;
2706 }
2707
Laurence Lundblade9b334962020-08-27 10:55:53 -07002708 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002709 return 201;
2710 }
2711
Laurence Lundblade9b334962020-08-27 10:55:53 -07002712 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002713 return 202;
2714 }
2715
Laurence Lundblade9b334962020-08-27 10:55:53 -07002716 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002717 return 203;
2718 }
2719
Laurence Lundblade9b334962020-08-27 10:55:53 -07002720 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002721 return 204;
2722 }
2723
Laurence Lundblade9b334962020-08-27 10:55:53 -07002724 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2725 return 205;
2726 }
2727
2728 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2729 return 206;
2730 }
2731
Laurence Lundbladec7114722020-08-13 05:11:40 -07002732 return 0;
2733}
2734
2735
2736
Laurence Lundblade9b334962020-08-27 10:55:53 -07002737// Input for one of the tagging tests
2738static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002739 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002740 0x81, // Array of one
2741 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2742 0x82, // Array of two that is the faction 1/3
2743 0x01,
2744 0x03,
2745
2746 /*
2747 More than 4 tags on an item 225(226(227(228(229([])))))
2748 */
2749 0xd8, 0xe1,
2750 0xd8, 0xe2,
2751 0xd8, 0xe3,
2752 0xd8, 0xe4,
2753 0xd8, 0xe5,
2754 0x80,
2755
2756 /* tag 10489608748473423768(
2757 2442302356(
2758 21590(
2759 240(
2760 []))))
2761 */
2762 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2763 0xda, 0x91, 0x92, 0x93, 0x94,
2764 0xd9, 0x54, 0x56,
2765 0xd8, 0xf0,
2766 0x80,
2767
2768 /* tag 21590(
2769 10489608748473423768(
2770 2442302357(
2771 65534(
2772 []))))
2773 */
2774 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2775 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2776 0xda, 0x91, 0x92, 0x93, 0x95,
2777 0xd9, 0xff, 0xfe,
2778 0x80,
2779
2780 /* Make sure to blow past the limit of tags that must be mapped.
2781 works in conjuntion with entries above.
2782 269488144(269488145(269488146(269488147([]))))
2783 */
2784 0xda, 0x10, 0x10, 0x10, 0x10,
2785 0xda, 0x10, 0x10, 0x10, 0x11,
2786 0xda, 0x10, 0x10, 0x10, 0x12,
2787 0xda, 0x10, 0x10, 0x10, 0x13,
2788 0x80,
2789
2790 /* An invalid decimal fraction with an additional tag */
2791 0xd9, 0xff, 0xfa,
2792 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2793 0x00, // the integer 0; should be a byte string
2794};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002795
Laurence Lundblade59289e52019-12-30 13:44:37 -08002796/*
2797 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002798 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002799 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002800static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2801 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002802
Laurence Lundblade59289e52019-12-30 13:44:37 -08002803/*
2804DB 9192939495969798 # tag(10489608748473423768)
2805 D8 88 # tag(136)
2806 C6 # tag(6)
2807 C7 # tag(7)
2808 80 # array(0)
2809*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002810static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2811 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002812
2813/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002814 55799(55799(55799({
2815 6(7(-23)): 5859837686836516696(7({
2816 7(-20): 11({
2817 17(-18): 17(17(17("Organization"))),
2818 9(-17): 773("SSG"),
2819 -15: 16(17(6(7("Confusion")))),
2820 17(-16): 17("San Diego"),
2821 17(-14): 17("US")
2822 }),
2823 23(-19): 19({
2824 -11: 9({
2825 -9: -7
2826 }),
2827 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2828 })
2829 })),
2830 16(-22): 23({
2831 11(8(7(-5))): 8(-3)
2832 })
2833 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002834 */
2835static uint8_t spCSRWithTags[] = {
2836 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2837 0xc6, 0xc7, 0x36,
2838 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2839 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2840 0xcb, 0xa5,
2841 0xd1, 0x31,
2842 0xd1, 0xd1, 0xd1, 0x6c,
2843 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2844 0xc9, 0x30,
2845 0xd9, 0x03, 0x05, 0x63,
2846 0x53, 0x53, 0x47,
2847 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002848 0xd0, 0xd1, 0xc6, 0xc7,
2849 0x69,
2850 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002851 0xd1, 0x2f,
2852 0xd1, 0x69,
2853 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2854 0xd1, 0x2d,
2855 0xd1, 0x62,
2856 0x55, 0x53,
2857 0xd7, 0x32,
2858 0xd3, 0xa2,
2859 0x2a,
2860 0xc9, 0xa1,
2861 0x28,
2862 0x26,
2863 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2864 0xcc, 0x4a,
2865 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2866 0xd0, 0x35,
2867 0xd7, 0xa1,
2868 0xcb, 0xc8, 0xc7, 0x24,
2869 0xc8, 0x22};
2870
Laurence Lundblade9b334962020-08-27 10:55:53 -07002871
2872static uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002873 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07002874
2875 0xc0, // tag for string date
2876 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2877
2878 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2879
2880 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2881
2882 0xd8, 0x23, // tag for regex
2883 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2884
2885 0xc0, // tag for string date
2886 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002887};
2888
2889
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002890static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002891
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002892
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002893int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002894{
2895 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002896 QCBORItem Item;
2897 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002898
Laurence Lundbladeee851742020-01-08 08:37:05 -08002899 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002900 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002901 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002902
Laurence Lundblade9b334962020-08-27 10:55:53 -07002903 /*
2904 This test matches the magic number tag and the fraction tag
2905 55799([...])
2906 */
2907 uError = QCBORDecode_GetNext(&DCtx, &Item);
2908 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002909 return -2;
2910 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002911 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002912 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2913 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002914 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002915
Laurence Lundblade9b334962020-08-27 10:55:53 -07002916 /*
2917 4([1,3])
2918 */
2919 uError = QCBORDecode_GetNext(&DCtx, &Item);
2920#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2921 if(uError != QCBOR_SUCCESS ||
2922 Item.uDataType != QCBOR_TYPE_ARRAY ||
2923 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2924 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2925 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2926 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2927 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2928 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2929 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002930 return -4;
2931 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002932 // consume the items in the array
2933 uError = QCBORDecode_GetNext(&DCtx, &Item);
2934 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002935
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002936#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07002937 if(uError != QCBOR_SUCCESS ||
2938 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2939 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2940 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2941 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2942 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2943 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2944 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002945 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002946#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002947
Laurence Lundblade9b334962020-08-27 10:55:53 -07002948 /*
2949 More than 4 tags on an item 225(226(227(228(229([])))))
2950 */
2951 uError = QCBORDecode_GetNext(&DCtx, &Item);
2952 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002953 return -6;
2954 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002955
Laurence Lundblade88e9db22020-11-02 03:56:33 -08002956 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
2957 return -106;
2958 }
2959
2960
Laurence Lundblade9b334962020-08-27 10:55:53 -07002961 /* tag 10489608748473423768(
2962 2442302356(
2963 21590(
2964 240(
2965 []))))
2966 */
2967 uError = QCBORDecode_GetNext(&DCtx, &Item);
2968 if(uError != QCBOR_SUCCESS ||
2969 Item.uDataType != QCBOR_TYPE_ARRAY ||
2970 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2971 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2972 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2973 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002974 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002975 }
2976
2977 /* tag 21590(
2978 10489608748473423768(
2979 2442302357(
2980 21591(
2981 []))))
2982 */
2983 uError = QCBORDecode_GetNext(&DCtx, &Item);
2984 if(uError != QCBOR_SUCCESS ||
2985 Item.uDataType != QCBOR_TYPE_ARRAY ||
2986 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2987 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2988 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2989 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2990 return -8;
2991 }
2992
2993 /* Make sure to blow past the limit of tags that must be mapped.
2994 works in conjuntion with entries above.
2995 269488144(269488145(269488146(269488147([]))))
2996 */
2997 uError = QCBORDecode_GetNext(&DCtx, &Item);
2998 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2999 return -9;
3000 }
3001
3002 uError = QCBORDecode_GetNext(&DCtx, &Item);
3003 if(uError == QCBOR_SUCCESS) {
3004 return -10;
3005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003007 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003008 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003009 // tage and then matches it. Caller-config lists are no longer
3010 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003011 QCBORDecode_Init(&DCtx,
3012 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3013 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003014 const uint64_t puList[] = {0x9192939495969798, 257};
3015 const QCBORTagListIn TL = {2, puList};
3016 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003017
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003018 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3019 return -8;
3020 }
3021 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3022 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3023 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3024 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3025 Item.val.uCount != 0) {
3026 return -9;
3027 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003028
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003029 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003030 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003031 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003032 const uint64_t puLongList[17] = {1,2,1};
3033 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003034 QCBORDecode_Init(&DCtx,
3035 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3036 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003037 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3038 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3039 return -11;
3040 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003041
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003042 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003043 QCBORTagListOut Out = {0, 4, puTags};
3044
3045
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003046 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003047 QCBORDecode_Init(&DCtx,
3048 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3049 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003050 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3051 return -12;
3052 }
3053 if(puTags[0] != 0x9192939495969798 ||
3054 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003055 puTags[2] != 0x06 ||
3056 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003057 return -13;
3058 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003059
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003060 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003061 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003062 QCBORDecode_Init(&DCtx,
3063 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3064 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003065 QCBORTagListOut OutSmall = {0, 3, puTags};
3066 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3067 return -14;
3068 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003069
Laurence Lundblade9b334962020-08-27 10:55:53 -07003070
3071
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003072 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003073 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3074 // It is a bit of a messy test and maybe could be improved, but
3075 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003076 QCBORDecode_Init(&DCtx,
3077 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3078 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003079 int n = CheckCSRMaps(&DCtx);
3080 if(n) {
3081 return n-2000;
3082 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003083
Laurence Lundblade59289e52019-12-30 13:44:37 -08003084 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003085 QCBORDecode_Init(&DCtx,
3086 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3087 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003088
Laurence Lundblade9b334962020-08-27 10:55:53 -07003089 /* With the spiffy decode revision, this tag list is not used.
3090 It doesn't matter if a tag is in this list or not so some
3091 tests that couldn't process a tag because it isn't in this list
3092 now can process these unlisted tags. The tests have been
3093 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003094 const uint64_t puTagList[] = {773, 1, 90599561};
3095 const QCBORTagListIn TagList = {3, puTagList};
3096 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003097
3098
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003099 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3100 return -100;
3101 }
3102 if(Item.uDataType != QCBOR_TYPE_MAP ||
3103 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3104 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3105 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3106 Item.val.uCount != 2 ||
3107 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3108 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3109 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3110 Out.uNumUsed != 3) {
3111 return -101;
3112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003113
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003114 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3115 return -102;
3116 }
3117 if(Item.uDataType != QCBOR_TYPE_MAP ||
3118 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3119 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003120 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003121 Item.val.uCount != 2 ||
3122 puTags[0] != 5859837686836516696 ||
3123 puTags[1] != 7 ||
3124 Out.uNumUsed != 2) {
3125 return -103;
3126 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003127
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003128 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3129 return -104;
3130 }
3131 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003132 Item.val.uCount != 5 ||
3133 puTags[0] != 0x0b ||
3134 Out.uNumUsed != 1) {
3135 return -105;
3136 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003137
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003138 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3139 return -106;
3140 }
3141 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3142 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3143 Item.val.string.len != 12 ||
3144 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3145 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3146 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3147 Out.uNumUsed != 3) {
3148 return -105;
3149 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003150
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003151 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3152 return -107;
3153 }
3154 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3155 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3156 Item.val.string.len != 3 ||
3157 puTags[0] != 773 ||
3158 Out.uNumUsed != 1) {
3159 return -108;
3160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003161
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003162 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3163 return -109;
3164 }
3165 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003166 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003167 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003168 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003169 puTags[3] != 7 ||
3170 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003171 return -110;
3172 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003173
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003174 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3175 return -111;
3176 }
3177 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3178 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3179 Item.val.string.len != 9 ||
3180 puTags[0] != 17 ||
3181 Out.uNumUsed != 1) {
3182 return -112;
3183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003184
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003185 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3186 return -111;
3187 }
3188 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3189 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3190 Item.val.string.len != 2 ||
3191 puTags[0] != 17 ||
3192 Out.uNumUsed != 1) {
3193 return -112;
3194 }
3195
3196 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3197 return -113;
3198 }
3199 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003200 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003201 Item.val.uCount != 2 ||
3202 puTags[0] != 19 ||
3203 Out.uNumUsed != 1) {
3204 return -114;
3205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003206
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003207 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3208 return -115;
3209 }
3210 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003211 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003212 Item.val.uCount != 1 ||
3213 puTags[0] != 9 ||
3214 Out.uNumUsed != 1) {
3215 return -116;
3216 }
3217
3218 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3219 return -116;
3220 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003221 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003222 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003223 Out.uNumUsed != 0) {
3224 return -117;
3225 }
3226
3227 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3228 return -118;
3229 }
3230 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3231 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003232 puTags[0] != 12 ||
3233 Out.uNumUsed != 1) {
3234 return -119;
3235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003237 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3238 return -120;
3239 }
3240 if(Item.uDataType != QCBOR_TYPE_MAP ||
3241 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3242 Item.val.uCount != 1 ||
3243 puTags[0] != 0x17 ||
3244 Out.uNumUsed != 1) {
3245 return -121;
3246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003248 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3249 return -122;
3250 }
3251 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003252 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003253 Item.val.int64 != -3 ||
3254 puTags[0] != 8 ||
3255 Out.uNumUsed != 1) {
3256 return -123;
3257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003258
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003259 if(QCBORDecode_Finish(&DCtx)) {
3260 return -124;
3261 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003262
3263 UsefulBufC DateString;
3264 QCBORDecode_Init(&DCtx,
3265 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3266 QCBOR_DECODE_MODE_NORMAL);
3267
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003268 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003269 // tagged date string
3270 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3271 // untagged date string
3272 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3273 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3274 return 100;
3275 }
3276 // untagged byte string
3277 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3278 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3279 return 101;
3280 }
3281 // tagged regex
3282 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3283 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3284 return 102;
3285 }
3286 // tagged date string with a byte string
3287 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3288 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3289 return 103;
3290 }
3291 QCBORDecode_ExitArray(&DCtx);
3292 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3293 return 104;
3294 }
3295
3296
3297 QCBORDecode_Init(&DCtx,
3298 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3299 QCBOR_DECODE_MODE_NORMAL);
3300
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003301 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003302 // tagged date string
3303 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3304 // untagged date string
3305 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3306 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3307 return 200;
3308 }
3309 // untagged byte string
3310 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3311 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3312 return 201;
3313 }
3314 // tagged regex
3315 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3316 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3317 return 202;
3318 }
3319 // tagged date string with a byte string
3320 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3321 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3322 return 203;
3323 }
3324 QCBORDecode_ExitArray(&DCtx);
3325 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3326 return 204;
3327 }
3328
3329 QCBORDecode_Init(&DCtx,
3330 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3331 QCBOR_DECODE_MODE_NORMAL);
3332
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003333 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003334 // tagged date string
3335 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3336 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3337 return 300;
3338 }
3339 // untagged date string
3340 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3341 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3342 return 301;
3343 }
3344 // untagged byte string
3345 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3346 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3347 return 302;
3348 }
3349 // tagged regex
3350 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3351 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3352 return 303;
3353 }
3354 // tagged date string with a byte string
3355 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3356 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3357 return 304;
3358 }
3359 QCBORDecode_ExitArray(&DCtx);
3360 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3361 return 305;
3362 }
3363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003364 return 0;
3365}
3366
3367
3368
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003369
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003370static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003371 0x83,
3372 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3373 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3374 0xA4,
3375 0x63, 0x42, 0x4E, 0x2B,
3376 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3377 0x18, 0x40,
3378 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3379 0x63, 0x42, 0x4E, 0x2D,
3380 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3381 0x38, 0x3F,
3382 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3383
3384
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003385static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003386
3387
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003388int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003389{
3390 QCBORDecodeContext DCtx;
3391 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003392 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003393
Laurence Lundbladeee851742020-01-08 08:37:05 -08003394 QCBORDecode_Init(&DCtx,
3395 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3396 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003397
3398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003399 //
3400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3401 return -1;
3402 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003403 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003405
3406 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003407 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003408 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003409 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003410 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003411 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003412 }
3413
3414 //
3415 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003416 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003417 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003418 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003419 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003421
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003422 //
3423 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003424 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003425 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003426 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003429 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003430 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003431 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3432 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003433 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003434 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003435 }
3436
3437 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003438 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003439 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3440 Item.uLabelType != QCBOR_TYPE_INT64 ||
3441 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003442 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003443 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003444 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003445
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003446 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003447 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003448 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3449 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003450 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003451 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003452 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003453
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003454 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003455 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003456 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3457 Item.uLabelType != QCBOR_TYPE_INT64 ||
3458 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003459 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003460 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003461 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003462
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003463 return 0;
3464}
3465
3466
3467
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003468static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003469 uint8_t uDataType,
3470 uint8_t uNestingLevel,
3471 uint8_t uNextNest,
3472 int64_t nLabel,
3473 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003474{
3475 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003476 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003477
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003478 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3479 if(Item.uDataType != uDataType) return -1;
3480 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003481 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3482 Item.uLabelType != QCBOR_TYPE_UINT64) {
3483 return -1;
3484 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003485 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3486 if(Item.label.int64 != nLabel) return -1;
3487 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003488 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003489 }
3490 }
3491 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303492 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003493
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003494 if(pItem) {
3495 *pItem = Item;
3496 }
3497 return 0;
3498}
3499
3500
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003501// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003502static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003503{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303504 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003505
Laurence Lundblade9b334962020-08-27 10:55:53 -07003506 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003507
Laurence Lundblade9b334962020-08-27 10:55:53 -07003508 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003509
Laurence Lundblade9b334962020-08-27 10:55:53 -07003510 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3511 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3512 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3513 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3514 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003515
Laurence Lundblade9b334962020-08-27 10:55:53 -07003516 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3517 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003518
Laurence Lundblade9b334962020-08-27 10:55:53 -07003519 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3520 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003521
Laurence Lundblade9b334962020-08-27 10:55:53 -07003522 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3523 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003524
Laurence Lundblade9b334962020-08-27 10:55:53 -07003525 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003526
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003527 return 0;
3528}
3529
3530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003531/*
3532// cbor.me decoded output
3533{
3534 -23: {
3535 -20: {
3536 -18: "Organization",
3537 -17: "SSG",
3538 -15: "Confusion",
3539 -16: "San Diego",
3540 -14: "US"
3541 },
3542 -19: {
3543 -11: {
3544 -9: -7
3545 },
3546 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3547 }
3548 },
3549 -22: {
3550 -5: -3
3551 }
3552}
3553 */
3554
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003555
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003556static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003557 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3558 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3559 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3560 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3561 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3562 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3563 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3564 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3565 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3566
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003567int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003568{
3569 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003570
Laurence Lundbladeee851742020-01-08 08:37:05 -08003571 QCBORDecode_Init(&DCtx,
3572 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3573 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003574
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003575 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003576}
3577
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003578
3579
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003580int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003581{
3582 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003583
Laurence Lundbladeee851742020-01-08 08:37:05 -08003584 QCBORDecode_Init(&DCtx,
3585 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3586 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003587
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003588 QCBORItem Item;
3589 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003590
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003591 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3592 return -1;
3593 }
3594 if(Item.uDataType != QCBOR_TYPE_MAP) {
3595 return -2;
3596 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003597
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003598 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3599 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3600 return -3;
3601 }
3602
3603 return 0;
3604}
3605
3606
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003607// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003608static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003609 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3610 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3611 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3612 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3613 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3614 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003615 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3616 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3617 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3618 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003619
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003620int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003621{
3622 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003623
Laurence Lundbladeee851742020-01-08 08:37:05 -08003624 QCBORDecode_Init(&DCtx,
3625 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3626 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003627
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003628 return CheckCSRMaps(&DCtx);
3629}
3630
3631
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003632
Laurence Lundblade17ede402018-10-13 11:43:07 +08003633static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3634{
3635 UsefulOutBuf UOB;
3636 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003637
Laurence Lundblade17ede402018-10-13 11:43:07 +08003638 int i;
3639 for(i = 0; i < n; i++) {
3640 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3641 }
3642
3643 for(i = 0; i < n; i++) {
3644 UsefulOutBuf_AppendByte(&UOB, 0xff);
3645 }
3646 return UsefulOutBuf_OutUBuf(&UOB);
3647}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003648
3649
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003650static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003651{
3652 QCBORDecodeContext DC;
3653 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003654
Laurence Lundblade17ede402018-10-13 11:43:07 +08003655 int j;
3656 for(j = 0; j < nNestLevel; j++) {
3657 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003658 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003659 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3660 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003661 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003662 return -4;
3663 } else {
3664 return 0; // Decoding doesn't recover after an error
3665 }
3666 } else {
3667 // Should be no error
3668 if(nReturn) {
3669 return -9; // Should not have got an error
3670 }
3671 }
3672 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3673 return -7;
3674 }
3675 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003676 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003677 if(nReturn) {
3678 return -3;
3679 }
3680 return 0;
3681}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003682
3683
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003684int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003685{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303686 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003687 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003688 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003689 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003690 int nReturn = parse_indeflen_nested(Nested, i);
3691 if(nReturn) {
3692 return nReturn;
3693 }
3694 }
3695 return 0;
3696}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003697
Laurence Lundbladeee851742020-01-08 08:37:05 -08003698// [1, [2, 3]]
3699static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3700// No closing break
3701static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3702// Not enough closing breaks
3703static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3704// Too many closing breaks
3705static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3706// Unclosed indeflen inside def len
3707static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3708// confused tag
3709static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003710
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003711int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003712{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003713 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003714 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003715 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003716
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003717 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003718 QCBORDecodeContext DC;
3719 QCBORItem Item;
3720 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003721
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003722 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303723
3724 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3725 Item.uNestingLevel != 0 ||
3726 Item.uNextNestLevel != 1) {
3727 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003728 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003729
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003730 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303731 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3732 Item.uNestingLevel != 1 ||
3733 Item.uNextNestLevel != 1) {
3734 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003735 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003736
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003737 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303738 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3739 Item.uNestingLevel != 1 ||
3740 Item.uNextNestLevel != 2) {
3741 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003742 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003743
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003744 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003745 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303746 Item.uNestingLevel != 2 ||
3747 Item.uNextNestLevel != 2) {
3748 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003749 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003750
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003751 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003752 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303753 Item.uNestingLevel != 2 ||
3754 Item.uNextNestLevel != 0) {
3755 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003756 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003757
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003758 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303759 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003760 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003761
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003762 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003763 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003764
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003765 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003766
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003767 nResult = QCBORDecode_GetNext(&DC, &Item);
3768 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303769 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003770 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003771
Laurence Lundblade570fab52018-10-13 18:28:27 +08003772 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003773 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303774 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003775 }
3776
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003777
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003778 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003779 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003780
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003781 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003782
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003783 nResult = QCBORDecode_GetNext(&DC, &Item);
3784 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303785 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003786 }
3787
3788 nResult = QCBORDecode_GetNext(&DC, &Item);
3789 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303790 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003791 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003792
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003793 nResult = QCBORDecode_GetNext(&DC, &Item);
3794 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303795 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003796 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003797
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003798 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003799 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303800 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003801 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003802
3803
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003804 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003805 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003806
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003807 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003808
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003809 nResult = QCBORDecode_GetNext(&DC, &Item);
3810 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303811 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003812 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003813
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003814 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003815 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303816 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003817 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303818
Laurence Lundblade642282a2020-06-23 12:00:33 -07003819 nResult = QCBORDecode_GetNext(&DC, &Item);
3820 if(nResult != QCBOR_ERR_BAD_BREAK) {
3821 return -140;
3822 }
3823
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003824
Laurence Lundblade570fab52018-10-13 18:28:27 +08003825 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003826 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003827
Laurence Lundblade570fab52018-10-13 18:28:27 +08003828 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003829
Laurence Lundblade570fab52018-10-13 18:28:27 +08003830 nResult = QCBORDecode_GetNext(&DC, &Item);
3831 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303832 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003833 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003834
Laurence Lundblade570fab52018-10-13 18:28:27 +08003835 nResult = QCBORDecode_GetNext(&DC, &Item);
3836 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303837 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003838 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003839
Laurence Lundblade570fab52018-10-13 18:28:27 +08003840 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003841 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303842 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003843 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003844
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303845 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003846 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003847
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303848 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003849
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303850 nResult = QCBORDecode_GetNext(&DC, &Item);
3851 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303852 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303853 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003854
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303855 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303856 if(nResult != QCBOR_ERR_BAD_BREAK) {
3857 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303858 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003859
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003860 return 0;
3861}
3862
Laurence Lundblade17ede402018-10-13 11:43:07 +08003863
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08003864#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
3865
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003866static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003867 0x81, // Array of length one
3868 0x7f, // text string marked with indefinite length
3869 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3870 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3871 0xff // ending break
3872};
3873
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003874static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303875 0x81, // Array of length one
3876 0x7f, // text string marked with indefinite length
3877 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3878 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3879 0xff // ending break
3880};
3881
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003882static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303883 0x81, // Array of length one
3884 0x7f, // text string marked with indefinite length
3885 0x01, 0x02, // Not a string
3886 0xff // ending break
3887};
3888
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003889static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303890 0x81, // Array of length one
3891 0x7f, // text string marked with indefinite length
3892 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3893 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3894 // missing end of string
3895};
3896
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003897static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303898 0xa1, // Array of length one
3899 0x7f, // text string marked with indefinite length
3900 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3901 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3902 0xff, // ending break
3903 0x01 // integer being labeled.
3904};
3905
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003906/**
3907 Make an indefinite length string
3908
3909 @param Storage Storage for string, must be 144 bytes in size
3910 @return The indefinite length string
3911
3912 This makes an array with one indefinite length string that has 7 chunks
3913 from size of 1 byte up to 64 bytes.
3914 */
3915static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303916{
3917 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003918
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303919 UsefulOutBuf_Init(&UOB, Storage);
3920 UsefulOutBuf_AppendByte(&UOB, 0x81);
3921 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003922
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003923 uint8_t uStringByte = 0;
3924 // Use of type int is intentional
3925 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3926 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303927 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003928 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3929 for(int j = 0; j < uChunkSize; j++) {
3930 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3931 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303932 }
3933 }
3934 UsefulOutBuf_AppendByte(&UOB, 0xff);
3935
3936 return UsefulOutBuf_OutUBuf(&UOB);
3937}
3938
3939static int CheckBigString(UsefulBufC BigString)
3940{
3941 if(BigString.len != 255) {
3942 return 1;
3943 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003944
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303945 for(uint8_t i = 0; i < 255; i++){
3946 if(((const uint8_t *)BigString.ptr)[i] != i) {
3947 return 1;
3948 }
3949 }
3950 return 0;
3951}
3952
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303953
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003954int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303955{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303956 QCBORDecodeContext DC;
3957 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303958 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003959 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003960
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303961 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003962 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303963 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003964
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303965 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303966 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303967 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003968
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303969 if(QCBORDecode_GetNext(&DC, &Item)) {
3970 return -2;
3971 }
3972 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3973 return -3;
3974 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003975
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303976 if(QCBORDecode_GetNext(&DC, &Item)) {
3977 return -4;
3978 }
3979 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3980 return -5;
3981 }
3982 if(QCBORDecode_Finish(&DC)) {
3983 return -6;
3984 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303985
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303986 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003987 QCBORDecode_Init(&DC,
3988 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3989 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303991 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3992 return -7;
3993 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003994
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303995 if(QCBORDecode_GetNext(&DC, &Item)) {
3996 return -8;
3997 }
3998 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3999 return -9;
4000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004001
Laurence Lundblade30816f22018-11-10 13:40:22 +07004002 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304003 return -10;
4004 }
4005
4006 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004007 QCBORDecode_Init(&DC,
4008 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4009 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004010
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304011 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4012 return -11;
4013 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004014
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304015 if(QCBORDecode_GetNext(&DC, &Item)) {
4016 return -12;
4017 }
4018 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4019 return -13;
4020 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004021
Laurence Lundblade30816f22018-11-10 13:40:22 +07004022 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304023 return -14;
4024 }
4025
4026 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004027 QCBORDecode_Init(&DC,
4028 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4029 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004030
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304031 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4032 return -15;
4033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004034
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304035 if(QCBORDecode_GetNext(&DC, &Item)) {
4036 return -16;
4037 }
4038 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4039 return -17;
4040 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004041
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304042 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4043 return -18;
4044 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004045
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304046 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304047 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004048
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304049 QCBORDecode_GetNext(&DC, &Item);
4050 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304051 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304052 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004053
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304054 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304055 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304056 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004057
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304058 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004059 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304060
4061 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4062 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304063 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304064 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004065
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304066 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304067 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004068 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004069
Laurence Lundbladeee851742020-01-08 08:37:05 -08004070 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4071 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004072
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304073 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304074 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304075 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004077
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304078 QCBORDecode_GetNext(&DC, &Item);
4079 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304080 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304081 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004082 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304083 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304084 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004085
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304086 // ---- big bstr -----
4087 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004088
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304089 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4090 return -25;
4091 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004092
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304093 if(QCBORDecode_GetNext(&DC, &Item)) {
4094 return -26;
4095 }
4096 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304097 return -26;
4098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004099
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304100 if(QCBORDecode_GetNext(&DC, &Item)) {
4101 return -27;
4102 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304103 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304104 return -28;
4105 }
4106 if(CheckBigString(Item.val.string)) {
4107 return -3;
4108 }
4109 if(QCBORDecode_Finish(&DC)) {
4110 return -29;
4111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004112
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304113 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004114 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004115
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304116 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4117 return -30;
4118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004119
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304120 QCBORDecode_GetNext(&DC, &Item);
4121 if(Item.uDataType != QCBOR_TYPE_MAP) {
4122 return -31;
4123 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004124
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304125 if(QCBORDecode_GetNext(&DC, &Item)){
4126 return -32;
4127 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004128 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4129 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304130 Item.uDataAlloc || !Item.uLabelAlloc ||
4131 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4132 return -33;
4133 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004134
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304135 if(QCBORDecode_Finish(&DC)) {
4136 return -34;
4137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004138
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004139 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004140}
4141
4142
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004143int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304144{
4145 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004146 QCBORError nCBORError;
4147
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004148
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304149 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004150 QCBORDecode_Init(&DC,
4151 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4152 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004153
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004154 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004155
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004156 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4157 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304158 return -1;
4159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004160
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004161 if(CheckCSRMaps(&DC)) {
4162 return -2;
4163 }
4164
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004165 // Next parse, save pointers to a few strings, destroy original and
4166 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004167 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004168 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004169
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304170 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004171 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304172 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004173
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304174 QCBORItem Item1, Item2, Item3, Item4;
4175 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004176 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304177 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4178 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004179 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304180 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004181 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304182 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004183 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304184 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004185 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304186 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004187 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004188
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304189 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004190
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304191 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304192 Item1.uDataType != QCBOR_TYPE_INT64 ||
4193 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004194 Item1.uDataAlloc != 0 ||
4195 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004196 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004197 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004198 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004199
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304200
4201 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004202 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304203 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004204 Item2.uDataAlloc != 0 ||
4205 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304206 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004207 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004208
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304209 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004210 Item3.uDataAlloc == 0 ||
4211 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004212 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004213 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004215
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304216 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004217 Item4.uDataAlloc == 0 ||
4218 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004219 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004220 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004222
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304223 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004224 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004225 QCBORDecode_Init(&DC,
4226 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4227 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304228 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4229 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004230 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304231 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004232 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004233 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004234 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304235 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4236 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4237 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4238 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4239 }
4240 }
4241 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004242 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004243 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304244 }
4245
4246 return 0;
4247}
4248
Laurence Lundbladef6531662018-12-04 10:42:22 +09004249
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004250int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004251{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004252 // Set up the decoder with a tiny bit of CBOR to parse because
4253 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004254 QCBORDecodeContext DC;
4255 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4256 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004257
Laurence Lundbladef6531662018-12-04 10:42:22 +09004258 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004259 // Then fish into the internals of the decode context
4260 // to get the allocator function so it can be called directly.
4261 // Also figure out how much pool is available for use
4262 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004263 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004264 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4265 if(nError) {
4266 return -9;
4267 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004268 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4269 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4270 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004271
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004272 // First test -- ask for one more byte than available and see failure
4273 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004274 if(!UsefulBuf_IsNULL(Allocated)) {
4275 return -1;
4276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004277
Laurence Lundbladef6531662018-12-04 10:42:22 +09004278 // Re do the set up for the next test that will do a successful alloc,
4279 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004280 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004281 pAlloc = DC.StringAllocator.pfAllocator;
4282 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4283 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004284
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004285 // Allocate one byte less than available and see success
4286 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004287 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4288 return -2;
4289 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004290 // Ask for some more and see failure
4291 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004292 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4293 return -3;
4294 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004295 // Free the first allocate, retry the second and see success
4296 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4297 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004298 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4299 return -4;
4300 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004301
Laurence Lundbladef6531662018-12-04 10:42:22 +09004302 // Re do set up for next test that involves a successful alloc,
4303 // and a successful realloc and a failed realloc
4304 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004305 pAlloc = DC.StringAllocator.pfAllocator;
4306 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004307
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004308 // Allocate half the pool and see success
4309 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004310 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4311 return -5;
4312 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004313 // Reallocate to take up the whole pool and see success
4314 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004315 if(UsefulBuf_IsNULL(Allocated2)) {
4316 return -6;
4317 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004318 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004319 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4320 return -7;
4321 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004322 // Try to allocate more to be sure there is failure after a realloc
4323 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4324 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004325 return -8;
4326 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004327
Laurence Lundbladef6531662018-12-04 10:42:22 +09004328 return 0;
4329}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004330
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004331
4332/* Just enough of an allocator to test configuration of one */
4333static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4334{
4335 (void)pOldMem; // unused variable
4336
4337 if(uNewSize) {
4338 // Assumes the context pointer is the buffer and
4339 // nothing too big will ever be asked for.
4340 // This is only good for this basic test!
4341 return (UsefulBuf) {pCtx, uNewSize};
4342 } else {
4343 return NULLUsefulBuf;
4344 }
4345}
4346
4347
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004348int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004349{
4350 // Set up the decoder with a tiny bit of CBOR to parse because
4351 // nothing can be done with it unless that is set up.
4352 QCBORDecodeContext DC;
4353 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4354 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4355
4356 uint8_t pAllocatorBuffer[50];
4357
4358 // This is really just to test that this call works.
4359 // The full functionality of string allocators is tested
4360 // elsewhere with the MemPool internal allocator.
4361 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4362
4363 QCBORItem Item;
4364 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4365 return -1;
4366 }
4367
4368 if(Item.uDataAlloc == 0 ||
4369 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4370 Item.val.string.ptr != pAllocatorBuffer) {
4371 return -2;
4372 }
4373
4374 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4375 return -3;
4376 }
4377
4378 return 0;
4379}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004380#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4381
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004382
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004383#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004384
Laurence Lundbladea826c502020-05-10 21:07:00 -07004385/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004386 [
4387 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004388 4([-20, 4759477275222530853136]),
4389 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004390 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004391 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004392 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004393 5([ 9223372036854775806, -4759477275222530853137])
4394 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004395 ]
4396 */
4397
4398static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004399 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004400 0xC4, 0x82, 0x20,
4401 0x03,
4402 0xC4, 0x82, 0x33,
4403 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4404 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4405 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4406 0xC5, 0x82, 0x19, 0x01, 0x2C,
4407 0x18, 0x64,
4408 0xC5, 0x82, 0x33,
4409 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4410 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4411 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004412 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4413 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004414 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4415 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4416};
4417
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004418
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004419int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004420{
4421 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004422 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004423 QCBORItem item;
4424
Laurence Lundblade17af4902020-01-07 19:11:55 -08004425 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4426 0x06, 0x07, 0x08, 0x09, 0x010};
4427 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004428
4429
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004430 QCBORDecode_Init(&DC,
4431 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4432 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004433
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004434 uErr = QCBORDecode_GetNext(&DC, &item);
4435 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004436 return 1;
4437 }
4438
4439 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4440 return 2;
4441 }
4442
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004443 uErr = QCBORDecode_GetNext(&DC, &item);
4444 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004445 return 3;
4446 }
4447
4448 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4449 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4450 item.val.expAndMantissa.nExponent != -1) {
4451 return 4;
4452 }
4453
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004454 uErr = QCBORDecode_GetNext(&DC, &item);
4455 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004456 return 5;
4457 }
4458
4459 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4460 item.val.expAndMantissa.nExponent != -20 ||
4461 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4462 return 6;
4463 }
4464
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004465 uErr = QCBORDecode_GetNext(&DC, &item);
4466 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004467 return 7;
4468 }
4469
4470 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4471 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4472 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4473 return 8;
4474 }
4475
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004476 uErr = QCBORDecode_GetNext(&DC, &item);
4477 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004478 return 9;
4479 }
4480
4481 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4482 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4483 item.val.expAndMantissa.nExponent != 300) {
4484 return 10;
4485 }
4486
Laurence Lundbladea826c502020-05-10 21:07:00 -07004487 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004488 uErr = QCBORDecode_GetNext(&DC, &item);
4489 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004490 return 11;
4491 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004492 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4493 item.val.expAndMantissa.nExponent != -20 ||
4494 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4495 return 12;
4496 }
4497
Laurence Lundbladea826c502020-05-10 21:07:00 -07004498 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004499 uErr = QCBORDecode_GetNext(&DC, &item);
4500 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004501 return 13;
4502 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004503 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4504 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4505 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4506 return 14;
4507 }
4508
Laurence Lundbladea826c502020-05-10 21:07:00 -07004509 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004510 uErr = QCBORDecode_GetNext(&DC, &item);
4511 if(uErr != QCBOR_SUCCESS) {
4512 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004513 }
4514 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4515 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4516 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004517 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004518 }
4519
Laurence Lundbladea826c502020-05-10 21:07:00 -07004520 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004521 uErr = QCBORDecode_GetNext(&DC, &item);
4522 if(uErr != QCBOR_SUCCESS) {
4523 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004524 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004525 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4526 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4527 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004528 return 18;
4529 }
4530
4531 uErr = QCBORDecode_Finish(&DC);
4532 if(uErr != QCBOR_SUCCESS) {
4533 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004534 }
4535
4536 /* Now encode some stuff and then decode it */
4537 uint8_t pBuf[40];
4538 QCBOREncodeContext EC;
4539 UsefulBufC Encoded;
4540
4541 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4542 QCBOREncode_OpenArray(&EC);
4543 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4544 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4545 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4546 QCBOREncode_CloseArray(&EC);
4547 QCBOREncode_Finish(&EC, &Encoded);
4548
4549
4550 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004551 uErr = QCBORDecode_GetNext(&DC, &item);
4552 if(uErr != QCBOR_SUCCESS) {
4553 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004554 }
4555
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004556 uErr = QCBORDecode_GetNext(&DC, &item);
4557 if(uErr != QCBOR_SUCCESS) {
4558 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004559 }
4560
4561 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4562 item.val.expAndMantissa.nExponent != 1000 ||
4563 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004564 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004565 }
4566
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004567 uErr = QCBORDecode_GetNext(&DC, &item);
4568 if(uErr != QCBOR_SUCCESS) {
4569 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004570 }
4571
4572 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4573 item.val.expAndMantissa.nExponent != INT32_MIN ||
4574 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004575 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004576 }
4577
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004578 uErr = QCBORDecode_GetNext(&DC, &item);
4579 if(uErr != QCBOR_SUCCESS) {
4580 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004581 }
4582
4583 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4584 item.val.expAndMantissa.nExponent != INT32_MAX ||
4585 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004586 return 106;
4587 }
4588
4589
4590 int64_t nExp, nMant;
4591 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4592 UsefulBufC Mant;
4593 bool bIsNeg;
4594
4595 QCBORDecode_Init(&DC,
4596 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4597 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004598 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004599
4600 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004601 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004602
4603 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004604 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4605 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004606
4607 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004608 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4609 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004610
4611 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004612 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004613
4614 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004615 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4616 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004617
4618 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004619 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4620 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004621
4622 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004623 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4624 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004625
4626 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004627 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4628 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004629
4630 QCBORDecode_ExitArray(&DC);
4631
4632 uErr = QCBORDecode_Finish(&DC);
4633 if(uErr != QCBOR_SUCCESS) {
4634 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004635 }
4636
4637 return 0;
4638}
4639
4640
4641static struct FailInput ExponentAndMantissaFailures[] = {
4642 // Exponent > INT64_MAX
4643 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4644 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4645 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4646 // Mantissa > INT64_MAX
4647 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4648 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4649 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4650 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004651 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004652 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004653 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004654 // bad content for big num
4655 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4656 // bad content for big num
4657 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4658 // Bad integer for exponent
4659 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4660 // Bad integer for mantissa
4661 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4662 // 3 items in array
4663 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004664#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004665 // unterminated indefinite length array
4666 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004667#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4668 // unterminated indefinite length array
4669 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4670#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004671 // Empty array
4672 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4673 // Second is not an integer
4674 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4675 // First is not an integer
4676 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4677 // Not an array
4678 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4679};
4680
4681
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004682int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004683{
4684 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004685 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4686 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004687}
4688
4689#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004690
4691
4692
4693/*
4694 Some basic CBOR with map and array used in a lot of tests.
4695 The map labels are all strings
4696
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004697 {
4698 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004699 "an array of two strings": [
4700 "string1", "string2"
4701 ],
4702 "map in a map": {
4703 "bytes 1": h'78787878',
4704 "bytes 2": h'79797979',
4705 "another int": 98,
4706 "text 2": "lies, damn lies and statistics"
4707 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004708 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004709 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004710
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004711int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004712{
4713 QCBORItem Item1, Item2, Item3;
4714 int64_t nDecodedInt1, nDecodedInt2;
4715 UsefulBufC B1, B2, S1, S2, S3;
4716
4717 QCBORDecodeContext DCtx;
4718 QCBORError nCBORError;
4719
4720 QCBORDecode_Init(&DCtx, input, 0);
4721
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004722 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004723
4724 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4725
4726 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4727 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004728 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4729 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4730 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004731 QCBORDecode_ExitMap(&DCtx);
4732
4733 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4734 QCBORDecode_GetNext(&DCtx, &Item1);
4735 QCBORDecode_GetNext(&DCtx, &Item2);
4736 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4737 return -400;
4738 }
4739 QCBORDecode_ExitArray(&DCtx);
4740
4741 // Parse the same array again using GetText() instead of GetItem()
4742 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004743 QCBORDecode_GetTextString(&DCtx, &S2);
4744 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004745 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4746 return 5000;
4747 }
4748 /* QCBORDecode_GetText(&DCtx, &S3);
4749 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4750 return 5001;
4751 } */
4752
4753 QCBORDecode_ExitArray(&DCtx);
4754
4755 QCBORDecode_ExitMap(&DCtx);
4756
4757 nCBORError = QCBORDecode_Finish(&DCtx);
4758
4759 if(nCBORError) {
4760 return (int32_t)nCBORError;
4761 }
4762
4763 if(nDecodedInt1 != 42) {
4764 return 1001;
4765 }
4766
4767 if(nDecodedInt2 != 98) {
4768 return 1002;
4769 }
4770
4771 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004772 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004773 return 1003;
4774 }
4775
4776 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004777 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004778 return 1004;
4779 }
4780
Laurence Lundblade9b334962020-08-27 10:55:53 -07004781 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004782 return 1005;
4783 }
4784
4785 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4786 return 1006;
4787 }
4788
4789 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4790 return 1007;
4791 }
4792
4793 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4794 return 1008;
4795 }
4796
4797 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4798 return 1009;
4799 }
4800
4801 return 0;
4802}
4803
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004804/*
4805 {
4806 -75008: h'05083399',
4807 88: [
4808 ],
4809 100100: {
4810 "sub1": {
4811 10: [
4812 0
4813 ],
4814 -75009: h'A46823990001',
4815 100100: {
4816 "json": "{ \"ueid\", \"xyz\"}",
4817 "subsub": {
4818 100002: h'141813191001'
4819 }
4820 }
4821 }
4822 }
4823 }
4824 */
4825
4826static const uint8_t spNestedCBOR[] = {
48270xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
48280x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
48290x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
48300x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
48310x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
48320x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, 0x01
4833};
4834
4835/* Get item in multi-level nesting in spNestedCBOR */
4836static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4837{
4838 UsefulBufC String;
4839
4840 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4841 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4842
4843 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4844 QCBORDecode_EnterMap(pDCtx, NULL);
4845 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4846 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4847 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4848 if(QCBORDecode_GetError(pDCtx)) {
4849 return 4001;
4850 }
4851 if(UsefulBuf_Compare(String, test_oemid)) {
4852 return 4002;
4853 }
4854 QCBORDecode_ExitMap(pDCtx);
4855 QCBORDecode_ExitMap(pDCtx);
4856 QCBORDecode_ExitMap(pDCtx);
4857 QCBORDecode_ExitMap(pDCtx);
4858
4859 return 0;
4860}
4861
4862/* Iterations on the zero-length array in spNestedCBOR */
4863static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4864{
4865 QCBORItem Item;
4866 QCBORError uErr;
4867
4868 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4869 for(int x = 0; x < 20; x++) {
4870 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4871 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4872 return 4100;
4873
4874 }
4875 }
4876 QCBORDecode_ExitArray(pDCtx);
4877 if(QCBORDecode_GetError(pDCtx)) {
4878 return 4101;
4879 }
4880
4881 return 0;
4882}
4883
4884/* Various iterations on the array that contains a zero in spNestedCBOR */
4885static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
4886{
4887 QCBORError uErr;
4888
4889 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4890 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
4891 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4892 int64_t nInt = 99;
4893 QCBORDecode_GetInt64(pDCtx, &nInt);
4894 if(nInt != 0) {
4895 return 4200;
4896 }
4897 for(int x = 0; x < 20; x++) {
4898 QCBORItem Item;
4899 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4900 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4901 return 4201;
4902
4903 }
4904 }
4905 QCBORDecode_ExitArray(pDCtx);
4906 if(QCBORDecode_GetAndResetError(pDCtx)) {
4907 return 4202;
4908 }
4909 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4910 UsefulBufC dD;
4911 QCBORDecode_GetByteString(pDCtx, &dD);
4912 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4913 return 4203;
4914 }
4915 for(int x = 0; x < 20; x++) {
4916 QCBORDecode_GetByteString(pDCtx, &dD);
4917 uErr = QCBORDecode_GetAndResetError(pDCtx);
4918 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4919 return 4204;
4920 }
4921 }
4922 QCBORDecode_ExitArray(pDCtx);
4923 QCBORDecode_ExitMap(pDCtx);
4924 QCBORDecode_ExitMap(pDCtx);
4925
4926 return 0;
4927}
4928
4929/* Repeatedly enter and exit maps and arrays, go off the end of maps
4930 and arrays and such. */
4931static int32_t DecodeNestedIterate()
4932{
4933 QCBORDecodeContext DCtx;
4934 int32_t nReturn;
4935 QCBORError uErr;
4936
4937 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
4938 QCBORDecode_EnterMap(&DCtx, NULL);
4939
4940 for(int j = 0; j < 5; j++) {
4941 for(int i = 0; i < 20; i++) {
4942 nReturn = DecodeNestedGetSubSub(&DCtx);
4943 if(nReturn) {
4944 return nReturn;
4945 }
4946 }
4947
4948 for(int i = 0; i < 20; i++) {
4949 nReturn = DecodeNestedGetEmpty(&DCtx);
4950 if(nReturn ) {
4951 return nReturn;
4952 }
4953 }
4954
4955 for(int i = 0; i < 20; i++) {
4956 nReturn = DecodeNestedGetZero(&DCtx);
4957 if(nReturn ) {
4958 return nReturn;
4959 }
4960 }
4961 }
4962
4963 QCBORDecode_ExitMap(&DCtx);
4964 uErr = QCBORDecode_Finish(&DCtx);
4965 if(uErr) {
4966 return (int32_t)uErr + 4100;
4967 }
4968
4969 return 0;
4970}
4971
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004972
4973/*
4974 [23,
4975 6000,
4976 h'67616C6163746963',
4977 h'686176656E20746F6B656E'
4978 ]
4979 */
4980static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004981 0x84,
4982 0x17,
4983 0x19, 0x17, 0x70,
4984 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4985 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004986
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004987static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4988
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004989
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004990static const uint8_t spEmptyMap[] = {0xa0};
4991
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004992#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004993static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004994
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004995
4996
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004997
Laurence Lundbladef0499502020-08-01 11:55:57 -07004998/*
4999 {
5000 0: [],
5001 9: [
5002 [],
5003 []
5004 ],
5005 8: {
5006 1: [],
5007 2: {},
5008 3: []
5009 },
5010 4: {},
5011 5: [],
5012 6: [
5013 [],
5014 []
5015 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07005016 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005017 */
Laurence Lundblade44080632020-08-06 21:43:50 -07005018
Laurence Lundbladef0499502020-08-01 11:55:57 -07005019static const uint8_t spMapOfEmpty[] = {
5020 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
5021 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
5022 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005023
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005024#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5025
5026
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005027/*
5028 Too many tags
5029 Invalid tag content
5030 Duplicate label
5031 Integer overflow
5032 Date overflow
5033
5034 {1: 224(225(226(227(4(0))))),
5035 2: 1(h''),
5036 3: -18446744073709551616,
5037 4: 1(1.0e+300),
5038 5: 0, 8: 8}
5039 */
5040static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005041 0xa7,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005042 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5043 0x02, 0xc1, 0x40,
5044 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5045 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5046 0x05, 0x00,
5047 0x05, 0x00,
5048 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005049};
5050
5051// Bad break
5052static const uint8_t spUnRecoverableMapError1[] = {
5053 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5054};
5055
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005056#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005057// No more items
5058static const uint8_t spUnRecoverableMapError2[] = {
5059 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5060};
5061
5062// Hit end because string is too long
5063static const uint8_t spUnRecoverableMapError3[] = {
5064 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5065};
5066
5067// Hit end because string is too long
5068static const uint8_t spUnRecoverableMapError4[] = {
5069 0xbf,
5070 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5071 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5072 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5073 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5074 0xff
5075};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005076#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005077
5078
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005079int32_t EnterMapTest()
5080{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005081 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005082 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005083 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005084 int32_t nReturn;
5085 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005086
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005087#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005088 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005089 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005090
Laurence Lundbladef0499502020-08-01 11:55:57 -07005091
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005092 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005093 QCBORDecode_ExitArray(&DCtx);
5094
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005095 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5096 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005097 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005098 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005099 QCBORDecode_ExitArray(&DCtx);
5100 QCBORDecode_ExitArray(&DCtx);
5101
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005102 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5103 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005104 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005105 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005106 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005107 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005108 QCBORDecode_ExitArray(&DCtx);
5109 QCBORDecode_ExitMap(&DCtx);
5110
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005111 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005112 QCBORDecode_ExitMap(&DCtx);
5113
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005114 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005115 QCBORDecode_ExitArray(&DCtx);
5116
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005117 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5118 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005119 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005120 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005121 QCBORDecode_ExitArray(&DCtx);
5122 QCBORDecode_ExitArray(&DCtx);
5123
5124 QCBORDecode_ExitMap(&DCtx);
5125
5126 uErr = QCBORDecode_Finish(&DCtx);
5127 if(uErr != QCBOR_SUCCESS){
5128 return 3011;
5129 }
5130
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005131 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005132 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005133 if(nReturn) {
5134 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005135 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005136#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5137
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005138
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005139 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005140 if(nReturn) {
5141 return nReturn;
5142 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005143
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005144
Laurence Lundblade937ea812020-05-08 11:38:23 -07005145
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005146 // These tests confirm the cursor is at the right place after entering
5147 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005148 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005149
5150 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005151 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005152 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005153 QCBORDecode_GetNext(&DCtx, &Item1);
5154 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5155 return 2001;
5156 }
5157
5158
Laurence Lundblade9b334962020-08-27 10:55:53 -07005159 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005160 QCBORDecode_VGetNext(&DCtx, &Item1);
5161 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005162 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5163 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5164 UsefulBuf_Compare(ArrayItem.label.string,
5165 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5166 return 2051;
5167 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005168 QCBORDecode_GetNext(&DCtx, &Item1);
5169 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5170 return 2002;
5171 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005172 QCBORDecode_ExitArray(&DCtx);
5173 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5174 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5175 UsefulBuf_Compare(ArrayItem.label.string,
5176 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5177 return 2052;
5178 }
5179
Laurence Lundblade937ea812020-05-08 11:38:23 -07005180
Laurence Lundblade9b334962020-08-27 10:55:53 -07005181 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005182 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005183 QCBORDecode_GetNext(&DCtx, &Item1);
5184 QCBORDecode_GetNext(&DCtx, &Item1);
5185 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005186 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5187 QCBORDecode_GetNext(&DCtx, &Item1);
5188 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5189 return 2003;
5190 }
5191
Laurence Lundblade9b334962020-08-27 10:55:53 -07005192 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005193 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005194 QCBORDecode_GetNext(&DCtx, &Item1);
5195 QCBORDecode_GetNext(&DCtx, &Item1);
5196 QCBORDecode_GetNext(&DCtx, &Item1);
5197 QCBORDecode_GetNext(&DCtx, &Item1);
5198 QCBORDecode_GetNext(&DCtx, &Item1);
5199 QCBORDecode_GetNext(&DCtx, &Item1);
5200 QCBORDecode_GetNext(&DCtx, &Item1);
5201 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5202 QCBORDecode_GetNext(&DCtx, &Item1);
5203 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005204 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005205 }
5206
Laurence Lundblade9b334962020-08-27 10:55:53 -07005207 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005208 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005209 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5210 QCBORDecode_ExitArray(&DCtx);
5211 QCBORDecode_GetNext(&DCtx, &Item1);
5212 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5213 return 2006;
5214 }
5215 QCBORDecode_ExitMap(&DCtx);
5216 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5217 return 2007;
5218 }
5219
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005220 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005221 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005222 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005223 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5224 uErr = QCBORDecode_GetAndResetError(&DCtx);
5225 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005226 return 2008;
5227 }
5228 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005229 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005230 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005231 return 2009;
5232 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005233
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005234
5235 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005236 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005237 // This will fail because the map is empty.
5238 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5239 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005240 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005241 return 2010;
5242 }
5243 QCBORDecode_ExitMap(&DCtx);
5244 uErr = QCBORDecode_Finish(&DCtx);
5245 if(uErr != QCBOR_SUCCESS){
5246 return 2011;
5247 }
5248
5249
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005250#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005251 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005252 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005253 // This will fail because the map is empty.
5254 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5255 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005256 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005257 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005258 }
5259 QCBORDecode_ExitMap(&DCtx);
5260 uErr = QCBORDecode_Finish(&DCtx);
5261 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005262 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005263 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005264#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005265
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005266
5267 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005268 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005269 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005270 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005271 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005272 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005273 QCBORDecode_ExitArray(&DCtx);
5274 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5275 QCBORDecode_ExitArray(&DCtx);
5276 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005277 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005278 return 2014;
5279 }
5280
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005281 int64_t nInt;
5282 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005283 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005284 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005285 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005286 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5287 return 2021;
5288 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005289 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5290 return 2121;
5291 }
5292 (void)QCBORDecode_GetAndResetError(&DCtx);
5293
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005294
5295 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5296 uErr = QCBORDecode_GetAndResetError(&DCtx);
5297 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5298 return 2022;
5299 }
5300
5301 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5302 uErr = QCBORDecode_GetAndResetError(&DCtx);
5303 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5304 return 2023;
5305 }
5306
5307 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5308 uErr = QCBORDecode_GetAndResetError(&DCtx);
5309#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5310 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5311 return 2024;
5312 }
5313#else
5314 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5315 return 2027;
5316 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005317#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005318
5319 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5320 uErr = QCBORDecode_GetAndResetError(&DCtx);
5321 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5322 return 2025;
5323 }
5324
5325 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5326
5327 QCBORDecode_ExitMap(&DCtx);
5328 uErr = QCBORDecode_Finish(&DCtx);
5329 if(uErr != QCBOR_SUCCESS) {
5330 return 2026;
5331 }
5332
5333 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005334 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005335 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5336 uErr = QCBORDecode_GetAndResetError(&DCtx);
5337 if(uErr != QCBOR_ERR_BAD_BREAK) {
5338 return 2030;
5339 }
5340
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005341#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005342 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005343 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005344 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5345 uErr = QCBORDecode_GetAndResetError(&DCtx);
5346 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5347 return 2031;
5348 }
5349
5350 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005351 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005352 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5353 uErr = QCBORDecode_GetAndResetError(&DCtx);
5354 if(uErr != QCBOR_ERR_HIT_END) {
5355 return 2032;
5356 }
5357
5358 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 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);
5361 uErr = QCBORDecode_GetAndResetError(&DCtx);
5362 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5363 return 2033;
5364 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005365#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5366
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005367
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005368 nReturn = DecodeNestedIterate();
5369
5370 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005371}
5372
5373
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005374struct NumberConversion {
5375 char *szDescription;
5376 UsefulBufC CBOR;
5377 int64_t nConvertedToInt64;
5378 QCBORError uErrorInt64;
5379 uint64_t uConvertToUInt64;
5380 QCBORError uErrorUint64;
5381 double dConvertToDouble;
5382 QCBORError uErrorDouble;
5383};
5384
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005385static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005386 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005387 "too large to fit into int64_t",
5388 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5389 0,
5390 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5391 0,
5392 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5393 ((double)INT64_MIN) + 1 ,
5394 QCBOR_SUCCESS
5395 },
5396 {
5397 "largest negative int that fits in int64_t",
5398 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5399 INT64_MIN,
5400 QCBOR_SUCCESS,
5401 0,
5402 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5403 (double)INT64_MIN,
5404 QCBOR_SUCCESS
5405 },
5406 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005407 "negative bignum -1",
5408 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5409 -1,
5410 QCBOR_SUCCESS,
5411 0,
5412 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5413 -1.0,
5414 QCBOR_SUCCESS
5415 },
5416 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005417 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005418 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5419 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005420#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005421 257000,
5422 QCBOR_SUCCESS,
5423 257000,
5424 QCBOR_SUCCESS,
5425 257000.0,
5426 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005427#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5428 0,
5429 QCBOR_ERR_UNEXPECTED_TYPE,
5430 0,
5431 QCBOR_ERR_UNEXPECTED_TYPE,
5432 0.0,
5433 QCBOR_ERR_UNEXPECTED_TYPE
5434#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005435 },
5436 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005437 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005438 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5439 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005440#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005441 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005442 QCBOR_SUCCESS,
5443 0,
5444 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005445 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005446 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005447#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5448 0,
5449 QCBOR_ERR_UNEXPECTED_TYPE,
5450 0,
5451 QCBOR_ERR_UNEXPECTED_TYPE,
5452 0.0,
5453 QCBOR_ERR_UNEXPECTED_TYPE
5454#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005455 },
5456 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005457 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005458 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5459 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005460#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005461 2056,
5462 QCBOR_SUCCESS,
5463 2056,
5464 QCBOR_SUCCESS,
5465 2056.0,
5466 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005467#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5468 0,
5469 QCBOR_ERR_UNEXPECTED_TYPE,
5470 0,
5471 QCBOR_ERR_UNEXPECTED_TYPE,
5472 0.0,
5473 QCBOR_ERR_UNEXPECTED_TYPE
5474#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005475 },
5476 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005477 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005478 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5479 0,
5480 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5481 0,
5482 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5483 -18446744073709551617.0,
5484 QCBOR_SUCCESS
5485 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005486#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005487 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005488 "Positive bignum 0x01020304 indefinite length string",
5489 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5490 0x01020304,
5491 QCBOR_SUCCESS,
5492 0x01020304,
5493 QCBOR_SUCCESS,
5494 16909060.0,
5495 QCBOR_SUCCESS
5496 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005497#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005498 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005499 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005500 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5501 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005502#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005503 0,
5504 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5505 0,
5506 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5507 -INFINITY,
5508 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005509#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5510 0,
5511 QCBOR_ERR_UNEXPECTED_TYPE,
5512 0,
5513 QCBOR_ERR_UNEXPECTED_TYPE,
5514 0.0,
5515 QCBOR_ERR_UNEXPECTED_TYPE
5516#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005517 },
5518 {
5519 "big float [9223372036854775806, 9223372036854775806]",
5520 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5521 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005522#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005523 0,
5524 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5525 0,
5526 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5527 INFINITY,
5528 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005529#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5530 0,
5531 QCBOR_ERR_UNEXPECTED_TYPE,
5532 0,
5533 QCBOR_ERR_UNEXPECTED_TYPE,
5534 0.0,
5535 QCBOR_ERR_UNEXPECTED_TYPE
5536#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005537 },
5538 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005539 "Big float 3 * 2^^2",
5540 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005541#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005542 12,
5543 QCBOR_SUCCESS,
5544 12,
5545 QCBOR_SUCCESS,
5546 12.0,
5547 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005548#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5549 0,
5550 QCBOR_ERR_UNEXPECTED_TYPE,
5551 0,
5552 QCBOR_ERR_UNEXPECTED_TYPE,
5553 0.0,
5554 QCBOR_ERR_UNEXPECTED_TYPE
5555#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005556 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005557 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005558 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005559 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5560 0,
5561 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5562 18446744073709551615ULL,
5563 QCBOR_SUCCESS,
5564 18446744073709551615.0,
5565 QCBOR_SUCCESS
5566 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005567 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005568 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005569 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5570 65536-1,
5571 QCBOR_SUCCESS,
5572 0xffff,
5573 QCBOR_SUCCESS,
5574 65535.0,
5575 QCBOR_SUCCESS
5576 },
5577 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005578 "Postive integer 0",
5579 {(uint8_t[]){0x0}, 1},
5580 0LL,
5581 QCBOR_SUCCESS,
5582 0ULL,
5583 QCBOR_SUCCESS,
5584 0.0,
5585 QCBOR_SUCCESS
5586 },
5587 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005588 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005589 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5590 -9223372036854775807-1, // INT64_MIN
5591 QCBOR_SUCCESS,
5592 0ULL,
5593 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5594 -9223372036854775808.0,
5595 QCBOR_SUCCESS
5596 },
5597 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005598 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005599 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005600#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005601 100L,
5602 QCBOR_SUCCESS,
5603 100ULL,
5604 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005605#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5606 0,
5607 QCBOR_ERR_HW_FLOAT_DISABLED,
5608 0,
5609 QCBOR_ERR_HW_FLOAT_DISABLED,
5610#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005611 100.3,
5612 QCBOR_SUCCESS
5613 },
5614 {
5615 "Floating point value NaN 0xfa7fc00000",
5616 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005617#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005618 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005619 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005620 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005621 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005622#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5623 0,
5624 QCBOR_ERR_HW_FLOAT_DISABLED,
5625 0,
5626 QCBOR_ERR_HW_FLOAT_DISABLED,
5627#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005628 NAN,
5629 QCBOR_SUCCESS
5630 },
5631 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005632 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005633 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005634#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5635#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5636 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005637 -4,
5638 QCBOR_SUCCESS,
5639 0,
5640 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5641 -4.0,
5642 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005643#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5644 // Float HW disabled
5645 -4,
5646 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5647 0,
5648 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5649 -4.0,
5650 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5651#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005652#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005653 // Half-precision disabled
5654 -4,
5655 QCBOR_ERR_HALF_PRECISION_DISABLED,
5656 0,
5657 QCBOR_ERR_HALF_PRECISION_DISABLED,
5658 -4.0,
5659 QCBOR_ERR_HALF_PRECISION_DISABLED
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005660#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005661 },
5662 {
5663 "Decimal fraction 3/10",
5664 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005665#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005666 0,
5667 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5668 0,
5669 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5670 0.30000000000000004,
5671 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005672#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5673 0,
5674 QCBOR_ERR_UNEXPECTED_TYPE,
5675 0,
5676 QCBOR_ERR_UNEXPECTED_TYPE,
5677 0.0,
5678 QCBOR_ERR_UNEXPECTED_TYPE
5679#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005680 },
5681 {
5682 "+inifinity",
5683 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5684#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5685 0,
5686 QCBOR_ERR_FLOAT_EXCEPTION,
5687 0,
5688 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5689#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5690 0,
5691 QCBOR_ERR_HW_FLOAT_DISABLED,
5692 0,
5693 QCBOR_ERR_HW_FLOAT_DISABLED,
5694#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5695 INFINITY,
5696 QCBOR_SUCCESS
5697 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005698
5699 {
5700 "extreme pos bignum",
5701 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5702 // 50 rows of 8 is 400 digits.
5703 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5704 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5705 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5706 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5707 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5708 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5709 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5710 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5711 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5712 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5713 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5714 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5715 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5716 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5717 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5718 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5719 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5720 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5721 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5722 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5723 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5724 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5725 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5726 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5727 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5728 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5729 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5730 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5731 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5732 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5733 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5734 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5735 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5736 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5737 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5738 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5739 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5740 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5741 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5742 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5743 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5744 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5745 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5746 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5747 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5748 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5749 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5750 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5751 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5752 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5753 404},
5754 0,
5755 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5756 0,
5757 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5758#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5759 INFINITY,
5760 QCBOR_SUCCESS
5761#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5762 0,
5763 QCBOR_ERR_HW_FLOAT_DISABLED,
5764#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5765 },
5766
5767 {
5768 "extreme neg bignum",
5769 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5770 // 50 rows of 8 is 400 digits.
5771 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5772 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5773 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5774 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5775 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5776 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5777 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5778 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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},
5821 404},
5822 0,
5823 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5824 0,
5825 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5826#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5827 -INFINITY,
5828 QCBOR_SUCCESS
5829#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5830 0,
5831 QCBOR_ERR_HW_FLOAT_DISABLED,
5832#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5833 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005834
5835 {
5836 "big float underflow [9223372036854775806, -9223372036854775806]",
5837 {(uint8_t[]){
5838 0xC5, 0x82,
5839 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5840 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5841#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5842 0,
5843 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5844 0,
5845 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5846 0,
5847 QCBOR_SUCCESS
5848#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5849 0,
5850 QCBOR_ERR_UNEXPECTED_TYPE,
5851 0,
5852 QCBOR_ERR_UNEXPECTED_TYPE,
5853 0.0,
5854 QCBOR_ERR_UNEXPECTED_TYPE
5855#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5856 },
5857
5858 {
5859 "bigfloat that evaluates to -INFINITY",
5860 {(uint8_t[]){
5861 0xC5, 0x82,
5862 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5863 0xC3, 0x42, 0x01, 0x01}, 15},
5864#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5865 0,
5866 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5867 0,
5868 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5869 -INFINITY,
5870 QCBOR_SUCCESS
5871#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5872 0,
5873 QCBOR_ERR_UNEXPECTED_TYPE,
5874 0,
5875 QCBOR_ERR_UNEXPECTED_TYPE,
5876 0.0,
5877 QCBOR_ERR_UNEXPECTED_TYPE
5878#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5879 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005880};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005881
5882
5883
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005884
5885static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
5886{
5887 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
5888#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5889 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
5890 return 1;
5891 }
5892#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5893 (void)Pool;
5894#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5895 return 0;
5896}
5897
5898
Laurence Lundblade313b2862020-05-16 01:23:06 -07005899int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005900{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005901 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
5902 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005903
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005904 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5905 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005906
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005907 // Set up the decoding context including a memory pool so that
5908 // indefinite length items can be checked
5909 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005910 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005911
5912 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005913 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5914 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005915 }
5916
5917 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005918 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005919 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005920 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005921 }
5922 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005923 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005924 }
5925
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005926 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005927 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5928 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005929 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005930
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005931 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005932 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005933 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005934 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005935 }
5936 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005937 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005938 }
5939
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005940 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005941 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5942 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005943 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005944#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005945 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005946 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005947 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005948 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005949 }
5950 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5951 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005952 // NaN's can't be compared for equality. A NaN is
5953 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005954 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005955 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005956 }
5957 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005958 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005959 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005960 }
5961 }
5962 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005963#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005964 }
5965
5966 return 0;
5967}
5968
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005969
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005970
5971
Laurence Lundbladee3553422020-05-02 11:11:17 -07005972int32_t CBORSequenceDecodeTests(void)
5973{
5974 QCBORDecodeContext DCtx;
5975 QCBORItem Item;
5976 QCBORError uCBORError;
5977
5978 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005979
Laurence Lundbladee3553422020-05-02 11:11:17 -07005980 // The input for the date test happens to be a sequence so it
5981 // is reused. It is a sequence because it doesn't start as
5982 // an array or map.
5983 QCBORDecode_Init(&DCtx,
5984 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5985 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005986
Laurence Lundbladee3553422020-05-02 11:11:17 -07005987 // Get the first item
5988 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5989 if(uCBORError != QCBOR_SUCCESS) {
5990 return 1;
5991 }
5992 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5993 return 2;
5994 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005995
Laurence Lundbladee3553422020-05-02 11:11:17 -07005996 // Get a second item
5997 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005998 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5999 return 66;
6000 }
6001
6002 // Get a third item
6003 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006004 if(uCBORError != QCBOR_SUCCESS) {
6005 return 2;
6006 }
6007 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6008 return 3;
6009 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006010
Laurence Lundbladee3553422020-05-02 11:11:17 -07006011 // A sequence can have stuff at the end that may
6012 // or may not be valid CBOR. The protocol decoder knows
6013 // when to stop by definition of the protocol, not
6014 // when the top-level map or array is ended.
6015 // Finish still has to be called to know that
6016 // maps and arrays (if there were any) were closed
6017 // off correctly. When called like this it
6018 // must return the error QCBOR_ERR_EXTRA_BYTES.
6019 uCBORError = QCBORDecode_Finish(&DCtx);
6020 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6021 return 4;
6022 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006023
6024
Laurence Lundbladee3553422020-05-02 11:11:17 -07006025 // --- Test an empty input ----
6026 uint8_t empty[1];
6027 UsefulBufC Empty = {empty, 0};
6028 QCBORDecode_Init(&DCtx,
6029 Empty,
6030 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006031
Laurence Lundbladee3553422020-05-02 11:11:17 -07006032 uCBORError = QCBORDecode_Finish(&DCtx);
6033 if(uCBORError != QCBOR_SUCCESS) {
6034 return 5;
6035 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006036
6037
Laurence Lundbladee3553422020-05-02 11:11:17 -07006038 // --- Sequence with unclosed indefinite length array ---
6039 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006040
Laurence Lundbladee3553422020-05-02 11:11:17 -07006041 QCBORDecode_Init(&DCtx,
6042 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6043 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006044
Laurence Lundbladee3553422020-05-02 11:11:17 -07006045 // Get the first item
6046 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6047 if(uCBORError != QCBOR_SUCCESS) {
6048 return 7;
6049 }
6050 if(Item.uDataType != QCBOR_TYPE_INT64) {
6051 return 8;
6052 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006053
Laurence Lundbladee3553422020-05-02 11:11:17 -07006054 // Get a second item
6055 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006056#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006057 if(uCBORError != QCBOR_SUCCESS) {
6058 return 9;
6059 }
6060 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6061 return 10;
6062 }
6063
6064 // Try to finish before consuming all bytes to confirm
6065 // that the still-open error is returned.
6066 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006067 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006068 return 11;
6069 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006070#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6071 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6072 return 20;
6073 }
6074#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006075
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006076
Laurence Lundbladee3553422020-05-02 11:11:17 -07006077 // --- Sequence with a closed indefinite length array ---
6078 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006079
Laurence Lundbladee3553422020-05-02 11:11:17 -07006080 QCBORDecode_Init(&DCtx,
6081 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6082 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006083
Laurence Lundbladee3553422020-05-02 11:11:17 -07006084 // Get the first item
6085 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6086 if(uCBORError != QCBOR_SUCCESS) {
6087 return 12;
6088 }
6089 if(Item.uDataType != QCBOR_TYPE_INT64) {
6090 return 13;
6091 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006092
Laurence Lundbladee3553422020-05-02 11:11:17 -07006093 // Get a second item
6094 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006095#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6096
Laurence Lundbladee3553422020-05-02 11:11:17 -07006097 if(uCBORError != QCBOR_SUCCESS) {
6098 return 14;
6099 }
6100 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6101 return 15;
6102 }
6103
6104 // Try to finish before consuming all bytes to confirm
6105 // that the still-open error is returned.
6106 uCBORError = QCBORDecode_Finish(&DCtx);
6107 if(uCBORError != QCBOR_SUCCESS) {
6108 return 16;
6109 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006110#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6111 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6112 return 20;
6113 }
6114#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006115
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006116
Laurence Lundbladee3553422020-05-02 11:11:17 -07006117 return 0;
6118}
6119
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006120
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006121
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006122int32_t IntToTests()
6123{
6124 int nErrCode;
6125 int32_t n32;
6126 int16_t n16;
6127 int8_t n8;
6128 uint32_t u32;
6129 uint16_t u16;
6130 uint8_t u8;
6131 uint64_t u64;
6132
6133 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6134 if(nErrCode == -1 || n32 != 1) {
6135 return 1;
6136 }
6137
6138 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6139 if(nErrCode == -1 || n32 != INT32_MAX) {
6140 return 2;
6141 }
6142
6143 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6144 if(nErrCode == -1 || n32 != INT32_MIN) {
6145 return 3;
6146 }
6147
6148 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6149 if(nErrCode != -1) {
6150 return 4;
6151 }
6152
6153 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6154 if(nErrCode != -1) {
6155 return 5;
6156 }
6157
6158
6159 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6160 if(nErrCode == -1 || n16 != INT16_MAX) {
6161 return 6;
6162 }
6163
6164 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6165 if(nErrCode == -1 || n16 != INT16_MIN) {
6166 return 7;
6167 }
6168
6169 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6170 if(nErrCode == -1 || n16 != 1) {
6171 return 8;
6172 }
6173
6174 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6175 if(nErrCode != -1) {
6176 return 9;
6177 }
6178
6179 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6180 if(nErrCode != -1) {
6181 return 10;
6182 }
6183
6184
6185 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6186 if(nErrCode == -1 || n8 != 1) {
6187 return 11;
6188 }
6189
6190 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6191 if(nErrCode == -1 || n8 != INT8_MAX) {
6192 return 12;
6193 }
6194
6195 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6196 if(nErrCode == -1 || n8 != INT8_MIN) {
6197 return 13;
6198 }
6199
6200 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6201 if(nErrCode != -1) {
6202 return 14;
6203 }
6204
6205 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6206 if(nErrCode != -1) {
6207 return 15;
6208 }
6209
6210
6211 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6212 if(nErrCode == -1 || u32 != 1) {
6213 return 16;
6214 }
6215
6216 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6217 if(nErrCode == -1 || u32 != UINT32_MAX) {
6218 return 17;
6219 }
6220
6221 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6222 if(nErrCode == -1 || u32 != 0) {
6223 return 18;
6224 }
6225
6226 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6227 if(nErrCode != -1) {
6228 return 19;
6229 }
6230
6231 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6232 if(nErrCode != -1) {
6233 return 20;
6234 }
6235
6236
6237 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6238 if(nErrCode == -1 || u16 != UINT16_MAX) {
6239 return 21;
6240 }
6241
6242 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6243 if(nErrCode == -1 || u16 != 0) {
6244 return 22;
6245 }
6246
6247 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6248 if(nErrCode == -1 || u16 != 1) {
6249 return 23;
6250 }
6251
6252 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6253 if(nErrCode != -1) {
6254 return 24;
6255 }
6256
6257 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6258 if(nErrCode != -1) {
6259 return 25;
6260 }
6261
6262
6263 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6264 if(nErrCode == -1 || u8 != UINT8_MAX) {
6265 return 26;
6266 }
6267
6268 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6269 if(nErrCode == -1 || u8 != 0) {
6270 return 27;
6271 }
6272
6273 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6274 if(nErrCode == -1 || u8 != 1) {
6275 return 28;
6276 }
6277
6278 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6279 if(nErrCode != -1) {
6280 return 29;
6281 }
6282
6283 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6284 if(nErrCode != -1) {
6285 return 30;
6286 }
6287
6288
6289 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6290 if(nErrCode == -1 || u64 != 1) {
6291 return 31;
6292 }
6293
6294 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6295 if(nErrCode == -1 || u64 != INT64_MAX) {
6296 return 32;
6297 }
6298
6299 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6300 if(nErrCode == -1 || u64 != 0) {
6301 return 33;
6302 }
6303
6304 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6305 if(nErrCode != -1) {
6306 return 34;
6307 }
6308
6309 return 0;
6310}
6311
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006312
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006313
6314
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006315/*
6316A sequence with
6317 A wrapping bstr
6318 containing a map
6319 1
6320 2
6321 A wrapping bstr
6322 containing an array
6323 3
6324 wrapping bstr
6325 4
6326 5
6327 6
6328 array
6329 7
6330 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006331 */
6332
Laurence Lundblade55013642020-09-23 05:39:22 -07006333static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006334{
Laurence Lundblade55013642020-09-23 05:39:22 -07006335 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006336 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006337 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006338
Laurence Lundblade55013642020-09-23 05:39:22 -07006339 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006340
6341 QCBOREncode_BstrWrap(&EC);
6342 QCBOREncode_OpenMap(&EC);
6343 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6344 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6345 QCBOREncode_CloseMap(&EC);
6346 QCBOREncode_BstrWrap(&EC);
6347 QCBOREncode_OpenArray(&EC);
6348 QCBOREncode_AddInt64(&EC, 3);
6349 QCBOREncode_BstrWrap(&EC);
6350 QCBOREncode_AddInt64(&EC, 4);
6351 QCBOREncode_CloseBstrWrap(&EC, NULL);
6352 QCBOREncode_AddInt64(&EC, 5);
6353 QCBOREncode_CloseArray(&EC);
6354 QCBOREncode_CloseBstrWrap(&EC, NULL);
6355 QCBOREncode_AddInt64(&EC, 6);
6356 QCBOREncode_CloseBstrWrap(&EC, NULL);
6357 QCBOREncode_OpenArray(&EC);
6358 QCBOREncode_AddInt64(&EC, 7);
6359 QCBOREncode_AddInt64(&EC, 8);
6360 QCBOREncode_CloseArray(&EC);
6361
6362 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006363 if(uErr) {
6364 Encoded = NULLUsefulBufC;
6365 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006366
6367 return Encoded;
6368}
6369
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006370static const uint8_t spBreakInByteString[] = {
6371 0x41, 0xff
6372};
6373
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006374
6375int32_t EnterBstrTest()
6376{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006377 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006378
6379 QCBORDecodeContext DC;
6380
Laurence Lundblade55013642020-09-23 05:39:22 -07006381 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006382
Laurence Lundblade55013642020-09-23 05:39:22 -07006383 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006384
6385
Laurence Lundblade9b334962020-08-27 10:55:53 -07006386 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006387 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006388 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6389 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006390 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006391 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006392 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006393 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006394 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006395 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006396 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006397 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006398 QCBORDecode_ExitArray(&DC);
6399 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006400 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006401 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006402 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006403 QCBORDecode_GetInt64(&DC, &n7);
6404 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006405 QCBORDecode_ExitArray(&DC);
6406
6407 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006408 if(uErr) {
6409 return (int32_t)uErr;
6410 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006411
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006412
6413 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6414 * Successful because no items are fetched from byte string.
6415 */
6416 QCBORDecode_Init(&DC,
6417 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6418 0);
6419 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6420 uErr = QCBORDecode_GetError(&DC);
6421 if(uErr) {
6422 return 100 + (int32_t)uErr;
6423 }
6424
6425 QCBORDecode_ExitBstrWrapped(&DC);
6426 uErr = QCBORDecode_GetError(&DC);
6427 if(uErr) {
6428 return 200 + (int32_t)uErr;
6429 }
6430
6431 /* Try to get item that is a break out of a byte string wrapped CBOR.
6432 * It fails because there should be no break.
6433 */
6434 QCBORDecode_Init(&DC,
6435 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6436 0);
6437 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6438 QCBORItem Item;
6439 uErr = QCBORDecode_GetNext(&DC, &Item);
6440 if(uErr != QCBOR_ERR_BAD_BREAK) {
6441 return 300 + (int32_t)uErr;
6442 }
6443
6444 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006445}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006446
6447
6448
6449
6450static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006451 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006452
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006453 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006454 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006455 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6456 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6457 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006458
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006459 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006460 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6461 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6462 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006463
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006464 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006465 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006466 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6467 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006468
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006469 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006470 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6471 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006472
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006473 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006474 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006475 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6476 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006477
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006478 21,
6479 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6480 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6481
6482 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006483 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006484 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6485 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006486
6487 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006488 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6489 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006490
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006491 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006492 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006493 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6494 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006495
6496 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006497 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6498 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006499
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006500 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006501 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006502 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6503 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006504
6505 0x18, 0x33,
6506 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6507
6508 // MIME
6509 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006510 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6511 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6512 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006513
6514 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006515 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6516 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006517
6518 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006519 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6520 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6521 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006522
6523 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006524 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6525 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006526
6527 // UUID
6528 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006529 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6530 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006531
6532 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006533 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6534 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006535};
6536
6537int32_t DecodeTaggedTypeTests()
6538{
6539 QCBORDecodeContext DC;
6540 QCBORError uErr;
6541
6542 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6543
6544 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006545 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006546
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006547 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006548 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006549 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006550 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6551 return 1;
6552 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006553 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006554 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6555 return 2;
6556 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006557 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006558 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6559 return 3;
6560 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006561 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006562 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006563 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6564 return 4;
6565 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006566 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006567 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006568 return 5;
6569 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006570
Laurence Lundblade9b334962020-08-27 10:55:53 -07006571 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006572 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6573 bNeg != false) {
6574 return 10;
6575 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006576 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006577 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6578 bNeg != true) {
6579 return 11;
6580 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006581 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006582 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6583 return 12;
6584 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006585 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006586 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006587 return 13;
6588 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006589 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006590 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006591 return 14;
6592 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006593
Laurence Lundblade9b334962020-08-27 10:55:53 -07006594 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006595 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6596 return 20;
6597 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006598 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006599 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6600 return 21;
6601 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006602 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006603 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006604 return 22;
6605 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006606 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006607 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006608 return 23;
6609 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006610
Laurence Lundblade9b334962020-08-27 10:55:53 -07006611 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006612 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6613 return 30;
6614 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006615 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006616 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6617 return 31;
6618 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006619 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006620 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006621 return 32;
6622 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006623 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006624 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006625 return 33;
6626 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006627
Laurence Lundblade9b334962020-08-27 10:55:53 -07006628 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006629 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6630 return 40;
6631 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006632 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006633 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6634 return 41;
6635 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006636 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006637 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006638 return 42;
6639 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006640 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006641 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006642 return 43;
6643 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006644
Laurence Lundblade9b334962020-08-27 10:55:53 -07006645 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006646 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6647 return 50;
6648 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006649 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006650 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6651 return 51;
6652 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006653 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006654 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006655 return 52;
6656 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006657 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006658 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006659 return 53;
6660 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006661
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006662 // MIME
6663 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006664 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006665 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6666 bIsNot7Bit == true) {
6667 return 60;
6668 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006669 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006670 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6671 bIsNot7Bit == true) {
6672 return 61;
6673 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006674 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006675 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6676 bIsNot7Bit == false) {
6677 return 62;
6678 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006679 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006680 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6681 bIsNot7Bit == false) {
6682 return 63;
6683 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006684 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006685 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006686 return 64;
6687 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006688 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006689 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006690 return 65;
6691 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006692
Laurence Lundblade9b334962020-08-27 10:55:53 -07006693 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006694 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6695 return 70;
6696 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006697 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006698 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6699 return 71;
6700 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006701 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006702 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006703 return 72;
6704 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006705 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006706 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006707 return 73;
6708 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006709
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006710 // Improvement: add some more error test cases
6711
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006712 QCBORDecode_ExitMap(&DC);
6713
6714 uErr = QCBORDecode_Finish(&DC);
6715 if(uErr != QCBOR_SUCCESS) {
6716 return 100;
6717 }
6718
6719 return 0;
6720}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006721
6722
6723
6724
6725/*
6726 [
6727 "aaaaaaaaaa",
6728 {}
6729 ]
6730 */
6731static const uint8_t spTooLarge1[] = {
6732 0x9f,
6733 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6734 0xa0,
6735 0xff
6736};
6737
6738/*
6739 [
6740 {
6741 0: "aaaaaaaaaa"
6742 }
6743 ]
6744 */
6745static const uint8_t spTooLarge2[] = {
6746 0x9f,
6747 0xa1,
6748 0x00,
6749 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6750 0xff
6751};
6752
6753/*
6754 h'A1006A61616161616161616161'
6755
6756 {
6757 0: "aaaaaaaaaa"
6758 }
6759 */
6760static const uint8_t spTooLarge3[] = {
6761 0x4d,
6762 0xa1,
6763 0x00,
6764 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6765};
6766
6767int32_t TooLargeInputTest(void)
6768{
6769 QCBORDecodeContext DC;
6770 QCBORError uErr;
6771 UsefulBufC String;
6772
6773 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6774 // to 10 There's not really any way to test this error
6775 // condition. The error condition is not complex, so setting
6776 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6777
6778 // The input CBOR is only too large because the
6779 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6780 //
6781 // This test is disabled for the normal test runs because of the
6782 // special build requirement.
6783
6784
6785 // Tests the start of a map being too large
6786 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006787 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006788 QCBORDecode_GetTextString(&DC, &String);
6789 uErr = QCBORDecode_GetError(&DC);
6790 if(uErr != QCBOR_SUCCESS) {
6791 return 1;
6792 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006793 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006794 uErr = QCBORDecode_GetError(&DC);
6795 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6796 return 2;
6797 }
6798
6799 // Tests the end of a map being too large
6800 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006801 QCBORDecode_EnterArray(&DC, NULL);
6802 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006803 uErr = QCBORDecode_GetError(&DC);
6804 if(uErr != QCBOR_SUCCESS) {
6805 return 3;
6806 }
6807 QCBORDecode_ExitMap(&DC);
6808 uErr = QCBORDecode_GetError(&DC);
6809 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6810 return 4;
6811 }
6812
6813 // Tests the entire input CBOR being too large when processing bstr wrapping
6814 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6815 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6816 uErr = QCBORDecode_GetError(&DC);
6817 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6818 return 5;
6819 }
6820
6821 return 0;
6822}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006823
6824
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006825#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6826
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006827static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006828 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006829 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6830 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6831 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6832 0x03,
6833 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6834 0xc3,
6835 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006836};
6837
6838int32_t SpiffyIndefiniteLengthStringsTests()
6839{
6840 QCBORDecodeContext DCtx;
6841
6842 QCBORDecode_Init(&DCtx,
6843 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6844 QCBOR_DECODE_MODE_NORMAL);
6845
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006846 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006847 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6848
6849 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006850 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006851 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6852 if(QCBORDecode_GetAndResetError(&DCtx)) {
6853 return 1;
6854 }
6855
6856 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6857 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6858 return 2;
6859 }
6860
6861 uint64_t uInt;
6862 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6863 if(QCBORDecode_GetAndResetError(&DCtx)) {
6864 return 3;
6865 }
6866 if(uInt != 3) {
6867 return 4;
6868 }
6869
6870 double uDouble;
6871 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6872 "label2",
6873 0xff,
6874 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006875#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006876 if(QCBORDecode_GetAndResetError(&DCtx)) {
6877 return 5;
6878 }
6879 if(uDouble != -16777474) {
6880 return 6;
6881 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006882#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006883 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6884 return 7;
6885 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006886#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006887
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006888
6889 QCBORDecode_ExitMap(&DCtx);
6890
6891 if(QCBORDecode_Finish(&DCtx)) {
6892 return 99;
6893 }
6894
6895 return 0;
6896}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006897#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08006898
6899
6900
6901int32_t PeekTest()
6902{
6903 QCBORItem Item;
6904 QCBORError nCBORError;
6905 QCBORDecodeContext DCtx;
6906
6907 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6908
6909 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6910 return 100+(int32_t)nCBORError;
6911 }
6912 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6913 return 200;
6914 }
6915
6916 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6917 return (int32_t)nCBORError;
6918 }
6919 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6920 return 300;
6921 }
6922
6923 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6924 return 400 + (int32_t)nCBORError;
6925 }
6926 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6927 return 500;
6928 }
6929
6930 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6931 return (int32_t)nCBORError;
6932 }
6933 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6934 return 600;
6935 }
6936
6937 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6938 return 900 + (int32_t)nCBORError;
6939 }
6940 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6941 Item.uDataType != QCBOR_TYPE_INT64 ||
6942 Item.val.int64 != 42 ||
6943 Item.uDataAlloc ||
6944 Item.uLabelAlloc ||
6945 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6946 return 1000;
6947 }
6948
6949 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6950 return 1100 + (int32_t)nCBORError;
6951 }
6952
6953 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6954 Item.uDataType != QCBOR_TYPE_INT64 ||
6955 Item.val.int64 != 42 ||
6956 Item.uDataAlloc ||
6957 Item.uLabelAlloc ||
6958 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6959 return 1200;
6960 }
6961
6962
6963 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6964 return 1300 + (int32_t)nCBORError;
6965 }
6966 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6967 Item.uDataAlloc ||
6968 Item.uLabelAlloc ||
6969 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
6970 Item.uDataType != QCBOR_TYPE_ARRAY ||
6971 Item.val.uCount != 2)
6972 return 1400;
6973
6974 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6975 return 1500 + (int32_t)nCBORError;
6976 }
6977 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6978 Item.uDataAlloc ||
6979 Item.uLabelAlloc ||
6980 UsefulBufCompareToSZ(Item.val.string, "string1")) {
6981 return 1600;
6982 }
6983
6984 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6985 return 1700 + (int32_t)nCBORError;
6986 }
6987 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6988 Item.uDataAlloc ||
6989 Item.uLabelAlloc ||
6990 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6991 return 1800;
6992 }
6993
6994 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6995 return (int32_t)nCBORError;
6996 }
6997 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6998 Item.uDataAlloc ||
6999 Item.uLabelAlloc ||
7000 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7001 return 1900;
7002 }
7003
7004 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7005 return (int32_t)nCBORError;
7006 }
7007 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7008 Item.uDataAlloc ||
7009 Item.uLabelAlloc ||
7010 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7011 return 2000;
7012 }
7013
7014
7015 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7016 return 2100 + (int32_t)nCBORError;
7017 }
7018 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7019 Item.uDataAlloc ||
7020 Item.uLabelAlloc ||
7021 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7022 Item.uDataType != QCBOR_TYPE_MAP ||
7023 Item.val.uCount != 4) {
7024 return 2100;
7025 }
7026
7027 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7028 return 2200 + (int32_t)nCBORError;
7029 }
7030 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7031 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7032 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7033 Item.uDataAlloc ||
7034 Item.uLabelAlloc ||
7035 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7036 return 2300;
7037 }
7038
7039 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7040 return 2400 + (int32_t)nCBORError;
7041 }
7042 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7043 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7044 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7045 Item.uDataAlloc ||
7046 Item.uLabelAlloc ||
7047 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7048 return 2500;
7049 }
7050
7051 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7052 return 2600 + (int32_t)nCBORError;
7053 }
7054 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7055 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7056 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7057 Item.uDataAlloc ||
7058 Item.uLabelAlloc ||
7059 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7060 return 2700;
7061 }
7062
7063 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7064 return 2800 + (int32_t)nCBORError;
7065 }
7066 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7067 Item.uDataAlloc ||
7068 Item.uLabelAlloc ||
7069 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7070 Item.uDataType != QCBOR_TYPE_INT64 ||
7071 Item.val.int64 != 98)
7072 return 2900;
7073
7074 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7075 return 3000 + (int32_t)nCBORError;
7076 }
7077 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7078 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7079 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7080 Item.uDataAlloc ||
7081 Item.uLabelAlloc ||
7082 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7083 return 3100;
7084 }
7085
7086 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7087 return 3200 + (int32_t)nCBORError;
7088 }
7089 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7090 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7091 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7092 Item.uDataAlloc ||
7093 Item.uLabelAlloc ||
7094 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7095 return 3300;
7096 }
7097
7098 return 0;
7099}