blob: 9a5dc6df343d12a244a9f893221e74d6c450c6af [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 Lundblade20db9c92018-12-17 11:40:37 -080063#endif
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 Lundblade2c1faf92020-06-26 22:43:56 -0700547// Same as above, but with indefinite lengths.
548static const uint8_t pValidMapIndefEncoded[] = {
5490xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5500x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5510x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5520x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5530x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5540x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5550x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5560x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5570x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5580x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5590x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5600x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5610x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5620x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5630x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5640x73, 0xff, 0xff};
565
566
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800567static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700568 size_t nLen,
569 int64_t *pInt1,
570 int64_t *pInt2,
571 const uint8_t **pBuf3,
572 size_t *pBuf3Len,
573 const uint8_t **pBuf4,
574 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800575{
576 QCBORDecodeContext DCtx;
577 QCBORItem Item;
578 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundbladeee851742020-01-08 08:37:05 -0800580 QCBORDecode_Init(&DCtx,
581 (UsefulBufC){pEncoded, nLen},
582 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800583
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800584 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700585 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
586 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800590 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700591 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
592 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800593 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700594 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800596
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700598 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
599 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800600 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700601 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800602 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700605 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
606 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700608 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 *pBuf3 = Item.val.string.ptr;
610 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700613 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
614 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800615 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700616 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 *pBuf4 = Item.val.string.ptr;
618 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800621
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800622Done:
623 return(nReturn);
624}
625
626
627
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800628
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800629int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800630{
631 uint8_t *pEncoded;
632 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800633
Laurence Lundblade5e390822019-01-06 12:35:01 -0800634 int64_t i1=0, i2=0;
635 size_t i3=0, i4=0;
636 const uint8_t *s3= (uint8_t *)"";
637 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800638
639
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800640 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
641 return(-1);
642 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800644 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800646 if(i1 != 23 ||
647 i2 != 6000 ||
648 i3 != 8 ||
649 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530650 memcmp("galactic", s3, 8) !=0 ||
651 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800652 return(-1);
653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655 return(0);
656}
657
658
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700659/*
660 [
661 0,
662 [],
663 [
664 [],
665 [
666 0
667 ],
668 {},
669 {
670 1: {},
671 2: {},
672 3: []
673 }
674 ]
675 ]
676 */
677static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
678 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
679
Laurence Lundblade02625d42020-06-25 14:41:41 -0700680/* Same as above, but with indefinte lengths */
681static uint8_t sEmptiesIndef[] = {
6820x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700683 0x00,
684 0x9F,
685 0xFF,
686 0x9F,
687 0x9F,
688 0xFF,
689 0x9F,
690 0x00,
691 0xFF,
692 0xBF,
693 0xFF,
694 0xBF,
695 0x01,
696 0xBF,
697 0xFF,
698 0x02,
699 0xBF,
700 0xFF,
701 0x03,
702 0x9F,
703 0xFF,
704 0xFF,
705 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700706 0xFF};
707
708
709
710static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700711{
712 QCBORDecodeContext DCtx;
713 QCBORItem Item;
714
Laurence Lundbladeee851742020-01-08 08:37:05 -0800715 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700716 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800717 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700718
719 // Array with 3 items
720 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
721 Item.uDataType != QCBOR_TYPE_ARRAY ||
722 Item.uNestingLevel != 0 ||
723 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700724 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700725 return -1;
726 }
727
728 // An integer 0
729 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
730 Item.uDataType != QCBOR_TYPE_INT64 ||
731 Item.uNestingLevel != 1 ||
732 Item.uNextNestLevel != 1 ||
733 Item.val.uint64 != 0) {
734 return -2;
735 }
736
737 // An empty array
738 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
739 Item.uDataType != QCBOR_TYPE_ARRAY ||
740 Item.uNestingLevel != 1 ||
741 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700742 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700743 return -3;
744 }
745
746 // An array with 4 items
747 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
748 Item.uDataType != QCBOR_TYPE_ARRAY ||
749 Item.uNestingLevel != 1 ||
750 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700751 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700752 return -4;
753 }
754
755 // An empty array
756 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
757 Item.uDataType != QCBOR_TYPE_ARRAY ||
758 Item.uNestingLevel != 2 ||
759 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700760 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700761 return -5;
762 }
763
764 // An array with 1 item
765 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
766 Item.uDataType != QCBOR_TYPE_ARRAY ||
767 Item.uNestingLevel != 2 ||
768 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700769 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700770 return -6;
771 }
772
773 // An integer 0
774 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
775 Item.uDataType != QCBOR_TYPE_INT64 ||
776 Item.uNestingLevel != 3 ||
777 Item.uNextNestLevel != 2 ||
778 Item.val.uint64 != 0) {
779 return -7;
780 }
781
782 // An empty map
783 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
784 Item.uDataType != QCBOR_TYPE_MAP ||
785 Item.uNestingLevel != 2 ||
786 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700787 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700788 return -8;
789 }
790
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700791 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700792 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
793 Item.uDataType != QCBOR_TYPE_MAP ||
794 Item.uNestingLevel != 2 ||
795 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700796 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700797 return -9;
798 }
799
800 // An empty map
801 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
802 Item.uDataType != QCBOR_TYPE_MAP ||
803 Item.uNestingLevel != 3 ||
804 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700805 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700806 return -10;
807 }
808
809 // An empty map
810 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
811 Item.uDataType != QCBOR_TYPE_MAP ||
812 Item.uNestingLevel != 3 ||
813 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700814 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700815 return -11;
816 }
817
818 // An empty array
819 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
820 Item.uDataType != QCBOR_TYPE_ARRAY ||
821 Item.uNestingLevel != 3 ||
822 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700823 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700824 return -12;
825 }
826
827 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
828 return -13;
829 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700830 return 0;
831}
832
833
834int32_t EmptyMapsAndArraysTest()
835{
836 int nResult;
837 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
838 true);
839 if(nResult) {
840 return nResult;
841 }
842
843 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
844 false);
845
846 if(nResult) {
847 return nResult -100;
848 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700849
850 return 0;
851}
852
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800853
Laurence Lundbladeee851742020-01-08 08:37:05 -0800854static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
855 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800856
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800857int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800858{
859 QCBORDecodeContext DCtx;
860 int nReturn = 0;
861 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800862
Laurence Lundbladeee851742020-01-08 08:37:05 -0800863 QCBORDecode_Init(&DCtx,
864 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
865 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800866
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800867 for(i = 0; i < 10; i++) {
868 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
871 Item.uDataType != QCBOR_TYPE_ARRAY ||
872 Item.uNestingLevel != i) {
873 nReturn = -1;
874 break;
875 }
876 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800877
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800878 return(nReturn);
879}
880
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700881// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800882static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
883 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
884 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
885 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800886
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800887int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888{
889 QCBORDecodeContext DCtx;
890 int nReturn = 0;
891 int i;
892 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800893
894
Laurence Lundbladeee851742020-01-08 08:37:05 -0800895 QCBORDecode_Init(&DCtx,
896 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
897 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700899 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800900
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
902 Item.uDataType != QCBOR_TYPE_ARRAY ||
903 Item.uNestingLevel != i) {
904 nReturn = -1;
905 break;
906 }
907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800908
Laurence Lundbladea9489f82020-09-12 13:50:56 -0700909 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800911
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800912 return(nReturn);
913}
914
915
916
917
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800918int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700920 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800921
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800922 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700923 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800924
Laurence Lundbladeee851742020-01-08 08:37:05 -0800925 QCBORDecode_Init(&DCtx,
926 (UsefulBufC){spExpectedEncodedInts, nNum},
927 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800928
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800929 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700931 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800932 nResult = -1;
933 goto Done;
934 }
935 }
936Done:
937 return nResult;
938}
939
940
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800941
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800942int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800943{
944 uint8_t *pEncoded;
945 int nReturn;
946 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800947
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800948 int64_t i1, i2;
949 size_t i3, i4;
950 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800951
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800953
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
955 return(-1);
956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800957
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -0700959 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
960 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800961 if(nResult == 0) {
962 nReturn = -1;
963 }
964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800965
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800966 return(nReturn);
967}
968
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530969/*
970 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800971 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
972 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530973 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800974static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975{
976 QCBORDecodeContext DCtx;
977 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700978 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800979
Laurence Lundbladeee851742020-01-08 08:37:05 -0800980 QCBORDecode_Init(&DCtx,
981 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
982 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800983
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900984 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700985 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800987 if(Item.uDataType != QCBOR_TYPE_MAP ||
988 Item.val.uCount != 3)
989 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800990
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700992 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900993 }
Laurence Lundblade9b334962020-08-27 10:55:53 -0700994
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800995 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800996 Item.uDataType != QCBOR_TYPE_INT64 ||
997 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530998 Item.uDataAlloc ||
999 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001000 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001002 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001003
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001005 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001007 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301008 Item.uDataAlloc ||
1009 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001010 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001011 Item.uDataType != QCBOR_TYPE_ARRAY ||
1012 Item.val.uCount != 2)
1013 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001014
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001015 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001016 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001017 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001018 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301019 Item.uDataAlloc ||
1020 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001021 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001022 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001024
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001025 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001026 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301029 Item.uDataAlloc ||
1030 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001031 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001032 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001034
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001036 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001037 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301039 Item.uDataAlloc ||
1040 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001041 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001042 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001043 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001045 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001046
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001047 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001048 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001049 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001050 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001051 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001052 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301053 Item.uDataAlloc ||
1054 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001055 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001056 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001058
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001060 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001061 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001062 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001063 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001064 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301065 Item.uDataAlloc ||
1066 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001067 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001068 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001069 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001070
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001071 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001072 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001073 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301075 Item.uDataAlloc ||
1076 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001077 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001078 Item.uDataType != QCBOR_TYPE_INT64 ||
1079 Item.val.int64 != 98)
1080 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001081
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001082 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001083 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001084 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001085 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001086 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001087 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301088 Item.uDataAlloc ||
1089 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001090 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001093
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001094 return 0;
1095}
1096
1097
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098/*
1099 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001100 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001102int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001103{
1104 QCBORDecodeContext DCtx;
1105 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001106 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001107
Laurence Lundbladeee851742020-01-08 08:37:05 -08001108 QCBORDecode_Init(&DCtx,
1109 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1110 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001111
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001113 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001115 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1116 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 return -1;
1118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001119
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001120 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001121 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 }
1123 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1124 Item.uDataAlloc ||
1125 Item.uLabelAlloc ||
1126 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001127 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 return -2;
1129 }
1130
1131 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001132 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 }
1134 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1135 Item.uDataType != QCBOR_TYPE_INT64 ||
1136 Item.val.int64 != 42 ||
1137 Item.uDataAlloc ||
1138 Item.uLabelAlloc) {
1139 return -3;
1140 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001141
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001143 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 }
1145 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1146 Item.uDataAlloc ||
1147 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001148 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001149 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1150 return -4;
1151 }
1152
1153 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001154 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 }
1156 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1157 Item.uDataAlloc ||
1158 Item.uLabelAlloc ||
1159 Item.uDataType != QCBOR_TYPE_ARRAY ||
1160 Item.val.uCount != 2) {
1161 return -5;
1162 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001163
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001164 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001165 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 }
1167 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1168 Item.val.string.len != 7 ||
1169 Item.uDataAlloc ||
1170 Item.uLabelAlloc ||
1171 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1172 return -6;
1173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001174
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001176 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 }
1178 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1179 Item.uDataAlloc ||
1180 Item.uLabelAlloc ||
1181 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1182 return -7;
1183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001184
1185
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001186 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001187 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 }
1189 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1190 Item.uDataAlloc ||
1191 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001192 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 return -8;
1194 }
1195
1196 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001197 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001198 }
1199 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1200 Item.uDataAlloc ||
1201 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001202 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1203 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001204 return -9;
1205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001206
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001207 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001208 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 }
1210 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001211 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1213 Item.uDataAlloc ||
1214 Item.uLabelAlloc) {
1215 return -10;
1216 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001217
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001219 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 }
1221 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1222 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1223 Item.uDataAlloc ||
1224 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001225 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 return -11;
1227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001228
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001230 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 }
1232 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001233 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001234 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1235 Item.uDataAlloc ||
1236 Item.uLabelAlloc) {
1237 return -12;
1238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001239
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001240 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001241 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001242 }
1243 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1244 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1245 Item.uDataAlloc ||
1246 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001247 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001248 return -13;
1249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001250
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001251 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001252 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001253 }
1254 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1255 Item.uDataAlloc ||
1256 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001257 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1259 return -14;
1260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001261
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001262 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001263 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001264 }
1265 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1266 Item.uDataAlloc ||
1267 Item.uLabelAlloc ||
1268 Item.uDataType != QCBOR_TYPE_INT64 ||
1269 Item.val.int64 != 98) {
1270 return -15;
1271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001272
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001273 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001274 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001275 }
1276 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001277 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001278 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1279 Item.uDataAlloc ||
1280 Item.uLabelAlloc) {
1281 return -16;
1282 }
1283
1284 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001285 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001286 }
1287 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1288 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1289 Item.uDataAlloc ||
1290 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001291 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001292 return -17;
1293 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001294
1295
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001296 /*
1297 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1298 map that when interpreted as an array will be too many. Test
1299 data just has the start of the map, not all the items in the map.
1300 */
1301 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001302
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001303 QCBORDecode_Init(&DCtx,
1304 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1305 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001306
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001307 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001308 return -50;
1309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001310
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001311 // TODO: test decoding of labels that are arrays or such
1312 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1313
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001314 return 0;
1315}
1316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001317
1318/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301319 Fully or partially decode pValidMapEncoded. When
1320 partially decoding check for the right error code.
1321 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301323 The partial decodes test error conditions of
1324 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001325
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301326 This could be combined with the above test
1327 and made prettier and maybe a little more
1328 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001330static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001331{
1332 QCBORDecodeContext DCtx;
1333 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001334 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001335
Laurence Lundbladeee851742020-01-08 08:37:05 -08001336 QCBORDecode_Init(&DCtx,
1337 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1338 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001339
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001340 if(nLevel < 1) {
1341 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1342 return -1;
1343 } else {
1344 return 0;
1345 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001346 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301347
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001348
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001350 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 if(Item.uDataType != QCBOR_TYPE_MAP ||
1353 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001354 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001355
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001356 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001357 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1358 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001359 } else {
1360 return 0;
1361 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001363
1364
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001366 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001367 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 Item.uDataType != QCBOR_TYPE_INT64 ||
1370 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001371 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001372 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001374
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001375 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001376 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1377 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001378 } else {
1379 return 0;
1380 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001382
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001383 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001384 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001385 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001386 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001387 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001389 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001390 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001392
1393
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001394 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001395 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1396 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001397 } else {
1398 return 0;
1399 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001401
1402
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001403 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001404 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001405 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001407 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001408 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001409 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001410
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001411 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001412 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1413 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001414 } else {
1415 return 0;
1416 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001417 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001418
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001420 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001421 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001423 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001424 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001426
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001427 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001428 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1429 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001430 } else {
1431 return 0;
1432 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001433 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001434
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001435 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001436 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001437 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001439 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 Item.uDataType != QCBOR_TYPE_MAP ||
1441 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001442 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001443
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001445 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1446 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001447 } else {
1448 return 0;
1449 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001451
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001452 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001453 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001454 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001455 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001456 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001458 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001459 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001461
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001462 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001463 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1464 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001465 } else {
1466 return 0;
1467 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001469
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001470 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001471 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001472 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001474 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001475 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001476 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001477 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001479
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001480 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001481 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1482 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001483 } else {
1484 return 0;
1485 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001486 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001489 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001490 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001491 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001492 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001493 Item.uDataType != QCBOR_TYPE_INT64 ||
1494 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001495 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001496
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001497 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001498 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1499 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001500 } else {
1501 return 0;
1502 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001504
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001505 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001506 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001507 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001509 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001511 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001512 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001514
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301515 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001516 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001517 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001518
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001519 return 0;
1520}
1521
1522
1523
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001524
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001525int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001527 // Parse a moderatly complex map structure very thoroughly
1528 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1529 if(nResult) {
1530 return nResult;
1531 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001532
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001533 // Again, but in strings-only mode. It should succeed since the input
1534 // map has only string labels.
1535 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1536 if(nResult) {
1537 return nResult;
1538 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001539
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001540 // Again, but try to finish the decoding before the end of the
1541 // input at 10 different place and see that the right error code
1542 // is returned.
1543 for(int i = 0; i < 10; i++) {
1544 nResult = ExtraBytesTest(i);
1545 if(nResult) {
1546 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001547 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001549
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001550 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551}
1552
1553
Laurence Lundbladeee851742020-01-08 08:37:05 -08001554static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1555 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1556 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001558int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001559{
1560 QCBORDecodeContext DCtx;
1561 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001562 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001563
1564
Laurence Lundbladeee851742020-01-08 08:37:05 -08001565 QCBORDecode_Init(&DCtx,
1566 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1567 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001568
1569
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001570 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001571 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001572 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1573 Item.val.uCount != 10)
1574 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001575
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_FALSE)
1579 return -1;
1580
1581 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001582 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583 if(Item.uDataType != QCBOR_TYPE_TRUE)
1584 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001586 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001587 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588 if(Item.uDataType != QCBOR_TYPE_NULL)
1589 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001591 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001592 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001593 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1594 return -1;
1595
1596 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001597 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001598 return -1;
1599
1600 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001601 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001602 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1603 return -1;
1604
1605 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001606 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001607 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1608 return -1;
1609
Laurence Lundblade077475f2019-04-26 09:06:33 -07001610 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001611 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001612
Laurence Lundblade077475f2019-04-26 09:06:33 -07001613 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001614 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 Lundblade2ded3d92018-10-09 21:36:11 +08001619 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001620 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001621 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1622 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001624 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001625 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001626 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1627 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001628
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001629 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001630
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001631}
1632
1633
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001634int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001635{
1636 // Loop over all the not-well-formed instance of CBOR
1637 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001638 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
1639 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001640 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1641 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1642 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1643
Laurence Lundbladeee851742020-01-08 08:37:05 -08001644 // Set up decoder context. String allocator needed for indefinite
1645 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001646 QCBORDecodeContext DCtx;
1647 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001648#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001649 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1650 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001651#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001652
1653 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001654 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001655 do {
1656 QCBORItem Item;
1657
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001658 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1659 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001660
1661 // Every test vector must fail with
1662 // a not-well-formed error. If not
1663 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001664 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001665 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001666 // Return index of failure in the error code
1667 return 2000 + nIterate;
1668 }
1669 }
1670 return 0;
1671}
1672
1673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001674struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001675 UsefulBufC Input;
1676 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001677};
1678
Laurence Lundblade59289e52019-12-30 13:44:37 -08001679
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001680static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001681{
1682 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1683 // Set up the decoding context including a memory pool so that
1684 // indefinite length items can be checked
1685 QCBORDecodeContext DCtx;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001686 QCBORError nCBORError;
1687
Laurence Lundblade59289e52019-12-30 13:44:37 -08001688 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001689
1690#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08001691 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001692
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001693 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001694 if(nCBORError) {
1695 return -9;
1696 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001697#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1698
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001699
Laurence Lundblade59289e52019-12-30 13:44:37 -08001700 // Iterate until there is an error of some sort error
1701 QCBORItem Item;
1702 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001703 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001704 memset(&Item, 0x33, sizeof(Item));
1705
1706 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1707 } while(nCBORError == QCBOR_SUCCESS);
1708
1709 // Must get the expected error or the this test fails
1710 // The data and label type must also be QCBOR_TYPE_NONE
1711 if(nCBORError != pF->nError ||
1712 Item.uDataType != QCBOR_TYPE_NONE ||
1713 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001714 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001715 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001716 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001717 }
1718 }
1719
1720 return 0;
1721}
1722
1723
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001724struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001725 // Most of this is copied from not_well_formed.h. Here the error code
1726 // returned is also checked.
1727
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001728#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001729 // Indefinite length strings must be closed off
1730 // An indefinite length byte string not closed off
1731 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1732 // An indefinite length text string not closed off
1733 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1734
1735
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001736 // All the chunks in an indefinite length string must be of the type of
1737 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001738 // indefinite length byte string with text string chunk
1739 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1740 // indefinite length text string with a byte string chunk
1741 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1742 // indefinite length byte string with an positive integer chunk
1743 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1744 // indefinite length byte string with an negative integer chunk
1745 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1746 // indefinite length byte string with an array chunk
1747 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1748 // indefinite length byte string with an map chunk
1749 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1750 // indefinite length byte string with tagged integer chunk
1751 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1752 // indefinite length byte string with an simple type chunk
1753 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1754 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1755 // indefinite length text string with indefinite string inside
1756 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1757
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001758#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1759
1760 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1761 // An indefinite length text string not closed off
1762 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1763
1764
1765 // All the chunks in an indefinite length string must be of the type of
1766 // indefinite length string
1767 // indefinite length byte string with text string chunk
1768 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1769 // indefinite length text string with a byte string chunk
1770 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1771 // indefinite length byte string with an positive integer chunk
1772 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1773 // indefinite length byte string with an negative integer chunk
1774 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1775 // indefinite length byte string with an array chunk
1776 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1777 // indefinite length byte string with an map chunk
1778 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1779 // indefinite length byte string with tagged integer chunk
1780 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1781 // indefinite length byte string with an simple type chunk
1782 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1783 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1784 // indefinite length text string with indefinite string inside
1785 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1786#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1787
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001788
1789 // Definte length maps and arrays must be closed by having the right number of items
1790 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001791 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001792 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001793 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001794 // A definte length array that is supposed to have 511 items, but has only 1
1795 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1796 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001797 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001798 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001799 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001800
1801
1802 // Indefinte length maps and arrays must be ended by a break
1803 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001804 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001805 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001806 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001807 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001808 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001809 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001810 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001811
1812
1813 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001814 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001815 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001816 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001817 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001818 // Unclosed indefinite map containing a closed definite length array
1819 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1820 // Definite length map containing an unclosed indefinite length array
1821 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001822 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001823 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001824 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001825 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001826 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001827 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001828 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001829 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001830 // Unclosed indefinite length map in definite length maps
1831 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1832 QCBOR_ERR_NO_MORE_ITEMS},
1833 // Unclosed definite length map in indefinite length maps
1834 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1835 // Unclosed indefinite length array in definite length maps
1836 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1837 QCBOR_ERR_NO_MORE_ITEMS},
1838 // Unclosed definite length array in indefinite length maps
1839 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1840 // Unclosed indefinite length map in definite length arrays
1841 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1842 // Unclosed definite length map in indefinite length arrays
1843 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001844
1845
1846 // The "argument" for the data item is incomplete
1847 // Positive integer missing 1 byte argument
1848 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1849 // Positive integer missing 2 byte argument
1850 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1851 // Positive integer missing 4 byte argument
1852 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1853 // Positive integer missing 8 byte argument
1854 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1855 // Positive integer missing 1 byte of 2 byte argument
1856 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1857 // Positive integer missing 2 bytes of 4 byte argument
1858 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1859 // Positive integer missing 1 bytes of 7 byte argument
1860 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1861 // Negative integer missing 1 byte argument
1862 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1863 // Binary string missing 1 byte argument
1864 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1865 // Text string missing 1 byte argument
1866 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1867 // Array missing 1 byte argument
1868 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1869 // Map missing 1 byte argument
1870 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1871 // Tag missing 1 byte argument
1872 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1873 // Simple missing 1 byte argument
1874 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001875 // half-precision with 1 byte argument
1876 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1877 // single-precision with 2 byte argument
1878 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1879 // double-precision with 3 byte argument
1880 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1881
1882
1883 // Tag with no content
1884 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001885
1886
1887 // Breaks must not occur in definite length arrays and maps
1888 // Array of length 1 with sole member replaced by a break
1889 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1890 // Array of length 2 with 2nd member replaced by a break
1891 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1892 // Map of length 1 with sole member label replaced by a break
1893 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1894 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001895 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001896 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1897 // Array of length 1 with 2nd member value replaced by a break
1898 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1899 // Map of length 2 with 2nd member replaced by a break
1900 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1901
1902
1903 // Breaks must not occur on their own out of an indefinite length data item
1904 // A bare break is not well formed
1905 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1906 // A bare break after a zero length definite length array
1907 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1908 // A bare break after a zero length indefinite length map
1909 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001910 // A break inside a definite length array inside an indefenite length array
1911 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1912 // Complicated mixed nesting with break outside indefinite length array
1913 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001914
1915
1916 // Forbidden two byte encodings of simple types
1917 // Must use 0xe0 instead
1918 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1919 // Should use 0xe1 instead
1920 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1921 // Should use 0xe2 instead
1922 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1923 // Should use 0xe3 instead
1924 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1925 // Should use 0xe4 instead
1926 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1927 // Should use 0xe5 instead
1928 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1929 // Should use 0xe6 instead
1930 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1931 // Should use 0xe7 instead
1932 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1933 // Should use 0xe8 instead
1934 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1935 // Should use 0xe9 instead
1936 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1937 // Should use 0xea instead
1938 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1939 // Should use 0xeb instead
1940 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1941 // Should use 0xec instead
1942 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1943 // Should use 0xed instead
1944 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1945 // Should use 0xee instead
1946 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1947 // Should use 0xef instead
1948 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1949 // Should use 0xf0 instead
1950 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1951 // Should use 0xf1 instead
1952 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1953 // Should use 0xf2 instead
1954 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1955 // Must use 0xf3 instead
1956 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1957 // Must use 0xf4 instead
1958 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1959 // Must use 0xf5 instead
1960 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1961 // Must use 0xf6 instead
1962 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1963 // Must use 0xf7 instead
1964 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1965 // Must use 0xf8 instead
1966 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001967 // Reserved
1968 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001969
1970 // Integers with additional info indefinite length
1971 // Positive integer with additional info indefinite length
1972 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1973 // Negative integer with additional info indefinite length
1974 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1975 // CBOR tag with "argument" an indefinite length
1976 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1977 // CBOR tag with "argument" an indefinite length alternate vector
1978 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1979
1980
1981 // Missing bytes from a deterministic length string
1982 // A byte string is of length 1 without the 1 byte
1983 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1984 // A text string is of length 1 without the 1 byte
1985 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001986 // Byte string should have 2^32-15 bytes, but has one
1987 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1988 // Byte string should have 2^32-15 bytes, but has one
1989 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001990 // Byte string should have 2^64 bytes, but has 3
1991 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1992 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
1993 // Text string should have 2^64 bytes, but has 3
1994 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1995 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001996
1997 // Use of unassigned additional information values
1998 // Major type positive integer with reserved value 28
1999 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2000 // Major type positive integer with reserved value 29
2001 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2002 // Major type positive integer with reserved value 30
2003 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2004 // Major type negative integer with reserved value 28
2005 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2006 // Major type negative integer with reserved value 29
2007 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2008 // Major type negative integer with reserved value 30
2009 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2010 // Major type byte string with reserved value 28 length
2011 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2012 // Major type byte string with reserved value 29 length
2013 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2014 // Major type byte string with reserved value 30 length
2015 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2016 // Major type text string with reserved value 28 length
2017 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2018 // Major type text string with reserved value 29 length
2019 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2020 // Major type text string with reserved value 30 length
2021 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2022 // Major type array with reserved value 28 length
2023 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2024 // Major type array with reserved value 29 length
2025 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2026 // Major type array with reserved value 30 length
2027 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2028 // Major type map with reserved value 28 length
2029 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2030 // Major type map with reserved value 29 length
2031 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2032 // Major type map with reserved value 30 length
2033 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2034 // Major type tag with reserved value 28 length
2035 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2036 // Major type tag with reserved value 29 length
2037 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2038 // Major type tag with reserved value 30 length
2039 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2040 // Major type simple with reserved value 28 length
2041 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2042 // Major type simple with reserved value 29 length
2043 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2044 // Major type simple with reserved value 30 length
2045 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2046
2047
2048 // Maps must have an even number of data items (key & value)
2049 // Map with 1 item when it should have 2
2050 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2051 // Map with 3 item when it should have 4
2052 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
2053 // Map with 1 item when it should have 2
2054 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2055 // Map with 3 item when it should have 4
2056 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
2057
2058
2059 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002060 // Text-based date, with an integer
2061 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2062 // Epoch date, with an byte string
2063 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2064 // tagged as both epoch and string dates
2065 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2066 // big num tagged an int, not a byte string
2067 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002068};
2069
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002070int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002071{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002072 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002073
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002074 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002075 if(nResult) {
2076 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002078
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002079 // Corrupt the UsefulInputBuf and see that
2080 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002081 QCBORDecodeContext DCtx;
2082 QCBORItem Item;
2083 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002084
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002085 QCBORDecode_Init(&DCtx,
2086 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2087 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002088
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002089 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2090 return (int32_t)uQCBORError;
2091 }
2092 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2093 // This wasn't supposed to happen
2094 return -1;
2095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002096
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002097 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002098
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002099 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2100 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2101 // Did not get back the error expected
2102 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002104
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002105
Laurence Lundblade98427e92020-09-28 21:33:23 -07002106 /*
2107 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2108 tests here can be performed to see that the max length
2109 error check works correctly. See DecodeBytes(). If the max
2110 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002111
Laurence Lundblade98427e92020-09-28 21:33:23 -07002112 This test will automatocally adapt the all CPU sizes
2113 through the use of SIZE_MAX.
2114 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002115
Laurence Lundblade98427e92020-09-28 21:33:23 -07002116 MakeUsefulBufOnStack( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
2117 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002118
Laurence Lundblade98427e92020-09-28 21:33:23 -07002119 // This makes a CBOR head with a text string that is very long
2120 // but doesn't fill in the bytes of the text string as that is
2121 // not needed to test this part of QCBOR.
2122 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2123
2124 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2125
2126 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2127 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002128 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002129
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002130 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131}
2132
2133
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002134/* Try all 256 values of the byte at nLen including recursing for
2135 each of the values to try values at nLen+1 ... up to nLenMax
2136 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002137static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002138{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002139 if(nLen >= nLenMax) {
2140 return;
2141 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002142
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002143 for(int inputByte = 0; inputByte < 256; inputByte++) {
2144 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002145 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002146 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002147
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002148 // Get ready to parse
2149 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002150 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002151
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002152 // Parse by getting the next item until an error occurs
2153 // Just about every possible decoder error can occur here
2154 // The goal of this test is not to check for the correct
2155 // error since that is not really possible. It is to
2156 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002157 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002158 QCBORItem Item;
2159 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002160 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161 break;
2162 }
2163 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002165 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002166 }
2167}
2168
2169
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002170int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002171{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002172 // Size 2 tests 64K inputs and runs quickly
2173 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002174
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002175 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002177 return 0;
2178}
2179
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002180
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002181int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002182{
2183 // size 3 tests 16 million inputs and runs OK
2184 // in seconds on fast machines. Size 4 takes
2185 // 10+ minutes and 5 half a day on fast
2186 // machines. This test is kept separate from
2187 // the others so as to no slow down the use
2188 // of them as a very frequent regression.
2189 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002190
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002191 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002192
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002193 return 0;
2194}
2195
2196
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002197static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002198 0xc0, // tag for string date
2199 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002200
Laurence Lundbladec7114722020-08-13 05:11:40 -07002201 0xc0, // tag for string date
2202 0x00, // Wrong type for a string date
2203
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002204 0xc1, // tag for epoch date
2205 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2206
Laurence Lundbladec7114722020-08-13 05:11:40 -07002207 0xc1,
2208 0x62, 'h', 'i', // wrong type tagged
2209
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002210 // CBOR_TAG_B64
Laurence Lundblade9b334962020-08-27 10:55:53 -07002211 0xcf, 0xd8, 0x22, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002212 0x1a, 0x53, 0x72, 0x4E, 0x01,
2213
2214 0xc1, // tag for epoch date
2215 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002216
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002217 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002218 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002220 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002221 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002222
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002223 0xc1, // tag for epoch date
2224 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2225 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2226
2227 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002228 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2229
2230 0xc1, // tag for epoch date
2231 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2232
2233 0xc1,
2234 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2235
2236 0xc1, // tag for epoch date
2237 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002238};
2239
2240
Laurence Lundbladec7114722020-08-13 05:11:40 -07002241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002242// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002243#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002244static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002245
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002246 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002247
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002248 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002250 return diff > 0.0000001;
2251}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002252#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002253
2254
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002255int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002256{
2257 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002258 QCBORItem Item;
2259 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundbladeee851742020-01-08 08:37:05 -08002261 QCBORDecode_Init(&DCtx,
2262 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2263 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002264
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002265 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002266 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002267 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002268 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002269 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002270 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002271 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002272 }
2273
Laurence Lundbladec7114722020-08-13 05:11:40 -07002274 // Wrong type for a string date
2275 uError = QCBORDecode_GetNext(&DCtx, &Item);
2276 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002277 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002278 }
2279
2280 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2281 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2282 return -4;
2283 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002284 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2285 Item.val.epochDate.nSeconds != 1400000000 ||
2286 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002287 return -5;
2288 }
2289
2290 // Wrong type for an epoch date
2291 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2292 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002294
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002295 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2296 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002297 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2298 return -7;
2299 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002300 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2301 Item.val.epochDate.nSeconds != 1400000001 ||
2302 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002303 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002304 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002305 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002306
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002307 // Epoch date that is too large for our representation
2308 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002309 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002311
Laurence Lundblade9682a532020-06-06 18:33:04 -07002312#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002313 // Epoch date in float format with fractional seconds
2314 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2315 return -10;
2316 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002317 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2318 Item.val.epochDate.nSeconds != 1 ||
2319 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002320 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002322
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002323 // Epoch date float that is too large for our representation
2324 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002325 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002326 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002327
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002328 // Epoch date double that is just slightly too large
2329 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002330 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002331 }
2332
2333 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002334 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2335 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2336 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2337 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002338 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002339 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002340
2341 // Nan
2342 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2343 return -15;
2344 }
2345
2346 // +Inifinity double-precision
2347 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2348 return -16;
2349 }
2350
2351#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2352 // -Inifinity half-precision
2353 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2354 return -17;
2355 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002356#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002357 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2358 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002359 }
2360#endif
2361
Laurence Lundbladec7114722020-08-13 05:11:40 -07002362#else
2363 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2364 return -19;
2365 }
2366 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2367 return -20;
2368 }
2369 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2370 return -21;
2371 }
2372 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2373 return -22;
2374 }
2375 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2376 return -23;
2377 }
2378 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2379 return -24;
2380 }
2381#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2382 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2383 return -25;
2384 }
2385#else
2386 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2387 return -26;
2388 }
2389#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002390
Laurence Lundbladec7114722020-08-13 05:11:40 -07002391#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002392
2393 return 0;
2394}
2395
Laurence Lundblade4b270642020-08-14 12:53:07 -07002396/*
2397 Test cases covered here. Some items cover more than one of these.
2398 positive integer (zero counts as a positive integer)
2399 negative integer
2400 half-precision float
2401 single-precision float
2402 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002403
Laurence Lundblade4b270642020-08-14 12:53:07 -07002404 float Overflow error
2405 Wrong type error for epoch
2406 Wrong type error for date string
2407 float disabled error
2408 half-precision disabled error
2409 -Infinity
2410 Slightly too large integer
2411 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002412
Laurence Lundblade4b270642020-08-14 12:53:07 -07002413 Get epoch by int
2414 Get string by int
2415 Get epoch by string
2416 Get string by string
2417 Fail to get epoch by wrong int label
2418 Fail to get string by wrong string label
2419 Fail to get epoch by string because it is invalid
2420 Fail to get epoch by int because it is invalid
2421
2422 Untagged values
2423 */
2424static uint8_t spSpiffyDateTestInput[] = {
2425 0x86,
2426
2427 0xc1,
2428 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2429
Laurence Lundbladec7114722020-08-13 05:11:40 -07002430 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002431 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2432
2433 0xc1, // tag for epoch date
2434 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2435
2436 0xc1, // tag for epoch date
2437 0x9f, 0xff, // Erroneous empty array as content for date
2438
2439 0xc0, // tag for string date
2440 0xbf, 0xff, // Erroneous empty map as content for date
2441
2442 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002443
2444 0x00,
2445 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002446 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002447
2448 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002449 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002450 0xc1, // tag for epoch date
2451 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2452
2453 // Untagged integer 0
2454 0x08,
2455 0x00,
2456
2457 // Utagged date string with string label y
2458 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002459 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002460
2461 // Untagged -1000 with label z
2462 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002463 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002464 0x39, 0x03, 0xe7,
2465
Laurence Lundbladec7114722020-08-13 05:11:40 -07002466 0x07,
2467 0xc1, // tag for epoch date
2468 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2469
Laurence Lundblade4b270642020-08-14 12:53:07 -07002470 0x05,
2471 0xc1,
2472 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2473
Laurence Lundbladec7114722020-08-13 05:11:40 -07002474 // Untagged single-precision float with value 3.14 with string label x
2475 0x61, 0x78,
2476 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2477
Laurence Lundbladec7114722020-08-13 05:11:40 -07002478 // Untagged half-precision float with value -2
2479 0x09,
2480 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002481
2482 0xff,
2483};
2484
2485int32_t SpiffyDateDecodeTest()
2486{
2487 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002488 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002489 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2490 nEpochDate4, nEpochDate6, nEpochDateFail,
2491 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002492 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002493 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002494
2495 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002496 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002497 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002498 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002499
Laurence Lundblade9b334962020-08-27 10:55:53 -07002500 // Too-negative float, -9.2233720368547748E+18
2501 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002502 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002503#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002504 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2505 return 1111;
2506 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002507#else
2508 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2509 return 1112;
2510 }
2511#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002512
2513 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002514 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002515 uError = QCBORDecode_GetAndResetError(&DC);
2516 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002517 return 1;
2518 }
2519
Laurence Lundblade4b270642020-08-14 12:53:07 -07002520 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002521 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002522 uError = QCBORDecode_GetAndResetError(&DC);
2523#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2524#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2525 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2526#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2527 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2528#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2529#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2530 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2531#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2532 if(uError != uExpectedforHalfMinusInfinity) {
2533 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002534 }
2535
Laurence Lundblade4b270642020-08-14 12:53:07 -07002536 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002537 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002538 uError = QCBORDecode_GetAndResetError(&DC);
2539 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2540 return 3;
2541 }
2542
2543 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002544 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002545 uError = QCBORDecode_GetAndResetError(&DC);
2546 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2547 return 4;
2548 }
2549
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002550 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002551
2552 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002553 QCBORDecode_GetEpochDateInMapN(&DC,
2554 5,
2555 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2556 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2557 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002558#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2559 if(nEpochDate2 != -9223372036854773760LL) {
2560 return 101;
2561 }
2562#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2563 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002564 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002565 return 102;
2566 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002567#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002568
Laurence Lundblade4b270642020-08-14 12:53:07 -07002569 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002570 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2571 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002572#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2573 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002574 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002575 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002576#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2577 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002578 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002579 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002580 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002581#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2582
2583 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002584 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2585 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002586#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2587 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002588 return 103;
2589 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002590#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2591 uError = QCBORDecode_GetAndResetError(&DC);
2592 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2593 return 104;
2594 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002595#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2596
Laurence Lundblade9b334962020-08-27 10:55:53 -07002597 // A half-precision date with value -2 FFF
2598 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2599 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002600#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2601 if(nEpochDate4 != -2) {
2602 return 105;
2603 }
2604#else
2605 uError = QCBORDecode_GetAndResetError(&DC);
2606 if(uError == QCBOR_SUCCESS) {
2607 return 106;
2608 }
2609#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002610
Laurence Lundblade4b270642020-08-14 12:53:07 -07002611
2612 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002613 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2614 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2615 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002616 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002617 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002618 return 107;
2619 }
2620
2621 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002622 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2623 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002624 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002625 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002626 return 108;
2627 }
2628
2629 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002630 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2631 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2632 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002633 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002634 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002635 return 109;
2636 }
2637
2638 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002639 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2640 &StringDate1);
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 110;
2644 }
2645
2646 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002647
Laurence Lundblade4b270642020-08-14 12:53:07 -07002648 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002649 QCBORDecode_GetEpochDateInMapN(&DC,
2650 1,
2651 QCBOR_TAG_REQUIREMENT_TAG |
2652 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2653 &nEpochDate1400000000);
2654 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002655 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002656 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2657 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002658 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002659 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2660 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002661 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002662 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2663 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002664 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002665 QCBORDecode_GetEpochDateInMapSZ(&DC,
2666 "z",
2667 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2668 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2669 &nEpochDate6);
2670 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002671
2672 QCBORDecode_ExitMap(&DC);
2673 QCBORDecode_ExitArray(&DC);
2674 uError = QCBORDecode_Finish(&DC);
2675 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002676 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002677 }
2678
Laurence Lundblade9b334962020-08-27 10:55:53 -07002679 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002680 return 200;
2681 }
2682
Laurence Lundblade9b334962020-08-27 10:55:53 -07002683 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002684 return 201;
2685 }
2686
Laurence Lundblade9b334962020-08-27 10:55:53 -07002687 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002688 return 202;
2689 }
2690
Laurence Lundblade9b334962020-08-27 10:55:53 -07002691 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002692 return 203;
2693 }
2694
Laurence Lundblade9b334962020-08-27 10:55:53 -07002695 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002696 return 204;
2697 }
2698
Laurence Lundblade9b334962020-08-27 10:55:53 -07002699 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2700 return 205;
2701 }
2702
2703 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2704 return 206;
2705 }
2706
Laurence Lundbladec7114722020-08-13 05:11:40 -07002707 return 0;
2708}
2709
2710
2711
Laurence Lundblade9b334962020-08-27 10:55:53 -07002712// Input for one of the tagging tests
2713static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002714 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002715 0x81, // Array of one
2716 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2717 0x82, // Array of two that is the faction 1/3
2718 0x01,
2719 0x03,
2720
2721 /*
2722 More than 4 tags on an item 225(226(227(228(229([])))))
2723 */
2724 0xd8, 0xe1,
2725 0xd8, 0xe2,
2726 0xd8, 0xe3,
2727 0xd8, 0xe4,
2728 0xd8, 0xe5,
2729 0x80,
2730
2731 /* tag 10489608748473423768(
2732 2442302356(
2733 21590(
2734 240(
2735 []))))
2736 */
2737 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2738 0xda, 0x91, 0x92, 0x93, 0x94,
2739 0xd9, 0x54, 0x56,
2740 0xd8, 0xf0,
2741 0x80,
2742
2743 /* tag 21590(
2744 10489608748473423768(
2745 2442302357(
2746 65534(
2747 []))))
2748 */
2749 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2750 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2751 0xda, 0x91, 0x92, 0x93, 0x95,
2752 0xd9, 0xff, 0xfe,
2753 0x80,
2754
2755 /* Make sure to blow past the limit of tags that must be mapped.
2756 works in conjuntion with entries above.
2757 269488144(269488145(269488146(269488147([]))))
2758 */
2759 0xda, 0x10, 0x10, 0x10, 0x10,
2760 0xda, 0x10, 0x10, 0x10, 0x11,
2761 0xda, 0x10, 0x10, 0x10, 0x12,
2762 0xda, 0x10, 0x10, 0x10, 0x13,
2763 0x80,
2764
2765 /* An invalid decimal fraction with an additional tag */
2766 0xd9, 0xff, 0xfa,
2767 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2768 0x00, // the integer 0; should be a byte string
2769};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002770
Laurence Lundblade59289e52019-12-30 13:44:37 -08002771/*
2772 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002773 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002774 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002775static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2776 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002777
Laurence Lundblade59289e52019-12-30 13:44:37 -08002778/*
2779DB 9192939495969798 # tag(10489608748473423768)
2780 D8 88 # tag(136)
2781 C6 # tag(6)
2782 C7 # tag(7)
2783 80 # array(0)
2784*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002785static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2786 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002787
2788/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002789 55799(55799(55799({
2790 6(7(-23)): 5859837686836516696(7({
2791 7(-20): 11({
2792 17(-18): 17(17(17("Organization"))),
2793 9(-17): 773("SSG"),
2794 -15: 16(17(6(7("Confusion")))),
2795 17(-16): 17("San Diego"),
2796 17(-14): 17("US")
2797 }),
2798 23(-19): 19({
2799 -11: 9({
2800 -9: -7
2801 }),
2802 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2803 })
2804 })),
2805 16(-22): 23({
2806 11(8(7(-5))): 8(-3)
2807 })
2808 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002809 */
2810static uint8_t spCSRWithTags[] = {
2811 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2812 0xc6, 0xc7, 0x36,
2813 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2814 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2815 0xcb, 0xa5,
2816 0xd1, 0x31,
2817 0xd1, 0xd1, 0xd1, 0x6c,
2818 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2819 0xc9, 0x30,
2820 0xd9, 0x03, 0x05, 0x63,
2821 0x53, 0x53, 0x47,
2822 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002823 0xd0, 0xd1, 0xc6, 0xc7,
2824 0x69,
2825 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002826 0xd1, 0x2f,
2827 0xd1, 0x69,
2828 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2829 0xd1, 0x2d,
2830 0xd1, 0x62,
2831 0x55, 0x53,
2832 0xd7, 0x32,
2833 0xd3, 0xa2,
2834 0x2a,
2835 0xc9, 0xa1,
2836 0x28,
2837 0x26,
2838 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2839 0xcc, 0x4a,
2840 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2841 0xd0, 0x35,
2842 0xd7, 0xa1,
2843 0xcb, 0xc8, 0xc7, 0x24,
2844 0xc8, 0x22};
2845
Laurence Lundblade9b334962020-08-27 10:55:53 -07002846
2847static uint8_t spSpiffyTagInput[] = {
2848 0x9f, // Open indefinite array
2849
2850 0xc0, // tag for string date
2851 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2852
2853 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2854
2855 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2856
2857 0xd8, 0x23, // tag for regex
2858 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2859
2860 0xc0, // tag for string date
2861 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2862
2863 0xff
2864};
2865
2866
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002867static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002868
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002869
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002870int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002871{
2872 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002873 QCBORItem Item;
2874 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002875
Laurence Lundbladeee851742020-01-08 08:37:05 -08002876 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002877 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002878 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002879
Laurence Lundblade9b334962020-08-27 10:55:53 -07002880 /*
2881 This test matches the magic number tag and the fraction tag
2882 55799([...])
2883 */
2884 uError = QCBORDecode_GetNext(&DCtx, &Item);
2885 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002886 return -2;
2887 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002888 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002889 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2890 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002891 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002892
Laurence Lundblade9b334962020-08-27 10:55:53 -07002893 /*
2894 4([1,3])
2895 */
2896 uError = QCBORDecode_GetNext(&DCtx, &Item);
2897#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2898 if(uError != QCBOR_SUCCESS ||
2899 Item.uDataType != QCBOR_TYPE_ARRAY ||
2900 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2901 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2902 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2903 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2904 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2905 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2906 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002907 return -4;
2908 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002909 // consume the items in the array
2910 uError = QCBORDecode_GetNext(&DCtx, &Item);
2911 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002912
Laurence Lundblade59289e52019-12-30 13:44:37 -08002913#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002914 if(uError != QCBOR_SUCCESS ||
2915 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2916 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2917 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2918 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2919 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2920 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2921 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002922 }
2923#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002924
Laurence Lundblade9b334962020-08-27 10:55:53 -07002925 /*
2926 More than 4 tags on an item 225(226(227(228(229([])))))
2927 */
2928 uError = QCBORDecode_GetNext(&DCtx, &Item);
2929 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002930 return -6;
2931 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002932
Laurence Lundblade88e9db22020-11-02 03:56:33 -08002933 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
2934 return -106;
2935 }
2936
2937
Laurence Lundblade9b334962020-08-27 10:55:53 -07002938 /* tag 10489608748473423768(
2939 2442302356(
2940 21590(
2941 240(
2942 []))))
2943 */
2944 uError = QCBORDecode_GetNext(&DCtx, &Item);
2945 if(uError != QCBOR_SUCCESS ||
2946 Item.uDataType != QCBOR_TYPE_ARRAY ||
2947 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2948 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2949 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2950 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002951 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002952 }
2953
2954 /* tag 21590(
2955 10489608748473423768(
2956 2442302357(
2957 21591(
2958 []))))
2959 */
2960 uError = QCBORDecode_GetNext(&DCtx, &Item);
2961 if(uError != QCBOR_SUCCESS ||
2962 Item.uDataType != QCBOR_TYPE_ARRAY ||
2963 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2964 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2965 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2966 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2967 return -8;
2968 }
2969
2970 /* Make sure to blow past the limit of tags that must be mapped.
2971 works in conjuntion with entries above.
2972 269488144(269488145(269488146(269488147([]))))
2973 */
2974 uError = QCBORDecode_GetNext(&DCtx, &Item);
2975 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2976 return -9;
2977 }
2978
2979 uError = QCBORDecode_GetNext(&DCtx, &Item);
2980 if(uError == QCBOR_SUCCESS) {
2981 return -10;
2982 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002983
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002984 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002985 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002986 // tage and then matches it. Caller-config lists are no longer
2987 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002988 QCBORDecode_Init(&DCtx,
2989 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2990 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002991 const uint64_t puList[] = {0x9192939495969798, 257};
2992 const QCBORTagListIn TL = {2, puList};
2993 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002994
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002995 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2996 return -8;
2997 }
2998 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2999 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3000 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3001 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3002 Item.val.uCount != 0) {
3003 return -9;
3004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003005
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003006 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003007 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003008 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003009 const uint64_t puLongList[17] = {1,2,1};
3010 const QCBORTagListIn TLLong = {17, puLongList};
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 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3015 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3016 return -11;
3017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003018
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003019 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003020 QCBORTagListOut Out = {0, 4, puTags};
3021
3022
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003023 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003024 QCBORDecode_Init(&DCtx,
3025 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3026 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003027 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3028 return -12;
3029 }
3030 if(puTags[0] != 0x9192939495969798 ||
3031 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003032 puTags[2] != 0x06 ||
3033 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003034 return -13;
3035 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003036
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003037 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003038 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003039 QCBORDecode_Init(&DCtx,
3040 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3041 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003042 QCBORTagListOut OutSmall = {0, 3, puTags};
3043 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3044 return -14;
3045 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003046
Laurence Lundblade9b334962020-08-27 10:55:53 -07003047
3048
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003049 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003050 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3051 // It is a bit of a messy test and maybe could be improved, but
3052 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003053 QCBORDecode_Init(&DCtx,
3054 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3055 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003056 int n = CheckCSRMaps(&DCtx);
3057 if(n) {
3058 return n-2000;
3059 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003060
Laurence Lundblade59289e52019-12-30 13:44:37 -08003061 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003062 QCBORDecode_Init(&DCtx,
3063 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3064 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003065
Laurence Lundblade9b334962020-08-27 10:55:53 -07003066 /* With the spiffy decode revision, this tag list is not used.
3067 It doesn't matter if a tag is in this list or not so some
3068 tests that couldn't process a tag because it isn't in this list
3069 now can process these unlisted tags. The tests have been
3070 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003071 const uint64_t puTagList[] = {773, 1, 90599561};
3072 const QCBORTagListIn TagList = {3, puTagList};
3073 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003074
3075
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003076 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3077 return -100;
3078 }
3079 if(Item.uDataType != QCBOR_TYPE_MAP ||
3080 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3081 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3082 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3083 Item.val.uCount != 2 ||
3084 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3085 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3086 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3087 Out.uNumUsed != 3) {
3088 return -101;
3089 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003090
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003091 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3092 return -102;
3093 }
3094 if(Item.uDataType != QCBOR_TYPE_MAP ||
3095 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3096 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003097 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003098 Item.val.uCount != 2 ||
3099 puTags[0] != 5859837686836516696 ||
3100 puTags[1] != 7 ||
3101 Out.uNumUsed != 2) {
3102 return -103;
3103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003104
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003105 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3106 return -104;
3107 }
3108 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003109 Item.val.uCount != 5 ||
3110 puTags[0] != 0x0b ||
3111 Out.uNumUsed != 1) {
3112 return -105;
3113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003114
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003115 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3116 return -106;
3117 }
3118 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3119 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3120 Item.val.string.len != 12 ||
3121 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3122 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3123 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3124 Out.uNumUsed != 3) {
3125 return -105;
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 -107;
3130 }
3131 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3132 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3133 Item.val.string.len != 3 ||
3134 puTags[0] != 773 ||
3135 Out.uNumUsed != 1) {
3136 return -108;
3137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003138
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003139 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3140 return -109;
3141 }
3142 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003143 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003144 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003145 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003146 puTags[3] != 7 ||
3147 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003148 return -110;
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 -111;
3153 }
3154 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3155 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3156 Item.val.string.len != 9 ||
3157 puTags[0] != 17 ||
3158 Out.uNumUsed != 1) {
3159 return -112;
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 -111;
3164 }
3165 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3166 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3167 Item.val.string.len != 2 ||
3168 puTags[0] != 17 ||
3169 Out.uNumUsed != 1) {
3170 return -112;
3171 }
3172
3173 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3174 return -113;
3175 }
3176 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003177 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003178 Item.val.uCount != 2 ||
3179 puTags[0] != 19 ||
3180 Out.uNumUsed != 1) {
3181 return -114;
3182 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003183
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003184 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3185 return -115;
3186 }
3187 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003188 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003189 Item.val.uCount != 1 ||
3190 puTags[0] != 9 ||
3191 Out.uNumUsed != 1) {
3192 return -116;
3193 }
3194
3195 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3196 return -116;
3197 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003198 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003199 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003200 Out.uNumUsed != 0) {
3201 return -117;
3202 }
3203
3204 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3205 return -118;
3206 }
3207 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3208 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003209 puTags[0] != 12 ||
3210 Out.uNumUsed != 1) {
3211 return -119;
3212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003213
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003214 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3215 return -120;
3216 }
3217 if(Item.uDataType != QCBOR_TYPE_MAP ||
3218 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3219 Item.val.uCount != 1 ||
3220 puTags[0] != 0x17 ||
3221 Out.uNumUsed != 1) {
3222 return -121;
3223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003224
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003225 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3226 return -122;
3227 }
3228 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003229 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003230 Item.val.int64 != -3 ||
3231 puTags[0] != 8 ||
3232 Out.uNumUsed != 1) {
3233 return -123;
3234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003235
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003236 if(QCBORDecode_Finish(&DCtx)) {
3237 return -124;
3238 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003239
3240 UsefulBufC DateString;
3241 QCBORDecode_Init(&DCtx,
3242 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3243 QCBOR_DECODE_MODE_NORMAL);
3244
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003245 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003246 // tagged date string
3247 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3248 // untagged date string
3249 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3250 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3251 return 100;
3252 }
3253 // untagged byte string
3254 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3255 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3256 return 101;
3257 }
3258 // tagged regex
3259 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3260 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3261 return 102;
3262 }
3263 // tagged date string with a byte string
3264 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3265 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3266 return 103;
3267 }
3268 QCBORDecode_ExitArray(&DCtx);
3269 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3270 return 104;
3271 }
3272
3273
3274 QCBORDecode_Init(&DCtx,
3275 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3276 QCBOR_DECODE_MODE_NORMAL);
3277
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003278 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003279 // tagged date string
3280 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3281 // untagged date string
3282 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3283 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3284 return 200;
3285 }
3286 // untagged byte string
3287 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3288 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3289 return 201;
3290 }
3291 // tagged regex
3292 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3293 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3294 return 202;
3295 }
3296 // tagged date string with a byte string
3297 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3298 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3299 return 203;
3300 }
3301 QCBORDecode_ExitArray(&DCtx);
3302 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3303 return 204;
3304 }
3305
3306 QCBORDecode_Init(&DCtx,
3307 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3308 QCBOR_DECODE_MODE_NORMAL);
3309
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003310 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003311 // tagged date string
3312 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3313 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3314 return 300;
3315 }
3316 // untagged date string
3317 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3318 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3319 return 301;
3320 }
3321 // untagged byte string
3322 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3323 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3324 return 302;
3325 }
3326 // tagged regex
3327 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3328 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3329 return 303;
3330 }
3331 // tagged date string with a byte string
3332 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3333 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3334 return 304;
3335 }
3336 QCBORDecode_ExitArray(&DCtx);
3337 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3338 return 305;
3339 }
3340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003341 return 0;
3342}
3343
3344
3345
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003346
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003347static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003348 0x83,
3349 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3350 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3351 0xA4,
3352 0x63, 0x42, 0x4E, 0x2B,
3353 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3354 0x18, 0x40,
3355 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3356 0x63, 0x42, 0x4E, 0x2D,
3357 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3358 0x38, 0x3F,
3359 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3360
3361
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003362static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003363
3364
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003365int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003366{
3367 QCBORDecodeContext DCtx;
3368 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003369 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003370
Laurence Lundbladeee851742020-01-08 08:37:05 -08003371 QCBORDecode_Init(&DCtx,
3372 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3373 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003374
3375
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003376 //
3377 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3378 return -1;
3379 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003380 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003382
3383 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003384 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003385 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003386 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003387 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003388 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003389 }
3390
3391 //
3392 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003393 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003394 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003395 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003396 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003397 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003399 //
3400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003401 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003402 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003403 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003405
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003406 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003407 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003408 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3409 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
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 -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003412 }
3413
3414 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003415 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003416 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3417 Item.uLabelType != QCBOR_TYPE_INT64 ||
3418 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003419 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003420 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003423 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003424 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003425 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3426 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003427 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003428 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003429 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003431 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003432 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003433 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3434 Item.uLabelType != QCBOR_TYPE_INT64 ||
3435 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003436 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003437 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003438 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003439
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003440 return 0;
3441}
3442
3443
3444
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003445static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003446 uint8_t uDataType,
3447 uint8_t uNestingLevel,
3448 uint8_t uNextNest,
3449 int64_t nLabel,
3450 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003451{
3452 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003453 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003454
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003455 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3456 if(Item.uDataType != uDataType) return -1;
3457 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003458 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3459 Item.uLabelType != QCBOR_TYPE_UINT64) {
3460 return -1;
3461 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003462 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3463 if(Item.label.int64 != nLabel) return -1;
3464 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003465 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003466 }
3467 }
3468 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303469 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003470
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003471 if(pItem) {
3472 *pItem = Item;
3473 }
3474 return 0;
3475}
3476
3477
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003478// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003479static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003480{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303481 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003482
Laurence Lundblade9b334962020-08-27 10:55:53 -07003483 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003484
Laurence Lundblade9b334962020-08-27 10:55:53 -07003485 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003486
Laurence Lundblade9b334962020-08-27 10:55:53 -07003487 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3488 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3489 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3490 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3491 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003492
Laurence Lundblade9b334962020-08-27 10:55:53 -07003493 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3494 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003495
Laurence Lundblade9b334962020-08-27 10:55:53 -07003496 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3497 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003498
Laurence Lundblade9b334962020-08-27 10:55:53 -07003499 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3500 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003501
Laurence Lundblade9b334962020-08-27 10:55:53 -07003502 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003503
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003504 return 0;
3505}
3506
3507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003508/*
3509// cbor.me decoded output
3510{
3511 -23: {
3512 -20: {
3513 -18: "Organization",
3514 -17: "SSG",
3515 -15: "Confusion",
3516 -16: "San Diego",
3517 -14: "US"
3518 },
3519 -19: {
3520 -11: {
3521 -9: -7
3522 },
3523 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3524 }
3525 },
3526 -22: {
3527 -5: -3
3528 }
3529}
3530 */
3531
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003532
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003533static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003534 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3535 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3536 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3537 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3538 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3539 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3540 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3541 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3542 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3543
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003544int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003545{
3546 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003547
Laurence Lundbladeee851742020-01-08 08:37:05 -08003548 QCBORDecode_Init(&DCtx,
3549 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3550 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003551
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003552 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003553}
3554
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003555
3556
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003557int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003558{
3559 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003560
Laurence Lundbladeee851742020-01-08 08:37:05 -08003561 QCBORDecode_Init(&DCtx,
3562 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3563 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003564
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003565 QCBORItem Item;
3566 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003567
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003568 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3569 return -1;
3570 }
3571 if(Item.uDataType != QCBOR_TYPE_MAP) {
3572 return -2;
3573 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003574
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003575 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3576 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3577 return -3;
3578 }
3579
3580 return 0;
3581}
3582
3583
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003584// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003585static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003586 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3587 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3588 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3589 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3590 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3591 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003592 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3593 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3594 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3595 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003596
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003597int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003598{
3599 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003600
Laurence Lundbladeee851742020-01-08 08:37:05 -08003601 QCBORDecode_Init(&DCtx,
3602 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3603 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003604
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003605 return CheckCSRMaps(&DCtx);
3606}
3607
3608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003609
Laurence Lundblade17ede402018-10-13 11:43:07 +08003610static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3611{
3612 UsefulOutBuf UOB;
3613 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003614
Laurence Lundblade17ede402018-10-13 11:43:07 +08003615 int i;
3616 for(i = 0; i < n; i++) {
3617 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3618 }
3619
3620 for(i = 0; i < n; i++) {
3621 UsefulOutBuf_AppendByte(&UOB, 0xff);
3622 }
3623 return UsefulOutBuf_OutUBuf(&UOB);
3624}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003625
3626
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003627static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003628{
3629 QCBORDecodeContext DC;
3630 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003631
Laurence Lundblade17ede402018-10-13 11:43:07 +08003632 int j;
3633 for(j = 0; j < nNestLevel; j++) {
3634 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003635 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003636 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3637 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003638 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003639 return -4;
3640 } else {
3641 return 0; // Decoding doesn't recover after an error
3642 }
3643 } else {
3644 // Should be no error
3645 if(nReturn) {
3646 return -9; // Should not have got an error
3647 }
3648 }
3649 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3650 return -7;
3651 }
3652 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003653 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003654 if(nReturn) {
3655 return -3;
3656 }
3657 return 0;
3658}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003659
3660
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003661int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003662{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303663 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003664 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003665 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003666 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003667 int nReturn = parse_indeflen_nested(Nested, i);
3668 if(nReturn) {
3669 return nReturn;
3670 }
3671 }
3672 return 0;
3673}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003674
Laurence Lundbladeee851742020-01-08 08:37:05 -08003675// [1, [2, 3]]
3676static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3677// No closing break
3678static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3679// Not enough closing breaks
3680static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3681// Too many closing breaks
3682static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3683// Unclosed indeflen inside def len
3684static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3685// confused tag
3686static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003687
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003688int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003689{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003690 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003691 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003692 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003693
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003694 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003695 QCBORDecodeContext DC;
3696 QCBORItem Item;
3697 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003698
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003699 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303700
3701 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3702 Item.uNestingLevel != 0 ||
3703 Item.uNextNestLevel != 1) {
3704 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003705 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003706
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003707 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303708 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3709 Item.uNestingLevel != 1 ||
3710 Item.uNextNestLevel != 1) {
3711 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003712 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003713
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003714 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303715 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3716 Item.uNestingLevel != 1 ||
3717 Item.uNextNestLevel != 2) {
3718 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003720
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003721 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003722 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303723 Item.uNestingLevel != 2 ||
3724 Item.uNextNestLevel != 2) {
3725 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003726 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003727
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003728 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003729 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303730 Item.uNestingLevel != 2 ||
3731 Item.uNextNestLevel != 0) {
3732 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003733 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003734
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003735 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303736 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003737 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003738
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003739 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003740 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003741
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003742 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003743
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003744 nResult = QCBORDecode_GetNext(&DC, &Item);
3745 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303746 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003747 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003748
Laurence Lundblade570fab52018-10-13 18:28:27 +08003749 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003750 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303751 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003752 }
3753
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003754
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003755 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003756 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003757
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003758 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003759
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003760 nResult = QCBORDecode_GetNext(&DC, &Item);
3761 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303762 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003763 }
3764
3765 nResult = QCBORDecode_GetNext(&DC, &Item);
3766 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303767 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003768 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003769
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003770 nResult = QCBORDecode_GetNext(&DC, &Item);
3771 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303772 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003773 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003774
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003775 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003776 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303777 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003778 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003779
3780
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003781 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003782 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003783
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003784 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003785
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003786 nResult = QCBORDecode_GetNext(&DC, &Item);
3787 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303788 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003789 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003790
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003791 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003792 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303793 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003794 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303795
Laurence Lundblade642282a2020-06-23 12:00:33 -07003796 nResult = QCBORDecode_GetNext(&DC, &Item);
3797 if(nResult != QCBOR_ERR_BAD_BREAK) {
3798 return -140;
3799 }
3800
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003801
Laurence Lundblade570fab52018-10-13 18:28:27 +08003802 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003803 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003804
Laurence Lundblade570fab52018-10-13 18:28:27 +08003805 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003806
Laurence Lundblade570fab52018-10-13 18:28:27 +08003807 nResult = QCBORDecode_GetNext(&DC, &Item);
3808 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303809 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003810 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003811
Laurence Lundblade570fab52018-10-13 18:28:27 +08003812 nResult = QCBORDecode_GetNext(&DC, &Item);
3813 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303814 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003815 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003816
Laurence Lundblade570fab52018-10-13 18:28:27 +08003817 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003818 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303819 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003820 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003821
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303822 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003823 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003824
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303825 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003826
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303827 nResult = QCBORDecode_GetNext(&DC, &Item);
3828 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303829 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303830 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003831
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303832 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303833 if(nResult != QCBOR_ERR_BAD_BREAK) {
3834 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303835 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003836
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003837 return 0;
3838}
3839
Laurence Lundblade17ede402018-10-13 11:43:07 +08003840
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08003841#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
3842
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003843static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003844 0x81, // Array of length one
3845 0x7f, // text string marked with indefinite length
3846 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3847 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3848 0xff // ending break
3849};
3850
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003851static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303852 0x81, // Array of length one
3853 0x7f, // text string marked with indefinite length
3854 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3855 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3856 0xff // ending break
3857};
3858
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003859static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303860 0x81, // Array of length one
3861 0x7f, // text string marked with indefinite length
3862 0x01, 0x02, // Not a string
3863 0xff // ending break
3864};
3865
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003866static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303867 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 // missing end of string
3872};
3873
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003874static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303875 0xa1, // Array of length one
3876 0x7f, // text string marked with indefinite length
3877 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3878 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3879 0xff, // ending break
3880 0x01 // integer being labeled.
3881};
3882
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003883/**
3884 Make an indefinite length string
3885
3886 @param Storage Storage for string, must be 144 bytes in size
3887 @return The indefinite length string
3888
3889 This makes an array with one indefinite length string that has 7 chunks
3890 from size of 1 byte up to 64 bytes.
3891 */
3892static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303893{
3894 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003895
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303896 UsefulOutBuf_Init(&UOB, Storage);
3897 UsefulOutBuf_AppendByte(&UOB, 0x81);
3898 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003899
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003900 uint8_t uStringByte = 0;
3901 // Use of type int is intentional
3902 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3903 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303904 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003905 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3906 for(int j = 0; j < uChunkSize; j++) {
3907 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3908 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303909 }
3910 }
3911 UsefulOutBuf_AppendByte(&UOB, 0xff);
3912
3913 return UsefulOutBuf_OutUBuf(&UOB);
3914}
3915
3916static int CheckBigString(UsefulBufC BigString)
3917{
3918 if(BigString.len != 255) {
3919 return 1;
3920 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003921
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303922 for(uint8_t i = 0; i < 255; i++){
3923 if(((const uint8_t *)BigString.ptr)[i] != i) {
3924 return 1;
3925 }
3926 }
3927 return 0;
3928}
3929
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303930
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003931int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303932{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303933 QCBORDecodeContext DC;
3934 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303935 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003936 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003937
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303938 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003939 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303940 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003941
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303942 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303943 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303944 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003945
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303946 if(QCBORDecode_GetNext(&DC, &Item)) {
3947 return -2;
3948 }
3949 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3950 return -3;
3951 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003952
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303953 if(QCBORDecode_GetNext(&DC, &Item)) {
3954 return -4;
3955 }
3956 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3957 return -5;
3958 }
3959 if(QCBORDecode_Finish(&DC)) {
3960 return -6;
3961 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303962
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303963 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003964 QCBORDecode_Init(&DC,
3965 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3966 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003967
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303968 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3969 return -7;
3970 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003971
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303972 if(QCBORDecode_GetNext(&DC, &Item)) {
3973 return -8;
3974 }
3975 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3976 return -9;
3977 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003978
Laurence Lundblade30816f22018-11-10 13:40:22 +07003979 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303980 return -10;
3981 }
3982
3983 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003984 QCBORDecode_Init(&DC,
3985 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3986 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003987
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303988 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3989 return -11;
3990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003991
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303992 if(QCBORDecode_GetNext(&DC, &Item)) {
3993 return -12;
3994 }
3995 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3996 return -13;
3997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003998
Laurence Lundblade30816f22018-11-10 13:40:22 +07003999 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304000 return -14;
4001 }
4002
4003 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004004 QCBORDecode_Init(&DC,
4005 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4006 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004007
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304008 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4009 return -15;
4010 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004011
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304012 if(QCBORDecode_GetNext(&DC, &Item)) {
4013 return -16;
4014 }
4015 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4016 return -17;
4017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004018
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304019 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4020 return -18;
4021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004022
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304023 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304024 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004025
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304026 QCBORDecode_GetNext(&DC, &Item);
4027 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304028 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004030
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304031 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304032 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004034
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304035 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004036 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304037
4038 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4039 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304040 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304041 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004042
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304043 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304044 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004045 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004046
Laurence Lundbladeee851742020-01-08 08:37:05 -08004047 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4048 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004049
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304050 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304051 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304052 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304053 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004054
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304055 QCBORDecode_GetNext(&DC, &Item);
4056 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304057 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304058 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004059 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304060 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304061 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004062
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304063 // ---- big bstr -----
4064 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004065
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304066 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4067 return -25;
4068 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004069
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304070 if(QCBORDecode_GetNext(&DC, &Item)) {
4071 return -26;
4072 }
4073 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304074 return -26;
4075 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004076
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304077 if(QCBORDecode_GetNext(&DC, &Item)) {
4078 return -27;
4079 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304080 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304081 return -28;
4082 }
4083 if(CheckBigString(Item.val.string)) {
4084 return -3;
4085 }
4086 if(QCBORDecode_Finish(&DC)) {
4087 return -29;
4088 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004089
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304090 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004091 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004092
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304093 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4094 return -30;
4095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004096
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304097 QCBORDecode_GetNext(&DC, &Item);
4098 if(Item.uDataType != QCBOR_TYPE_MAP) {
4099 return -31;
4100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004101
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304102 if(QCBORDecode_GetNext(&DC, &Item)){
4103 return -32;
4104 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004105 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4106 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304107 Item.uDataAlloc || !Item.uLabelAlloc ||
4108 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4109 return -33;
4110 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004111
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304112 if(QCBORDecode_Finish(&DC)) {
4113 return -34;
4114 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004115
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004116 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004117}
4118
4119
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004120int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304121{
4122 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004123 QCBORError nCBORError;
4124
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004125
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304126 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004127 QCBORDecode_Init(&DC,
4128 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4129 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004130
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004131 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004133 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4134 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304135 return -1;
4136 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004137
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004138 if(CheckCSRMaps(&DC)) {
4139 return -2;
4140 }
4141
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004142 // Next parse, save pointers to a few strings, destroy original and
4143 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004144 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004145 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004146
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304147 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004148 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304149 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004150
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304151 QCBORItem Item1, Item2, Item3, Item4;
4152 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004153 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304154 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4155 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004156 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304157 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004158 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304159 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004160 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304161 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004162 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304163 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004164 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004165
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304166 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004167
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304168 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304169 Item1.uDataType != QCBOR_TYPE_INT64 ||
4170 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004171 Item1.uDataAlloc != 0 ||
4172 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004173 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004174 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004176
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304177
4178 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004179 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304180 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004181 Item2.uDataAlloc != 0 ||
4182 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304183 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004184 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004185
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304186 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004187 Item3.uDataAlloc == 0 ||
4188 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004189 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004190 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004191 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004192
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304193 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004194 Item4.uDataAlloc == 0 ||
4195 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004196 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004197 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004198 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004199
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304200 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004201 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004202 QCBORDecode_Init(&DC,
4203 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4204 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304205 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4206 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004207 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304208 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004209 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004210 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004211 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304212 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4213 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4214 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4215 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4216 }
4217 }
4218 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004219 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004220 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304221 }
4222
4223 return 0;
4224}
4225
Laurence Lundbladef6531662018-12-04 10:42:22 +09004226
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004227int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004228{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004229 // Set up the decoder with a tiny bit of CBOR to parse because
4230 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004231 QCBORDecodeContext DC;
4232 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4233 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004234
Laurence Lundbladef6531662018-12-04 10:42:22 +09004235 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004236 // Then fish into the internals of the decode context
4237 // to get the allocator function so it can be called directly.
4238 // Also figure out how much pool is available for use
4239 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004240 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004241 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4242 if(nError) {
4243 return -9;
4244 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004245 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4246 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4247 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004248
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004249 // First test -- ask for one more byte than available and see failure
4250 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004251 if(!UsefulBuf_IsNULL(Allocated)) {
4252 return -1;
4253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004254
Laurence Lundbladef6531662018-12-04 10:42:22 +09004255 // Re do the set up for the next test that will do a successful alloc,
4256 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004257 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004258 pAlloc = DC.StringAllocator.pfAllocator;
4259 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4260 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004261
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004262 // Allocate one byte less than available and see success
4263 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004264 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4265 return -2;
4266 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004267 // Ask for some more and see failure
4268 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004269 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4270 return -3;
4271 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004272 // Free the first allocate, retry the second and see success
4273 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4274 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004275 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4276 return -4;
4277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004278
Laurence Lundbladef6531662018-12-04 10:42:22 +09004279 // Re do set up for next test that involves a successful alloc,
4280 // and a successful realloc and a failed realloc
4281 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004282 pAlloc = DC.StringAllocator.pfAllocator;
4283 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004284
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004285 // Allocate half the pool and see success
4286 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004287 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4288 return -5;
4289 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004290 // Reallocate to take up the whole pool and see success
4291 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004292 if(UsefulBuf_IsNULL(Allocated2)) {
4293 return -6;
4294 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004295 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004296 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4297 return -7;
4298 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004299 // Try to allocate more to be sure there is failure after a realloc
4300 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4301 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004302 return -8;
4303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004304
Laurence Lundbladef6531662018-12-04 10:42:22 +09004305 return 0;
4306}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004307
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004308
4309/* Just enough of an allocator to test configuration of one */
4310static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4311{
4312 (void)pOldMem; // unused variable
4313
4314 if(uNewSize) {
4315 // Assumes the context pointer is the buffer and
4316 // nothing too big will ever be asked for.
4317 // This is only good for this basic test!
4318 return (UsefulBuf) {pCtx, uNewSize};
4319 } else {
4320 return NULLUsefulBuf;
4321 }
4322}
4323
4324
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004325int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004326{
4327 // Set up the decoder with a tiny bit of CBOR to parse because
4328 // nothing can be done with it unless that is set up.
4329 QCBORDecodeContext DC;
4330 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4331 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4332
4333 uint8_t pAllocatorBuffer[50];
4334
4335 // This is really just to test that this call works.
4336 // The full functionality of string allocators is tested
4337 // elsewhere with the MemPool internal allocator.
4338 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4339
4340 QCBORItem Item;
4341 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4342 return -1;
4343 }
4344
4345 if(Item.uDataAlloc == 0 ||
4346 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4347 Item.val.string.ptr != pAllocatorBuffer) {
4348 return -2;
4349 }
4350
4351 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4352 return -3;
4353 }
4354
4355 return 0;
4356}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004357#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4358
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004359
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004360#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004361
Laurence Lundbladea826c502020-05-10 21:07:00 -07004362/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004363 [
4364 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004365 4([-20, 4759477275222530853136]),
4366 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004367 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004368 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004369 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004370 5([ 9223372036854775806, -4759477275222530853137])
4371 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004372 ]
4373 */
4374
4375static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004376 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004377 0xC4, 0x82, 0x20,
4378 0x03,
4379 0xC4, 0x82, 0x33,
4380 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4381 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4382 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4383 0xC5, 0x82, 0x19, 0x01, 0x2C,
4384 0x18, 0x64,
4385 0xC5, 0x82, 0x33,
4386 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4387 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4388 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004389 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4390 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004391 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4392 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4393};
4394
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004395
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004396int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004397{
4398 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004399 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004400 QCBORItem item;
4401
Laurence Lundblade17af4902020-01-07 19:11:55 -08004402 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4403 0x06, 0x07, 0x08, 0x09, 0x010};
4404 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004405
4406
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004407 QCBORDecode_Init(&DC,
4408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4409 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004410
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004411 uErr = QCBORDecode_GetNext(&DC, &item);
4412 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004413 return 1;
4414 }
4415
4416 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4417 return 2;
4418 }
4419
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004420 uErr = QCBORDecode_GetNext(&DC, &item);
4421 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004422 return 3;
4423 }
4424
4425 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4426 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4427 item.val.expAndMantissa.nExponent != -1) {
4428 return 4;
4429 }
4430
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004431 uErr = QCBORDecode_GetNext(&DC, &item);
4432 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004433 return 5;
4434 }
4435
4436 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4437 item.val.expAndMantissa.nExponent != -20 ||
4438 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4439 return 6;
4440 }
4441
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004442 uErr = QCBORDecode_GetNext(&DC, &item);
4443 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004444 return 7;
4445 }
4446
4447 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4448 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4449 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4450 return 8;
4451 }
4452
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004453 uErr = QCBORDecode_GetNext(&DC, &item);
4454 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004455 return 9;
4456 }
4457
4458 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4459 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4460 item.val.expAndMantissa.nExponent != 300) {
4461 return 10;
4462 }
4463
Laurence Lundbladea826c502020-05-10 21:07:00 -07004464 // 5([-20, 4759477275222530853136]),
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 11;
4468 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004469 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4470 item.val.expAndMantissa.nExponent != -20 ||
4471 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4472 return 12;
4473 }
4474
Laurence Lundbladea826c502020-05-10 21:07:00 -07004475 // 5([-9223372036854775807, -4759477275222530853137])
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 13;
4479 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004480 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4481 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4482 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4483 return 14;
4484 }
4485
Laurence Lundbladea826c502020-05-10 21:07:00 -07004486 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004487 uErr = QCBORDecode_GetNext(&DC, &item);
4488 if(uErr != QCBOR_SUCCESS) {
4489 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004490 }
4491 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4492 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4493 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004494 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004495 }
4496
Laurence Lundbladea826c502020-05-10 21:07:00 -07004497 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004498 uErr = QCBORDecode_GetNext(&DC, &item);
4499 if(uErr != QCBOR_SUCCESS) {
4500 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004501 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004502 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4503 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4504 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004505 return 18;
4506 }
4507
4508 uErr = QCBORDecode_Finish(&DC);
4509 if(uErr != QCBOR_SUCCESS) {
4510 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004511 }
4512
4513 /* Now encode some stuff and then decode it */
4514 uint8_t pBuf[40];
4515 QCBOREncodeContext EC;
4516 UsefulBufC Encoded;
4517
4518 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4519 QCBOREncode_OpenArray(&EC);
4520 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4521 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4522 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4523 QCBOREncode_CloseArray(&EC);
4524 QCBOREncode_Finish(&EC, &Encoded);
4525
4526
4527 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004528 uErr = QCBORDecode_GetNext(&DC, &item);
4529 if(uErr != QCBOR_SUCCESS) {
4530 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004531 }
4532
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004533 uErr = QCBORDecode_GetNext(&DC, &item);
4534 if(uErr != QCBOR_SUCCESS) {
4535 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004536 }
4537
4538 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4539 item.val.expAndMantissa.nExponent != 1000 ||
4540 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004541 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004542 }
4543
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004544 uErr = QCBORDecode_GetNext(&DC, &item);
4545 if(uErr != QCBOR_SUCCESS) {
4546 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004547 }
4548
4549 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4550 item.val.expAndMantissa.nExponent != INT32_MIN ||
4551 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004552 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004553 }
4554
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004555 uErr = QCBORDecode_GetNext(&DC, &item);
4556 if(uErr != QCBOR_SUCCESS) {
4557 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004558 }
4559
4560 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4561 item.val.expAndMantissa.nExponent != INT32_MAX ||
4562 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004563 return 106;
4564 }
4565
4566
4567 int64_t nExp, nMant;
4568 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4569 UsefulBufC Mant;
4570 bool bIsNeg;
4571
4572 QCBORDecode_Init(&DC,
4573 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4574 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004575 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004576
4577 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004578 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004579
4580 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004581 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4582 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004583
4584 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004585 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4586 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004587
4588 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004589 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004590
4591 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004592 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4593 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004594
4595 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004596 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4597 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004598
4599 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004600 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4601 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004602
4603 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004604 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4605 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004606
4607 QCBORDecode_ExitArray(&DC);
4608
4609 uErr = QCBORDecode_Finish(&DC);
4610 if(uErr != QCBOR_SUCCESS) {
4611 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004612 }
4613
4614 return 0;
4615}
4616
4617
4618static struct FailInput ExponentAndMantissaFailures[] = {
4619 // Exponent > INT64_MAX
4620 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4621 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4622 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4623 // Mantissa > INT64_MAX
4624 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4625 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4626 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4627 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004628 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004629 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004630 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004631 // bad content for big num
4632 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4633 // bad content for big num
4634 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4635 // Bad integer for exponent
4636 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4637 // Bad integer for mantissa
4638 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4639 // 3 items in array
4640 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4641 // unterminated indefinite length array
4642 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4643 // Empty array
4644 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4645 // Second is not an integer
4646 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4647 // First is not an integer
4648 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4649 // Not an array
4650 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4651};
4652
4653
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004654int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004655{
4656 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004657 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4658 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004659}
4660
4661#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004662
4663
4664
4665/*
4666 Some basic CBOR with map and array used in a lot of tests.
4667 The map labels are all strings
4668
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004669 {
4670 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004671 "an array of two strings": [
4672 "string1", "string2"
4673 ],
4674 "map in a map": {
4675 "bytes 1": h'78787878',
4676 "bytes 2": h'79797979',
4677 "another int": 98,
4678 "text 2": "lies, damn lies and statistics"
4679 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004680 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004681 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004682
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004683int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004684{
4685 QCBORItem Item1, Item2, Item3;
4686 int64_t nDecodedInt1, nDecodedInt2;
4687 UsefulBufC B1, B2, S1, S2, S3;
4688
4689 QCBORDecodeContext DCtx;
4690 QCBORError nCBORError;
4691
4692 QCBORDecode_Init(&DCtx, input, 0);
4693
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004694 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004695
4696 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4697
4698 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4699 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004700 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4701 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4702 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004703 QCBORDecode_ExitMap(&DCtx);
4704
4705 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4706 QCBORDecode_GetNext(&DCtx, &Item1);
4707 QCBORDecode_GetNext(&DCtx, &Item2);
4708 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4709 return -400;
4710 }
4711 QCBORDecode_ExitArray(&DCtx);
4712
4713 // Parse the same array again using GetText() instead of GetItem()
4714 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004715 QCBORDecode_GetTextString(&DCtx, &S2);
4716 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004717 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4718 return 5000;
4719 }
4720 /* QCBORDecode_GetText(&DCtx, &S3);
4721 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4722 return 5001;
4723 } */
4724
4725 QCBORDecode_ExitArray(&DCtx);
4726
4727 QCBORDecode_ExitMap(&DCtx);
4728
4729 nCBORError = QCBORDecode_Finish(&DCtx);
4730
4731 if(nCBORError) {
4732 return (int32_t)nCBORError;
4733 }
4734
4735 if(nDecodedInt1 != 42) {
4736 return 1001;
4737 }
4738
4739 if(nDecodedInt2 != 98) {
4740 return 1002;
4741 }
4742
4743 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004744 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004745 return 1003;
4746 }
4747
4748 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004749 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004750 return 1004;
4751 }
4752
Laurence Lundblade9b334962020-08-27 10:55:53 -07004753 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004754 return 1005;
4755 }
4756
4757 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4758 return 1006;
4759 }
4760
4761 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4762 return 1007;
4763 }
4764
4765 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4766 return 1008;
4767 }
4768
4769 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4770 return 1009;
4771 }
4772
4773 return 0;
4774}
4775
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004776/*
4777 {
4778 -75008: h'05083399',
4779 88: [
4780 ],
4781 100100: {
4782 "sub1": {
4783 10: [
4784 0
4785 ],
4786 -75009: h'A46823990001',
4787 100100: {
4788 "json": "{ \"ueid\", \"xyz\"}",
4789 "subsub": {
4790 100002: h'141813191001'
4791 }
4792 }
4793 }
4794 }
4795 }
4796 */
4797
4798static const uint8_t spNestedCBOR[] = {
47990xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
48000x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
48010x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
48020x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
48030x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
48040x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, 0x01
4805};
4806
4807/* Get item in multi-level nesting in spNestedCBOR */
4808static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4809{
4810 UsefulBufC String;
4811
4812 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4813 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4814
4815 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4816 QCBORDecode_EnterMap(pDCtx, NULL);
4817 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4818 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4819 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4820 if(QCBORDecode_GetError(pDCtx)) {
4821 return 4001;
4822 }
4823 if(UsefulBuf_Compare(String, test_oemid)) {
4824 return 4002;
4825 }
4826 QCBORDecode_ExitMap(pDCtx);
4827 QCBORDecode_ExitMap(pDCtx);
4828 QCBORDecode_ExitMap(pDCtx);
4829 QCBORDecode_ExitMap(pDCtx);
4830
4831 return 0;
4832}
4833
4834/* Iterations on the zero-length array in spNestedCBOR */
4835static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4836{
4837 QCBORItem Item;
4838 QCBORError uErr;
4839
4840 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4841 for(int x = 0; x < 20; x++) {
4842 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4843 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4844 return 4100;
4845
4846 }
4847 }
4848 QCBORDecode_ExitArray(pDCtx);
4849 if(QCBORDecode_GetError(pDCtx)) {
4850 return 4101;
4851 }
4852
4853 return 0;
4854}
4855
4856/* Various iterations on the array that contains a zero in spNestedCBOR */
4857static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
4858{
4859 QCBORError uErr;
4860
4861 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4862 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
4863 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4864 int64_t nInt = 99;
4865 QCBORDecode_GetInt64(pDCtx, &nInt);
4866 if(nInt != 0) {
4867 return 4200;
4868 }
4869 for(int x = 0; x < 20; x++) {
4870 QCBORItem Item;
4871 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4872 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4873 return 4201;
4874
4875 }
4876 }
4877 QCBORDecode_ExitArray(pDCtx);
4878 if(QCBORDecode_GetAndResetError(pDCtx)) {
4879 return 4202;
4880 }
4881 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4882 UsefulBufC dD;
4883 QCBORDecode_GetByteString(pDCtx, &dD);
4884 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4885 return 4203;
4886 }
4887 for(int x = 0; x < 20; x++) {
4888 QCBORDecode_GetByteString(pDCtx, &dD);
4889 uErr = QCBORDecode_GetAndResetError(pDCtx);
4890 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4891 return 4204;
4892 }
4893 }
4894 QCBORDecode_ExitArray(pDCtx);
4895 QCBORDecode_ExitMap(pDCtx);
4896 QCBORDecode_ExitMap(pDCtx);
4897
4898 return 0;
4899}
4900
4901/* Repeatedly enter and exit maps and arrays, go off the end of maps
4902 and arrays and such. */
4903static int32_t DecodeNestedIterate()
4904{
4905 QCBORDecodeContext DCtx;
4906 int32_t nReturn;
4907 QCBORError uErr;
4908
4909 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
4910 QCBORDecode_EnterMap(&DCtx, NULL);
4911
4912 for(int j = 0; j < 5; j++) {
4913 for(int i = 0; i < 20; i++) {
4914 nReturn = DecodeNestedGetSubSub(&DCtx);
4915 if(nReturn) {
4916 return nReturn;
4917 }
4918 }
4919
4920 for(int i = 0; i < 20; i++) {
4921 nReturn = DecodeNestedGetEmpty(&DCtx);
4922 if(nReturn ) {
4923 return nReturn;
4924 }
4925 }
4926
4927 for(int i = 0; i < 20; i++) {
4928 nReturn = DecodeNestedGetZero(&DCtx);
4929 if(nReturn ) {
4930 return nReturn;
4931 }
4932 }
4933 }
4934
4935 QCBORDecode_ExitMap(&DCtx);
4936 uErr = QCBORDecode_Finish(&DCtx);
4937 if(uErr) {
4938 return (int32_t)uErr + 4100;
4939 }
4940
4941 return 0;
4942}
4943
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004944
4945/*
4946 [23,
4947 6000,
4948 h'67616C6163746963',
4949 h'686176656E20746F6B656E'
4950 ]
4951 */
4952static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004953 0x84,
4954 0x17,
4955 0x19, 0x17, 0x70,
4956 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4957 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004958
4959
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004960static const uint8_t spEmptyMap[] = {0xa0};
4961
4962static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004963
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004964static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4965
Laurence Lundbladef0499502020-08-01 11:55:57 -07004966/*
4967 {
4968 0: [],
4969 9: [
4970 [],
4971 []
4972 ],
4973 8: {
4974 1: [],
4975 2: {},
4976 3: []
4977 },
4978 4: {},
4979 5: [],
4980 6: [
4981 [],
4982 []
4983 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004984 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004985 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004986
Laurence Lundbladef0499502020-08-01 11:55:57 -07004987static const uint8_t spMapOfEmpty[] = {
4988 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4989 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4990 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004991
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004992/*
4993 Too many tags
4994 Invalid tag content
4995 Duplicate label
4996 Integer overflow
4997 Date overflow
4998
4999 {1: 224(225(226(227(4(0))))),
5000 2: 1(h''),
5001 3: -18446744073709551616,
5002 4: 1(1.0e+300),
5003 5: 0, 8: 8}
5004 */
5005static const uint8_t spRecoverableMapErrors[] = {
5006 0xbf,
5007 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5008 0x02, 0xc1, 0x40,
5009 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5010 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5011 0x05, 0x00,
5012 0x05, 0x00,
5013 0x08, 0x08,
5014 0xff
5015};
5016
5017// Bad break
5018static const uint8_t spUnRecoverableMapError1[] = {
5019 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5020};
5021
5022// No more items
5023static const uint8_t spUnRecoverableMapError2[] = {
5024 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5025};
5026
5027// Hit end because string is too long
5028static const uint8_t spUnRecoverableMapError3[] = {
5029 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5030};
5031
5032// Hit end because string is too long
5033static const uint8_t spUnRecoverableMapError4[] = {
5034 0xbf,
5035 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5036 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5037 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5038 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5039 0xff
5040};
5041
5042
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005043int32_t EnterMapTest()
5044{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005045 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005046 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005047 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005048 int32_t nReturn;
5049 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005050
5051
5052 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005053 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005054
Laurence Lundbladef0499502020-08-01 11:55:57 -07005055
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005056 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005057 QCBORDecode_ExitArray(&DCtx);
5058
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005059 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5060 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005061 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005062 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005063 QCBORDecode_ExitArray(&DCtx);
5064 QCBORDecode_ExitArray(&DCtx);
5065
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005066 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5067 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005068 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005069 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005070 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005071 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005072 QCBORDecode_ExitArray(&DCtx);
5073 QCBORDecode_ExitMap(&DCtx);
5074
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005075 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005076 QCBORDecode_ExitMap(&DCtx);
5077
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005078 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005079 QCBORDecode_ExitArray(&DCtx);
5080
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005081 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5082 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005083 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005084 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005085 QCBORDecode_ExitArray(&DCtx);
5086 QCBORDecode_ExitArray(&DCtx);
5087
5088 QCBORDecode_ExitMap(&DCtx);
5089
5090 uErr = QCBORDecode_Finish(&DCtx);
5091 if(uErr != QCBOR_SUCCESS){
5092 return 3011;
5093 }
5094
5095
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005096 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005097 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005098 if(nReturn) {
5099 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005100 }
5101
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005102 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005103 if(nReturn) {
5104 return nReturn;
5105 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005106
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005107
Laurence Lundblade937ea812020-05-08 11:38:23 -07005108
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005109 // These tests confirm the cursor is at the right place after entering
5110 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005111 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005112
5113 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005114 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005115 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005116 QCBORDecode_GetNext(&DCtx, &Item1);
5117 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5118 return 2001;
5119 }
5120
5121
Laurence Lundblade9b334962020-08-27 10:55:53 -07005122 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005123 QCBORDecode_VGetNext(&DCtx, &Item1);
5124 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005125 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5126 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5127 UsefulBuf_Compare(ArrayItem.label.string,
5128 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5129 return 2051;
5130 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005131 QCBORDecode_GetNext(&DCtx, &Item1);
5132 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5133 return 2002;
5134 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005135 QCBORDecode_ExitArray(&DCtx);
5136 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5137 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5138 UsefulBuf_Compare(ArrayItem.label.string,
5139 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5140 return 2052;
5141 }
5142
Laurence Lundblade937ea812020-05-08 11:38:23 -07005143
Laurence Lundblade9b334962020-08-27 10:55:53 -07005144 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005145 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005146 QCBORDecode_GetNext(&DCtx, &Item1);
5147 QCBORDecode_GetNext(&DCtx, &Item1);
5148 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005149 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5150 QCBORDecode_GetNext(&DCtx, &Item1);
5151 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5152 return 2003;
5153 }
5154
Laurence Lundblade9b334962020-08-27 10:55:53 -07005155 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005156 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005157 QCBORDecode_GetNext(&DCtx, &Item1);
5158 QCBORDecode_GetNext(&DCtx, &Item1);
5159 QCBORDecode_GetNext(&DCtx, &Item1);
5160 QCBORDecode_GetNext(&DCtx, &Item1);
5161 QCBORDecode_GetNext(&DCtx, &Item1);
5162 QCBORDecode_GetNext(&DCtx, &Item1);
5163 QCBORDecode_GetNext(&DCtx, &Item1);
5164 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5165 QCBORDecode_GetNext(&DCtx, &Item1);
5166 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005167 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005168 }
5169
Laurence Lundblade9b334962020-08-27 10:55:53 -07005170 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005171 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005172 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5173 QCBORDecode_ExitArray(&DCtx);
5174 QCBORDecode_GetNext(&DCtx, &Item1);
5175 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5176 return 2006;
5177 }
5178 QCBORDecode_ExitMap(&DCtx);
5179 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5180 return 2007;
5181 }
5182
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005183 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005184 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005185 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005186 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5187 uErr = QCBORDecode_GetAndResetError(&DCtx);
5188 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005189 return 2008;
5190 }
5191 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005192 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005193 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005194 return 2009;
5195 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005196
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005197
5198 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005199 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005200 // This will fail because the map is empty.
5201 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5202 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005203 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005204 return 2010;
5205 }
5206 QCBORDecode_ExitMap(&DCtx);
5207 uErr = QCBORDecode_Finish(&DCtx);
5208 if(uErr != QCBOR_SUCCESS){
5209 return 2011;
5210 }
5211
5212
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005213 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005214 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005215 // This will fail because the map is empty.
5216 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5217 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005218 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005219 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005220 }
5221 QCBORDecode_ExitMap(&DCtx);
5222 uErr = QCBORDecode_Finish(&DCtx);
5223 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005224 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005225 }
5226
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005227
5228 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005229 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005230 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005231 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005232 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005233 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005234 QCBORDecode_ExitArray(&DCtx);
5235 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5236 QCBORDecode_ExitArray(&DCtx);
5237 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005238 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005239 return 2014;
5240 }
5241
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005242 int64_t nInt;
5243 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005244 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005245 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005246 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005247 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5248 return 2021;
5249 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005250 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5251 return 2121;
5252 }
5253 (void)QCBORDecode_GetAndResetError(&DCtx);
5254
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005255
5256 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5257 uErr = QCBORDecode_GetAndResetError(&DCtx);
5258 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5259 return 2022;
5260 }
5261
5262 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5263 uErr = QCBORDecode_GetAndResetError(&DCtx);
5264 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5265 return 2023;
5266 }
5267
5268 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5269 uErr = QCBORDecode_GetAndResetError(&DCtx);
5270#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5271 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5272 return 2024;
5273 }
5274#else
5275 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5276 return 2027;
5277 }
5278#endif
5279
5280 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5281 uErr = QCBORDecode_GetAndResetError(&DCtx);
5282 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5283 return 2025;
5284 }
5285
5286 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5287
5288 QCBORDecode_ExitMap(&DCtx);
5289 uErr = QCBORDecode_Finish(&DCtx);
5290 if(uErr != QCBOR_SUCCESS) {
5291 return 2026;
5292 }
5293
5294 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005295 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005296 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5297 uErr = QCBORDecode_GetAndResetError(&DCtx);
5298 if(uErr != QCBOR_ERR_BAD_BREAK) {
5299 return 2030;
5300 }
5301
5302 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005303 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005304 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5305 uErr = QCBORDecode_GetAndResetError(&DCtx);
5306 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5307 return 2031;
5308 }
5309
5310 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005311 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005312 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5313 uErr = QCBORDecode_GetAndResetError(&DCtx);
5314 if(uErr != QCBOR_ERR_HIT_END) {
5315 return 2032;
5316 }
5317
5318 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005319 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005320 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5321 uErr = QCBORDecode_GetAndResetError(&DCtx);
5322 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5323 return 2033;
5324 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005325
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005326 nReturn = DecodeNestedIterate();
5327
5328 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005329}
5330
5331
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005332struct NumberConversion {
5333 char *szDescription;
5334 UsefulBufC CBOR;
5335 int64_t nConvertedToInt64;
5336 QCBORError uErrorInt64;
5337 uint64_t uConvertToUInt64;
5338 QCBORError uErrorUint64;
5339 double dConvertToDouble;
5340 QCBORError uErrorDouble;
5341};
5342
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005343static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005344 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005345 "too large to fit into int64_t",
5346 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5347 0,
5348 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5349 0,
5350 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5351 ((double)INT64_MIN) + 1 ,
5352 QCBOR_SUCCESS
5353 },
5354 {
5355 "largest negative int that fits in int64_t",
5356 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5357 INT64_MIN,
5358 QCBOR_SUCCESS,
5359 0,
5360 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5361 (double)INT64_MIN,
5362 QCBOR_SUCCESS
5363 },
5364 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005365 "negative bignum -1",
5366 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5367 -1,
5368 QCBOR_SUCCESS,
5369 0,
5370 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5371 -1.0,
5372 QCBOR_SUCCESS
5373 },
5374 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005375 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005376 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5377 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005378#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005379 257000,
5380 QCBOR_SUCCESS,
5381 257000,
5382 QCBOR_SUCCESS,
5383 257000.0,
5384 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005385#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5386 0,
5387 QCBOR_ERR_UNEXPECTED_TYPE,
5388 0,
5389 QCBOR_ERR_UNEXPECTED_TYPE,
5390 0.0,
5391 QCBOR_ERR_UNEXPECTED_TYPE
5392#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005393 },
5394 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005395 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005396 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5397 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005398#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005399 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005400 QCBOR_SUCCESS,
5401 0,
5402 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005403 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005404 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005405#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5406 0,
5407 QCBOR_ERR_UNEXPECTED_TYPE,
5408 0,
5409 QCBOR_ERR_UNEXPECTED_TYPE,
5410 0.0,
5411 QCBOR_ERR_UNEXPECTED_TYPE
5412#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005413 },
5414 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005415 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005416 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5417 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005418#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005419 2056,
5420 QCBOR_SUCCESS,
5421 2056,
5422 QCBOR_SUCCESS,
5423 2056.0,
5424 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005425#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5426 0,
5427 QCBOR_ERR_UNEXPECTED_TYPE,
5428 0,
5429 QCBOR_ERR_UNEXPECTED_TYPE,
5430 0.0,
5431 QCBOR_ERR_UNEXPECTED_TYPE
5432#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005433 },
5434 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005435 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005436 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5437 0,
5438 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5439 0,
5440 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5441 -18446744073709551617.0,
5442 QCBOR_SUCCESS
5443 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005444#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005445 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005446 "Positive bignum 0x01020304 indefinite length string",
5447 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5448 0x01020304,
5449 QCBOR_SUCCESS,
5450 0x01020304,
5451 QCBOR_SUCCESS,
5452 16909060.0,
5453 QCBOR_SUCCESS
5454 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005455#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005456 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005457 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005458 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5459 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005460#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005461 0,
5462 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5463 0,
5464 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5465 -INFINITY,
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 Lundblade313b2862020-05-16 01:23:06 -07005475 },
5476 {
5477 "big float [9223372036854775806, 9223372036854775806]",
5478 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5479 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005480#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005481 0,
5482 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5483 0,
5484 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5485 INFINITY,
5486 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005487#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5488 0,
5489 QCBOR_ERR_UNEXPECTED_TYPE,
5490 0,
5491 QCBOR_ERR_UNEXPECTED_TYPE,
5492 0.0,
5493 QCBOR_ERR_UNEXPECTED_TYPE
5494#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005495 },
5496 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005497 "Big float 3 * 2^^2",
5498 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005499#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005500 12,
5501 QCBOR_SUCCESS,
5502 12,
5503 QCBOR_SUCCESS,
5504 12.0,
5505 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005506#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5507 0,
5508 QCBOR_ERR_UNEXPECTED_TYPE,
5509 0,
5510 QCBOR_ERR_UNEXPECTED_TYPE,
5511 0.0,
5512 QCBOR_ERR_UNEXPECTED_TYPE
5513#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005514 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005515 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005516 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005517 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5518 0,
5519 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5520 18446744073709551615ULL,
5521 QCBOR_SUCCESS,
5522 18446744073709551615.0,
5523 QCBOR_SUCCESS
5524 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005525 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005526 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005527 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5528 65536-1,
5529 QCBOR_SUCCESS,
5530 0xffff,
5531 QCBOR_SUCCESS,
5532 65535.0,
5533 QCBOR_SUCCESS
5534 },
5535 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005536 "Postive integer 0",
5537 {(uint8_t[]){0x0}, 1},
5538 0LL,
5539 QCBOR_SUCCESS,
5540 0ULL,
5541 QCBOR_SUCCESS,
5542 0.0,
5543 QCBOR_SUCCESS
5544 },
5545 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005546 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005547 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5548 -9223372036854775807-1, // INT64_MIN
5549 QCBOR_SUCCESS,
5550 0ULL,
5551 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5552 -9223372036854775808.0,
5553 QCBOR_SUCCESS
5554 },
5555 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005556 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005557 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005558#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005559 100L,
5560 QCBOR_SUCCESS,
5561 100ULL,
5562 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005563#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5564 0,
5565 QCBOR_ERR_HW_FLOAT_DISABLED,
5566 0,
5567 QCBOR_ERR_HW_FLOAT_DISABLED,
5568#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005569 100.3,
5570 QCBOR_SUCCESS
5571 },
5572 {
5573 "Floating point value NaN 0xfa7fc00000",
5574 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005575#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005576 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005577 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005578 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005579 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005580#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5581 0,
5582 QCBOR_ERR_HW_FLOAT_DISABLED,
5583 0,
5584 QCBOR_ERR_HW_FLOAT_DISABLED,
5585#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005586 NAN,
5587 QCBOR_SUCCESS
5588 },
5589 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005590 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005591 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005592#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5593#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5594 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005595 -4,
5596 QCBOR_SUCCESS,
5597 0,
5598 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5599 -4.0,
5600 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005601#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5602 // Float HW disabled
5603 -4,
5604 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5605 0,
5606 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5607 -4.0,
5608 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5609#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5610#else
5611 // Half-precision disabled
5612 -4,
5613 QCBOR_ERR_HALF_PRECISION_DISABLED,
5614 0,
5615 QCBOR_ERR_HALF_PRECISION_DISABLED,
5616 -4.0,
5617 QCBOR_ERR_HALF_PRECISION_DISABLED
5618#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005619 },
5620 {
5621 "Decimal fraction 3/10",
5622 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005623#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005624 0,
5625 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5626 0,
5627 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5628 0.30000000000000004,
5629 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005630#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5631 0,
5632 QCBOR_ERR_UNEXPECTED_TYPE,
5633 0,
5634 QCBOR_ERR_UNEXPECTED_TYPE,
5635 0.0,
5636 QCBOR_ERR_UNEXPECTED_TYPE
5637#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005638 },
5639 {
5640 "+inifinity",
5641 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5642#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5643 0,
5644 QCBOR_ERR_FLOAT_EXCEPTION,
5645 0,
5646 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5647#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5648 0,
5649 QCBOR_ERR_HW_FLOAT_DISABLED,
5650 0,
5651 QCBOR_ERR_HW_FLOAT_DISABLED,
5652#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5653 INFINITY,
5654 QCBOR_SUCCESS
5655 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005656
5657 {
5658 "extreme pos bignum",
5659 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5660 // 50 rows of 8 is 400 digits.
5661 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5662 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5663 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5664 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5665 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5666 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5667 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5668 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5669 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5670 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5671 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5672 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5673 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5674 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5675 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5676 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5677 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5678 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5679 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5680 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5681 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5682 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5683 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5684 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5685 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5686 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5687 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5688 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5689 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5690 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5691 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5692 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5693 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5694 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5695 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5696 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5697 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5698 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5699 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5700 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5701 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5702 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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},
5711 404},
5712 0,
5713 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5714 0,
5715 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5716#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5717 INFINITY,
5718 QCBOR_SUCCESS
5719#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5720 0,
5721 QCBOR_ERR_HW_FLOAT_DISABLED,
5722#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5723 },
5724
5725 {
5726 "extreme neg bignum",
5727 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5728 // 50 rows of 8 is 400 digits.
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, 0xf0,
5753 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5754 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5755 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5756 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5757 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5758 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5759 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5760 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5761 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5762 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5763 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5764 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5765 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5766 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5767 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5768 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5769 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5770 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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},
5779 404},
5780 0,
5781 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5782 0,
5783 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5784#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5785 -INFINITY,
5786 QCBOR_SUCCESS
5787#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5788 0,
5789 QCBOR_ERR_HW_FLOAT_DISABLED,
5790#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5791 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005792
5793 {
5794 "big float underflow [9223372036854775806, -9223372036854775806]",
5795 {(uint8_t[]){
5796 0xC5, 0x82,
5797 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5798 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5799#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5800 0,
5801 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5802 0,
5803 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5804 0,
5805 QCBOR_SUCCESS
5806#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5807 0,
5808 QCBOR_ERR_UNEXPECTED_TYPE,
5809 0,
5810 QCBOR_ERR_UNEXPECTED_TYPE,
5811 0.0,
5812 QCBOR_ERR_UNEXPECTED_TYPE
5813#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5814 },
5815
5816 {
5817 "bigfloat that evaluates to -INFINITY",
5818 {(uint8_t[]){
5819 0xC5, 0x82,
5820 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5821 0xC3, 0x42, 0x01, 0x01}, 15},
5822#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5823 0,
5824 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5825 0,
5826 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5827 -INFINITY,
5828 QCBOR_SUCCESS
5829#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5830 0,
5831 QCBOR_ERR_UNEXPECTED_TYPE,
5832 0,
5833 QCBOR_ERR_UNEXPECTED_TYPE,
5834 0.0,
5835 QCBOR_ERR_UNEXPECTED_TYPE
5836#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5837 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005838};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005839
5840
5841
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005842
5843static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
5844{
5845 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
5846#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5847 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
5848 return 1;
5849 }
5850#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5851 (void)Pool;
5852#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5853 return 0;
5854}
5855
5856
Laurence Lundblade313b2862020-05-16 01:23:06 -07005857int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005858{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005859 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
5860 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005861
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005862 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5863 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005864
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005865 // Set up the decoding context including a memory pool so that
5866 // indefinite length items can be checked
5867 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005868 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005869
5870 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005871 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5872 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005873 }
5874
5875 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005876 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005877 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005878 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005879 }
5880 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005881 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005882 }
5883
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005884 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005885 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5886 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005887 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005888
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005889 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005890 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005891 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005892 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005893 }
5894 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005895 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005896 }
5897
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005898 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005899 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5900 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005901 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005902#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005903 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005904 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005905 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005906 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005907 }
5908 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5909 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005910 // NaN's can't be compared for equality. A NaN is
5911 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005912 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005913 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005914 }
5915 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005916 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005917 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005918 }
5919 }
5920 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005921#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005922 }
5923
5924 return 0;
5925}
5926
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005927
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005928
5929
Laurence Lundbladee3553422020-05-02 11:11:17 -07005930int32_t CBORSequenceDecodeTests(void)
5931{
5932 QCBORDecodeContext DCtx;
5933 QCBORItem Item;
5934 QCBORError uCBORError;
5935
5936 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005937
Laurence Lundbladee3553422020-05-02 11:11:17 -07005938 // The input for the date test happens to be a sequence so it
5939 // is reused. It is a sequence because it doesn't start as
5940 // an array or map.
5941 QCBORDecode_Init(&DCtx,
5942 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5943 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005944
Laurence Lundbladee3553422020-05-02 11:11:17 -07005945 // Get the first item
5946 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5947 if(uCBORError != QCBOR_SUCCESS) {
5948 return 1;
5949 }
5950 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5951 return 2;
5952 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005953
Laurence Lundbladee3553422020-05-02 11:11:17 -07005954 // Get a second item
5955 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005956 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5957 return 66;
5958 }
5959
5960 // Get a third item
5961 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005962 if(uCBORError != QCBOR_SUCCESS) {
5963 return 2;
5964 }
5965 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5966 return 3;
5967 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005968
Laurence Lundbladee3553422020-05-02 11:11:17 -07005969 // A sequence can have stuff at the end that may
5970 // or may not be valid CBOR. The protocol decoder knows
5971 // when to stop by definition of the protocol, not
5972 // when the top-level map or array is ended.
5973 // Finish still has to be called to know that
5974 // maps and arrays (if there were any) were closed
5975 // off correctly. When called like this it
5976 // must return the error QCBOR_ERR_EXTRA_BYTES.
5977 uCBORError = QCBORDecode_Finish(&DCtx);
5978 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5979 return 4;
5980 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005981
5982
Laurence Lundbladee3553422020-05-02 11:11:17 -07005983 // --- Test an empty input ----
5984 uint8_t empty[1];
5985 UsefulBufC Empty = {empty, 0};
5986 QCBORDecode_Init(&DCtx,
5987 Empty,
5988 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005989
Laurence Lundbladee3553422020-05-02 11:11:17 -07005990 uCBORError = QCBORDecode_Finish(&DCtx);
5991 if(uCBORError != QCBOR_SUCCESS) {
5992 return 5;
5993 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005994
5995
Laurence Lundbladee3553422020-05-02 11:11:17 -07005996 // --- Sequence with unclosed indefinite length array ---
5997 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005998
Laurence Lundbladee3553422020-05-02 11:11:17 -07005999 QCBORDecode_Init(&DCtx,
6000 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6001 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006002
Laurence Lundbladee3553422020-05-02 11:11:17 -07006003 // Get the first item
6004 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6005 if(uCBORError != QCBOR_SUCCESS) {
6006 return 7;
6007 }
6008 if(Item.uDataType != QCBOR_TYPE_INT64) {
6009 return 8;
6010 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006011
Laurence Lundbladee3553422020-05-02 11:11:17 -07006012 // Get a second item
6013 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6014 if(uCBORError != QCBOR_SUCCESS) {
6015 return 9;
6016 }
6017 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6018 return 10;
6019 }
6020
6021 // Try to finish before consuming all bytes to confirm
6022 // that the still-open error is returned.
6023 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006024 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006025 return 11;
6026 }
6027
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006028
Laurence Lundbladee3553422020-05-02 11:11:17 -07006029 // --- Sequence with a closed indefinite length array ---
6030 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006031
Laurence Lundbladee3553422020-05-02 11:11:17 -07006032 QCBORDecode_Init(&DCtx,
6033 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6034 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006035
Laurence Lundbladee3553422020-05-02 11:11:17 -07006036 // Get the first item
6037 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6038 if(uCBORError != QCBOR_SUCCESS) {
6039 return 12;
6040 }
6041 if(Item.uDataType != QCBOR_TYPE_INT64) {
6042 return 13;
6043 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006044
Laurence Lundbladee3553422020-05-02 11:11:17 -07006045 // Get a second item
6046 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6047 if(uCBORError != QCBOR_SUCCESS) {
6048 return 14;
6049 }
6050 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6051 return 15;
6052 }
6053
6054 // Try to finish before consuming all bytes to confirm
6055 // that the still-open error is returned.
6056 uCBORError = QCBORDecode_Finish(&DCtx);
6057 if(uCBORError != QCBOR_SUCCESS) {
6058 return 16;
6059 }
6060
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006061
Laurence Lundbladee3553422020-05-02 11:11:17 -07006062 return 0;
6063}
6064
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006065
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006066
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006067int32_t IntToTests()
6068{
6069 int nErrCode;
6070 int32_t n32;
6071 int16_t n16;
6072 int8_t n8;
6073 uint32_t u32;
6074 uint16_t u16;
6075 uint8_t u8;
6076 uint64_t u64;
6077
6078 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6079 if(nErrCode == -1 || n32 != 1) {
6080 return 1;
6081 }
6082
6083 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6084 if(nErrCode == -1 || n32 != INT32_MAX) {
6085 return 2;
6086 }
6087
6088 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6089 if(nErrCode == -1 || n32 != INT32_MIN) {
6090 return 3;
6091 }
6092
6093 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6094 if(nErrCode != -1) {
6095 return 4;
6096 }
6097
6098 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6099 if(nErrCode != -1) {
6100 return 5;
6101 }
6102
6103
6104 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6105 if(nErrCode == -1 || n16 != INT16_MAX) {
6106 return 6;
6107 }
6108
6109 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6110 if(nErrCode == -1 || n16 != INT16_MIN) {
6111 return 7;
6112 }
6113
6114 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6115 if(nErrCode == -1 || n16 != 1) {
6116 return 8;
6117 }
6118
6119 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6120 if(nErrCode != -1) {
6121 return 9;
6122 }
6123
6124 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6125 if(nErrCode != -1) {
6126 return 10;
6127 }
6128
6129
6130 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6131 if(nErrCode == -1 || n8 != 1) {
6132 return 11;
6133 }
6134
6135 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6136 if(nErrCode == -1 || n8 != INT8_MAX) {
6137 return 12;
6138 }
6139
6140 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6141 if(nErrCode == -1 || n8 != INT8_MIN) {
6142 return 13;
6143 }
6144
6145 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6146 if(nErrCode != -1) {
6147 return 14;
6148 }
6149
6150 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6151 if(nErrCode != -1) {
6152 return 15;
6153 }
6154
6155
6156 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6157 if(nErrCode == -1 || u32 != 1) {
6158 return 16;
6159 }
6160
6161 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6162 if(nErrCode == -1 || u32 != UINT32_MAX) {
6163 return 17;
6164 }
6165
6166 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6167 if(nErrCode == -1 || u32 != 0) {
6168 return 18;
6169 }
6170
6171 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6172 if(nErrCode != -1) {
6173 return 19;
6174 }
6175
6176 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6177 if(nErrCode != -1) {
6178 return 20;
6179 }
6180
6181
6182 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6183 if(nErrCode == -1 || u16 != UINT16_MAX) {
6184 return 21;
6185 }
6186
6187 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6188 if(nErrCode == -1 || u16 != 0) {
6189 return 22;
6190 }
6191
6192 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6193 if(nErrCode == -1 || u16 != 1) {
6194 return 23;
6195 }
6196
6197 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6198 if(nErrCode != -1) {
6199 return 24;
6200 }
6201
6202 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6203 if(nErrCode != -1) {
6204 return 25;
6205 }
6206
6207
6208 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6209 if(nErrCode == -1 || u8 != UINT8_MAX) {
6210 return 26;
6211 }
6212
6213 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6214 if(nErrCode == -1 || u8 != 0) {
6215 return 27;
6216 }
6217
6218 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6219 if(nErrCode == -1 || u8 != 1) {
6220 return 28;
6221 }
6222
6223 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6224 if(nErrCode != -1) {
6225 return 29;
6226 }
6227
6228 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6229 if(nErrCode != -1) {
6230 return 30;
6231 }
6232
6233
6234 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6235 if(nErrCode == -1 || u64 != 1) {
6236 return 31;
6237 }
6238
6239 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6240 if(nErrCode == -1 || u64 != INT64_MAX) {
6241 return 32;
6242 }
6243
6244 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6245 if(nErrCode == -1 || u64 != 0) {
6246 return 33;
6247 }
6248
6249 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6250 if(nErrCode != -1) {
6251 return 34;
6252 }
6253
6254 return 0;
6255}
6256
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006257
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006258
6259
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006260/*
6261A sequence with
6262 A wrapping bstr
6263 containing a map
6264 1
6265 2
6266 A wrapping bstr
6267 containing an array
6268 3
6269 wrapping bstr
6270 4
6271 5
6272 6
6273 array
6274 7
6275 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006276 */
6277
Laurence Lundblade55013642020-09-23 05:39:22 -07006278static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006279{
Laurence Lundblade55013642020-09-23 05:39:22 -07006280 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006281 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006282 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006283
Laurence Lundblade55013642020-09-23 05:39:22 -07006284 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006285
6286 QCBOREncode_BstrWrap(&EC);
6287 QCBOREncode_OpenMap(&EC);
6288 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6289 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6290 QCBOREncode_CloseMap(&EC);
6291 QCBOREncode_BstrWrap(&EC);
6292 QCBOREncode_OpenArray(&EC);
6293 QCBOREncode_AddInt64(&EC, 3);
6294 QCBOREncode_BstrWrap(&EC);
6295 QCBOREncode_AddInt64(&EC, 4);
6296 QCBOREncode_CloseBstrWrap(&EC, NULL);
6297 QCBOREncode_AddInt64(&EC, 5);
6298 QCBOREncode_CloseArray(&EC);
6299 QCBOREncode_CloseBstrWrap(&EC, NULL);
6300 QCBOREncode_AddInt64(&EC, 6);
6301 QCBOREncode_CloseBstrWrap(&EC, NULL);
6302 QCBOREncode_OpenArray(&EC);
6303 QCBOREncode_AddInt64(&EC, 7);
6304 QCBOREncode_AddInt64(&EC, 8);
6305 QCBOREncode_CloseArray(&EC);
6306
6307 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006308 if(uErr) {
6309 Encoded = NULLUsefulBufC;
6310 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006311
6312 return Encoded;
6313}
6314
6315
6316int32_t EnterBstrTest()
6317{
Laurence Lundblade55013642020-09-23 05:39:22 -07006318 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006319
6320 QCBORDecodeContext DC;
6321
Laurence Lundblade55013642020-09-23 05:39:22 -07006322 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006323
Laurence Lundblade55013642020-09-23 05:39:22 -07006324 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006325
6326
Laurence Lundblade9b334962020-08-27 10:55:53 -07006327 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006328 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006329 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6330 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006331 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006332 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006333 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006334 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006335 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006336 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006337 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006338 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006339 QCBORDecode_ExitArray(&DC);
6340 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006341 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006342 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006343 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006344 QCBORDecode_GetInt64(&DC, &n7);
6345 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006346 QCBORDecode_ExitArray(&DC);
6347
6348 QCBORError uErr = QCBORDecode_Finish(&DC);
6349
6350 return (int32_t)uErr;
6351}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006352
6353
6354
6355
6356static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006357 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006358
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006359 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006360 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006361 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6362 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6363 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006364
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006365 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006366 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6367 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6368 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006369
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006370 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006371 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006372 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6373 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006374
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006375 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006376 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6377 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006378
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006379 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006380 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006381 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6382 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006383
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006384 21,
6385 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6386 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6387
6388 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006389 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006390 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6391 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006392
6393 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006394 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6395 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006396
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006397 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006398 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006399 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6400 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006401
6402 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006403 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6404 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006405
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006406 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006407 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006408 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6409 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006410
6411 0x18, 0x33,
6412 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6413
6414 // MIME
6415 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006416 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6417 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6418 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006419
6420 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006421 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6422 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006423
6424 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006425 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6426 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6427 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006428
6429 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006430 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6431 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006432
6433 // UUID
6434 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006435 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6436 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006437
6438 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006439 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6440 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006441};
6442
6443int32_t DecodeTaggedTypeTests()
6444{
6445 QCBORDecodeContext DC;
6446 QCBORError uErr;
6447
6448 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6449
6450 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006451 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006452
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006453 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006454 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006455 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006456 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6457 return 1;
6458 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006459 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006460 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6461 return 2;
6462 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006463 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006464 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6465 return 3;
6466 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006467 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006468 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006469 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6470 return 4;
6471 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006472 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006473 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006474 return 5;
6475 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006476
Laurence Lundblade9b334962020-08-27 10:55:53 -07006477 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006478 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6479 bNeg != false) {
6480 return 10;
6481 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006482 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006483 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6484 bNeg != true) {
6485 return 11;
6486 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006487 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006488 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6489 return 12;
6490 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006491 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006492 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006493 return 13;
6494 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006495 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006496 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006497 return 14;
6498 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006499
Laurence Lundblade9b334962020-08-27 10:55:53 -07006500 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006501 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6502 return 20;
6503 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006504 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006505 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6506 return 21;
6507 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006508 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006509 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006510 return 22;
6511 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006512 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006513 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006514 return 23;
6515 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006516
Laurence Lundblade9b334962020-08-27 10:55:53 -07006517 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006518 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6519 return 30;
6520 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006521 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006522 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6523 return 31;
6524 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006525 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006526 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006527 return 32;
6528 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006529 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006530 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006531 return 33;
6532 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006533
Laurence Lundblade9b334962020-08-27 10:55:53 -07006534 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006535 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6536 return 40;
6537 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006538 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006539 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6540 return 41;
6541 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006542 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006543 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006544 return 42;
6545 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006546 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006547 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006548 return 43;
6549 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006550
Laurence Lundblade9b334962020-08-27 10:55:53 -07006551 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006552 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6553 return 50;
6554 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006555 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006556 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6557 return 51;
6558 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006559 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006560 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006561 return 52;
6562 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006563 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006564 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006565 return 53;
6566 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006567
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006568 // MIME
6569 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006570 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006571 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6572 bIsNot7Bit == true) {
6573 return 60;
6574 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006575 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006576 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6577 bIsNot7Bit == true) {
6578 return 61;
6579 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006580 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006581 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6582 bIsNot7Bit == false) {
6583 return 62;
6584 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006585 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006586 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6587 bIsNot7Bit == false) {
6588 return 63;
6589 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006590 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006591 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006592 return 64;
6593 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006594 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006595 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006596 return 65;
6597 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006598
Laurence Lundblade9b334962020-08-27 10:55:53 -07006599 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006600 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6601 return 70;
6602 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006603 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006604 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6605 return 71;
6606 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006607 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006608 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006609 return 72;
6610 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006611 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006612 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006613 return 73;
6614 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006615
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006616 // Improvement: add some more error test cases
6617
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006618 QCBORDecode_ExitMap(&DC);
6619
6620 uErr = QCBORDecode_Finish(&DC);
6621 if(uErr != QCBOR_SUCCESS) {
6622 return 100;
6623 }
6624
6625 return 0;
6626}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006627
6628
6629
6630
6631/*
6632 [
6633 "aaaaaaaaaa",
6634 {}
6635 ]
6636 */
6637static const uint8_t spTooLarge1[] = {
6638 0x9f,
6639 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6640 0xa0,
6641 0xff
6642};
6643
6644/*
6645 [
6646 {
6647 0: "aaaaaaaaaa"
6648 }
6649 ]
6650 */
6651static const uint8_t spTooLarge2[] = {
6652 0x9f,
6653 0xa1,
6654 0x00,
6655 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6656 0xff
6657};
6658
6659/*
6660 h'A1006A61616161616161616161'
6661
6662 {
6663 0: "aaaaaaaaaa"
6664 }
6665 */
6666static const uint8_t spTooLarge3[] = {
6667 0x4d,
6668 0xa1,
6669 0x00,
6670 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6671};
6672
6673int32_t TooLargeInputTest(void)
6674{
6675 QCBORDecodeContext DC;
6676 QCBORError uErr;
6677 UsefulBufC String;
6678
6679 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6680 // to 10 There's not really any way to test this error
6681 // condition. The error condition is not complex, so setting
6682 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6683
6684 // The input CBOR is only too large because the
6685 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6686 //
6687 // This test is disabled for the normal test runs because of the
6688 // special build requirement.
6689
6690
6691 // Tests the start of a map being too large
6692 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006693 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006694 QCBORDecode_GetTextString(&DC, &String);
6695 uErr = QCBORDecode_GetError(&DC);
6696 if(uErr != QCBOR_SUCCESS) {
6697 return 1;
6698 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006699 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006700 uErr = QCBORDecode_GetError(&DC);
6701 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6702 return 2;
6703 }
6704
6705 // Tests the end of a map being too large
6706 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006707 QCBORDecode_EnterArray(&DC, NULL);
6708 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006709 uErr = QCBORDecode_GetError(&DC);
6710 if(uErr != QCBOR_SUCCESS) {
6711 return 3;
6712 }
6713 QCBORDecode_ExitMap(&DC);
6714 uErr = QCBORDecode_GetError(&DC);
6715 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6716 return 4;
6717 }
6718
6719 // Tests the entire input CBOR being too large when processing bstr wrapping
6720 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6721 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6722 uErr = QCBORDecode_GetError(&DC);
6723 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6724 return 5;
6725 }
6726
6727 return 0;
6728}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006729
6730
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006731#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6732
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006733static const uint8_t spMapWithIndefLenStrings[] = {
6734 0xbf,
6735 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6736 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6737 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6738 0x03,
6739 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6740 0xc3,
6741 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6742 0xff
6743};
6744
6745int32_t SpiffyIndefiniteLengthStringsTests()
6746{
6747 QCBORDecodeContext DCtx;
6748
6749 QCBORDecode_Init(&DCtx,
6750 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6751 QCBOR_DECODE_MODE_NORMAL);
6752
6753 MakeUsefulBufOnStack(StringBuf, 200);
6754 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6755
6756 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006757 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006758 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6759 if(QCBORDecode_GetAndResetError(&DCtx)) {
6760 return 1;
6761 }
6762
6763 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6764 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6765 return 2;
6766 }
6767
6768 uint64_t uInt;
6769 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6770 if(QCBORDecode_GetAndResetError(&DCtx)) {
6771 return 3;
6772 }
6773 if(uInt != 3) {
6774 return 4;
6775 }
6776
6777 double uDouble;
6778 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6779 "label2",
6780 0xff,
6781 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006782#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006783 if(QCBORDecode_GetAndResetError(&DCtx)) {
6784 return 5;
6785 }
6786 if(uDouble != -16777474) {
6787 return 6;
6788 }
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006789#else
6790 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6791 return 7;
6792 }
6793#endif
6794
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006795
6796 QCBORDecode_ExitMap(&DCtx);
6797
6798 if(QCBORDecode_Finish(&DCtx)) {
6799 return 99;
6800 }
6801
6802 return 0;
6803}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006804#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08006805
6806
6807
6808int32_t PeekTest()
6809{
6810 QCBORItem Item;
6811 QCBORError nCBORError;
6812 QCBORDecodeContext DCtx;
6813
6814 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6815
6816 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6817 return 100+(int32_t)nCBORError;
6818 }
6819 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6820 return 200;
6821 }
6822
6823 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6824 return (int32_t)nCBORError;
6825 }
6826 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6827 return 300;
6828 }
6829
6830 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6831 return 400 + (int32_t)nCBORError;
6832 }
6833 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6834 return 500;
6835 }
6836
6837 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6838 return (int32_t)nCBORError;
6839 }
6840 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6841 return 600;
6842 }
6843
6844 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6845 return 900 + (int32_t)nCBORError;
6846 }
6847 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6848 Item.uDataType != QCBOR_TYPE_INT64 ||
6849 Item.val.int64 != 42 ||
6850 Item.uDataAlloc ||
6851 Item.uLabelAlloc ||
6852 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6853 return 1000;
6854 }
6855
6856 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6857 return 1100 + (int32_t)nCBORError;
6858 }
6859
6860 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6861 Item.uDataType != QCBOR_TYPE_INT64 ||
6862 Item.val.int64 != 42 ||
6863 Item.uDataAlloc ||
6864 Item.uLabelAlloc ||
6865 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6866 return 1200;
6867 }
6868
6869
6870 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6871 return 1300 + (int32_t)nCBORError;
6872 }
6873 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6874 Item.uDataAlloc ||
6875 Item.uLabelAlloc ||
6876 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
6877 Item.uDataType != QCBOR_TYPE_ARRAY ||
6878 Item.val.uCount != 2)
6879 return 1400;
6880
6881 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6882 return 1500 + (int32_t)nCBORError;
6883 }
6884 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6885 Item.uDataAlloc ||
6886 Item.uLabelAlloc ||
6887 UsefulBufCompareToSZ(Item.val.string, "string1")) {
6888 return 1600;
6889 }
6890
6891 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6892 return 1700 + (int32_t)nCBORError;
6893 }
6894 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6895 Item.uDataAlloc ||
6896 Item.uLabelAlloc ||
6897 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6898 return 1800;
6899 }
6900
6901 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6902 return (int32_t)nCBORError;
6903 }
6904 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6905 Item.uDataAlloc ||
6906 Item.uLabelAlloc ||
6907 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6908 return 1900;
6909 }
6910
6911 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6912 return (int32_t)nCBORError;
6913 }
6914 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6915 Item.uDataAlloc ||
6916 Item.uLabelAlloc ||
6917 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6918 return 2000;
6919 }
6920
6921
6922 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6923 return 2100 + (int32_t)nCBORError;
6924 }
6925 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6926 Item.uDataAlloc ||
6927 Item.uLabelAlloc ||
6928 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
6929 Item.uDataType != QCBOR_TYPE_MAP ||
6930 Item.val.uCount != 4) {
6931 return 2100;
6932 }
6933
6934 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6935 return 2200 + (int32_t)nCBORError;
6936 }
6937 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6938 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
6939 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6940 Item.uDataAlloc ||
6941 Item.uLabelAlloc ||
6942 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
6943 return 2300;
6944 }
6945
6946 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6947 return 2400 + (int32_t)nCBORError;
6948 }
6949 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6950 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6951 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6952 Item.uDataAlloc ||
6953 Item.uLabelAlloc ||
6954 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6955 return 2500;
6956 }
6957
6958 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6959 return 2600 + (int32_t)nCBORError;
6960 }
6961 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6962 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6963 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6964 Item.uDataAlloc ||
6965 Item.uLabelAlloc ||
6966 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6967 return 2700;
6968 }
6969
6970 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6971 return 2800 + (int32_t)nCBORError;
6972 }
6973 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6974 Item.uDataAlloc ||
6975 Item.uLabelAlloc ||
6976 UsefulBufCompareToSZ(Item.label.string, "another int") ||
6977 Item.uDataType != QCBOR_TYPE_INT64 ||
6978 Item.val.int64 != 98)
6979 return 2900;
6980
6981 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6982 return 3000 + (int32_t)nCBORError;
6983 }
6984 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6985 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
6986 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6987 Item.uDataAlloc ||
6988 Item.uLabelAlloc ||
6989 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
6990 return 3100;
6991 }
6992
6993 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6994 return 3200 + (int32_t)nCBORError;
6995 }
6996 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6997 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
6998 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6999 Item.uDataAlloc ||
7000 Item.uLabelAlloc ||
7001 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7002 return 3300;
7003 }
7004
7005 return 0;
7006}