blob: f52484946f64a9d7383b3ceb7de811c31c76c78b [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 Lundblade4bf6e712020-12-10 11:53:21 -08001666 /* Return index of failure and QCBOR error in the result */
1667 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001668 }
1669 }
1670 return 0;
1671}
1672
1673
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001674// TODO: add a test index and report it so it is eaier to figure out which test failed.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001675struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001676 UsefulBufC Input;
1677 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001678};
1679
Laurence Lundblade59289e52019-12-30 13:44:37 -08001680
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001681static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001682{
1683 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001684 QCBORDecodeContext DCtx;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001685 QCBORError uCBORError;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001686
Laurence Lundblade59289e52019-12-30 13:44:37 -08001687 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001688
1689#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001690 // Set up the decoding context including a memory pool so that
1691 // indefinite length items can be checked
Laurence Lundblade59289e52019-12-30 13:44:37 -08001692 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001693
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001694 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1695 if(uCBORError) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001696 return -9;
1697 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001698#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1699
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001700
Laurence Lundblade59289e52019-12-30 13:44:37 -08001701 // Iterate until there is an error of some sort error
1702 QCBORItem Item;
1703 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001704 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001705 memset(&Item, 0x33, sizeof(Item));
1706
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001707 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1708 } while(uCBORError == QCBOR_SUCCESS);
1709
1710
Laurence Lundblade59289e52019-12-30 13:44:37 -08001711
1712 // Must get the expected error or the this test fails
1713 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001714 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001715 Item.uDataType != QCBOR_TYPE_NONE ||
1716 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001717 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001718 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001719 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001720 }
1721 }
1722
1723 return 0;
1724}
1725
1726
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001727struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001728 // Most of this is copied from not_well_formed.h. Here the error code
1729 // returned is also checked.
1730
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001731#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001732 // Indefinite length strings must be closed off
1733 // An indefinite length byte string not closed off
1734 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1735 // An indefinite length text string not closed off
1736 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1737
1738
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001739 // All the chunks in an indefinite length string must be of the type of
1740 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001741 // indefinite length byte string with text string chunk
1742 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1743 // indefinite length text string with a byte string chunk
1744 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1745 // indefinite length byte string with an positive integer chunk
1746 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1747 // indefinite length byte string with an negative integer chunk
1748 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1749 // indefinite length byte string with an array chunk
1750 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1751 // indefinite length byte string with an map chunk
1752 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1753 // indefinite length byte string with tagged integer chunk
1754 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1755 // indefinite length byte string with an simple type chunk
1756 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1757 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1758 // indefinite length text string with indefinite string inside
1759 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1760
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001761#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1762
1763 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1764 // An indefinite length text string not closed off
1765 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1766
1767
1768 // All the chunks in an indefinite length string must be of the type of
1769 // indefinite length string
1770 // indefinite length byte string with text string chunk
1771 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1772 // indefinite length text string with a byte string chunk
1773 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1774 // indefinite length byte string with an positive integer chunk
1775 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1776 // indefinite length byte string with an negative integer chunk
1777 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1778 // indefinite length byte string with an array chunk
1779 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1780 // indefinite length byte string with an map chunk
1781 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1782 // indefinite length byte string with tagged integer chunk
1783 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1784 // indefinite length byte string with an simple type chunk
1785 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1786 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1787 // indefinite length text string with indefinite string inside
1788 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1789#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1790
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001791
1792 // Definte length maps and arrays must be closed by having the right number of items
1793 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001794 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001795 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001796 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001797 // A definte length array that is supposed to have 511 items, but has only 1
1798 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1799 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001800 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001801 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001802 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001803
1804
1805 // Indefinte length maps and arrays must be ended by a break
1806 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001807 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001808 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001809 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001810 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001811 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001812 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001813 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001814
1815
1816 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001817 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001818 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001819 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001820 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001821 // Unclosed indefinite map containing a closed definite length array
1822 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1823 // Definite length map containing an unclosed indefinite length array
1824 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001825 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001826 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001827 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001828 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001829 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001830 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001831 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001832 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001833 // Unclosed indefinite length map in definite length maps
1834 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1835 QCBOR_ERR_NO_MORE_ITEMS},
1836 // Unclosed definite length map in indefinite length maps
1837 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1838 // Unclosed indefinite length array in definite length maps
1839 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1840 QCBOR_ERR_NO_MORE_ITEMS},
1841 // Unclosed definite length array in indefinite length maps
1842 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1843 // Unclosed indefinite length map in definite length arrays
1844 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1845 // Unclosed definite length map in indefinite length arrays
1846 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001847
1848
1849 // The "argument" for the data item is incomplete
1850 // Positive integer missing 1 byte argument
1851 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1852 // Positive integer missing 2 byte argument
1853 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1854 // Positive integer missing 4 byte argument
1855 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1856 // Positive integer missing 8 byte argument
1857 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1858 // Positive integer missing 1 byte of 2 byte argument
1859 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1860 // Positive integer missing 2 bytes of 4 byte argument
1861 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1862 // Positive integer missing 1 bytes of 7 byte argument
1863 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1864 // Negative integer missing 1 byte argument
1865 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1866 // Binary string missing 1 byte argument
1867 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1868 // Text string missing 1 byte argument
1869 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1870 // Array missing 1 byte argument
1871 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1872 // Map missing 1 byte argument
1873 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1874 // Tag missing 1 byte argument
1875 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1876 // Simple missing 1 byte argument
1877 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001878 // half-precision with 1 byte argument
1879 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1880 // single-precision with 2 byte argument
1881 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1882 // double-precision with 3 byte argument
1883 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1884
1885
1886 // Tag with no content
1887 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001888
1889
1890 // Breaks must not occur in definite length arrays and maps
1891 // Array of length 1 with sole member replaced by a break
1892 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1893 // Array of length 2 with 2nd member replaced by a break
1894 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1895 // Map of length 1 with sole member label replaced by a break
1896 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1897 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001898 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001899 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1900 // Array of length 1 with 2nd member value replaced by a break
1901 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1902 // Map of length 2 with 2nd member replaced by a break
1903 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1904
1905
1906 // Breaks must not occur on their own out of an indefinite length data item
1907 // A bare break is not well formed
1908 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1909 // A bare break after a zero length definite length array
1910 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1911 // A bare break after a zero length indefinite length map
1912 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001913 // A break inside a definite length array inside an indefenite length array
1914 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1915 // Complicated mixed nesting with break outside indefinite length array
1916 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001917
1918
1919 // Forbidden two byte encodings of simple types
1920 // Must use 0xe0 instead
1921 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1922 // Should use 0xe1 instead
1923 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1924 // Should use 0xe2 instead
1925 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1926 // Should use 0xe3 instead
1927 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1928 // Should use 0xe4 instead
1929 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1930 // Should use 0xe5 instead
1931 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1932 // Should use 0xe6 instead
1933 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1934 // Should use 0xe7 instead
1935 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1936 // Should use 0xe8 instead
1937 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1938 // Should use 0xe9 instead
1939 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1940 // Should use 0xea instead
1941 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1942 // Should use 0xeb instead
1943 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1944 // Should use 0xec instead
1945 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1946 // Should use 0xed instead
1947 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1948 // Should use 0xee instead
1949 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1950 // Should use 0xef instead
1951 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1952 // Should use 0xf0 instead
1953 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1954 // Should use 0xf1 instead
1955 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1956 // Should use 0xf2 instead
1957 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1958 // Must use 0xf3 instead
1959 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1960 // Must use 0xf4 instead
1961 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1962 // Must use 0xf5 instead
1963 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1964 // Must use 0xf6 instead
1965 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1966 // Must use 0xf7 instead
1967 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1968 // Must use 0xf8 instead
1969 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001970 // Reserved
1971 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001972
1973 // Integers with additional info indefinite length
1974 // Positive integer with additional info indefinite length
1975 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1976 // Negative integer with additional info indefinite length
1977 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1978 // CBOR tag with "argument" an indefinite length
1979 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1980 // CBOR tag with "argument" an indefinite length alternate vector
1981 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1982
1983
1984 // Missing bytes from a deterministic length string
1985 // A byte string is of length 1 without the 1 byte
1986 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1987 // A text string is of length 1 without the 1 byte
1988 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001989
1990#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08001991 // Byte string should have 2^32-15 bytes, but has one
1992 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1993 // Byte string should have 2^32-15 bytes, but has one
1994 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001995 // Byte string should have 2^64 bytes, but has 3
1996 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1997 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
1998 // Text string should have 2^64 bytes, but has 3
1999 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2000 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002001#else
2002 // Byte string should have 2^32-15 bytes, but has one
2003 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2004 // Byte string should have 2^32-15 bytes, but has one
2005 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2006 // Byte string should have 2^16 bytes, but has 3
2007 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2008 // Text string should have 2^64 bytes, but has 3
2009 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2010#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002011
2012 // Use of unassigned additional information values
2013 // Major type positive integer with reserved value 28
2014 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2015 // Major type positive integer with reserved value 29
2016 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2017 // Major type positive integer with reserved value 30
2018 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2019 // Major type negative integer with reserved value 28
2020 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2021 // Major type negative integer with reserved value 29
2022 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2023 // Major type negative integer with reserved value 30
2024 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2025 // Major type byte string with reserved value 28 length
2026 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2027 // Major type byte string with reserved value 29 length
2028 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2029 // Major type byte string with reserved value 30 length
2030 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2031 // Major type text string with reserved value 28 length
2032 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2033 // Major type text string with reserved value 29 length
2034 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2035 // Major type text string with reserved value 30 length
2036 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2037 // Major type array with reserved value 28 length
2038 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2039 // Major type array with reserved value 29 length
2040 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2041 // Major type array with reserved value 30 length
2042 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2043 // Major type map with reserved value 28 length
2044 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2045 // Major type map with reserved value 29 length
2046 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2047 // Major type map with reserved value 30 length
2048 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2049 // Major type tag with reserved value 28 length
2050 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2051 // Major type tag with reserved value 29 length
2052 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2053 // Major type tag with reserved value 30 length
2054 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2055 // Major type simple with reserved value 28 length
2056 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2057 // Major type simple with reserved value 29 length
2058 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2059 // Major type simple with reserved value 30 length
2060 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2061
2062
2063 // Maps must have an even number of data items (key & value)
2064 // Map with 1 item when it should have 2
2065 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2066 // Map with 3 item when it should have 4
2067 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
2068 // Map with 1 item when it should have 2
2069 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2070 // Map with 3 item when it should have 4
2071 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
2072
2073
2074 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002075 // Text-based date, with an integer
2076 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2077 // Epoch date, with an byte string
2078 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2079 // tagged as both epoch and string dates
2080 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2081 // big num tagged an int, not a byte string
2082 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002083};
2084
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002085int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002086{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002087 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002088
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002089 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002090 if(nResult) {
2091 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002093
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002094 // Corrupt the UsefulInputBuf and see that
2095 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002096 QCBORDecodeContext DCtx;
2097 QCBORItem Item;
2098 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002099
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002100 QCBORDecode_Init(&DCtx,
2101 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2102 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002103
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002104 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2105 return (int32_t)uQCBORError;
2106 }
2107 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2108 // This wasn't supposed to happen
2109 return -1;
2110 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002111
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002112 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002113
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002114 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2115 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2116 // Did not get back the error expected
2117 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002119
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002120
Laurence Lundblade98427e92020-09-28 21:33:23 -07002121 /*
2122 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2123 tests here can be performed to see that the max length
2124 error check works correctly. See DecodeBytes(). If the max
2125 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002126
Laurence Lundblade98427e92020-09-28 21:33:23 -07002127 This test will automatocally adapt the all CPU sizes
2128 through the use of SIZE_MAX.
2129 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002130
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002131 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002132 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002133
Laurence Lundblade98427e92020-09-28 21:33:23 -07002134 // This makes a CBOR head with a text string that is very long
2135 // but doesn't fill in the bytes of the text string as that is
2136 // not needed to test this part of QCBOR.
2137 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2138
2139 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2140
2141 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2142 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002143 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002144
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002145 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002146}
2147
2148
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002149/* Try all 256 values of the byte at nLen including recursing for
2150 each of the values to try values at nLen+1 ... up to nLenMax
2151 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002152static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002153{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002154 if(nLen >= nLenMax) {
2155 return;
2156 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002157
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002158 for(int inputByte = 0; inputByte < 256; inputByte++) {
2159 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002160 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002161 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002162
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002163 // Get ready to parse
2164 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002165 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002166
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002167 // Parse by getting the next item until an error occurs
2168 // Just about every possible decoder error can occur here
2169 // The goal of this test is not to check for the correct
2170 // error since that is not really possible. It is to
2171 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002172 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002173 QCBORItem Item;
2174 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002175 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002176 break;
2177 }
2178 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002179
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002180 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002181 }
2182}
2183
2184
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002185int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002186{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002187 // Size 2 tests 64K inputs and runs quickly
2188 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002189
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002190 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002191
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002192 return 0;
2193}
2194
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002195
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002196int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002197{
2198 // size 3 tests 16 million inputs and runs OK
2199 // in seconds on fast machines. Size 4 takes
2200 // 10+ minutes and 5 half a day on fast
2201 // machines. This test is kept separate from
2202 // the others so as to no slow down the use
2203 // of them as a very frequent regression.
2204 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002205
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002206 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002207
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002208 return 0;
2209}
2210
2211
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002212static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 0xc0, // tag for string date
2214 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002215
Laurence Lundbladec7114722020-08-13 05:11:40 -07002216 0xc0, // tag for string date
2217 0x00, // Wrong type for a string date
2218
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002219 0xc1, // tag for epoch date
2220 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2221
Laurence Lundbladec7114722020-08-13 05:11:40 -07002222 0xc1,
2223 0x62, 'h', 'i', // wrong type tagged
2224
Laurence Lundblade99615302020-11-29 11:19:47 -08002225 // CBOR_TAG_ENC_AS_B64
2226 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227 0x1a, 0x53, 0x72, 0x4E, 0x01,
2228
2229 0xc1, // tag for epoch date
2230 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002232 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002233 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002234
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002235 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002236 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002237
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002238 0xc1, // tag for epoch date
2239 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2240 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2241
2242 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002243 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2244
2245 0xc1, // tag for epoch date
2246 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2247
2248 0xc1,
2249 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2250
2251 0xc1, // tag for epoch date
2252 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002253};
2254
2255
Laurence Lundbladec7114722020-08-13 05:11:40 -07002256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002257// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002258#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002259static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002261 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002262
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002263 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002264
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002265 return diff > 0.0000001;
2266}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002267#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002268
2269
Laurence Lundblade99615302020-11-29 11:19:47 -08002270
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002271int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002272{
2273 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002274 QCBORItem Item;
2275 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002276
Laurence Lundbladeee851742020-01-08 08:37:05 -08002277 QCBORDecode_Init(&DCtx,
2278 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2279 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002281 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002282 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002283 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002284 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002285 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002286 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002287 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002288 }
2289
Laurence Lundbladec7114722020-08-13 05:11:40 -07002290 // Wrong type for a string date
2291 uError = QCBORDecode_GetNext(&DCtx, &Item);
2292 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002293 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002294 }
2295
2296 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2297 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2298 return -4;
2299 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002300 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2301 Item.val.epochDate.nSeconds != 1400000000 ||
2302 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002303 return -5;
2304 }
2305
2306 // Wrong type for an epoch date
2307 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2308 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002310
Laurence Lundblade99615302020-11-29 11:19:47 -08002311 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2312 // The date is decoded and the two tags are returned. This is to
2313 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002314 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2315 return -7;
2316 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002317 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2318 Item.val.epochDate.nSeconds != 1400000001 ||
2319 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundblade99615302020-11-29 11:19:47 -08002320 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002321 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002322 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002323
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002324 // Epoch date that is too large for our representation
2325 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002326 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002327 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002328
Laurence Lundblade9682a532020-06-06 18:33:04 -07002329#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002330 // Epoch date in float format with fractional seconds
2331 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2332 return -10;
2333 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002334 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2335 Item.val.epochDate.nSeconds != 1 ||
2336 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002337 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002338 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002340 // Epoch date float that is too large for our representation
2341 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002342 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002344
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002345 // Epoch date double that is just slightly too large
2346 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002347 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002348 }
2349
2350 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002351 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2352 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2353 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2354 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002355 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002356 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002357
2358 // Nan
2359 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2360 return -15;
2361 }
2362
2363 // +Inifinity double-precision
2364 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2365 return -16;
2366 }
2367
2368#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2369 // -Inifinity half-precision
2370 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2371 return -17;
2372 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002373#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002374 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2375 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002376 }
2377#endif
2378
Laurence Lundbladec7114722020-08-13 05:11:40 -07002379#else
2380 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2381 return -19;
2382 }
2383 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2384 return -20;
2385 }
2386 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2387 return -21;
2388 }
2389 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2390 return -22;
2391 }
2392 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2393 return -23;
2394 }
2395 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2396 return -24;
2397 }
2398#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2399 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2400 return -25;
2401 }
2402#else
2403 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2404 return -26;
2405 }
2406#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002407
Laurence Lundbladec7114722020-08-13 05:11:40 -07002408#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002409
2410 return 0;
2411}
2412
Laurence Lundblade4b270642020-08-14 12:53:07 -07002413/*
2414 Test cases covered here. Some items cover more than one of these.
2415 positive integer (zero counts as a positive integer)
2416 negative integer
2417 half-precision float
2418 single-precision float
2419 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002420
Laurence Lundblade4b270642020-08-14 12:53:07 -07002421 float Overflow error
2422 Wrong type error for epoch
2423 Wrong type error for date string
2424 float disabled error
2425 half-precision disabled error
2426 -Infinity
2427 Slightly too large integer
2428 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002429
Laurence Lundblade4b270642020-08-14 12:53:07 -07002430 Get epoch by int
2431 Get string by int
2432 Get epoch by string
2433 Get string by string
2434 Fail to get epoch by wrong int label
2435 Fail to get string by wrong string label
2436 Fail to get epoch by string because it is invalid
2437 Fail to get epoch by int because it is invalid
2438
2439 Untagged values
2440 */
2441static uint8_t spSpiffyDateTestInput[] = {
2442 0x86,
2443
2444 0xc1,
2445 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2446
Laurence Lundbladec7114722020-08-13 05:11:40 -07002447 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002448 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2449
2450 0xc1, // tag for epoch date
2451 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2452
2453 0xc1, // tag for epoch date
2454 0x9f, 0xff, // Erroneous empty array as content for date
2455
2456 0xc0, // tag for string date
2457 0xbf, 0xff, // Erroneous empty map as content for date
2458
2459 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002460
2461 0x00,
2462 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002463 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002464
2465 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002466 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002467 0xc1, // tag for epoch date
2468 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2469
2470 // Untagged integer 0
2471 0x08,
2472 0x00,
2473
2474 // Utagged date string with string label y
2475 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002476 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002477
2478 // Untagged -1000 with label z
2479 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002480 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002481 0x39, 0x03, 0xe7,
2482
Laurence Lundbladec7114722020-08-13 05:11:40 -07002483 0x07,
2484 0xc1, // tag for epoch date
2485 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2486
Laurence Lundblade4b270642020-08-14 12:53:07 -07002487 0x05,
2488 0xc1,
2489 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2490
Laurence Lundbladec7114722020-08-13 05:11:40 -07002491 // Untagged single-precision float with value 3.14 with string label x
2492 0x61, 0x78,
2493 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2494
Laurence Lundbladec7114722020-08-13 05:11:40 -07002495 // Untagged half-precision float with value -2
2496 0x09,
2497 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002498
2499 0xff,
2500};
2501
2502int32_t SpiffyDateDecodeTest()
2503{
2504 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002505 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002506 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2507 nEpochDate4, nEpochDate6, nEpochDateFail,
2508 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002509 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002510 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002511
2512 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002513 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002514 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002515 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002516
Laurence Lundblade9b334962020-08-27 10:55:53 -07002517 // Too-negative float, -9.2233720368547748E+18
2518 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002519 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002520#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002521 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2522 return 1111;
2523 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002524#else
2525 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2526 return 1112;
2527 }
2528#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002529
2530 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002531 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002532 uError = QCBORDecode_GetAndResetError(&DC);
2533 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002534 return 1;
2535 }
2536
Laurence Lundblade4b270642020-08-14 12:53:07 -07002537 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002538 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002539 uError = QCBORDecode_GetAndResetError(&DC);
2540#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2541#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2542 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2543#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2544 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2545#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2546#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2547 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2548#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2549 if(uError != uExpectedforHalfMinusInfinity) {
2550 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002551 }
2552
Laurence Lundblade4b270642020-08-14 12:53:07 -07002553 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002554 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002555 uError = QCBORDecode_GetAndResetError(&DC);
2556 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2557 return 3;
2558 }
2559
2560 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002561 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002562 uError = QCBORDecode_GetAndResetError(&DC);
2563 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2564 return 4;
2565 }
2566
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002567 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002568
2569 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002570 QCBORDecode_GetEpochDateInMapN(&DC,
2571 5,
2572 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2573 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2574 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002575#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2576 if(nEpochDate2 != -9223372036854773760LL) {
2577 return 101;
2578 }
2579#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2580 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002581 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002582 return 102;
2583 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002584#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002585
Laurence Lundblade4b270642020-08-14 12:53:07 -07002586 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002587 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2588 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002589#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2590 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002591 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002592 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002593#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2594 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002595 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002596 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002597 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002598#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2599
2600 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002601 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2602 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002603#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2604 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002605 return 103;
2606 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002607#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2608 uError = QCBORDecode_GetAndResetError(&DC);
2609 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2610 return 104;
2611 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002612#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2613
Laurence Lundblade9b334962020-08-27 10:55:53 -07002614 // A half-precision date with value -2 FFF
2615 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2616 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002617#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2618 if(nEpochDate4 != -2) {
2619 return 105;
2620 }
2621#else
2622 uError = QCBORDecode_GetAndResetError(&DC);
2623 if(uError == QCBOR_SUCCESS) {
2624 return 106;
2625 }
2626#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002627
Laurence Lundblade4b270642020-08-14 12:53:07 -07002628
2629 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002630 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2631 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2632 &nEpochDate6);
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 107;
2636 }
2637
2638 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002639 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2640 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002641 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002642 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002643 return 108;
2644 }
2645
2646 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002647 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2648 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2649 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002650 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002651 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002652 return 109;
2653 }
2654
2655 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002656 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2657 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002658 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002659 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002660 return 110;
2661 }
2662
2663 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002664
Laurence Lundblade4b270642020-08-14 12:53:07 -07002665 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002666 QCBORDecode_GetEpochDateInMapN(&DC,
2667 1,
2668 QCBOR_TAG_REQUIREMENT_TAG |
2669 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2670 &nEpochDate1400000000);
2671 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002672 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002673 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2674 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002675 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002676 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2677 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002678 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002679 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2680 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002681 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002682 QCBORDecode_GetEpochDateInMapSZ(&DC,
2683 "z",
2684 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2685 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2686 &nEpochDate6);
2687 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002688
2689 QCBORDecode_ExitMap(&DC);
2690 QCBORDecode_ExitArray(&DC);
2691 uError = QCBORDecode_Finish(&DC);
2692 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002693 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002694 }
2695
Laurence Lundblade9b334962020-08-27 10:55:53 -07002696 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002697 return 200;
2698 }
2699
Laurence Lundblade9b334962020-08-27 10:55:53 -07002700 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002701 return 201;
2702 }
2703
Laurence Lundblade9b334962020-08-27 10:55:53 -07002704 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002705 return 202;
2706 }
2707
Laurence Lundblade9b334962020-08-27 10:55:53 -07002708 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002709 return 203;
2710 }
2711
Laurence Lundblade9b334962020-08-27 10:55:53 -07002712 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002713 return 204;
2714 }
2715
Laurence Lundblade9b334962020-08-27 10:55:53 -07002716 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2717 return 205;
2718 }
2719
2720 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2721 return 206;
2722 }
2723
Laurence Lundbladec7114722020-08-13 05:11:40 -07002724 return 0;
2725}
2726
2727
2728
Laurence Lundblade9b334962020-08-27 10:55:53 -07002729// Input for one of the tagging tests
2730static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002731 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002732 0x81, // Array of one
2733 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2734 0x82, // Array of two that is the faction 1/3
2735 0x01,
2736 0x03,
2737
2738 /*
2739 More than 4 tags on an item 225(226(227(228(229([])))))
2740 */
2741 0xd8, 0xe1,
2742 0xd8, 0xe2,
2743 0xd8, 0xe3,
2744 0xd8, 0xe4,
2745 0xd8, 0xe5,
2746 0x80,
2747
2748 /* tag 10489608748473423768(
2749 2442302356(
2750 21590(
2751 240(
2752 []))))
2753 */
2754 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2755 0xda, 0x91, 0x92, 0x93, 0x94,
2756 0xd9, 0x54, 0x56,
2757 0xd8, 0xf0,
2758 0x80,
2759
2760 /* tag 21590(
2761 10489608748473423768(
2762 2442302357(
2763 65534(
2764 []))))
2765 */
2766 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2767 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2768 0xda, 0x91, 0x92, 0x93, 0x95,
2769 0xd9, 0xff, 0xfe,
2770 0x80,
2771
2772 /* Make sure to blow past the limit of tags that must be mapped.
2773 works in conjuntion with entries above.
2774 269488144(269488145(269488146(269488147([]))))
2775 */
2776 0xda, 0x10, 0x10, 0x10, 0x10,
2777 0xda, 0x10, 0x10, 0x10, 0x11,
2778 0xda, 0x10, 0x10, 0x10, 0x12,
2779 0xda, 0x10, 0x10, 0x10, 0x13,
2780 0x80,
2781
2782 /* An invalid decimal fraction with an additional tag */
2783 0xd9, 0xff, 0xfa,
2784 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2785 0x00, // the integer 0; should be a byte string
2786};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002787
Laurence Lundblade59289e52019-12-30 13:44:37 -08002788/*
2789 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002790 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002791 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002792static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2793 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002794
Laurence Lundblade59289e52019-12-30 13:44:37 -08002795/*
2796DB 9192939495969798 # tag(10489608748473423768)
2797 D8 88 # tag(136)
2798 C6 # tag(6)
2799 C7 # tag(7)
2800 80 # array(0)
2801*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002802static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2803 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002804
2805/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002806 55799(55799(55799({
2807 6(7(-23)): 5859837686836516696(7({
2808 7(-20): 11({
2809 17(-18): 17(17(17("Organization"))),
2810 9(-17): 773("SSG"),
2811 -15: 16(17(6(7("Confusion")))),
2812 17(-16): 17("San Diego"),
2813 17(-14): 17("US")
2814 }),
2815 23(-19): 19({
2816 -11: 9({
2817 -9: -7
2818 }),
2819 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2820 })
2821 })),
2822 16(-22): 23({
2823 11(8(7(-5))): 8(-3)
2824 })
2825 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002826 */
2827static uint8_t spCSRWithTags[] = {
2828 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2829 0xc6, 0xc7, 0x36,
2830 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2831 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2832 0xcb, 0xa5,
2833 0xd1, 0x31,
2834 0xd1, 0xd1, 0xd1, 0x6c,
2835 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2836 0xc9, 0x30,
2837 0xd9, 0x03, 0x05, 0x63,
2838 0x53, 0x53, 0x47,
2839 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002840 0xd0, 0xd1, 0xc6, 0xc7,
2841 0x69,
2842 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002843 0xd1, 0x2f,
2844 0xd1, 0x69,
2845 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2846 0xd1, 0x2d,
2847 0xd1, 0x62,
2848 0x55, 0x53,
2849 0xd7, 0x32,
2850 0xd3, 0xa2,
2851 0x2a,
2852 0xc9, 0xa1,
2853 0x28,
2854 0x26,
2855 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2856 0xcc, 0x4a,
2857 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2858 0xd0, 0x35,
2859 0xd7, 0xa1,
2860 0xcb, 0xc8, 0xc7, 0x24,
2861 0xc8, 0x22};
2862
Laurence Lundblade9b334962020-08-27 10:55:53 -07002863
2864static uint8_t spSpiffyTagInput[] = {
2865 0x9f, // Open indefinite array
2866
2867 0xc0, // tag for string date
2868 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2869
2870 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2871
2872 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2873
2874 0xd8, 0x23, // tag for regex
2875 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2876
2877 0xc0, // tag for string date
2878 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2879
2880 0xff
2881};
2882
2883
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002884static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002885
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002886
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002887int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002888{
2889 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002890 QCBORItem Item;
2891 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002892
Laurence Lundbladeee851742020-01-08 08:37:05 -08002893 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002894 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002895 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002896
Laurence Lundblade9b334962020-08-27 10:55:53 -07002897 /*
2898 This test matches the magic number tag and the fraction tag
2899 55799([...])
2900 */
2901 uError = QCBORDecode_GetNext(&DCtx, &Item);
2902 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002903 return -2;
2904 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002905 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002906 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2907 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002908 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002909
Laurence Lundblade9b334962020-08-27 10:55:53 -07002910 /*
2911 4([1,3])
2912 */
2913 uError = QCBORDecode_GetNext(&DCtx, &Item);
2914#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2915 if(uError != QCBOR_SUCCESS ||
2916 Item.uDataType != QCBOR_TYPE_ARRAY ||
2917 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2918 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2919 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2920 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2921 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2922 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2923 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002924 return -4;
2925 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002926 // consume the items in the array
2927 uError = QCBORDecode_GetNext(&DCtx, &Item);
2928 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002929
Laurence Lundblade59289e52019-12-30 13:44:37 -08002930#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002931 if(uError != QCBOR_SUCCESS ||
2932 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2933 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2934 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2935 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2936 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2937 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2938 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002939 }
2940#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002941
Laurence Lundblade9b334962020-08-27 10:55:53 -07002942 /*
2943 More than 4 tags on an item 225(226(227(228(229([])))))
2944 */
2945 uError = QCBORDecode_GetNext(&DCtx, &Item);
2946 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002947 return -6;
2948 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002949
Laurence Lundblade88e9db22020-11-02 03:56:33 -08002950 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
2951 return -106;
2952 }
2953
2954
Laurence Lundblade9b334962020-08-27 10:55:53 -07002955 /* tag 10489608748473423768(
2956 2442302356(
2957 21590(
2958 240(
2959 []))))
2960 */
2961 uError = QCBORDecode_GetNext(&DCtx, &Item);
2962 if(uError != QCBOR_SUCCESS ||
2963 Item.uDataType != QCBOR_TYPE_ARRAY ||
2964 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2965 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2966 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2967 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002968 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002969 }
2970
2971 /* tag 21590(
2972 10489608748473423768(
2973 2442302357(
2974 21591(
2975 []))))
2976 */
2977 uError = QCBORDecode_GetNext(&DCtx, &Item);
2978 if(uError != QCBOR_SUCCESS ||
2979 Item.uDataType != QCBOR_TYPE_ARRAY ||
2980 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2981 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2982 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2983 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2984 return -8;
2985 }
2986
2987 /* Make sure to blow past the limit of tags that must be mapped.
2988 works in conjuntion with entries above.
2989 269488144(269488145(269488146(269488147([]))))
2990 */
2991 uError = QCBORDecode_GetNext(&DCtx, &Item);
2992 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2993 return -9;
2994 }
2995
2996 uError = QCBORDecode_GetNext(&DCtx, &Item);
2997 if(uError == QCBOR_SUCCESS) {
2998 return -10;
2999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003000
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003001 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003002 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003003 // tage and then matches it. Caller-config lists are no longer
3004 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003005 QCBORDecode_Init(&DCtx,
3006 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3007 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003008 const uint64_t puList[] = {0x9192939495969798, 257};
3009 const QCBORTagListIn TL = {2, puList};
3010 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003011
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003012 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3013 return -8;
3014 }
3015 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3016 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3017 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3018 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3019 Item.val.uCount != 0) {
3020 return -9;
3021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003022
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003023 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003024 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003025 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003026 const uint64_t puLongList[17] = {1,2,1};
3027 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003028 QCBORDecode_Init(&DCtx,
3029 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3030 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003031 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3032 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3033 return -11;
3034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003035
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003036 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003037 QCBORTagListOut Out = {0, 4, puTags};
3038
3039
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003040 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003041 QCBORDecode_Init(&DCtx,
3042 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3043 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003044 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3045 return -12;
3046 }
3047 if(puTags[0] != 0x9192939495969798 ||
3048 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003049 puTags[2] != 0x06 ||
3050 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003051 return -13;
3052 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003053
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003054 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003055 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003056 QCBORDecode_Init(&DCtx,
3057 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3058 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003059 QCBORTagListOut OutSmall = {0, 3, puTags};
3060 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3061 return -14;
3062 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003063
Laurence Lundblade9b334962020-08-27 10:55:53 -07003064
3065
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003066 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003067 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3068 // It is a bit of a messy test and maybe could be improved, but
3069 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003070 QCBORDecode_Init(&DCtx,
3071 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3072 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003073 int n = CheckCSRMaps(&DCtx);
3074 if(n) {
3075 return n-2000;
3076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003077
Laurence Lundblade59289e52019-12-30 13:44:37 -08003078 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003079 QCBORDecode_Init(&DCtx,
3080 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3081 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003082
Laurence Lundblade9b334962020-08-27 10:55:53 -07003083 /* With the spiffy decode revision, this tag list is not used.
3084 It doesn't matter if a tag is in this list or not so some
3085 tests that couldn't process a tag because it isn't in this list
3086 now can process these unlisted tags. The tests have been
3087 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003088 const uint64_t puTagList[] = {773, 1, 90599561};
3089 const QCBORTagListIn TagList = {3, puTagList};
3090 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003091
3092
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003093 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3094 return -100;
3095 }
3096 if(Item.uDataType != QCBOR_TYPE_MAP ||
3097 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3098 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3099 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3100 Item.val.uCount != 2 ||
3101 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3102 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3103 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3104 Out.uNumUsed != 3) {
3105 return -101;
3106 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003107
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003108 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3109 return -102;
3110 }
3111 if(Item.uDataType != QCBOR_TYPE_MAP ||
3112 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3113 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003114 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003115 Item.val.uCount != 2 ||
3116 puTags[0] != 5859837686836516696 ||
3117 puTags[1] != 7 ||
3118 Out.uNumUsed != 2) {
3119 return -103;
3120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003121
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003122 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3123 return -104;
3124 }
3125 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003126 Item.val.uCount != 5 ||
3127 puTags[0] != 0x0b ||
3128 Out.uNumUsed != 1) {
3129 return -105;
3130 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003131
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003132 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3133 return -106;
3134 }
3135 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3136 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3137 Item.val.string.len != 12 ||
3138 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3139 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3140 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3141 Out.uNumUsed != 3) {
3142 return -105;
3143 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003144
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003145 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3146 return -107;
3147 }
3148 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3149 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3150 Item.val.string.len != 3 ||
3151 puTags[0] != 773 ||
3152 Out.uNumUsed != 1) {
3153 return -108;
3154 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003155
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003156 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3157 return -109;
3158 }
3159 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003160 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003161 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003162 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003163 puTags[3] != 7 ||
3164 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003165 return -110;
3166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003167
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003168 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3169 return -111;
3170 }
3171 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3172 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3173 Item.val.string.len != 9 ||
3174 puTags[0] != 17 ||
3175 Out.uNumUsed != 1) {
3176 return -112;
3177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003178
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003179 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3180 return -111;
3181 }
3182 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3183 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3184 Item.val.string.len != 2 ||
3185 puTags[0] != 17 ||
3186 Out.uNumUsed != 1) {
3187 return -112;
3188 }
3189
3190 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3191 return -113;
3192 }
3193 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003194 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003195 Item.val.uCount != 2 ||
3196 puTags[0] != 19 ||
3197 Out.uNumUsed != 1) {
3198 return -114;
3199 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003200
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003201 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3202 return -115;
3203 }
3204 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003205 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003206 Item.val.uCount != 1 ||
3207 puTags[0] != 9 ||
3208 Out.uNumUsed != 1) {
3209 return -116;
3210 }
3211
3212 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3213 return -116;
3214 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003215 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003216 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003217 Out.uNumUsed != 0) {
3218 return -117;
3219 }
3220
3221 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3222 return -118;
3223 }
3224 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3225 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003226 puTags[0] != 12 ||
3227 Out.uNumUsed != 1) {
3228 return -119;
3229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003230
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003231 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3232 return -120;
3233 }
3234 if(Item.uDataType != QCBOR_TYPE_MAP ||
3235 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3236 Item.val.uCount != 1 ||
3237 puTags[0] != 0x17 ||
3238 Out.uNumUsed != 1) {
3239 return -121;
3240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003241
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003242 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3243 return -122;
3244 }
3245 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003246 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003247 Item.val.int64 != -3 ||
3248 puTags[0] != 8 ||
3249 Out.uNumUsed != 1) {
3250 return -123;
3251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003252
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003253 if(QCBORDecode_Finish(&DCtx)) {
3254 return -124;
3255 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003256
3257 UsefulBufC DateString;
3258 QCBORDecode_Init(&DCtx,
3259 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3260 QCBOR_DECODE_MODE_NORMAL);
3261
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003262 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003263 // tagged date string
3264 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3265 // untagged date string
3266 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3267 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3268 return 100;
3269 }
3270 // untagged byte string
3271 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3272 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3273 return 101;
3274 }
3275 // tagged regex
3276 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3277 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3278 return 102;
3279 }
3280 // tagged date string with a byte string
3281 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3282 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3283 return 103;
3284 }
3285 QCBORDecode_ExitArray(&DCtx);
3286 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3287 return 104;
3288 }
3289
3290
3291 QCBORDecode_Init(&DCtx,
3292 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3293 QCBOR_DECODE_MODE_NORMAL);
3294
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003295 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003296 // tagged date string
3297 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3298 // untagged date string
3299 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3300 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3301 return 200;
3302 }
3303 // untagged byte string
3304 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3305 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3306 return 201;
3307 }
3308 // tagged regex
3309 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3310 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3311 return 202;
3312 }
3313 // tagged date string with a byte string
3314 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3315 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3316 return 203;
3317 }
3318 QCBORDecode_ExitArray(&DCtx);
3319 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3320 return 204;
3321 }
3322
3323 QCBORDecode_Init(&DCtx,
3324 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3325 QCBOR_DECODE_MODE_NORMAL);
3326
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003327 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003328 // tagged date string
3329 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3330 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3331 return 300;
3332 }
3333 // untagged date string
3334 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3335 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3336 return 301;
3337 }
3338 // untagged byte string
3339 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3340 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3341 return 302;
3342 }
3343 // tagged regex
3344 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3345 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3346 return 303;
3347 }
3348 // tagged date string with a byte string
3349 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3350 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3351 return 304;
3352 }
3353 QCBORDecode_ExitArray(&DCtx);
3354 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3355 return 305;
3356 }
3357
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003358 return 0;
3359}
3360
3361
3362
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003363
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003364static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003365 0x83,
3366 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3367 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3368 0xA4,
3369 0x63, 0x42, 0x4E, 0x2B,
3370 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3371 0x18, 0x40,
3372 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3373 0x63, 0x42, 0x4E, 0x2D,
3374 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3375 0x38, 0x3F,
3376 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3377
3378
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003379static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003380
3381
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003382int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003383{
3384 QCBORDecodeContext DCtx;
3385 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003386 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003387
Laurence Lundbladeee851742020-01-08 08:37:05 -08003388 QCBORDecode_Init(&DCtx,
3389 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3390 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003391
3392
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003393 //
3394 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3395 return -1;
3396 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003397 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003399
3400 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003401 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003402 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003403 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003404 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003405 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003406 }
3407
3408 //
3409 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003410 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003411 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003412 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003413 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003414 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003415
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003416 //
3417 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003418 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003419 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003420 return -8;
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 -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003425 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
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 -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003429 }
3430
3431 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003432 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003433 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
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 -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003438 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003439
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003440 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003441 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003442 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3443 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003444 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003445 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003446 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003447
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003448 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003449 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003450 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3451 Item.uLabelType != QCBOR_TYPE_INT64 ||
3452 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003453 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003454 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003455 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003456
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003457 return 0;
3458}
3459
3460
3461
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003462static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003463 uint8_t uDataType,
3464 uint8_t uNestingLevel,
3465 uint8_t uNextNest,
3466 int64_t nLabel,
3467 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003468{
3469 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003470 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003471
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003472 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3473 if(Item.uDataType != uDataType) return -1;
3474 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003475 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3476 Item.uLabelType != QCBOR_TYPE_UINT64) {
3477 return -1;
3478 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003479 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3480 if(Item.label.int64 != nLabel) return -1;
3481 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003482 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003483 }
3484 }
3485 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303486 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003487
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003488 if(pItem) {
3489 *pItem = Item;
3490 }
3491 return 0;
3492}
3493
3494
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003495// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003496static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003497{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303498 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003499
Laurence Lundblade9b334962020-08-27 10:55:53 -07003500 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003501
Laurence Lundblade9b334962020-08-27 10:55:53 -07003502 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003503
Laurence Lundblade9b334962020-08-27 10:55:53 -07003504 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3505 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3506 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3507 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3508 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003509
Laurence Lundblade9b334962020-08-27 10:55:53 -07003510 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3511 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003512
Laurence Lundblade9b334962020-08-27 10:55:53 -07003513 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3514 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003515
Laurence Lundblade9b334962020-08-27 10:55:53 -07003516 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3517 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003518
Laurence Lundblade9b334962020-08-27 10:55:53 -07003519 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003520
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003521 return 0;
3522}
3523
3524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003525/*
3526// cbor.me decoded output
3527{
3528 -23: {
3529 -20: {
3530 -18: "Organization",
3531 -17: "SSG",
3532 -15: "Confusion",
3533 -16: "San Diego",
3534 -14: "US"
3535 },
3536 -19: {
3537 -11: {
3538 -9: -7
3539 },
3540 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3541 }
3542 },
3543 -22: {
3544 -5: -3
3545 }
3546}
3547 */
3548
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003549
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003550static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003551 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3552 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3553 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3554 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3555 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3556 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3557 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3558 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3559 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3560
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003561int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003562{
3563 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003564
Laurence Lundbladeee851742020-01-08 08:37:05 -08003565 QCBORDecode_Init(&DCtx,
3566 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3567 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003568
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003569 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003570}
3571
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003572
3573
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003574int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003575{
3576 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003577
Laurence Lundbladeee851742020-01-08 08:37:05 -08003578 QCBORDecode_Init(&DCtx,
3579 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3580 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003581
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003582 QCBORItem Item;
3583 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003584
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003585 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3586 return -1;
3587 }
3588 if(Item.uDataType != QCBOR_TYPE_MAP) {
3589 return -2;
3590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003591
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003592 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3593 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3594 return -3;
3595 }
3596
3597 return 0;
3598}
3599
3600
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003601// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003602static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003603 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3604 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3605 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3606 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3607 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3608 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003609 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3610 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3611 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3612 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003613
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003614int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003615{
3616 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003617
Laurence Lundbladeee851742020-01-08 08:37:05 -08003618 QCBORDecode_Init(&DCtx,
3619 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3620 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003621
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003622 return CheckCSRMaps(&DCtx);
3623}
3624
3625
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003626
Laurence Lundblade17ede402018-10-13 11:43:07 +08003627static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3628{
3629 UsefulOutBuf UOB;
3630 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003631
Laurence Lundblade17ede402018-10-13 11:43:07 +08003632 int i;
3633 for(i = 0; i < n; i++) {
3634 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3635 }
3636
3637 for(i = 0; i < n; i++) {
3638 UsefulOutBuf_AppendByte(&UOB, 0xff);
3639 }
3640 return UsefulOutBuf_OutUBuf(&UOB);
3641}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003642
3643
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003644static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003645{
3646 QCBORDecodeContext DC;
3647 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003648
Laurence Lundblade17ede402018-10-13 11:43:07 +08003649 int j;
3650 for(j = 0; j < nNestLevel; j++) {
3651 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003652 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003653 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3654 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003655 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003656 return -4;
3657 } else {
3658 return 0; // Decoding doesn't recover after an error
3659 }
3660 } else {
3661 // Should be no error
3662 if(nReturn) {
3663 return -9; // Should not have got an error
3664 }
3665 }
3666 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3667 return -7;
3668 }
3669 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003670 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003671 if(nReturn) {
3672 return -3;
3673 }
3674 return 0;
3675}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003676
3677
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003678int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003679{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303680 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003681 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003682 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003683 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003684 int nReturn = parse_indeflen_nested(Nested, i);
3685 if(nReturn) {
3686 return nReturn;
3687 }
3688 }
3689 return 0;
3690}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003691
Laurence Lundbladeee851742020-01-08 08:37:05 -08003692// [1, [2, 3]]
3693static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3694// No closing break
3695static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3696// Not enough closing breaks
3697static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3698// Too many closing breaks
3699static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3700// Unclosed indeflen inside def len
3701static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3702// confused tag
3703static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003704
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003705int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003706{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003707 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003708 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003709 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003710
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003711 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003712 QCBORDecodeContext DC;
3713 QCBORItem Item;
3714 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003715
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003716 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303717
3718 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3719 Item.uNestingLevel != 0 ||
3720 Item.uNextNestLevel != 1) {
3721 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003722 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003723
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003724 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303725 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3726 Item.uNestingLevel != 1 ||
3727 Item.uNextNestLevel != 1) {
3728 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003729 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003730
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003731 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303732 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3733 Item.uNestingLevel != 1 ||
3734 Item.uNextNestLevel != 2) {
3735 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003736 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003737
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003738 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003739 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303740 Item.uNestingLevel != 2 ||
3741 Item.uNextNestLevel != 2) {
3742 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003743 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003744
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003745 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003746 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303747 Item.uNestingLevel != 2 ||
3748 Item.uNextNestLevel != 0) {
3749 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003750 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003751
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003752 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303753 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003754 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003755
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003756 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003757 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003758
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003759 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003760
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003761 nResult = QCBORDecode_GetNext(&DC, &Item);
3762 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303763 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003764 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003765
Laurence Lundblade570fab52018-10-13 18:28:27 +08003766 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003767 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303768 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003769 }
3770
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003771
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003772 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003773 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003774
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003775 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003776
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003777 nResult = QCBORDecode_GetNext(&DC, &Item);
3778 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303779 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003780 }
3781
3782 nResult = QCBORDecode_GetNext(&DC, &Item);
3783 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303784 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003785 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003786
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003787 nResult = QCBORDecode_GetNext(&DC, &Item);
3788 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303789 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003790 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003791
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003792 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003793 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303794 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003795 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003796
3797
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003798 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003799 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003800
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003801 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003802
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003803 nResult = QCBORDecode_GetNext(&DC, &Item);
3804 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303805 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003806 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003807
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003808 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003809 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303810 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003811 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303812
Laurence Lundblade642282a2020-06-23 12:00:33 -07003813 nResult = QCBORDecode_GetNext(&DC, &Item);
3814 if(nResult != QCBOR_ERR_BAD_BREAK) {
3815 return -140;
3816 }
3817
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003818
Laurence Lundblade570fab52018-10-13 18:28:27 +08003819 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003820 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003821
Laurence Lundblade570fab52018-10-13 18:28:27 +08003822 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003823
Laurence Lundblade570fab52018-10-13 18:28:27 +08003824 nResult = QCBORDecode_GetNext(&DC, &Item);
3825 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303826 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003827 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003828
Laurence Lundblade570fab52018-10-13 18:28:27 +08003829 nResult = QCBORDecode_GetNext(&DC, &Item);
3830 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303831 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003832 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003833
Laurence Lundblade570fab52018-10-13 18:28:27 +08003834 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003835 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303836 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003837 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003838
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303839 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003840 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003841
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303842 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003843
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303844 nResult = QCBORDecode_GetNext(&DC, &Item);
3845 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303846 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303847 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003848
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303849 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303850 if(nResult != QCBOR_ERR_BAD_BREAK) {
3851 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303852 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003853
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003854 return 0;
3855}
3856
Laurence Lundblade17ede402018-10-13 11:43:07 +08003857
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08003858#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
3859
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003860static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003861 0x81, // Array of length one
3862 0x7f, // text string marked with indefinite length
3863 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3864 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3865 0xff // ending break
3866};
3867
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003868static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303869 0x81, // Array of length one
3870 0x7f, // text string marked with indefinite length
3871 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3872 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3873 0xff // ending break
3874};
3875
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003876static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303877 0x81, // Array of length one
3878 0x7f, // text string marked with indefinite length
3879 0x01, 0x02, // Not a string
3880 0xff // ending break
3881};
3882
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003883static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303884 0x81, // Array of length one
3885 0x7f, // text string marked with indefinite length
3886 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3887 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3888 // missing end of string
3889};
3890
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003891static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303892 0xa1, // Array of length one
3893 0x7f, // text string marked with indefinite length
3894 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3895 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3896 0xff, // ending break
3897 0x01 // integer being labeled.
3898};
3899
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003900/**
3901 Make an indefinite length string
3902
3903 @param Storage Storage for string, must be 144 bytes in size
3904 @return The indefinite length string
3905
3906 This makes an array with one indefinite length string that has 7 chunks
3907 from size of 1 byte up to 64 bytes.
3908 */
3909static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303910{
3911 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003912
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303913 UsefulOutBuf_Init(&UOB, Storage);
3914 UsefulOutBuf_AppendByte(&UOB, 0x81);
3915 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003916
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003917 uint8_t uStringByte = 0;
3918 // Use of type int is intentional
3919 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3920 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303921 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003922 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3923 for(int j = 0; j < uChunkSize; j++) {
3924 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3925 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303926 }
3927 }
3928 UsefulOutBuf_AppendByte(&UOB, 0xff);
3929
3930 return UsefulOutBuf_OutUBuf(&UOB);
3931}
3932
3933static int CheckBigString(UsefulBufC BigString)
3934{
3935 if(BigString.len != 255) {
3936 return 1;
3937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003938
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303939 for(uint8_t i = 0; i < 255; i++){
3940 if(((const uint8_t *)BigString.ptr)[i] != i) {
3941 return 1;
3942 }
3943 }
3944 return 0;
3945}
3946
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303947
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003948int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303949{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303950 QCBORDecodeContext DC;
3951 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303952 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003953 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003954
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303955 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003956 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303957 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303959 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303960 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303961 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003962
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303963 if(QCBORDecode_GetNext(&DC, &Item)) {
3964 return -2;
3965 }
3966 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3967 return -3;
3968 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003969
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303970 if(QCBORDecode_GetNext(&DC, &Item)) {
3971 return -4;
3972 }
3973 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3974 return -5;
3975 }
3976 if(QCBORDecode_Finish(&DC)) {
3977 return -6;
3978 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303979
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303980 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003981 QCBORDecode_Init(&DC,
3982 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3983 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003984
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303985 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3986 return -7;
3987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003988
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303989 if(QCBORDecode_GetNext(&DC, &Item)) {
3990 return -8;
3991 }
3992 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3993 return -9;
3994 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003995
Laurence Lundblade30816f22018-11-10 13:40:22 +07003996 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303997 return -10;
3998 }
3999
4000 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004001 QCBORDecode_Init(&DC,
4002 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4003 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004004
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304005 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4006 return -11;
4007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004008
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304009 if(QCBORDecode_GetNext(&DC, &Item)) {
4010 return -12;
4011 }
4012 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4013 return -13;
4014 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004015
Laurence Lundblade30816f22018-11-10 13:40:22 +07004016 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304017 return -14;
4018 }
4019
4020 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004021 QCBORDecode_Init(&DC,
4022 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4023 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004024
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304025 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4026 return -15;
4027 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004028
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304029 if(QCBORDecode_GetNext(&DC, &Item)) {
4030 return -16;
4031 }
4032 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4033 return -17;
4034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004035
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304036 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4037 return -18;
4038 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004039
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304040 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304041 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004042
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304043 QCBORDecode_GetNext(&DC, &Item);
4044 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304045 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304046 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004047
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304048 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304049 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304050 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004051
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304052 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004053 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304054
4055 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4056 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304057 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304058 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004059
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304060 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304061 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004062 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004063
Laurence Lundbladeee851742020-01-08 08:37:05 -08004064 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4065 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004066
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304067 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304068 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304069 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304070 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004071
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304072 QCBORDecode_GetNext(&DC, &Item);
4073 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304074 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304075 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004076 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304077 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304078 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004079
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304080 // ---- big bstr -----
4081 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004082
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304083 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4084 return -25;
4085 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004086
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304087 if(QCBORDecode_GetNext(&DC, &Item)) {
4088 return -26;
4089 }
4090 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304091 return -26;
4092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004093
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304094 if(QCBORDecode_GetNext(&DC, &Item)) {
4095 return -27;
4096 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304097 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304098 return -28;
4099 }
4100 if(CheckBigString(Item.val.string)) {
4101 return -3;
4102 }
4103 if(QCBORDecode_Finish(&DC)) {
4104 return -29;
4105 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004106
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304107 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004108 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004109
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304110 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4111 return -30;
4112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004113
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304114 QCBORDecode_GetNext(&DC, &Item);
4115 if(Item.uDataType != QCBOR_TYPE_MAP) {
4116 return -31;
4117 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004118
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304119 if(QCBORDecode_GetNext(&DC, &Item)){
4120 return -32;
4121 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004122 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4123 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304124 Item.uDataAlloc || !Item.uLabelAlloc ||
4125 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4126 return -33;
4127 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004128
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304129 if(QCBORDecode_Finish(&DC)) {
4130 return -34;
4131 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004133 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004134}
4135
4136
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004137int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304138{
4139 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004140 QCBORError nCBORError;
4141
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004142
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304143 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004144 QCBORDecode_Init(&DC,
4145 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4146 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004147
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004148 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004149
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004150 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4151 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304152 return -1;
4153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004154
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004155 if(CheckCSRMaps(&DC)) {
4156 return -2;
4157 }
4158
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004159 // Next parse, save pointers to a few strings, destroy original and
4160 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004161 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004162 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004163
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304164 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004165 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304166 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004167
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304168 QCBORItem Item1, Item2, Item3, Item4;
4169 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004170 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304171 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4172 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004173 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304174 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004175 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304176 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004177 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304178 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004179 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304180 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004181 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004182
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304183 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004184
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304185 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304186 Item1.uDataType != QCBOR_TYPE_INT64 ||
4187 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004188 Item1.uDataAlloc != 0 ||
4189 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004190 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004191 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004193
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304194
4195 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004196 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304197 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004198 Item2.uDataAlloc != 0 ||
4199 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304200 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004201 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004202
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304203 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004204 Item3.uDataAlloc == 0 ||
4205 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004206 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004207 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004209
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304210 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004211 Item4.uDataAlloc == 0 ||
4212 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004213 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004214 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004215 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004216
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304217 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004218 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004219 QCBORDecode_Init(&DC,
4220 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4221 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304222 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4223 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004224 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304225 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004226 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004227 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004228 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304229 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4230 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4231 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4232 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4233 }
4234 }
4235 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004236 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004237 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304238 }
4239
4240 return 0;
4241}
4242
Laurence Lundbladef6531662018-12-04 10:42:22 +09004243
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004244int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004245{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004246 // Set up the decoder with a tiny bit of CBOR to parse because
4247 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004248 QCBORDecodeContext DC;
4249 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4250 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004251
Laurence Lundbladef6531662018-12-04 10:42:22 +09004252 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004253 // Then fish into the internals of the decode context
4254 // to get the allocator function so it can be called directly.
4255 // Also figure out how much pool is available for use
4256 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004257 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004258 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4259 if(nError) {
4260 return -9;
4261 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004262 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4263 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4264 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004265
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004266 // First test -- ask for one more byte than available and see failure
4267 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004268 if(!UsefulBuf_IsNULL(Allocated)) {
4269 return -1;
4270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004271
Laurence Lundbladef6531662018-12-04 10:42:22 +09004272 // Re do the set up for the next test that will do a successful alloc,
4273 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004274 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004275 pAlloc = DC.StringAllocator.pfAllocator;
4276 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4277 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004278
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004279 // Allocate one byte less than available and see success
4280 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004281 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4282 return -2;
4283 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004284 // Ask for some more and see failure
4285 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004286 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4287 return -3;
4288 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004289 // Free the first allocate, retry the second and see success
4290 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4291 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004292 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4293 return -4;
4294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004295
Laurence Lundbladef6531662018-12-04 10:42:22 +09004296 // Re do set up for next test that involves a successful alloc,
4297 // and a successful realloc and a failed realloc
4298 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004299 pAlloc = DC.StringAllocator.pfAllocator;
4300 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004301
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004302 // Allocate half the pool and see success
4303 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004304 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4305 return -5;
4306 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004307 // Reallocate to take up the whole pool and see success
4308 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004309 if(UsefulBuf_IsNULL(Allocated2)) {
4310 return -6;
4311 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004312 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004313 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4314 return -7;
4315 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004316 // Try to allocate more to be sure there is failure after a realloc
4317 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4318 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004319 return -8;
4320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004321
Laurence Lundbladef6531662018-12-04 10:42:22 +09004322 return 0;
4323}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004324
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004325
4326/* Just enough of an allocator to test configuration of one */
4327static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4328{
4329 (void)pOldMem; // unused variable
4330
4331 if(uNewSize) {
4332 // Assumes the context pointer is the buffer and
4333 // nothing too big will ever be asked for.
4334 // This is only good for this basic test!
4335 return (UsefulBuf) {pCtx, uNewSize};
4336 } else {
4337 return NULLUsefulBuf;
4338 }
4339}
4340
4341
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004342int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004343{
4344 // Set up the decoder with a tiny bit of CBOR to parse because
4345 // nothing can be done with it unless that is set up.
4346 QCBORDecodeContext DC;
4347 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4348 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4349
4350 uint8_t pAllocatorBuffer[50];
4351
4352 // This is really just to test that this call works.
4353 // The full functionality of string allocators is tested
4354 // elsewhere with the MemPool internal allocator.
4355 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4356
4357 QCBORItem Item;
4358 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4359 return -1;
4360 }
4361
4362 if(Item.uDataAlloc == 0 ||
4363 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4364 Item.val.string.ptr != pAllocatorBuffer) {
4365 return -2;
4366 }
4367
4368 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4369 return -3;
4370 }
4371
4372 return 0;
4373}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004374#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4375
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004376
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004377#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004378
Laurence Lundbladea826c502020-05-10 21:07:00 -07004379/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004380 [
4381 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004382 4([-20, 4759477275222530853136]),
4383 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004384 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004385 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004386 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004387 5([ 9223372036854775806, -4759477275222530853137])
4388 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004389 ]
4390 */
4391
4392static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004393 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004394 0xC4, 0x82, 0x20,
4395 0x03,
4396 0xC4, 0x82, 0x33,
4397 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4398 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4399 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4400 0xC5, 0x82, 0x19, 0x01, 0x2C,
4401 0x18, 0x64,
4402 0xC5, 0x82, 0x33,
4403 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4404 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4405 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004406 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4407 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004408 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4409 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4410};
4411
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004412
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004413int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004414{
4415 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004416 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004417 QCBORItem item;
4418
Laurence Lundblade17af4902020-01-07 19:11:55 -08004419 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4420 0x06, 0x07, 0x08, 0x09, 0x010};
4421 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004422
4423
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004424 QCBORDecode_Init(&DC,
4425 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4426 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004427
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004428 uErr = QCBORDecode_GetNext(&DC, &item);
4429 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004430 return 1;
4431 }
4432
4433 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4434 return 2;
4435 }
4436
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004437 uErr = QCBORDecode_GetNext(&DC, &item);
4438 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004439 return 3;
4440 }
4441
4442 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4443 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4444 item.val.expAndMantissa.nExponent != -1) {
4445 return 4;
4446 }
4447
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004448 uErr = QCBORDecode_GetNext(&DC, &item);
4449 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004450 return 5;
4451 }
4452
4453 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4454 item.val.expAndMantissa.nExponent != -20 ||
4455 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4456 return 6;
4457 }
4458
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004459 uErr = QCBORDecode_GetNext(&DC, &item);
4460 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004461 return 7;
4462 }
4463
4464 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4465 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4466 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4467 return 8;
4468 }
4469
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004470 uErr = QCBORDecode_GetNext(&DC, &item);
4471 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004472 return 9;
4473 }
4474
4475 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4476 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4477 item.val.expAndMantissa.nExponent != 300) {
4478 return 10;
4479 }
4480
Laurence Lundbladea826c502020-05-10 21:07:00 -07004481 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004482 uErr = QCBORDecode_GetNext(&DC, &item);
4483 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004484 return 11;
4485 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004486 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4487 item.val.expAndMantissa.nExponent != -20 ||
4488 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4489 return 12;
4490 }
4491
Laurence Lundbladea826c502020-05-10 21:07:00 -07004492 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004493 uErr = QCBORDecode_GetNext(&DC, &item);
4494 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004495 return 13;
4496 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004497 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4498 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4499 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4500 return 14;
4501 }
4502
Laurence Lundbladea826c502020-05-10 21:07:00 -07004503 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004504 uErr = QCBORDecode_GetNext(&DC, &item);
4505 if(uErr != QCBOR_SUCCESS) {
4506 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004507 }
4508 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4509 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4510 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004511 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004512 }
4513
Laurence Lundbladea826c502020-05-10 21:07:00 -07004514 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004515 uErr = QCBORDecode_GetNext(&DC, &item);
4516 if(uErr != QCBOR_SUCCESS) {
4517 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004518 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004519 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4520 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4521 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004522 return 18;
4523 }
4524
4525 uErr = QCBORDecode_Finish(&DC);
4526 if(uErr != QCBOR_SUCCESS) {
4527 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004528 }
4529
4530 /* Now encode some stuff and then decode it */
4531 uint8_t pBuf[40];
4532 QCBOREncodeContext EC;
4533 UsefulBufC Encoded;
4534
4535 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4536 QCBOREncode_OpenArray(&EC);
4537 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4538 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4539 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4540 QCBOREncode_CloseArray(&EC);
4541 QCBOREncode_Finish(&EC, &Encoded);
4542
4543
4544 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004545 uErr = QCBORDecode_GetNext(&DC, &item);
4546 if(uErr != QCBOR_SUCCESS) {
4547 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004548 }
4549
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004550 uErr = QCBORDecode_GetNext(&DC, &item);
4551 if(uErr != QCBOR_SUCCESS) {
4552 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004553 }
4554
4555 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4556 item.val.expAndMantissa.nExponent != 1000 ||
4557 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004558 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004559 }
4560
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004561 uErr = QCBORDecode_GetNext(&DC, &item);
4562 if(uErr != QCBOR_SUCCESS) {
4563 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004564 }
4565
4566 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4567 item.val.expAndMantissa.nExponent != INT32_MIN ||
4568 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004569 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004570 }
4571
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004572 uErr = QCBORDecode_GetNext(&DC, &item);
4573 if(uErr != QCBOR_SUCCESS) {
4574 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004575 }
4576
4577 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4578 item.val.expAndMantissa.nExponent != INT32_MAX ||
4579 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004580 return 106;
4581 }
4582
4583
4584 int64_t nExp, nMant;
4585 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4586 UsefulBufC Mant;
4587 bool bIsNeg;
4588
4589 QCBORDecode_Init(&DC,
4590 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4591 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004592 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004593
4594 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004595 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004596
4597 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004598 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4599 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004600
4601 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004602 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4603 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004604
4605 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004606 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004607
4608 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004609 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4610 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004611
4612 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004613 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4614 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004615
4616 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004617 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4618 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004619
4620 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004621 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4622 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004623
4624 QCBORDecode_ExitArray(&DC);
4625
4626 uErr = QCBORDecode_Finish(&DC);
4627 if(uErr != QCBOR_SUCCESS) {
4628 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004629 }
4630
4631 return 0;
4632}
4633
4634
4635static struct FailInput ExponentAndMantissaFailures[] = {
4636 // Exponent > INT64_MAX
4637 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4638 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4639 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4640 // Mantissa > INT64_MAX
4641 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4642 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4643 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4644 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004645 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004646 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004647 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004648 // bad content for big num
4649 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4650 // bad content for big num
4651 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4652 // Bad integer for exponent
4653 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4654 // Bad integer for mantissa
4655 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4656 // 3 items in array
4657 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4658 // unterminated indefinite length array
4659 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4660 // Empty array
4661 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4662 // Second is not an integer
4663 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4664 // First is not an integer
4665 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4666 // Not an array
4667 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4668};
4669
4670
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004671int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004672{
4673 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004674 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4675 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004676}
4677
4678#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004679
4680
4681
4682/*
4683 Some basic CBOR with map and array used in a lot of tests.
4684 The map labels are all strings
4685
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004686 {
4687 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004688 "an array of two strings": [
4689 "string1", "string2"
4690 ],
4691 "map in a map": {
4692 "bytes 1": h'78787878',
4693 "bytes 2": h'79797979',
4694 "another int": 98,
4695 "text 2": "lies, damn lies and statistics"
4696 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004697 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004698 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004699
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004700int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004701{
4702 QCBORItem Item1, Item2, Item3;
4703 int64_t nDecodedInt1, nDecodedInt2;
4704 UsefulBufC B1, B2, S1, S2, S3;
4705
4706 QCBORDecodeContext DCtx;
4707 QCBORError nCBORError;
4708
4709 QCBORDecode_Init(&DCtx, input, 0);
4710
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004711 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004712
4713 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4714
4715 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4716 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004717 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4718 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4719 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004720 QCBORDecode_ExitMap(&DCtx);
4721
4722 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4723 QCBORDecode_GetNext(&DCtx, &Item1);
4724 QCBORDecode_GetNext(&DCtx, &Item2);
4725 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4726 return -400;
4727 }
4728 QCBORDecode_ExitArray(&DCtx);
4729
4730 // Parse the same array again using GetText() instead of GetItem()
4731 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004732 QCBORDecode_GetTextString(&DCtx, &S2);
4733 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004734 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4735 return 5000;
4736 }
4737 /* QCBORDecode_GetText(&DCtx, &S3);
4738 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4739 return 5001;
4740 } */
4741
4742 QCBORDecode_ExitArray(&DCtx);
4743
4744 QCBORDecode_ExitMap(&DCtx);
4745
4746 nCBORError = QCBORDecode_Finish(&DCtx);
4747
4748 if(nCBORError) {
4749 return (int32_t)nCBORError;
4750 }
4751
4752 if(nDecodedInt1 != 42) {
4753 return 1001;
4754 }
4755
4756 if(nDecodedInt2 != 98) {
4757 return 1002;
4758 }
4759
4760 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004761 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004762 return 1003;
4763 }
4764
4765 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004766 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004767 return 1004;
4768 }
4769
Laurence Lundblade9b334962020-08-27 10:55:53 -07004770 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004771 return 1005;
4772 }
4773
4774 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4775 return 1006;
4776 }
4777
4778 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4779 return 1007;
4780 }
4781
4782 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4783 return 1008;
4784 }
4785
4786 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4787 return 1009;
4788 }
4789
4790 return 0;
4791}
4792
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004793/*
4794 {
4795 -75008: h'05083399',
4796 88: [
4797 ],
4798 100100: {
4799 "sub1": {
4800 10: [
4801 0
4802 ],
4803 -75009: h'A46823990001',
4804 100100: {
4805 "json": "{ \"ueid\", \"xyz\"}",
4806 "subsub": {
4807 100002: h'141813191001'
4808 }
4809 }
4810 }
4811 }
4812 }
4813 */
4814
4815static const uint8_t spNestedCBOR[] = {
48160xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
48170x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
48180x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
48190x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
48200x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
48210x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, 0x01
4822};
4823
4824/* Get item in multi-level nesting in spNestedCBOR */
4825static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4826{
4827 UsefulBufC String;
4828
4829 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4830 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4831
4832 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4833 QCBORDecode_EnterMap(pDCtx, NULL);
4834 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4835 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4836 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4837 if(QCBORDecode_GetError(pDCtx)) {
4838 return 4001;
4839 }
4840 if(UsefulBuf_Compare(String, test_oemid)) {
4841 return 4002;
4842 }
4843 QCBORDecode_ExitMap(pDCtx);
4844 QCBORDecode_ExitMap(pDCtx);
4845 QCBORDecode_ExitMap(pDCtx);
4846 QCBORDecode_ExitMap(pDCtx);
4847
4848 return 0;
4849}
4850
4851/* Iterations on the zero-length array in spNestedCBOR */
4852static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4853{
4854 QCBORItem Item;
4855 QCBORError uErr;
4856
4857 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4858 for(int x = 0; x < 20; x++) {
4859 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4860 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4861 return 4100;
4862
4863 }
4864 }
4865 QCBORDecode_ExitArray(pDCtx);
4866 if(QCBORDecode_GetError(pDCtx)) {
4867 return 4101;
4868 }
4869
4870 return 0;
4871}
4872
4873/* Various iterations on the array that contains a zero in spNestedCBOR */
4874static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
4875{
4876 QCBORError uErr;
4877
4878 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4879 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
4880 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4881 int64_t nInt = 99;
4882 QCBORDecode_GetInt64(pDCtx, &nInt);
4883 if(nInt != 0) {
4884 return 4200;
4885 }
4886 for(int x = 0; x < 20; x++) {
4887 QCBORItem Item;
4888 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4889 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4890 return 4201;
4891
4892 }
4893 }
4894 QCBORDecode_ExitArray(pDCtx);
4895 if(QCBORDecode_GetAndResetError(pDCtx)) {
4896 return 4202;
4897 }
4898 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4899 UsefulBufC dD;
4900 QCBORDecode_GetByteString(pDCtx, &dD);
4901 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4902 return 4203;
4903 }
4904 for(int x = 0; x < 20; x++) {
4905 QCBORDecode_GetByteString(pDCtx, &dD);
4906 uErr = QCBORDecode_GetAndResetError(pDCtx);
4907 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4908 return 4204;
4909 }
4910 }
4911 QCBORDecode_ExitArray(pDCtx);
4912 QCBORDecode_ExitMap(pDCtx);
4913 QCBORDecode_ExitMap(pDCtx);
4914
4915 return 0;
4916}
4917
4918/* Repeatedly enter and exit maps and arrays, go off the end of maps
4919 and arrays and such. */
4920static int32_t DecodeNestedIterate()
4921{
4922 QCBORDecodeContext DCtx;
4923 int32_t nReturn;
4924 QCBORError uErr;
4925
4926 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
4927 QCBORDecode_EnterMap(&DCtx, NULL);
4928
4929 for(int j = 0; j < 5; j++) {
4930 for(int i = 0; i < 20; i++) {
4931 nReturn = DecodeNestedGetSubSub(&DCtx);
4932 if(nReturn) {
4933 return nReturn;
4934 }
4935 }
4936
4937 for(int i = 0; i < 20; i++) {
4938 nReturn = DecodeNestedGetEmpty(&DCtx);
4939 if(nReturn ) {
4940 return nReturn;
4941 }
4942 }
4943
4944 for(int i = 0; i < 20; i++) {
4945 nReturn = DecodeNestedGetZero(&DCtx);
4946 if(nReturn ) {
4947 return nReturn;
4948 }
4949 }
4950 }
4951
4952 QCBORDecode_ExitMap(&DCtx);
4953 uErr = QCBORDecode_Finish(&DCtx);
4954 if(uErr) {
4955 return (int32_t)uErr + 4100;
4956 }
4957
4958 return 0;
4959}
4960
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004961
4962/*
4963 [23,
4964 6000,
4965 h'67616C6163746963',
4966 h'686176656E20746F6B656E'
4967 ]
4968 */
4969static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004970 0x84,
4971 0x17,
4972 0x19, 0x17, 0x70,
4973 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4974 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004975
4976
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004977static const uint8_t spEmptyMap[] = {0xa0};
4978
4979static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004980
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004981static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4982
Laurence Lundbladef0499502020-08-01 11:55:57 -07004983/*
4984 {
4985 0: [],
4986 9: [
4987 [],
4988 []
4989 ],
4990 8: {
4991 1: [],
4992 2: {},
4993 3: []
4994 },
4995 4: {},
4996 5: [],
4997 6: [
4998 [],
4999 []
5000 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07005001 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005002 */
Laurence Lundblade44080632020-08-06 21:43:50 -07005003
Laurence Lundbladef0499502020-08-01 11:55:57 -07005004static const uint8_t spMapOfEmpty[] = {
5005 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
5006 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
5007 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005008
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005009/*
5010 Too many tags
5011 Invalid tag content
5012 Duplicate label
5013 Integer overflow
5014 Date overflow
5015
5016 {1: 224(225(226(227(4(0))))),
5017 2: 1(h''),
5018 3: -18446744073709551616,
5019 4: 1(1.0e+300),
5020 5: 0, 8: 8}
5021 */
5022static const uint8_t spRecoverableMapErrors[] = {
5023 0xbf,
5024 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5025 0x02, 0xc1, 0x40,
5026 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5027 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5028 0x05, 0x00,
5029 0x05, 0x00,
5030 0x08, 0x08,
5031 0xff
5032};
5033
5034// Bad break
5035static const uint8_t spUnRecoverableMapError1[] = {
5036 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5037};
5038
5039// No more items
5040static const uint8_t spUnRecoverableMapError2[] = {
5041 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5042};
5043
5044// Hit end because string is too long
5045static const uint8_t spUnRecoverableMapError3[] = {
5046 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5047};
5048
5049// Hit end because string is too long
5050static const uint8_t spUnRecoverableMapError4[] = {
5051 0xbf,
5052 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5053 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5054 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5055 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5056 0xff
5057};
5058
5059
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005060int32_t EnterMapTest()
5061{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005062 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005063 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005064 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005065 int32_t nReturn;
5066 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005067
5068
5069 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005070 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005071
Laurence Lundbladef0499502020-08-01 11:55:57 -07005072
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005073 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005074 QCBORDecode_ExitArray(&DCtx);
5075
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005076 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5077 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005078 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005079 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005080 QCBORDecode_ExitArray(&DCtx);
5081 QCBORDecode_ExitArray(&DCtx);
5082
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005083 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5084 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005085 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005086 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005087 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005088 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005089 QCBORDecode_ExitArray(&DCtx);
5090 QCBORDecode_ExitMap(&DCtx);
5091
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005092 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005093 QCBORDecode_ExitMap(&DCtx);
5094
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005095 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005096 QCBORDecode_ExitArray(&DCtx);
5097
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005098 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5099 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005100 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005101 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005102 QCBORDecode_ExitArray(&DCtx);
5103 QCBORDecode_ExitArray(&DCtx);
5104
5105 QCBORDecode_ExitMap(&DCtx);
5106
5107 uErr = QCBORDecode_Finish(&DCtx);
5108 if(uErr != QCBOR_SUCCESS){
5109 return 3011;
5110 }
5111
5112
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005113 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005114 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005115 if(nReturn) {
5116 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005117 }
5118
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005119 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005120 if(nReturn) {
5121 return nReturn;
5122 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005123
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005124
Laurence Lundblade937ea812020-05-08 11:38:23 -07005125
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005126 // These tests confirm the cursor is at the right place after entering
5127 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005128 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005129
5130 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005131 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005132 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005133 QCBORDecode_GetNext(&DCtx, &Item1);
5134 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5135 return 2001;
5136 }
5137
5138
Laurence Lundblade9b334962020-08-27 10:55:53 -07005139 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005140 QCBORDecode_VGetNext(&DCtx, &Item1);
5141 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005142 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5143 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5144 UsefulBuf_Compare(ArrayItem.label.string,
5145 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5146 return 2051;
5147 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005148 QCBORDecode_GetNext(&DCtx, &Item1);
5149 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5150 return 2002;
5151 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005152 QCBORDecode_ExitArray(&DCtx);
5153 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5154 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5155 UsefulBuf_Compare(ArrayItem.label.string,
5156 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5157 return 2052;
5158 }
5159
Laurence Lundblade937ea812020-05-08 11:38:23 -07005160
Laurence Lundblade9b334962020-08-27 10:55:53 -07005161 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005162 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005163 QCBORDecode_GetNext(&DCtx, &Item1);
5164 QCBORDecode_GetNext(&DCtx, &Item1);
5165 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005166 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5167 QCBORDecode_GetNext(&DCtx, &Item1);
5168 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5169 return 2003;
5170 }
5171
Laurence Lundblade9b334962020-08-27 10:55:53 -07005172 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005173 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005174 QCBORDecode_GetNext(&DCtx, &Item1);
5175 QCBORDecode_GetNext(&DCtx, &Item1);
5176 QCBORDecode_GetNext(&DCtx, &Item1);
5177 QCBORDecode_GetNext(&DCtx, &Item1);
5178 QCBORDecode_GetNext(&DCtx, &Item1);
5179 QCBORDecode_GetNext(&DCtx, &Item1);
5180 QCBORDecode_GetNext(&DCtx, &Item1);
5181 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5182 QCBORDecode_GetNext(&DCtx, &Item1);
5183 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005184 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005185 }
5186
Laurence Lundblade9b334962020-08-27 10:55:53 -07005187 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005188 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005189 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5190 QCBORDecode_ExitArray(&DCtx);
5191 QCBORDecode_GetNext(&DCtx, &Item1);
5192 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5193 return 2006;
5194 }
5195 QCBORDecode_ExitMap(&DCtx);
5196 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5197 return 2007;
5198 }
5199
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005200 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005201 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005202 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005203 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5204 uErr = QCBORDecode_GetAndResetError(&DCtx);
5205 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005206 return 2008;
5207 }
5208 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005209 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005210 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005211 return 2009;
5212 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005213
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005214
5215 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005216 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005217 // This will fail because the map is empty.
5218 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5219 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005220 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005221 return 2010;
5222 }
5223 QCBORDecode_ExitMap(&DCtx);
5224 uErr = QCBORDecode_Finish(&DCtx);
5225 if(uErr != QCBOR_SUCCESS){
5226 return 2011;
5227 }
5228
5229
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005230 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005231 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005232 // This will fail because the map is empty.
5233 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5234 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005235 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005236 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005237 }
5238 QCBORDecode_ExitMap(&DCtx);
5239 uErr = QCBORDecode_Finish(&DCtx);
5240 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005241 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005242 }
5243
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005244
5245 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005246 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005247 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005248 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005249 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005250 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005251 QCBORDecode_ExitArray(&DCtx);
5252 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5253 QCBORDecode_ExitArray(&DCtx);
5254 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005255 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005256 return 2014;
5257 }
5258
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005259 int64_t nInt;
5260 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005261 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005262 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005263 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005264 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5265 return 2021;
5266 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005267 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5268 return 2121;
5269 }
5270 (void)QCBORDecode_GetAndResetError(&DCtx);
5271
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005272
5273 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5274 uErr = QCBORDecode_GetAndResetError(&DCtx);
5275 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5276 return 2022;
5277 }
5278
5279 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5280 uErr = QCBORDecode_GetAndResetError(&DCtx);
5281 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5282 return 2023;
5283 }
5284
5285 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5286 uErr = QCBORDecode_GetAndResetError(&DCtx);
5287#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5288 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5289 return 2024;
5290 }
5291#else
5292 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5293 return 2027;
5294 }
5295#endif
5296
5297 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5298 uErr = QCBORDecode_GetAndResetError(&DCtx);
5299 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5300 return 2025;
5301 }
5302
5303 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5304
5305 QCBORDecode_ExitMap(&DCtx);
5306 uErr = QCBORDecode_Finish(&DCtx);
5307 if(uErr != QCBOR_SUCCESS) {
5308 return 2026;
5309 }
5310
5311 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005312 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005313 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5314 uErr = QCBORDecode_GetAndResetError(&DCtx);
5315 if(uErr != QCBOR_ERR_BAD_BREAK) {
5316 return 2030;
5317 }
5318
5319 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005320 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005321 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5322 uErr = QCBORDecode_GetAndResetError(&DCtx);
5323 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5324 return 2031;
5325 }
5326
5327 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005328 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005329 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5330 uErr = QCBORDecode_GetAndResetError(&DCtx);
5331 if(uErr != QCBOR_ERR_HIT_END) {
5332 return 2032;
5333 }
5334
5335 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005336 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005337 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5338 uErr = QCBORDecode_GetAndResetError(&DCtx);
5339 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5340 return 2033;
5341 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005342
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005343 nReturn = DecodeNestedIterate();
5344
5345 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005346}
5347
5348
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005349struct NumberConversion {
5350 char *szDescription;
5351 UsefulBufC CBOR;
5352 int64_t nConvertedToInt64;
5353 QCBORError uErrorInt64;
5354 uint64_t uConvertToUInt64;
5355 QCBORError uErrorUint64;
5356 double dConvertToDouble;
5357 QCBORError uErrorDouble;
5358};
5359
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005360static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005361 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005362 "too large to fit into int64_t",
5363 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5364 0,
5365 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5366 0,
5367 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5368 ((double)INT64_MIN) + 1 ,
5369 QCBOR_SUCCESS
5370 },
5371 {
5372 "largest negative int that fits in int64_t",
5373 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5374 INT64_MIN,
5375 QCBOR_SUCCESS,
5376 0,
5377 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5378 (double)INT64_MIN,
5379 QCBOR_SUCCESS
5380 },
5381 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005382 "negative bignum -1",
5383 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5384 -1,
5385 QCBOR_SUCCESS,
5386 0,
5387 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5388 -1.0,
5389 QCBOR_SUCCESS
5390 },
5391 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005392 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005393 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5394 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005395#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005396 257000,
5397 QCBOR_SUCCESS,
5398 257000,
5399 QCBOR_SUCCESS,
5400 257000.0,
5401 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005402#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5403 0,
5404 QCBOR_ERR_UNEXPECTED_TYPE,
5405 0,
5406 QCBOR_ERR_UNEXPECTED_TYPE,
5407 0.0,
5408 QCBOR_ERR_UNEXPECTED_TYPE
5409#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005410 },
5411 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005412 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005413 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5414 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005415#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005416 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005417 QCBOR_SUCCESS,
5418 0,
5419 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005420 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005421 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005422#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5423 0,
5424 QCBOR_ERR_UNEXPECTED_TYPE,
5425 0,
5426 QCBOR_ERR_UNEXPECTED_TYPE,
5427 0.0,
5428 QCBOR_ERR_UNEXPECTED_TYPE
5429#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005430 },
5431 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005432 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005433 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5434 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005435#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005436 2056,
5437 QCBOR_SUCCESS,
5438 2056,
5439 QCBOR_SUCCESS,
5440 2056.0,
5441 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005442#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5443 0,
5444 QCBOR_ERR_UNEXPECTED_TYPE,
5445 0,
5446 QCBOR_ERR_UNEXPECTED_TYPE,
5447 0.0,
5448 QCBOR_ERR_UNEXPECTED_TYPE
5449#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005450 },
5451 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005452 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005453 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5454 0,
5455 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5456 0,
5457 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5458 -18446744073709551617.0,
5459 QCBOR_SUCCESS
5460 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005461#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005462 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005463 "Positive bignum 0x01020304 indefinite length string",
5464 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5465 0x01020304,
5466 QCBOR_SUCCESS,
5467 0x01020304,
5468 QCBOR_SUCCESS,
5469 16909060.0,
5470 QCBOR_SUCCESS
5471 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005472#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005473 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005474 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005475 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5476 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005477#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005478 0,
5479 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5480 0,
5481 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5482 -INFINITY,
5483 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005484#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5485 0,
5486 QCBOR_ERR_UNEXPECTED_TYPE,
5487 0,
5488 QCBOR_ERR_UNEXPECTED_TYPE,
5489 0.0,
5490 QCBOR_ERR_UNEXPECTED_TYPE
5491#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005492 },
5493 {
5494 "big float [9223372036854775806, 9223372036854775806]",
5495 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5496 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005497#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005498 0,
5499 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5500 0,
5501 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5502 INFINITY,
5503 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005504#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5505 0,
5506 QCBOR_ERR_UNEXPECTED_TYPE,
5507 0,
5508 QCBOR_ERR_UNEXPECTED_TYPE,
5509 0.0,
5510 QCBOR_ERR_UNEXPECTED_TYPE
5511#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005512 },
5513 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005514 "Big float 3 * 2^^2",
5515 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005516#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005517 12,
5518 QCBOR_SUCCESS,
5519 12,
5520 QCBOR_SUCCESS,
5521 12.0,
5522 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005523#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5524 0,
5525 QCBOR_ERR_UNEXPECTED_TYPE,
5526 0,
5527 QCBOR_ERR_UNEXPECTED_TYPE,
5528 0.0,
5529 QCBOR_ERR_UNEXPECTED_TYPE
5530#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005531 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005532 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005533 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005534 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5535 0,
5536 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5537 18446744073709551615ULL,
5538 QCBOR_SUCCESS,
5539 18446744073709551615.0,
5540 QCBOR_SUCCESS
5541 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005542 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005543 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005544 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5545 65536-1,
5546 QCBOR_SUCCESS,
5547 0xffff,
5548 QCBOR_SUCCESS,
5549 65535.0,
5550 QCBOR_SUCCESS
5551 },
5552 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005553 "Postive integer 0",
5554 {(uint8_t[]){0x0}, 1},
5555 0LL,
5556 QCBOR_SUCCESS,
5557 0ULL,
5558 QCBOR_SUCCESS,
5559 0.0,
5560 QCBOR_SUCCESS
5561 },
5562 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005563 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005564 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5565 -9223372036854775807-1, // INT64_MIN
5566 QCBOR_SUCCESS,
5567 0ULL,
5568 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5569 -9223372036854775808.0,
5570 QCBOR_SUCCESS
5571 },
5572 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005573 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005574 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005575#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005576 100L,
5577 QCBOR_SUCCESS,
5578 100ULL,
5579 QCBOR_SUCCESS,
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 100.3,
5587 QCBOR_SUCCESS
5588 },
5589 {
5590 "Floating point value NaN 0xfa7fc00000",
5591 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005592#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005593 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005594 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005595 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005596 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005597#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5598 0,
5599 QCBOR_ERR_HW_FLOAT_DISABLED,
5600 0,
5601 QCBOR_ERR_HW_FLOAT_DISABLED,
5602#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005603 NAN,
5604 QCBOR_SUCCESS
5605 },
5606 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005607 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005608 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005609#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5610#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5611 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005612 -4,
5613 QCBOR_SUCCESS,
5614 0,
5615 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5616 -4.0,
5617 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005618#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5619 // Float HW disabled
5620 -4,
5621 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5622 0,
5623 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5624 -4.0,
5625 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5626#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5627#else
5628 // Half-precision disabled
5629 -4,
5630 QCBOR_ERR_HALF_PRECISION_DISABLED,
5631 0,
5632 QCBOR_ERR_HALF_PRECISION_DISABLED,
5633 -4.0,
5634 QCBOR_ERR_HALF_PRECISION_DISABLED
5635#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005636 },
5637 {
5638 "Decimal fraction 3/10",
5639 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005640#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005641 0,
5642 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5643 0,
5644 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5645 0.30000000000000004,
5646 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005647#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5648 0,
5649 QCBOR_ERR_UNEXPECTED_TYPE,
5650 0,
5651 QCBOR_ERR_UNEXPECTED_TYPE,
5652 0.0,
5653 QCBOR_ERR_UNEXPECTED_TYPE
5654#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005655 },
5656 {
5657 "+inifinity",
5658 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5659#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5660 0,
5661 QCBOR_ERR_FLOAT_EXCEPTION,
5662 0,
5663 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5664#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5665 0,
5666 QCBOR_ERR_HW_FLOAT_DISABLED,
5667 0,
5668 QCBOR_ERR_HW_FLOAT_DISABLED,
5669#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5670 INFINITY,
5671 QCBOR_SUCCESS
5672 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005673
5674 {
5675 "extreme pos bignum",
5676 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5677 // 50 rows of 8 is 400 digits.
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, 0xf0,
5711 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5712 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5713 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5714 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5715 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5716 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5717 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5718 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5719 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5720 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5721 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5722 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5723 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5724 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5725 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5726 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5727 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5728 404},
5729 0,
5730 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5731 0,
5732 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5733#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5734 INFINITY,
5735 QCBOR_SUCCESS
5736#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5737 0,
5738 QCBOR_ERR_HW_FLOAT_DISABLED,
5739#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5740 },
5741
5742 {
5743 "extreme neg bignum",
5744 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5745 // 50 rows of 8 is 400 digits.
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, 0xf0,
5779 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5780 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5781 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5782 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5783 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5784 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5785 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5786 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5787 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5788 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5789 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5790 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5791 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5792 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5793 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5794 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5795 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5796 404},
5797 0,
5798 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5799 0,
5800 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5801#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5802 -INFINITY,
5803 QCBOR_SUCCESS
5804#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5805 0,
5806 QCBOR_ERR_HW_FLOAT_DISABLED,
5807#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5808 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005809
5810 {
5811 "big float underflow [9223372036854775806, -9223372036854775806]",
5812 {(uint8_t[]){
5813 0xC5, 0x82,
5814 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5815 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5816#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5817 0,
5818 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5819 0,
5820 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5821 0,
5822 QCBOR_SUCCESS
5823#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5824 0,
5825 QCBOR_ERR_UNEXPECTED_TYPE,
5826 0,
5827 QCBOR_ERR_UNEXPECTED_TYPE,
5828 0.0,
5829 QCBOR_ERR_UNEXPECTED_TYPE
5830#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5831 },
5832
5833 {
5834 "bigfloat that evaluates to -INFINITY",
5835 {(uint8_t[]){
5836 0xC5, 0x82,
5837 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5838 0xC3, 0x42, 0x01, 0x01}, 15},
5839#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5840 0,
5841 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5842 0,
5843 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5844 -INFINITY,
5845 QCBOR_SUCCESS
5846#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5847 0,
5848 QCBOR_ERR_UNEXPECTED_TYPE,
5849 0,
5850 QCBOR_ERR_UNEXPECTED_TYPE,
5851 0.0,
5852 QCBOR_ERR_UNEXPECTED_TYPE
5853#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5854 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005855};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005856
5857
5858
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005859
5860static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
5861{
5862 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
5863#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5864 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
5865 return 1;
5866 }
5867#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5868 (void)Pool;
5869#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5870 return 0;
5871}
5872
5873
Laurence Lundblade313b2862020-05-16 01:23:06 -07005874int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005875{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005876 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
5877 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005878
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005879 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5880 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005881
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005882 // Set up the decoding context including a memory pool so that
5883 // indefinite length items can be checked
5884 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005885 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005886
5887 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005888 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5889 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005890 }
5891
5892 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005893 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005894 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005895 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005896 }
5897 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005898 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005899 }
5900
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005901 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005902 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5903 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005904 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005905
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005906 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005907 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005908 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005909 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005910 }
5911 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005912 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005913 }
5914
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005915 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005916 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5917 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005918 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005919#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005920 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005921 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005922 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005923 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005924 }
5925 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5926 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005927 // NaN's can't be compared for equality. A NaN is
5928 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005929 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005930 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005931 }
5932 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005933 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005934 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005935 }
5936 }
5937 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005938#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005939 }
5940
5941 return 0;
5942}
5943
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005944
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005945
5946
Laurence Lundbladee3553422020-05-02 11:11:17 -07005947int32_t CBORSequenceDecodeTests(void)
5948{
5949 QCBORDecodeContext DCtx;
5950 QCBORItem Item;
5951 QCBORError uCBORError;
5952
5953 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005954
Laurence Lundbladee3553422020-05-02 11:11:17 -07005955 // The input for the date test happens to be a sequence so it
5956 // is reused. It is a sequence because it doesn't start as
5957 // an array or map.
5958 QCBORDecode_Init(&DCtx,
5959 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5960 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005961
Laurence Lundbladee3553422020-05-02 11:11:17 -07005962 // Get the first item
5963 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5964 if(uCBORError != QCBOR_SUCCESS) {
5965 return 1;
5966 }
5967 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5968 return 2;
5969 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005970
Laurence Lundbladee3553422020-05-02 11:11:17 -07005971 // Get a second item
5972 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005973 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5974 return 66;
5975 }
5976
5977 // Get a third item
5978 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005979 if(uCBORError != QCBOR_SUCCESS) {
5980 return 2;
5981 }
5982 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5983 return 3;
5984 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005985
Laurence Lundbladee3553422020-05-02 11:11:17 -07005986 // A sequence can have stuff at the end that may
5987 // or may not be valid CBOR. The protocol decoder knows
5988 // when to stop by definition of the protocol, not
5989 // when the top-level map or array is ended.
5990 // Finish still has to be called to know that
5991 // maps and arrays (if there were any) were closed
5992 // off correctly. When called like this it
5993 // must return the error QCBOR_ERR_EXTRA_BYTES.
5994 uCBORError = QCBORDecode_Finish(&DCtx);
5995 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5996 return 4;
5997 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005998
5999
Laurence Lundbladee3553422020-05-02 11:11:17 -07006000 // --- Test an empty input ----
6001 uint8_t empty[1];
6002 UsefulBufC Empty = {empty, 0};
6003 QCBORDecode_Init(&DCtx,
6004 Empty,
6005 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006006
Laurence Lundbladee3553422020-05-02 11:11:17 -07006007 uCBORError = QCBORDecode_Finish(&DCtx);
6008 if(uCBORError != QCBOR_SUCCESS) {
6009 return 5;
6010 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006011
6012
Laurence Lundbladee3553422020-05-02 11:11:17 -07006013 // --- Sequence with unclosed indefinite length array ---
6014 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006015
Laurence Lundbladee3553422020-05-02 11:11:17 -07006016 QCBORDecode_Init(&DCtx,
6017 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6018 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006019
Laurence Lundbladee3553422020-05-02 11:11:17 -07006020 // Get the first item
6021 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6022 if(uCBORError != QCBOR_SUCCESS) {
6023 return 7;
6024 }
6025 if(Item.uDataType != QCBOR_TYPE_INT64) {
6026 return 8;
6027 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006028
Laurence Lundbladee3553422020-05-02 11:11:17 -07006029 // Get a second item
6030 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6031 if(uCBORError != QCBOR_SUCCESS) {
6032 return 9;
6033 }
6034 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6035 return 10;
6036 }
6037
6038 // Try to finish before consuming all bytes to confirm
6039 // that the still-open error is returned.
6040 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006041 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006042 return 11;
6043 }
6044
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006045
Laurence Lundbladee3553422020-05-02 11:11:17 -07006046 // --- Sequence with a closed indefinite length array ---
6047 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006048
Laurence Lundbladee3553422020-05-02 11:11:17 -07006049 QCBORDecode_Init(&DCtx,
6050 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6051 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006052
Laurence Lundbladee3553422020-05-02 11:11:17 -07006053 // Get the first item
6054 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6055 if(uCBORError != QCBOR_SUCCESS) {
6056 return 12;
6057 }
6058 if(Item.uDataType != QCBOR_TYPE_INT64) {
6059 return 13;
6060 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006061
Laurence Lundbladee3553422020-05-02 11:11:17 -07006062 // Get a second item
6063 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6064 if(uCBORError != QCBOR_SUCCESS) {
6065 return 14;
6066 }
6067 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6068 return 15;
6069 }
6070
6071 // Try to finish before consuming all bytes to confirm
6072 // that the still-open error is returned.
6073 uCBORError = QCBORDecode_Finish(&DCtx);
6074 if(uCBORError != QCBOR_SUCCESS) {
6075 return 16;
6076 }
6077
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006078
Laurence Lundbladee3553422020-05-02 11:11:17 -07006079 return 0;
6080}
6081
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006082
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006083
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006084int32_t IntToTests()
6085{
6086 int nErrCode;
6087 int32_t n32;
6088 int16_t n16;
6089 int8_t n8;
6090 uint32_t u32;
6091 uint16_t u16;
6092 uint8_t u8;
6093 uint64_t u64;
6094
6095 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6096 if(nErrCode == -1 || n32 != 1) {
6097 return 1;
6098 }
6099
6100 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6101 if(nErrCode == -1 || n32 != INT32_MAX) {
6102 return 2;
6103 }
6104
6105 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6106 if(nErrCode == -1 || n32 != INT32_MIN) {
6107 return 3;
6108 }
6109
6110 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6111 if(nErrCode != -1) {
6112 return 4;
6113 }
6114
6115 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6116 if(nErrCode != -1) {
6117 return 5;
6118 }
6119
6120
6121 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6122 if(nErrCode == -1 || n16 != INT16_MAX) {
6123 return 6;
6124 }
6125
6126 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6127 if(nErrCode == -1 || n16 != INT16_MIN) {
6128 return 7;
6129 }
6130
6131 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6132 if(nErrCode == -1 || n16 != 1) {
6133 return 8;
6134 }
6135
6136 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6137 if(nErrCode != -1) {
6138 return 9;
6139 }
6140
6141 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6142 if(nErrCode != -1) {
6143 return 10;
6144 }
6145
6146
6147 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6148 if(nErrCode == -1 || n8 != 1) {
6149 return 11;
6150 }
6151
6152 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6153 if(nErrCode == -1 || n8 != INT8_MAX) {
6154 return 12;
6155 }
6156
6157 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6158 if(nErrCode == -1 || n8 != INT8_MIN) {
6159 return 13;
6160 }
6161
6162 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6163 if(nErrCode != -1) {
6164 return 14;
6165 }
6166
6167 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6168 if(nErrCode != -1) {
6169 return 15;
6170 }
6171
6172
6173 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6174 if(nErrCode == -1 || u32 != 1) {
6175 return 16;
6176 }
6177
6178 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6179 if(nErrCode == -1 || u32 != UINT32_MAX) {
6180 return 17;
6181 }
6182
6183 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6184 if(nErrCode == -1 || u32 != 0) {
6185 return 18;
6186 }
6187
6188 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6189 if(nErrCode != -1) {
6190 return 19;
6191 }
6192
6193 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6194 if(nErrCode != -1) {
6195 return 20;
6196 }
6197
6198
6199 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6200 if(nErrCode == -1 || u16 != UINT16_MAX) {
6201 return 21;
6202 }
6203
6204 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6205 if(nErrCode == -1 || u16 != 0) {
6206 return 22;
6207 }
6208
6209 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6210 if(nErrCode == -1 || u16 != 1) {
6211 return 23;
6212 }
6213
6214 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6215 if(nErrCode != -1) {
6216 return 24;
6217 }
6218
6219 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6220 if(nErrCode != -1) {
6221 return 25;
6222 }
6223
6224
6225 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6226 if(nErrCode == -1 || u8 != UINT8_MAX) {
6227 return 26;
6228 }
6229
6230 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6231 if(nErrCode == -1 || u8 != 0) {
6232 return 27;
6233 }
6234
6235 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6236 if(nErrCode == -1 || u8 != 1) {
6237 return 28;
6238 }
6239
6240 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6241 if(nErrCode != -1) {
6242 return 29;
6243 }
6244
6245 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6246 if(nErrCode != -1) {
6247 return 30;
6248 }
6249
6250
6251 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6252 if(nErrCode == -1 || u64 != 1) {
6253 return 31;
6254 }
6255
6256 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6257 if(nErrCode == -1 || u64 != INT64_MAX) {
6258 return 32;
6259 }
6260
6261 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6262 if(nErrCode == -1 || u64 != 0) {
6263 return 33;
6264 }
6265
6266 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6267 if(nErrCode != -1) {
6268 return 34;
6269 }
6270
6271 return 0;
6272}
6273
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006274
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006275
6276
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006277/*
6278A sequence with
6279 A wrapping bstr
6280 containing a map
6281 1
6282 2
6283 A wrapping bstr
6284 containing an array
6285 3
6286 wrapping bstr
6287 4
6288 5
6289 6
6290 array
6291 7
6292 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006293 */
6294
Laurence Lundblade55013642020-09-23 05:39:22 -07006295static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006296{
Laurence Lundblade55013642020-09-23 05:39:22 -07006297 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006298 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006299 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006300
Laurence Lundblade55013642020-09-23 05:39:22 -07006301 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006302
6303 QCBOREncode_BstrWrap(&EC);
6304 QCBOREncode_OpenMap(&EC);
6305 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6306 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6307 QCBOREncode_CloseMap(&EC);
6308 QCBOREncode_BstrWrap(&EC);
6309 QCBOREncode_OpenArray(&EC);
6310 QCBOREncode_AddInt64(&EC, 3);
6311 QCBOREncode_BstrWrap(&EC);
6312 QCBOREncode_AddInt64(&EC, 4);
6313 QCBOREncode_CloseBstrWrap(&EC, NULL);
6314 QCBOREncode_AddInt64(&EC, 5);
6315 QCBOREncode_CloseArray(&EC);
6316 QCBOREncode_CloseBstrWrap(&EC, NULL);
6317 QCBOREncode_AddInt64(&EC, 6);
6318 QCBOREncode_CloseBstrWrap(&EC, NULL);
6319 QCBOREncode_OpenArray(&EC);
6320 QCBOREncode_AddInt64(&EC, 7);
6321 QCBOREncode_AddInt64(&EC, 8);
6322 QCBOREncode_CloseArray(&EC);
6323
6324 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006325 if(uErr) {
6326 Encoded = NULLUsefulBufC;
6327 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006328
6329 return Encoded;
6330}
6331
6332
6333int32_t EnterBstrTest()
6334{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006335 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006336
6337 QCBORDecodeContext DC;
6338
Laurence Lundblade55013642020-09-23 05:39:22 -07006339 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006340
Laurence Lundblade55013642020-09-23 05:39:22 -07006341 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006342
6343
Laurence Lundblade9b334962020-08-27 10:55:53 -07006344 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006345 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006346 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6347 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006348 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006349 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006350 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006351 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006352 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006353 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006354 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006355 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006356 QCBORDecode_ExitArray(&DC);
6357 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006358 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006359 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006360 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006361 QCBORDecode_GetInt64(&DC, &n7);
6362 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006363 QCBORDecode_ExitArray(&DC);
6364
6365 QCBORError uErr = QCBORDecode_Finish(&DC);
6366
6367 return (int32_t)uErr;
6368}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006369
6370
6371
6372
6373static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006374 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006375
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006376 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006377 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006378 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6379 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6380 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006381
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006382 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006383 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6384 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6385 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006386
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006387 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006388 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006389 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6390 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006391
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006392 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006393 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6394 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006395
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006396 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006397 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006398 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6399 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006400
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006401 21,
6402 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6403 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6404
6405 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006406 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006407 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6408 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006409
6410 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006411 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6412 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006413
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006414 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006415 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006416 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6417 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006418
6419 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006420 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6421 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006422
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006423 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006424 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006425 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6426 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006427
6428 0x18, 0x33,
6429 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6430
6431 // MIME
6432 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006433 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6434 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6435 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006436
6437 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006438 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6439 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006440
6441 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006442 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6443 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6444 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006445
6446 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006447 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6448 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006449
6450 // UUID
6451 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006452 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6453 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006454
6455 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006456 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6457 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006458};
6459
6460int32_t DecodeTaggedTypeTests()
6461{
6462 QCBORDecodeContext DC;
6463 QCBORError uErr;
6464
6465 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6466
6467 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006468 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006469
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006470 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006471 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006472 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006473 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6474 return 1;
6475 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006476 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006477 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6478 return 2;
6479 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006480 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006481 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6482 return 3;
6483 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006484 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006485 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006486 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6487 return 4;
6488 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006489 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006490 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006491 return 5;
6492 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006493
Laurence Lundblade9b334962020-08-27 10:55:53 -07006494 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006495 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6496 bNeg != false) {
6497 return 10;
6498 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006499 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006500 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6501 bNeg != true) {
6502 return 11;
6503 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006504 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006505 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6506 return 12;
6507 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006508 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
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 13;
6511 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006512 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
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 14;
6515 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006516
Laurence Lundblade9b334962020-08-27 10:55:53 -07006517 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006518 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6519 return 20;
6520 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006521 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006522 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6523 return 21;
6524 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006525 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_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 22;
6528 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006529 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_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 23;
6532 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006533
Laurence Lundblade9b334962020-08-27 10:55:53 -07006534 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006535 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6536 return 30;
6537 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006538 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006539 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6540 return 31;
6541 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006542 QCBORDecode_GetB64InMapN(&DC, 32, 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 32;
6545 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006546 QCBORDecode_GetB64InMapSZ(&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 33;
6549 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006550
Laurence Lundblade9b334962020-08-27 10:55:53 -07006551 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006552 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6553 return 40;
6554 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006555 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006556 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6557 return 41;
6558 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006559 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_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 42;
6562 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006563 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_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 43;
6566 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006567
Laurence Lundblade9b334962020-08-27 10:55:53 -07006568 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006569 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6570 return 50;
6571 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006572 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006573 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6574 return 51;
6575 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006576 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006577 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006578 return 52;
6579 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006580 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006581 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006582 return 53;
6583 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006584
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006585 // MIME
6586 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006587 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006588 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6589 bIsNot7Bit == true) {
6590 return 60;
6591 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006592 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006593 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6594 bIsNot7Bit == true) {
6595 return 61;
6596 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006597 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006598 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6599 bIsNot7Bit == false) {
6600 return 62;
6601 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006602 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006603 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6604 bIsNot7Bit == false) {
6605 return 63;
6606 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006607 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
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 64;
6610 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006611 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
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 65;
6614 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006615
Laurence Lundblade9b334962020-08-27 10:55:53 -07006616 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006617 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6618 return 70;
6619 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006620 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006621 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6622 return 71;
6623 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006624 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006625 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006626 return 72;
6627 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006628 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006629 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006630 return 73;
6631 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006632
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006633 // Improvement: add some more error test cases
6634
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006635 QCBORDecode_ExitMap(&DC);
6636
6637 uErr = QCBORDecode_Finish(&DC);
6638 if(uErr != QCBOR_SUCCESS) {
6639 return 100;
6640 }
6641
6642 return 0;
6643}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006644
6645
6646
6647
6648/*
6649 [
6650 "aaaaaaaaaa",
6651 {}
6652 ]
6653 */
6654static const uint8_t spTooLarge1[] = {
6655 0x9f,
6656 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6657 0xa0,
6658 0xff
6659};
6660
6661/*
6662 [
6663 {
6664 0: "aaaaaaaaaa"
6665 }
6666 ]
6667 */
6668static const uint8_t spTooLarge2[] = {
6669 0x9f,
6670 0xa1,
6671 0x00,
6672 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6673 0xff
6674};
6675
6676/*
6677 h'A1006A61616161616161616161'
6678
6679 {
6680 0: "aaaaaaaaaa"
6681 }
6682 */
6683static const uint8_t spTooLarge3[] = {
6684 0x4d,
6685 0xa1,
6686 0x00,
6687 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6688};
6689
6690int32_t TooLargeInputTest(void)
6691{
6692 QCBORDecodeContext DC;
6693 QCBORError uErr;
6694 UsefulBufC String;
6695
6696 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6697 // to 10 There's not really any way to test this error
6698 // condition. The error condition is not complex, so setting
6699 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6700
6701 // The input CBOR is only too large because the
6702 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6703 //
6704 // This test is disabled for the normal test runs because of the
6705 // special build requirement.
6706
6707
6708 // Tests the start of a map being too large
6709 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006710 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006711 QCBORDecode_GetTextString(&DC, &String);
6712 uErr = QCBORDecode_GetError(&DC);
6713 if(uErr != QCBOR_SUCCESS) {
6714 return 1;
6715 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006716 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006717 uErr = QCBORDecode_GetError(&DC);
6718 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6719 return 2;
6720 }
6721
6722 // Tests the end of a map being too large
6723 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006724 QCBORDecode_EnterArray(&DC, NULL);
6725 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006726 uErr = QCBORDecode_GetError(&DC);
6727 if(uErr != QCBOR_SUCCESS) {
6728 return 3;
6729 }
6730 QCBORDecode_ExitMap(&DC);
6731 uErr = QCBORDecode_GetError(&DC);
6732 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6733 return 4;
6734 }
6735
6736 // Tests the entire input CBOR being too large when processing bstr wrapping
6737 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6738 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6739 uErr = QCBORDecode_GetError(&DC);
6740 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6741 return 5;
6742 }
6743
6744 return 0;
6745}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006746
6747
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006748#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6749
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006750static const uint8_t spMapWithIndefLenStrings[] = {
6751 0xbf,
6752 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6753 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6754 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6755 0x03,
6756 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6757 0xc3,
6758 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6759 0xff
6760};
6761
6762int32_t SpiffyIndefiniteLengthStringsTests()
6763{
6764 QCBORDecodeContext DCtx;
6765
6766 QCBORDecode_Init(&DCtx,
6767 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6768 QCBOR_DECODE_MODE_NORMAL);
6769
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006770 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006771 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6772
6773 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006774 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006775 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6776 if(QCBORDecode_GetAndResetError(&DCtx)) {
6777 return 1;
6778 }
6779
6780 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6781 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6782 return 2;
6783 }
6784
6785 uint64_t uInt;
6786 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6787 if(QCBORDecode_GetAndResetError(&DCtx)) {
6788 return 3;
6789 }
6790 if(uInt != 3) {
6791 return 4;
6792 }
6793
6794 double uDouble;
6795 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6796 "label2",
6797 0xff,
6798 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006799#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006800 if(QCBORDecode_GetAndResetError(&DCtx)) {
6801 return 5;
6802 }
6803 if(uDouble != -16777474) {
6804 return 6;
6805 }
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006806#else
6807 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6808 return 7;
6809 }
6810#endif
6811
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006812
6813 QCBORDecode_ExitMap(&DCtx);
6814
6815 if(QCBORDecode_Finish(&DCtx)) {
6816 return 99;
6817 }
6818
6819 return 0;
6820}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006821#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08006822
6823
6824
6825int32_t PeekTest()
6826{
6827 QCBORItem Item;
6828 QCBORError nCBORError;
6829 QCBORDecodeContext DCtx;
6830
6831 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6832
6833 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6834 return 100+(int32_t)nCBORError;
6835 }
6836 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6837 return 200;
6838 }
6839
6840 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6841 return (int32_t)nCBORError;
6842 }
6843 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6844 return 300;
6845 }
6846
6847 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6848 return 400 + (int32_t)nCBORError;
6849 }
6850 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6851 return 500;
6852 }
6853
6854 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6855 return (int32_t)nCBORError;
6856 }
6857 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6858 return 600;
6859 }
6860
6861 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6862 return 900 + (int32_t)nCBORError;
6863 }
6864 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6865 Item.uDataType != QCBOR_TYPE_INT64 ||
6866 Item.val.int64 != 42 ||
6867 Item.uDataAlloc ||
6868 Item.uLabelAlloc ||
6869 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6870 return 1000;
6871 }
6872
6873 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6874 return 1100 + (int32_t)nCBORError;
6875 }
6876
6877 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6878 Item.uDataType != QCBOR_TYPE_INT64 ||
6879 Item.val.int64 != 42 ||
6880 Item.uDataAlloc ||
6881 Item.uLabelAlloc ||
6882 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6883 return 1200;
6884 }
6885
6886
6887 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6888 return 1300 + (int32_t)nCBORError;
6889 }
6890 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6891 Item.uDataAlloc ||
6892 Item.uLabelAlloc ||
6893 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
6894 Item.uDataType != QCBOR_TYPE_ARRAY ||
6895 Item.val.uCount != 2)
6896 return 1400;
6897
6898 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6899 return 1500 + (int32_t)nCBORError;
6900 }
6901 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6902 Item.uDataAlloc ||
6903 Item.uLabelAlloc ||
6904 UsefulBufCompareToSZ(Item.val.string, "string1")) {
6905 return 1600;
6906 }
6907
6908 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6909 return 1700 + (int32_t)nCBORError;
6910 }
6911 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6912 Item.uDataAlloc ||
6913 Item.uLabelAlloc ||
6914 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6915 return 1800;
6916 }
6917
6918 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6919 return (int32_t)nCBORError;
6920 }
6921 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6922 Item.uDataAlloc ||
6923 Item.uLabelAlloc ||
6924 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6925 return 1900;
6926 }
6927
6928 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6929 return (int32_t)nCBORError;
6930 }
6931 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6932 Item.uDataAlloc ||
6933 Item.uLabelAlloc ||
6934 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6935 return 2000;
6936 }
6937
6938
6939 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6940 return 2100 + (int32_t)nCBORError;
6941 }
6942 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6943 Item.uDataAlloc ||
6944 Item.uLabelAlloc ||
6945 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
6946 Item.uDataType != QCBOR_TYPE_MAP ||
6947 Item.val.uCount != 4) {
6948 return 2100;
6949 }
6950
6951 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6952 return 2200 + (int32_t)nCBORError;
6953 }
6954 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6955 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
6956 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6957 Item.uDataAlloc ||
6958 Item.uLabelAlloc ||
6959 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
6960 return 2300;
6961 }
6962
6963 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6964 return 2400 + (int32_t)nCBORError;
6965 }
6966 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6967 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6968 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6969 Item.uDataAlloc ||
6970 Item.uLabelAlloc ||
6971 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6972 return 2500;
6973 }
6974
6975 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6976 return 2600 + (int32_t)nCBORError;
6977 }
6978 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6979 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6980 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6981 Item.uDataAlloc ||
6982 Item.uLabelAlloc ||
6983 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6984 return 2700;
6985 }
6986
6987 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6988 return 2800 + (int32_t)nCBORError;
6989 }
6990 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6991 Item.uDataAlloc ||
6992 Item.uLabelAlloc ||
6993 UsefulBufCompareToSZ(Item.label.string, "another int") ||
6994 Item.uDataType != QCBOR_TYPE_INT64 ||
6995 Item.val.int64 != 98)
6996 return 2900;
6997
6998 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6999 return 3000 + (int32_t)nCBORError;
7000 }
7001 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7002 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7003 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7004 Item.uDataAlloc ||
7005 Item.uLabelAlloc ||
7006 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7007 return 3100;
7008 }
7009
7010 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7011 return 3200 + (int32_t)nCBORError;
7012 }
7013 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7014 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7015 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7016 Item.uDataAlloc ||
7017 Item.uLabelAlloc ||
7018 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7019 return 3300;
7020 }
7021
7022 return 0;
7023}