blob: 205eada00f235cacc6508782108356c2693cc418 [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
1100 set of maps
1101 */
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 Lundbladeccfb8cd2018-12-07 21:11:30 +09001311 return 0;
1312}
1313
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001314
1315/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301316 Fully or partially decode pValidMapEncoded. When
1317 partially decoding check for the right error code.
1318 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001319
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301320 The partial decodes test error conditions of
1321 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301323 This could be combined with the above test
1324 and made prettier and maybe a little more
1325 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001326 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001327static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328{
1329 QCBORDecodeContext DCtx;
1330 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001331 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001332
Laurence Lundbladeee851742020-01-08 08:37:05 -08001333 QCBORDecode_Init(&DCtx,
1334 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1335 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001336
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001337 if(nLevel < 1) {
1338 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1339 return -1;
1340 } else {
1341 return 0;
1342 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001343 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301344
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001345
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001346 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001347 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001348 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349 if(Item.uDataType != QCBOR_TYPE_MAP ||
1350 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001351 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001353 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001354 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1355 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001356 } else {
1357 return 0;
1358 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001359 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001360
1361
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001362 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001363 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001364 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001365 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001366 Item.uDataType != QCBOR_TYPE_INT64 ||
1367 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001368 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001369 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001370 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001371
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001372 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001373 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1374 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001375 } else {
1376 return 0;
1377 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001378 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001379
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001380 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001381 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001382 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001383 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001384 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001387 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001388 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001389
1390
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001391 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001392 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1393 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001394 } else {
1395 return 0;
1396 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001397 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001398
1399
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001401 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001402 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001404 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001405 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001406 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001407
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001408 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001409 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1410 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001411 } else {
1412 return 0;
1413 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001414 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001415
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001416 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001417 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001418 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001419 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001420 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001421 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001422 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001423
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001424 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001425 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1426 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001427 } else {
1428 return 0;
1429 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001430 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001431
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001432 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001433 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001434 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001435 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001436 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437 Item.uDataType != QCBOR_TYPE_MAP ||
1438 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001439 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001440
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001441 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001442 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1443 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 } else {
1445 return 0;
1446 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001447 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001448
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001449 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001450 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001451 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001452 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001453 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001455 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001456 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001457 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001458
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001459 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001460 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1461 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001462 } else {
1463 return 0;
1464 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001465 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001466
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001467 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001468 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001469 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001470 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001471 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001472 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001473 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001474 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001475 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001476
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001477 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001478 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1479 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001480 } else {
1481 return 0;
1482 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001483 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001484
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001485 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001486 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001487 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001488 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001489 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 Item.uDataType != QCBOR_TYPE_INT64 ||
1491 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001492 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001493
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001494 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001495 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1496 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001497 } else {
1498 return 0;
1499 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001500 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001501
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001502 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001503 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001504 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001505 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001506 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001507 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001508 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001509 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001510 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001511
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301512 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001513 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001515
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001516 return 0;
1517}
1518
1519
1520
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001521
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001522int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001524 // Parse a moderatly complex map structure very thoroughly
1525 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1526 if(nResult) {
1527 return nResult;
1528 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001529
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001530 // Again, but in strings-only mode. It should succeed since the input
1531 // map has only string labels.
1532 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1533 if(nResult) {
1534 return nResult;
1535 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001536
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001537 // Again, but try to finish the decoding before the end of the
1538 // input at 10 different place and see that the right error code
1539 // is returned.
1540 for(int i = 0; i < 10; i++) {
1541 nResult = ExtraBytesTest(i);
1542 if(nResult) {
1543 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001544 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001545 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001546
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001547 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548}
1549
1550
Laurence Lundbladeee851742020-01-08 08:37:05 -08001551static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1552 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1553 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001555int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001556{
1557 QCBORDecodeContext DCtx;
1558 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001559 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001560
1561
Laurence Lundbladeee851742020-01-08 08:37:05 -08001562 QCBORDecode_Init(&DCtx,
1563 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1564 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001565
1566
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001567 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001568 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1570 Item.val.uCount != 10)
1571 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001573 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001574 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 if(Item.uDataType != QCBOR_TYPE_FALSE)
1576 return -1;
1577
1578 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001579 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001580 if(Item.uDataType != QCBOR_TYPE_TRUE)
1581 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001584 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585 if(Item.uDataType != QCBOR_TYPE_NULL)
1586 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001589 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1591 return -1;
1592
1593 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001594 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001595 return -1;
1596
1597 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001598 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001599 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1600 return -1;
1601
1602 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001603 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001604 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1605 return -1;
1606
Laurence Lundblade077475f2019-04-26 09:06:33 -07001607 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001608 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001609
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;
1612
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;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001615
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001616 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001617 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001618 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1619 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001620
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001621 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001622 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001623 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1624 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001625
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001626 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001627
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001628}
1629
1630
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001631int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001632{
1633 // Loop over all the not-well-formed instance of CBOR
1634 // that are test vectors in not_well_formed_cbor.h
1635 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1636 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1637 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1638 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1639
Laurence Lundbladeee851742020-01-08 08:37:05 -08001640 // Set up decoder context. String allocator needed for indefinite
1641 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001642 QCBORDecodeContext DCtx;
1643 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1644 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1645 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1646
1647 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001648 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001649 do {
1650 QCBORItem Item;
1651
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001652 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1653 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001654
1655 // Every test vector must fail with
1656 // a not-well-formed error. If not
1657 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001658 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001659 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001660 // Return index of failure in the error code
1661 return 2000 + nIterate;
1662 }
1663 }
1664 return 0;
1665}
1666
1667
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001668struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001669 UsefulBufC Input;
1670 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001671};
1672
Laurence Lundblade59289e52019-12-30 13:44:37 -08001673
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001674static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001675{
1676 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1677 // Set up the decoding context including a memory pool so that
1678 // indefinite length items can be checked
1679 QCBORDecodeContext DCtx;
1680 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1681 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001682
Laurence Lundblade59289e52019-12-30 13:44:37 -08001683 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1684 if(nCBORError) {
1685 return -9;
1686 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001687
Laurence Lundblade59289e52019-12-30 13:44:37 -08001688 // Iterate until there is an error of some sort error
1689 QCBORItem Item;
1690 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001691 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001692 memset(&Item, 0x33, sizeof(Item));
1693
1694 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1695 } while(nCBORError == QCBOR_SUCCESS);
1696
1697 // Must get the expected error or the this test fails
1698 // The data and label type must also be QCBOR_TYPE_NONE
1699 if(nCBORError != pF->nError ||
1700 Item.uDataType != QCBOR_TYPE_NONE ||
1701 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001702 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001703 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001704 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001705 }
1706 }
1707
1708 return 0;
1709}
1710
1711
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001712struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001713 // Most of this is copied from not_well_formed.h. Here the error code
1714 // returned is also checked.
1715
1716 // Indefinite length strings must be closed off
1717 // An indefinite length byte string not closed off
1718 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1719 // An indefinite length text string not closed off
1720 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1721
1722
1723 // All the chunks in an indefinite length string must be of the type of indefinite length string
1724 // indefinite length byte string with text string chunk
1725 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1726 // indefinite length text string with a byte string chunk
1727 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1728 // indefinite length byte string with an positive integer chunk
1729 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1730 // indefinite length byte string with an negative integer chunk
1731 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1732 // indefinite length byte string with an array chunk
1733 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1734 // indefinite length byte string with an map chunk
1735 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1736 // indefinite length byte string with tagged integer chunk
1737 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1738 // indefinite length byte string with an simple type chunk
1739 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1740 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1741 // indefinite length text string with indefinite string inside
1742 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1743
1744
1745 // Definte length maps and arrays must be closed by having the right number of items
1746 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001747 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001748 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001749 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001750 // A definte length array that is supposed to have 511 items, but has only 1
1751 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1752 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001753 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001754 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001755 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001756
1757
1758 // Indefinte length maps and arrays must be ended by a break
1759 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001760 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001761 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001762 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001764 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001766 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001767
1768
1769 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001770 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001771 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001772 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001773 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001774 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001775 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001776 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001777 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001778 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001779 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001780 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001781 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001782 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001783
1784
1785 // The "argument" for the data item is incomplete
1786 // Positive integer missing 1 byte argument
1787 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1788 // Positive integer missing 2 byte argument
1789 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1790 // Positive integer missing 4 byte argument
1791 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1792 // Positive integer missing 8 byte argument
1793 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1794 // Positive integer missing 1 byte of 2 byte argument
1795 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1796 // Positive integer missing 2 bytes of 4 byte argument
1797 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1798 // Positive integer missing 1 bytes of 7 byte argument
1799 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1800 // Negative integer missing 1 byte argument
1801 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1802 // Binary string missing 1 byte argument
1803 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1804 // Text string missing 1 byte argument
1805 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1806 // Array missing 1 byte argument
1807 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1808 // Map missing 1 byte argument
1809 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1810 // Tag missing 1 byte argument
1811 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1812 // Simple missing 1 byte argument
1813 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1814
1815
1816 // Breaks must not occur in definite length arrays and maps
1817 // Array of length 1 with sole member replaced by a break
1818 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1819 // Array of length 2 with 2nd member replaced by a break
1820 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1821 // Map of length 1 with sole member label replaced by a break
1822 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1823 // Map of length 1 with sole member label replaced by break
1824 // Alternate representation that some decoders handle difference
1825 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1826 // Array of length 1 with 2nd member value replaced by a break
1827 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1828 // Map of length 2 with 2nd member replaced by a break
1829 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1830
1831
1832 // Breaks must not occur on their own out of an indefinite length data item
1833 // A bare break is not well formed
1834 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1835 // A bare break after a zero length definite length array
1836 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1837 // A bare break after a zero length indefinite length map
1838 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1839
1840
1841 // Forbidden two byte encodings of simple types
1842 // Must use 0xe0 instead
1843 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1844 // Should use 0xe1 instead
1845 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1846 // Should use 0xe2 instead
1847 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1848 // Should use 0xe3 instead
1849 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1850 // Should use 0xe4 instead
1851 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1852 // Should use 0xe5 instead
1853 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1854 // Should use 0xe6 instead
1855 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1856 // Should use 0xe7 instead
1857 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1858 // Should use 0xe8 instead
1859 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1860 // Should use 0xe9 instead
1861 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1862 // Should use 0xea instead
1863 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1864 // Should use 0xeb instead
1865 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1866 // Should use 0xec instead
1867 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1868 // Should use 0xed instead
1869 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1870 // Should use 0xee instead
1871 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1872 // Should use 0xef instead
1873 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1874 // Should use 0xf0 instead
1875 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1876 // Should use 0xf1 instead
1877 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1878 // Should use 0xf2 instead
1879 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1880 // Must use 0xf3 instead
1881 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1882 // Must use 0xf4 instead
1883 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1884 // Must use 0xf5 instead
1885 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1886 // Must use 0xf6 instead
1887 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1888 // Must use 0xf7 instead
1889 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1890 // Must use 0xf8 instead
1891 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1892
1893
1894 // Integers with additional info indefinite length
1895 // Positive integer with additional info indefinite length
1896 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1897 // Negative integer with additional info indefinite length
1898 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1899 // CBOR tag with "argument" an indefinite length
1900 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1901 // CBOR tag with "argument" an indefinite length alternate vector
1902 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1903
1904
1905 // Missing bytes from a deterministic length string
1906 // A byte string is of length 1 without the 1 byte
1907 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1908 // A text string is of length 1 without the 1 byte
1909 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001910 // Byte string should have 2^32-15 bytes, but has one
1911 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1912 // Byte string should have 2^32-15 bytes, but has one
1913 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001914
1915
1916 // Use of unassigned additional information values
1917 // Major type positive integer with reserved value 28
1918 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1919 // Major type positive integer with reserved value 29
1920 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1921 // Major type positive integer with reserved value 30
1922 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1923 // Major type negative integer with reserved value 28
1924 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1925 // Major type negative integer with reserved value 29
1926 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1927 // Major type negative integer with reserved value 30
1928 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1929 // Major type byte string with reserved value 28 length
1930 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1931 // Major type byte string with reserved value 29 length
1932 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1933 // Major type byte string with reserved value 30 length
1934 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1935 // Major type text string with reserved value 28 length
1936 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1937 // Major type text string with reserved value 29 length
1938 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1939 // Major type text string with reserved value 30 length
1940 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1941 // Major type array with reserved value 28 length
1942 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1943 // Major type array with reserved value 29 length
1944 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1945 // Major type array with reserved value 30 length
1946 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1947 // Major type map with reserved value 28 length
1948 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1949 // Major type map with reserved value 29 length
1950 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1951 // Major type map with reserved value 30 length
1952 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1953 // Major type tag with reserved value 28 length
1954 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1955 // Major type tag with reserved value 29 length
1956 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1957 // Major type tag with reserved value 30 length
1958 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1959 // Major type simple with reserved value 28 length
1960 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1961 // Major type simple with reserved value 29 length
1962 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1963 // Major type simple with reserved value 30 length
1964 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1965
1966
1967 // Maps must have an even number of data items (key & value)
1968 // Map with 1 item when it should have 2
1969 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1970 // Map with 3 item when it should have 4
1971 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1972 // Map with 1 item when it should have 2
1973 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1974 // Map with 3 item when it should have 4
1975 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1976
1977
1978 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001979 // Text-based date, with an integer
1980 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1981 // Epoch date, with an byte string
1982 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1983 // tagged as both epoch and string dates
1984 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1985 // big num tagged an int, not a byte string
1986 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001987};
1988
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001989int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001990{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001991 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001992
Laurence Lundblade59289e52019-12-30 13:44:37 -08001993 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1994 if(nResult) {
1995 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001996 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001997
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001998 // Corrupt the UsefulInputBuf and see that
1999 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002000 QCBORDecodeContext DCtx;
2001 QCBORItem Item;
2002 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002003
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002004 QCBORDecode_Init(&DCtx,
2005 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2006 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002007
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002008 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2009 return (int32_t)uQCBORError;
2010 }
2011 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2012 // This wasn't supposed to happen
2013 return -1;
2014 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002015
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002016 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002017
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002018 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2019 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2020 // Did not get back the error expected
2021 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002022 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002023
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002024
Laurence Lundblade98427e92020-09-28 21:33:23 -07002025 /*
2026 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2027 tests here can be performed to see that the max length
2028 error check works correctly. See DecodeBytes(). If the max
2029 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002030
Laurence Lundblade98427e92020-09-28 21:33:23 -07002031 This test will automatocally adapt the all CPU sizes
2032 through the use of SIZE_MAX.
2033 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002034
Laurence Lundblade98427e92020-09-28 21:33:23 -07002035 MakeUsefulBufOnStack( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
2036 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002037
Laurence Lundblade98427e92020-09-28 21:33:23 -07002038 // This makes a CBOR head with a text string that is very long
2039 // but doesn't fill in the bytes of the text string as that is
2040 // not needed to test this part of QCBOR.
2041 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2042
2043 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2044
2045 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2046 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002047 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002048
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002049 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002050}
2051
2052
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002053/* Try all 256 values of the byte at nLen including recursing for
2054 each of the values to try values at nLen+1 ... up to nLenMax
2055 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002056static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002057{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002058 if(nLen >= nLenMax) {
2059 return;
2060 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002061
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002062 for(int inputByte = 0; inputByte < 256; inputByte++) {
2063 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002064 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002065 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002066
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002067 // Get ready to parse
2068 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002069 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002070
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002071 // Parse by getting the next item until an error occurs
2072 // Just about every possible decoder error can occur here
2073 // The goal of this test is not to check for the correct
2074 // error since that is not really possible. It is to
2075 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002076 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002077 QCBORItem Item;
2078 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002079 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080 break;
2081 }
2082 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002083
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002084 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002085 }
2086}
2087
2088
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002089int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002090{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002091 // Size 2 tests 64K inputs and runs quickly
2092 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002093
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002094 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002095
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002096 return 0;
2097}
2098
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002099
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002100int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002101{
2102 // size 3 tests 16 million inputs and runs OK
2103 // in seconds on fast machines. Size 4 takes
2104 // 10+ minutes and 5 half a day on fast
2105 // machines. This test is kept separate from
2106 // the others so as to no slow down the use
2107 // of them as a very frequent regression.
2108 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002109
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002110 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002111
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002112 return 0;
2113}
2114
2115
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002116static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002117 0xc0, // tag for string date
2118 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002119
Laurence Lundbladec7114722020-08-13 05:11:40 -07002120 0xc0, // tag for string date
2121 0x00, // Wrong type for a string date
2122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002123 0xc1, // tag for epoch date
2124 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2125
Laurence Lundbladec7114722020-08-13 05:11:40 -07002126 0xc1,
2127 0x62, 'h', 'i', // wrong type tagged
2128
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002129 // CBOR_TAG_B64
Laurence Lundblade9b334962020-08-27 10:55:53 -07002130 0xcf, 0xd8, 0x22, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131 0x1a, 0x53, 0x72, 0x4E, 0x01,
2132
2133 0xc1, // tag for epoch date
2134 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002135
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002136 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002137 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002138
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002139 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002140 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002141
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002142 0xc1, // tag for epoch date
2143 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2144 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2145
2146 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002147 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2148
2149 0xc1, // tag for epoch date
2150 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2151
2152 0xc1,
2153 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2154
2155 0xc1, // tag for epoch date
2156 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002157};
2158
2159
Laurence Lundbladec7114722020-08-13 05:11:40 -07002160
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002162#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002163static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002164
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002165 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002167 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002169 return diff > 0.0000001;
2170}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002171#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002172
2173
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002174int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002175{
2176 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002177 QCBORItem Item;
2178 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002179
Laurence Lundbladeee851742020-01-08 08:37:05 -08002180 QCBORDecode_Init(&DCtx,
2181 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2182 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002183
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002184 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002185 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002186 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002187 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002188 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002189 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002190 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002191 }
2192
Laurence Lundbladec7114722020-08-13 05:11:40 -07002193 // Wrong type for a string date
2194 uError = QCBORDecode_GetNext(&DCtx, &Item);
2195 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002196 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002197 }
2198
2199 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2200 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2201 return -4;
2202 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002203 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2204 Item.val.epochDate.nSeconds != 1400000000 ||
2205 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002206 return -5;
2207 }
2208
2209 // Wrong type for an epoch date
2210 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2211 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002213
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002214 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2215 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002216 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2217 return -7;
2218 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002219 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2220 Item.val.epochDate.nSeconds != 1400000001 ||
2221 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002222 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002223 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002225
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002226 // Epoch date that is too large for our representation
2227 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002228 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002230
Laurence Lundblade9682a532020-06-06 18:33:04 -07002231#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002232 // Epoch date in float format with fractional seconds
2233 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2234 return -10;
2235 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002236 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2237 Item.val.epochDate.nSeconds != 1 ||
2238 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002239 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002242 // Epoch date float that is too large for our representation
2243 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002244 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002247 // Epoch date double that is just slightly too large
2248 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002249 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002250 }
2251
2252 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002253 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2254 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2255 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2256 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002257 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002258 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002259
2260 // Nan
2261 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2262 return -15;
2263 }
2264
2265 // +Inifinity double-precision
2266 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2267 return -16;
2268 }
2269
2270#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2271 // -Inifinity half-precision
2272 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2273 return -17;
2274 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002275#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002276 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2277 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002278 }
2279#endif
2280
Laurence Lundbladec7114722020-08-13 05:11:40 -07002281#else
2282 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2283 return -19;
2284 }
2285 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2286 return -20;
2287 }
2288 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2289 return -21;
2290 }
2291 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2292 return -22;
2293 }
2294 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2295 return -23;
2296 }
2297 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2298 return -24;
2299 }
2300#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2301 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2302 return -25;
2303 }
2304#else
2305 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2306 return -26;
2307 }
2308#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002309
Laurence Lundbladec7114722020-08-13 05:11:40 -07002310#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002311
2312 return 0;
2313}
2314
Laurence Lundblade4b270642020-08-14 12:53:07 -07002315/*
2316 Test cases covered here. Some items cover more than one of these.
2317 positive integer (zero counts as a positive integer)
2318 negative integer
2319 half-precision float
2320 single-precision float
2321 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002322
Laurence Lundblade4b270642020-08-14 12:53:07 -07002323 float Overflow error
2324 Wrong type error for epoch
2325 Wrong type error for date string
2326 float disabled error
2327 half-precision disabled error
2328 -Infinity
2329 Slightly too large integer
2330 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002331
Laurence Lundblade4b270642020-08-14 12:53:07 -07002332 Get epoch by int
2333 Get string by int
2334 Get epoch by string
2335 Get string by string
2336 Fail to get epoch by wrong int label
2337 Fail to get string by wrong string label
2338 Fail to get epoch by string because it is invalid
2339 Fail to get epoch by int because it is invalid
2340
2341 Untagged values
2342 */
2343static uint8_t spSpiffyDateTestInput[] = {
2344 0x86,
2345
2346 0xc1,
2347 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2348
Laurence Lundbladec7114722020-08-13 05:11:40 -07002349 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002350 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2351
2352 0xc1, // tag for epoch date
2353 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2354
2355 0xc1, // tag for epoch date
2356 0x9f, 0xff, // Erroneous empty array as content for date
2357
2358 0xc0, // tag for string date
2359 0xbf, 0xff, // Erroneous empty map as content for date
2360
2361 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002362
2363 0x00,
2364 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002365 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002366
2367 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002368 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002369 0xc1, // tag for epoch date
2370 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2371
2372 // Untagged integer 0
2373 0x08,
2374 0x00,
2375
2376 // Utagged date string with string label y
2377 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002378 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002379
2380 // Untagged -1000 with label z
2381 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002382 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002383 0x39, 0x03, 0xe7,
2384
Laurence Lundbladec7114722020-08-13 05:11:40 -07002385 0x07,
2386 0xc1, // tag for epoch date
2387 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2388
Laurence Lundblade4b270642020-08-14 12:53:07 -07002389 0x05,
2390 0xc1,
2391 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2392
Laurence Lundbladec7114722020-08-13 05:11:40 -07002393 // Untagged single-precision float with value 3.14 with string label x
2394 0x61, 0x78,
2395 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2396
Laurence Lundbladec7114722020-08-13 05:11:40 -07002397 // Untagged half-precision float with value -2
2398 0x09,
2399 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002400
2401 0xff,
2402};
2403
2404int32_t SpiffyDateDecodeTest()
2405{
2406 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002407 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002408 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2409 nEpochDate4, nEpochDate6, nEpochDateFail,
2410 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002411 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002412 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002413
2414 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002415 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002416 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002417 QCBORDecode_EnterArray(&DC);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002418
Laurence Lundblade9b334962020-08-27 10:55:53 -07002419 // Too-negative float, -9.2233720368547748E+18
2420 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002421 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002422#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002423 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2424 return 1111;
2425 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002426#else
2427 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2428 return 1112;
2429 }
2430#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002431
2432 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002433 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002434 uError = QCBORDecode_GetAndResetError(&DC);
2435 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002436 return 1;
2437 }
2438
Laurence Lundblade4b270642020-08-14 12:53:07 -07002439 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002440 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002441 uError = QCBORDecode_GetAndResetError(&DC);
2442#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2443#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2444 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2445#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2446 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2447#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2448#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2449 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2450#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2451 if(uError != uExpectedforHalfMinusInfinity) {
2452 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002453 }
2454
Laurence Lundblade4b270642020-08-14 12:53:07 -07002455 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002456 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002457 uError = QCBORDecode_GetAndResetError(&DC);
2458 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2459 return 3;
2460 }
2461
2462 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002463 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002464 uError = QCBORDecode_GetAndResetError(&DC);
2465 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2466 return 4;
2467 }
2468
2469 QCBORDecode_EnterMap(&DC);
2470
2471 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002472 QCBORDecode_GetEpochDateInMapN(&DC,
2473 5,
2474 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2475 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2476 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002477#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2478 if(nEpochDate2 != -9223372036854773760LL) {
2479 return 101;
2480 }
2481#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2482 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002483 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002484 return 102;
2485 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002486#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002487
Laurence Lundblade4b270642020-08-14 12:53:07 -07002488 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002489 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2490 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002491#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2492 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002493 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002494 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002495#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2496 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002497 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002498 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002499 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002500#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2501
2502 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002503 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2504 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002505#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2506 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002507 return 103;
2508 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002509#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2510 uError = QCBORDecode_GetAndResetError(&DC);
2511 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2512 return 104;
2513 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002514#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2515
Laurence Lundblade9b334962020-08-27 10:55:53 -07002516 // A half-precision date with value -2 FFF
2517 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2518 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002519#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2520 if(nEpochDate4 != -2) {
2521 return 105;
2522 }
2523#else
2524 uError = QCBORDecode_GetAndResetError(&DC);
2525 if(uError == QCBOR_SUCCESS) {
2526 return 106;
2527 }
2528#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002529
Laurence Lundblade4b270642020-08-14 12:53:07 -07002530
2531 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002532 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2533 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2534 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002535 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002536 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002537 return 107;
2538 }
2539
2540 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002541 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2542 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002543 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002544 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002545 return 108;
2546 }
2547
2548 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002549 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2550 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2551 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002552 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002553 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002554 return 109;
2555 }
2556
2557 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002558 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2559 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002560 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002561 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002562 return 110;
2563 }
2564
2565 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002566
Laurence Lundblade4b270642020-08-14 12:53:07 -07002567 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002568 QCBORDecode_GetEpochDateInMapN(&DC,
2569 1,
2570 QCBOR_TAG_REQUIREMENT_TAG |
2571 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2572 &nEpochDate1400000000);
2573 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002574 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002575 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2576 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002577 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002578 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2579 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002580 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002581 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2582 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002583 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002584 QCBORDecode_GetEpochDateInMapSZ(&DC,
2585 "z",
2586 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2587 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2588 &nEpochDate6);
2589 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002590
2591 QCBORDecode_ExitMap(&DC);
2592 QCBORDecode_ExitArray(&DC);
2593 uError = QCBORDecode_Finish(&DC);
2594 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002595 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002596 }
2597
Laurence Lundblade9b334962020-08-27 10:55:53 -07002598 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002599 return 200;
2600 }
2601
Laurence Lundblade9b334962020-08-27 10:55:53 -07002602 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002603 return 201;
2604 }
2605
Laurence Lundblade9b334962020-08-27 10:55:53 -07002606 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002607 return 202;
2608 }
2609
Laurence Lundblade9b334962020-08-27 10:55:53 -07002610 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002611 return 203;
2612 }
2613
Laurence Lundblade9b334962020-08-27 10:55:53 -07002614 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002615 return 204;
2616 }
2617
Laurence Lundblade9b334962020-08-27 10:55:53 -07002618 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2619 return 205;
2620 }
2621
2622 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2623 return 206;
2624 }
2625
Laurence Lundbladec7114722020-08-13 05:11:40 -07002626 return 0;
2627}
2628
2629
2630
Laurence Lundblade9b334962020-08-27 10:55:53 -07002631// Input for one of the tagging tests
2632static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002633 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002634 0x81, // Array of one
2635 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2636 0x82, // Array of two that is the faction 1/3
2637 0x01,
2638 0x03,
2639
2640 /*
2641 More than 4 tags on an item 225(226(227(228(229([])))))
2642 */
2643 0xd8, 0xe1,
2644 0xd8, 0xe2,
2645 0xd8, 0xe3,
2646 0xd8, 0xe4,
2647 0xd8, 0xe5,
2648 0x80,
2649
2650 /* tag 10489608748473423768(
2651 2442302356(
2652 21590(
2653 240(
2654 []))))
2655 */
2656 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2657 0xda, 0x91, 0x92, 0x93, 0x94,
2658 0xd9, 0x54, 0x56,
2659 0xd8, 0xf0,
2660 0x80,
2661
2662 /* tag 21590(
2663 10489608748473423768(
2664 2442302357(
2665 65534(
2666 []))))
2667 */
2668 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2669 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2670 0xda, 0x91, 0x92, 0x93, 0x95,
2671 0xd9, 0xff, 0xfe,
2672 0x80,
2673
2674 /* Make sure to blow past the limit of tags that must be mapped.
2675 works in conjuntion with entries above.
2676 269488144(269488145(269488146(269488147([]))))
2677 */
2678 0xda, 0x10, 0x10, 0x10, 0x10,
2679 0xda, 0x10, 0x10, 0x10, 0x11,
2680 0xda, 0x10, 0x10, 0x10, 0x12,
2681 0xda, 0x10, 0x10, 0x10, 0x13,
2682 0x80,
2683
2684 /* An invalid decimal fraction with an additional tag */
2685 0xd9, 0xff, 0xfa,
2686 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2687 0x00, // the integer 0; should be a byte string
2688};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002689
Laurence Lundblade59289e52019-12-30 13:44:37 -08002690/*
2691 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002692 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002693 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002694static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2695 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002696
Laurence Lundblade59289e52019-12-30 13:44:37 -08002697/*
2698DB 9192939495969798 # tag(10489608748473423768)
2699 D8 88 # tag(136)
2700 C6 # tag(6)
2701 C7 # tag(7)
2702 80 # array(0)
2703*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002704static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2705 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002706
2707/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002708 55799(55799(55799({
2709 6(7(-23)): 5859837686836516696(7({
2710 7(-20): 11({
2711 17(-18): 17(17(17("Organization"))),
2712 9(-17): 773("SSG"),
2713 -15: 16(17(6(7("Confusion")))),
2714 17(-16): 17("San Diego"),
2715 17(-14): 17("US")
2716 }),
2717 23(-19): 19({
2718 -11: 9({
2719 -9: -7
2720 }),
2721 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2722 })
2723 })),
2724 16(-22): 23({
2725 11(8(7(-5))): 8(-3)
2726 })
2727 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002728 */
2729static uint8_t spCSRWithTags[] = {
2730 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2731 0xc6, 0xc7, 0x36,
2732 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2733 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2734 0xcb, 0xa5,
2735 0xd1, 0x31,
2736 0xd1, 0xd1, 0xd1, 0x6c,
2737 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2738 0xc9, 0x30,
2739 0xd9, 0x03, 0x05, 0x63,
2740 0x53, 0x53, 0x47,
2741 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002742 0xd0, 0xd1, 0xc6, 0xc7,
2743 0x69,
2744 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002745 0xd1, 0x2f,
2746 0xd1, 0x69,
2747 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2748 0xd1, 0x2d,
2749 0xd1, 0x62,
2750 0x55, 0x53,
2751 0xd7, 0x32,
2752 0xd3, 0xa2,
2753 0x2a,
2754 0xc9, 0xa1,
2755 0x28,
2756 0x26,
2757 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2758 0xcc, 0x4a,
2759 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2760 0xd0, 0x35,
2761 0xd7, 0xa1,
2762 0xcb, 0xc8, 0xc7, 0x24,
2763 0xc8, 0x22};
2764
Laurence Lundblade9b334962020-08-27 10:55:53 -07002765
2766static uint8_t spSpiffyTagInput[] = {
2767 0x9f, // Open indefinite array
2768
2769 0xc0, // tag for string date
2770 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2771
2772 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2773
2774 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2775
2776 0xd8, 0x23, // tag for regex
2777 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2778
2779 0xc0, // tag for string date
2780 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2781
2782 0xff
2783};
2784
2785
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002786static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002787
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002788
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002789int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002790{
2791 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002792 QCBORItem Item;
2793 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002794
Laurence Lundbladeee851742020-01-08 08:37:05 -08002795 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002796 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002797 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002798
Laurence Lundblade9b334962020-08-27 10:55:53 -07002799 /*
2800 This test matches the magic number tag and the fraction tag
2801 55799([...])
2802 */
2803 uError = QCBORDecode_GetNext(&DCtx, &Item);
2804 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002805 return -2;
2806 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002807 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002808 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2809 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002810 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002811
Laurence Lundblade9b334962020-08-27 10:55:53 -07002812 /*
2813 4([1,3])
2814 */
2815 uError = QCBORDecode_GetNext(&DCtx, &Item);
2816#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2817 if(uError != QCBOR_SUCCESS ||
2818 Item.uDataType != QCBOR_TYPE_ARRAY ||
2819 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2820 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2821 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2822 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2823 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2824 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2825 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002826 return -4;
2827 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002828 // consume the items in the array
2829 uError = QCBORDecode_GetNext(&DCtx, &Item);
2830 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002831
Laurence Lundblade59289e52019-12-30 13:44:37 -08002832#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002833 if(uError != QCBOR_SUCCESS ||
2834 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2835 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2836 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2837 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2838 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2839 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2840 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002841 }
2842#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002843
Laurence Lundblade9b334962020-08-27 10:55:53 -07002844 /*
2845 More than 4 tags on an item 225(226(227(228(229([])))))
2846 */
2847 uError = QCBORDecode_GetNext(&DCtx, &Item);
2848 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002849 return -6;
2850 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002851
2852 /* tag 10489608748473423768(
2853 2442302356(
2854 21590(
2855 240(
2856 []))))
2857 */
2858 uError = QCBORDecode_GetNext(&DCtx, &Item);
2859 if(uError != QCBOR_SUCCESS ||
2860 Item.uDataType != QCBOR_TYPE_ARRAY ||
2861 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2862 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2863 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2864 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002865 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002866 }
2867
2868 /* tag 21590(
2869 10489608748473423768(
2870 2442302357(
2871 21591(
2872 []))))
2873 */
2874 uError = QCBORDecode_GetNext(&DCtx, &Item);
2875 if(uError != QCBOR_SUCCESS ||
2876 Item.uDataType != QCBOR_TYPE_ARRAY ||
2877 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2878 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2879 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2880 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2881 return -8;
2882 }
2883
2884 /* Make sure to blow past the limit of tags that must be mapped.
2885 works in conjuntion with entries above.
2886 269488144(269488145(269488146(269488147([]))))
2887 */
2888 uError = QCBORDecode_GetNext(&DCtx, &Item);
2889 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2890 return -9;
2891 }
2892
2893 uError = QCBORDecode_GetNext(&DCtx, &Item);
2894 if(uError == QCBOR_SUCCESS) {
2895 return -10;
2896 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002897
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002898 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002899 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002900 // tage and then matches it. Caller-config lists are no longer
2901 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002902 QCBORDecode_Init(&DCtx,
2903 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2904 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002905 const uint64_t puList[] = {0x9192939495969798, 257};
2906 const QCBORTagListIn TL = {2, puList};
2907 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002908
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002909 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2910 return -8;
2911 }
2912 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2913 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2914 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2915 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2916 Item.val.uCount != 0) {
2917 return -9;
2918 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002919
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002920 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002921 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002922 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002923 const uint64_t puLongList[17] = {1,2,1};
2924 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002925 QCBORDecode_Init(&DCtx,
2926 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2927 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002928 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2929 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2930 return -11;
2931 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002932
Laurence Lundblade9b334962020-08-27 10:55:53 -07002933 uint64_t puTags[16];
2934 QCBORTagListOut Out = {0, 4, puTags};
2935
2936
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002937 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002938 QCBORDecode_Init(&DCtx,
2939 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2940 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002941 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2942 return -12;
2943 }
2944 if(puTags[0] != 0x9192939495969798 ||
2945 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002946 puTags[2] != 0x06 ||
2947 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002948 return -13;
2949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002950
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002951 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002952 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002953 QCBORDecode_Init(&DCtx,
2954 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2955 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002956 QCBORTagListOut OutSmall = {0, 3, puTags};
2957 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2958 return -14;
2959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002960
Laurence Lundblade9b334962020-08-27 10:55:53 -07002961
2962
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002963 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002964 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
2965 // It is a bit of a messy test and maybe could be improved, but
2966 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002967 QCBORDecode_Init(&DCtx,
2968 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2969 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002970 int n = CheckCSRMaps(&DCtx);
2971 if(n) {
2972 return n-2000;
2973 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002974
Laurence Lundblade59289e52019-12-30 13:44:37 -08002975 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002976 QCBORDecode_Init(&DCtx,
2977 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2978 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002979
Laurence Lundblade9b334962020-08-27 10:55:53 -07002980 /* With the spiffy decode revision, this tag list is not used.
2981 It doesn't matter if a tag is in this list or not so some
2982 tests that couldn't process a tag because it isn't in this list
2983 now can process these unlisted tags. The tests have been
2984 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002985 const uint64_t puTagList[] = {773, 1, 90599561};
2986 const QCBORTagListIn TagList = {3, puTagList};
2987 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002988
2989
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002990 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2991 return -100;
2992 }
2993 if(Item.uDataType != QCBOR_TYPE_MAP ||
2994 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2995 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2996 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2997 Item.val.uCount != 2 ||
2998 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2999 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3000 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3001 Out.uNumUsed != 3) {
3002 return -101;
3003 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003004
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003005 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3006 return -102;
3007 }
3008 if(Item.uDataType != QCBOR_TYPE_MAP ||
3009 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3010 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003011 !QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003012 Item.val.uCount != 2 ||
3013 puTags[0] != 5859837686836516696 ||
3014 puTags[1] != 7 ||
3015 Out.uNumUsed != 2) {
3016 return -103;
3017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003018
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003019 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3020 return -104;
3021 }
3022 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003023 //Item.uTagBits ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003024 Item.val.uCount != 5 ||
3025 puTags[0] != 0x0b ||
3026 Out.uNumUsed != 1) {
3027 return -105;
3028 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003029
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003030 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3031 return -106;
3032 }
3033 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3034 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3035 Item.val.string.len != 12 ||
3036 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3037 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3038 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3039 Out.uNumUsed != 3) {
3040 return -105;
3041 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003042
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003043 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3044 return -107;
3045 }
3046 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3047 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3048 Item.val.string.len != 3 ||
3049 puTags[0] != 773 ||
3050 Out.uNumUsed != 1) {
3051 return -108;
3052 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003053
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003054 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3055 return -109;
3056 }
3057 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003058 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003059 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003060 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003061 puTags[3] != 7 ||
3062 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003063 return -110;
3064 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003065
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003066 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3067 return -111;
3068 }
3069 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3070 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3071 Item.val.string.len != 9 ||
3072 puTags[0] != 17 ||
3073 Out.uNumUsed != 1) {
3074 return -112;
3075 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003076
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003077 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3078 return -111;
3079 }
3080 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3081 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3082 Item.val.string.len != 2 ||
3083 puTags[0] != 17 ||
3084 Out.uNumUsed != 1) {
3085 return -112;
3086 }
3087
3088 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3089 return -113;
3090 }
3091 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003092 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003093 Item.val.uCount != 2 ||
3094 puTags[0] != 19 ||
3095 Out.uNumUsed != 1) {
3096 return -114;
3097 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003098
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003099 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3100 return -115;
3101 }
3102 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003103 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003104 Item.val.uCount != 1 ||
3105 puTags[0] != 9 ||
3106 Out.uNumUsed != 1) {
3107 return -116;
3108 }
3109
3110 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3111 return -116;
3112 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003113 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003114 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003115 Out.uNumUsed != 0) {
3116 return -117;
3117 }
3118
3119 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3120 return -118;
3121 }
3122 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3123 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003124 puTags[0] != 12 ||
3125 Out.uNumUsed != 1) {
3126 return -119;
3127 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003128
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003129 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3130 return -120;
3131 }
3132 if(Item.uDataType != QCBOR_TYPE_MAP ||
3133 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3134 Item.val.uCount != 1 ||
3135 puTags[0] != 0x17 ||
3136 Out.uNumUsed != 1) {
3137 return -121;
3138 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003139
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003140 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3141 return -122;
3142 }
3143 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003144 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003145 Item.val.int64 != -3 ||
3146 puTags[0] != 8 ||
3147 Out.uNumUsed != 1) {
3148 return -123;
3149 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003150
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003151 if(QCBORDecode_Finish(&DCtx)) {
3152 return -124;
3153 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003154
3155 UsefulBufC DateString;
3156 QCBORDecode_Init(&DCtx,
3157 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3158 QCBOR_DECODE_MODE_NORMAL);
3159
3160 QCBORDecode_EnterArray(&DCtx);
3161 // tagged date string
3162 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3163 // untagged date string
3164 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3165 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3166 return 100;
3167 }
3168 // untagged byte string
3169 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3170 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3171 return 101;
3172 }
3173 // tagged regex
3174 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3175 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3176 return 102;
3177 }
3178 // tagged date string with a byte string
3179 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3180 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3181 return 103;
3182 }
3183 QCBORDecode_ExitArray(&DCtx);
3184 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3185 return 104;
3186 }
3187
3188
3189 QCBORDecode_Init(&DCtx,
3190 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3191 QCBOR_DECODE_MODE_NORMAL);
3192
3193 QCBORDecode_EnterArray(&DCtx);
3194 // tagged date string
3195 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3196 // untagged date string
3197 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3198 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3199 return 200;
3200 }
3201 // untagged byte string
3202 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3203 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3204 return 201;
3205 }
3206 // tagged regex
3207 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3208 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3209 return 202;
3210 }
3211 // tagged date string with a byte string
3212 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3213 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3214 return 203;
3215 }
3216 QCBORDecode_ExitArray(&DCtx);
3217 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3218 return 204;
3219 }
3220
3221 QCBORDecode_Init(&DCtx,
3222 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3223 QCBOR_DECODE_MODE_NORMAL);
3224
3225 QCBORDecode_EnterArray(&DCtx);
3226 // tagged date string
3227 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3228 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3229 return 300;
3230 }
3231 // untagged date string
3232 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3233 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3234 return 301;
3235 }
3236 // untagged byte string
3237 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3238 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3239 return 302;
3240 }
3241 // tagged regex
3242 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3243 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3244 return 303;
3245 }
3246 // tagged date string with a byte string
3247 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3248 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3249 return 304;
3250 }
3251 QCBORDecode_ExitArray(&DCtx);
3252 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3253 return 305;
3254 }
3255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003256 return 0;
3257}
3258
3259
3260
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003261
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003262static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003263 0x83,
3264 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3265 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3266 0xA4,
3267 0x63, 0x42, 0x4E, 0x2B,
3268 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3269 0x18, 0x40,
3270 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3271 0x63, 0x42, 0x4E, 0x2D,
3272 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3273 0x38, 0x3F,
3274 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3275
3276
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003277static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003278
3279
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003280int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003281{
3282 QCBORDecodeContext DCtx;
3283 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003284 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003285
Laurence Lundbladeee851742020-01-08 08:37:05 -08003286 QCBORDecode_Init(&DCtx,
3287 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3288 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003289
3290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003291 //
3292 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3293 return -1;
3294 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003295 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003296 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003297
3298 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003299 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003300 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003301 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003302 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003303 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003304 }
3305
3306 //
3307 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003308 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003309 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003310 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003311 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003313
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003314 //
3315 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003316 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003317 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003318 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003319 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003320
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003321 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003322 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003323 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3324 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003325 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003326 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003327 }
3328
3329 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003330 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003331 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3332 Item.uLabelType != QCBOR_TYPE_INT64 ||
3333 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003334 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003335 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003337
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003338 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003339 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003340 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3341 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003342 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003343 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003344 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003345
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003346 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003347 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003348 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3349 Item.uLabelType != QCBOR_TYPE_INT64 ||
3350 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003351 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003352 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003353 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003355 return 0;
3356}
3357
3358
3359
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003360static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003361 uint8_t uDataType,
3362 uint8_t uNestingLevel,
3363 uint8_t uNextNest,
3364 int64_t nLabel,
3365 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003366{
3367 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003368 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003370 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3371 if(Item.uDataType != uDataType) return -1;
3372 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003373 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3374 Item.uLabelType != QCBOR_TYPE_UINT64) {
3375 return -1;
3376 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003377 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3378 if(Item.label.int64 != nLabel) return -1;
3379 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003380 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003381 }
3382 }
3383 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303384 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003385
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003386 if(pItem) {
3387 *pItem = Item;
3388 }
3389 return 0;
3390}
3391
3392
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003393// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003394static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003395{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303396 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003397
Laurence Lundblade9b334962020-08-27 10:55:53 -07003398 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003399
Laurence Lundblade9b334962020-08-27 10:55:53 -07003400 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003401
Laurence Lundblade9b334962020-08-27 10:55:53 -07003402 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3403 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3404 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3405 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3406 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003407
Laurence Lundblade9b334962020-08-27 10:55:53 -07003408 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3409 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003410
Laurence Lundblade9b334962020-08-27 10:55:53 -07003411 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3412 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003413
Laurence Lundblade9b334962020-08-27 10:55:53 -07003414 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3415 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003416
Laurence Lundblade9b334962020-08-27 10:55:53 -07003417 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003418
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003419 return 0;
3420}
3421
3422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003423/*
3424// cbor.me decoded output
3425{
3426 -23: {
3427 -20: {
3428 -18: "Organization",
3429 -17: "SSG",
3430 -15: "Confusion",
3431 -16: "San Diego",
3432 -14: "US"
3433 },
3434 -19: {
3435 -11: {
3436 -9: -7
3437 },
3438 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3439 }
3440 },
3441 -22: {
3442 -5: -3
3443 }
3444}
3445 */
3446
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003447
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003448static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003449 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3450 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3451 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3452 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3453 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3454 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3455 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3456 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3457 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3458
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003459int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003460{
3461 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003462
Laurence Lundbladeee851742020-01-08 08:37:05 -08003463 QCBORDecode_Init(&DCtx,
3464 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3465 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003466
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003467 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003468}
3469
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003470
3471
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003472int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003473{
3474 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003475
Laurence Lundbladeee851742020-01-08 08:37:05 -08003476 QCBORDecode_Init(&DCtx,
3477 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3478 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003479
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003480 QCBORItem Item;
3481 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003482
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003483 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3484 return -1;
3485 }
3486 if(Item.uDataType != QCBOR_TYPE_MAP) {
3487 return -2;
3488 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003489
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003490 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3491 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3492 return -3;
3493 }
3494
3495 return 0;
3496}
3497
3498
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003499// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003500static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003501 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3502 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3503 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3504 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3505 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3506 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003507 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3508 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3509 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3510 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003511
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003512int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003513{
3514 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003515
Laurence Lundbladeee851742020-01-08 08:37:05 -08003516 QCBORDecode_Init(&DCtx,
3517 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3518 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003519
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003520 return CheckCSRMaps(&DCtx);
3521}
3522
3523
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003524
Laurence Lundblade17ede402018-10-13 11:43:07 +08003525static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3526{
3527 UsefulOutBuf UOB;
3528 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003529
Laurence Lundblade17ede402018-10-13 11:43:07 +08003530 int i;
3531 for(i = 0; i < n; i++) {
3532 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3533 }
3534
3535 for(i = 0; i < n; i++) {
3536 UsefulOutBuf_AppendByte(&UOB, 0xff);
3537 }
3538 return UsefulOutBuf_OutUBuf(&UOB);
3539}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003540
3541
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003542static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003543{
3544 QCBORDecodeContext DC;
3545 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003546
Laurence Lundblade17ede402018-10-13 11:43:07 +08003547 int j;
3548 for(j = 0; j < nNestLevel; j++) {
3549 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003550 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003551 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3552 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003553 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003554 return -4;
3555 } else {
3556 return 0; // Decoding doesn't recover after an error
3557 }
3558 } else {
3559 // Should be no error
3560 if(nReturn) {
3561 return -9; // Should not have got an error
3562 }
3563 }
3564 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3565 return -7;
3566 }
3567 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003568 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003569 if(nReturn) {
3570 return -3;
3571 }
3572 return 0;
3573}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003574
3575
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003576int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003577{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303578 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003579 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003580 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003581 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003582 int nReturn = parse_indeflen_nested(Nested, i);
3583 if(nReturn) {
3584 return nReturn;
3585 }
3586 }
3587 return 0;
3588}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003589
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003590
Laurence Lundbladeee851742020-01-08 08:37:05 -08003591// [1, [2, 3]]
3592static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3593// No closing break
3594static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3595// Not enough closing breaks
3596static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3597// Too many closing breaks
3598static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3599// Unclosed indeflen inside def len
3600static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3601// confused tag
3602static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003603
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003604int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003605{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003606 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003607 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003608 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003609
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003610 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303611 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003612 QCBORDecodeContext DC;
3613 QCBORItem Item;
3614 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003615
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003616 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003617
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003618 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303619
3620 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3621 Item.uNestingLevel != 0 ||
3622 Item.uNextNestLevel != 1) {
3623 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003624 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003625
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003626 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303627 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3628 Item.uNestingLevel != 1 ||
3629 Item.uNextNestLevel != 1) {
3630 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003631 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003632
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003633 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303634 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3635 Item.uNestingLevel != 1 ||
3636 Item.uNextNestLevel != 2) {
3637 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003638 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003639
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003640 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003641 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303642 Item.uNestingLevel != 2 ||
3643 Item.uNextNestLevel != 2) {
3644 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003645 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003646
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003647 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003648 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303649 Item.uNestingLevel != 2 ||
3650 Item.uNextNestLevel != 0) {
3651 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003652 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003653
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003654 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303655 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003656 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003657
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003658 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003659 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003660
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003661 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003662
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003663 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003664
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003665 nResult = QCBORDecode_GetNext(&DC, &Item);
3666 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303667 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003668 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003669
Laurence Lundblade570fab52018-10-13 18:28:27 +08003670 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003671 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303672 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003673 }
3674
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003675
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003676 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003677 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003678
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003679 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003680
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003681 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003682
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003683 nResult = QCBORDecode_GetNext(&DC, &Item);
3684 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303685 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003686 }
3687
3688 nResult = QCBORDecode_GetNext(&DC, &Item);
3689 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303690 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003691 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003692
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003693 nResult = QCBORDecode_GetNext(&DC, &Item);
3694 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303695 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003696 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003697
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003698 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003699 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303700 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003701 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003702
3703
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003704 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003705 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003706
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003707 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003708
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003709 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003710
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003711 nResult = QCBORDecode_GetNext(&DC, &Item);
3712 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303713 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003714 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003715
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003716 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003717 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303718 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003719 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303720
Laurence Lundblade642282a2020-06-23 12:00:33 -07003721 nResult = QCBORDecode_GetNext(&DC, &Item);
3722 if(nResult != QCBOR_ERR_BAD_BREAK) {
3723 return -140;
3724 }
3725
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003726
Laurence Lundblade570fab52018-10-13 18:28:27 +08003727 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003728 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003729
Laurence Lundblade570fab52018-10-13 18:28:27 +08003730 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003731
Laurence Lundblade570fab52018-10-13 18:28:27 +08003732 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003733
Laurence Lundblade570fab52018-10-13 18:28:27 +08003734 nResult = QCBORDecode_GetNext(&DC, &Item);
3735 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303736 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003737 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003738
Laurence Lundblade570fab52018-10-13 18:28:27 +08003739 nResult = QCBORDecode_GetNext(&DC, &Item);
3740 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303741 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003742 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003743
Laurence Lundblade570fab52018-10-13 18:28:27 +08003744 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003745 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303746 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003747 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003748
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303749 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003750 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003751
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303752 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003753
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303754 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003755
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303756 nResult = QCBORDecode_GetNext(&DC, &Item);
3757 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303758 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303759 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003760
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303761 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303762 if(nResult != QCBOR_ERR_BAD_BREAK) {
3763 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303764 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003765
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003766 return 0;
3767}
3768
Laurence Lundblade17ede402018-10-13 11:43:07 +08003769
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003770static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003771 0x81, // Array of length one
3772 0x7f, // text string marked with indefinite length
3773 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3774 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3775 0xff // ending break
3776};
3777
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003778static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303779 0x81, // Array of length one
3780 0x7f, // text string marked with indefinite length
3781 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3782 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3783 0xff // ending break
3784};
3785
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003786static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303787 0x81, // Array of length one
3788 0x7f, // text string marked with indefinite length
3789 0x01, 0x02, // Not a string
3790 0xff // ending break
3791};
3792
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003793static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303794 0x81, // Array of length one
3795 0x7f, // text string marked with indefinite length
3796 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3797 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3798 // missing end of string
3799};
3800
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003801static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303802 0xa1, // Array of length one
3803 0x7f, // text string marked with indefinite length
3804 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3805 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3806 0xff, // ending break
3807 0x01 // integer being labeled.
3808};
3809
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003810/**
3811 Make an indefinite length string
3812
3813 @param Storage Storage for string, must be 144 bytes in size
3814 @return The indefinite length string
3815
3816 This makes an array with one indefinite length string that has 7 chunks
3817 from size of 1 byte up to 64 bytes.
3818 */
3819static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303820{
3821 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003822
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303823 UsefulOutBuf_Init(&UOB, Storage);
3824 UsefulOutBuf_AppendByte(&UOB, 0x81);
3825 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003826
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003827 uint8_t uStringByte = 0;
3828 // Use of type int is intentional
3829 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3830 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303831 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003832 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3833 for(int j = 0; j < uChunkSize; j++) {
3834 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3835 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303836 }
3837 }
3838 UsefulOutBuf_AppendByte(&UOB, 0xff);
3839
3840 return UsefulOutBuf_OutUBuf(&UOB);
3841}
3842
3843static int CheckBigString(UsefulBufC BigString)
3844{
3845 if(BigString.len != 255) {
3846 return 1;
3847 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003848
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303849 for(uint8_t i = 0; i < 255; i++){
3850 if(((const uint8_t *)BigString.ptr)[i] != i) {
3851 return 1;
3852 }
3853 }
3854 return 0;
3855}
3856
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303857
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003858int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303859{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303860 QCBORDecodeContext DC;
3861 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303862 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003863 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003864
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303865 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003866 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303867 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003868
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303869 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303870 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303871 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003872
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303873 if(QCBORDecode_GetNext(&DC, &Item)) {
3874 return -2;
3875 }
3876 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3877 return -3;
3878 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003879
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303880 if(QCBORDecode_GetNext(&DC, &Item)) {
3881 return -4;
3882 }
3883 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3884 return -5;
3885 }
3886 if(QCBORDecode_Finish(&DC)) {
3887 return -6;
3888 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303889
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303890 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003891 QCBORDecode_Init(&DC,
3892 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3893 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003894
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303895 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3896 return -7;
3897 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003898
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303899 if(QCBORDecode_GetNext(&DC, &Item)) {
3900 return -8;
3901 }
3902 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3903 return -9;
3904 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003905
Laurence Lundblade30816f22018-11-10 13:40:22 +07003906 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303907 return -10;
3908 }
3909
3910 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003911 QCBORDecode_Init(&DC,
3912 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3913 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003914
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303915 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3916 return -11;
3917 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003918
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303919 if(QCBORDecode_GetNext(&DC, &Item)) {
3920 return -12;
3921 }
3922 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3923 return -13;
3924 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003925
Laurence Lundblade30816f22018-11-10 13:40:22 +07003926 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303927 return -14;
3928 }
3929
3930 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003931 QCBORDecode_Init(&DC,
3932 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3933 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003934
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303935 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3936 return -15;
3937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003938
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303939 if(QCBORDecode_GetNext(&DC, &Item)) {
3940 return -16;
3941 }
3942 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3943 return -17;
3944 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003945
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303946 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3947 return -18;
3948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003949
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303950 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303951 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003952
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303953 QCBORDecode_GetNext(&DC, &Item);
3954 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303955 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003957
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303958 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303959 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003961
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303962 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003963 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303964
3965 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3966 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303967 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303968 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003969
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303970 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303971 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003972 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003973
Laurence Lundbladeee851742020-01-08 08:37:05 -08003974 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3975 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003976
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303977 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303978 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303979 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303980 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003981
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303982 QCBORDecode_GetNext(&DC, &Item);
3983 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303984 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303985 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003986 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303987 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303988 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003989
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303990 // ---- big bstr -----
3991 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003992
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303993 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3994 return -25;
3995 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003996
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303997 if(QCBORDecode_GetNext(&DC, &Item)) {
3998 return -26;
3999 }
4000 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304001 return -26;
4002 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004003
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304004 if(QCBORDecode_GetNext(&DC, &Item)) {
4005 return -27;
4006 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304007 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304008 return -28;
4009 }
4010 if(CheckBigString(Item.val.string)) {
4011 return -3;
4012 }
4013 if(QCBORDecode_Finish(&DC)) {
4014 return -29;
4015 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004016
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304017 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004018 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004019
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304020 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4021 return -30;
4022 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004023
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304024 QCBORDecode_GetNext(&DC, &Item);
4025 if(Item.uDataType != QCBOR_TYPE_MAP) {
4026 return -31;
4027 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004028
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304029 if(QCBORDecode_GetNext(&DC, &Item)){
4030 return -32;
4031 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004032 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4033 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304034 Item.uDataAlloc || !Item.uLabelAlloc ||
4035 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4036 return -33;
4037 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004038
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304039 if(QCBORDecode_Finish(&DC)) {
4040 return -34;
4041 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004042
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004043 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004044}
4045
4046
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004047int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304048{
4049 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004050 QCBORError nCBORError;
4051
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004052
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304053 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004054 QCBORDecode_Init(&DC,
4055 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4056 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004057
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004058 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004059
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004060 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4061 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304062 return -1;
4063 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004064
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004065 if(CheckCSRMaps(&DC)) {
4066 return -2;
4067 }
4068
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304069 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004070 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004071 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004072
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304073 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004074 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304075 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004076
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304077 QCBORItem Item1, Item2, Item3, Item4;
4078 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004079 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304080 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4081 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004082 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304083 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004084 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304085 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004086 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304087 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004088 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304089 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004090 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004091
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304092 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004093
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304094 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304095 Item1.uDataType != QCBOR_TYPE_INT64 ||
4096 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004097 Item1.uDataAlloc != 0 ||
4098 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004099 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004100 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004101 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004102
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304103
4104 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004105 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304106 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004107 Item2.uDataAlloc != 0 ||
4108 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304109 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004110 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004111
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304112 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004113 Item3.uDataAlloc == 0 ||
4114 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004115 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004116 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004117 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004118
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304119 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004120 Item4.uDataAlloc == 0 ||
4121 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004122 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004123 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004124 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004125
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304126 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004127 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004128 QCBORDecode_Init(&DC,
4129 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4130 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304131 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4132 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004133 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304134 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004135 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004136 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004137 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304138 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4139 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4140 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4141 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4142 }
4143 }
4144 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004145 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004146 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304147 }
4148
4149 return 0;
4150}
4151
Laurence Lundbladef6531662018-12-04 10:42:22 +09004152
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304153
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004154int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004155{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004156 // Set up the decoder with a tiny bit of CBOR to parse because
4157 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004158 QCBORDecodeContext DC;
4159 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4160 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004161
Laurence Lundbladef6531662018-12-04 10:42:22 +09004162 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004163 // Then fish into the internals of the decode context
4164 // to get the allocator function so it can be called directly.
4165 // Also figure out how much pool is available for use
4166 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004167 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004168 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4169 if(nError) {
4170 return -9;
4171 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004172 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4173 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4174 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004175
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004176 // First test -- ask for one more byte than available and see failure
4177 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004178 if(!UsefulBuf_IsNULL(Allocated)) {
4179 return -1;
4180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004181
Laurence Lundbladef6531662018-12-04 10:42:22 +09004182 // Re do the set up for the next test that will do a successful alloc,
4183 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004184 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004185 pAlloc = DC.StringAllocator.pfAllocator;
4186 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4187 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004188
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004189 // Allocate one byte less than available and see success
4190 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004191 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4192 return -2;
4193 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004194 // Ask for some more and see failure
4195 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004196 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4197 return -3;
4198 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004199 // Free the first allocate, retry the second and see success
4200 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4201 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004202 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4203 return -4;
4204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004205
Laurence Lundbladef6531662018-12-04 10:42:22 +09004206 // Re do set up for next test that involves a successful alloc,
4207 // and a successful realloc and a failed realloc
4208 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004209 pAlloc = DC.StringAllocator.pfAllocator;
4210 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004211
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004212 // Allocate half the pool and see success
4213 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004214 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4215 return -5;
4216 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004217 // Reallocate to take up the whole pool and see success
4218 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004219 if(UsefulBuf_IsNULL(Allocated2)) {
4220 return -6;
4221 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004222 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004223 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4224 return -7;
4225 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004226 // Try to allocate more to be sure there is failure after a realloc
4227 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4228 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004229 return -8;
4230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004231
Laurence Lundbladef6531662018-12-04 10:42:22 +09004232 return 0;
4233}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004234
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004235
4236/* Just enough of an allocator to test configuration of one */
4237static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4238{
4239 (void)pOldMem; // unused variable
4240
4241 if(uNewSize) {
4242 // Assumes the context pointer is the buffer and
4243 // nothing too big will ever be asked for.
4244 // This is only good for this basic test!
4245 return (UsefulBuf) {pCtx, uNewSize};
4246 } else {
4247 return NULLUsefulBuf;
4248 }
4249}
4250
4251
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004252int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004253{
4254 // Set up the decoder with a tiny bit of CBOR to parse because
4255 // nothing can be done with it unless that is set up.
4256 QCBORDecodeContext DC;
4257 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4258 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4259
4260 uint8_t pAllocatorBuffer[50];
4261
4262 // This is really just to test that this call works.
4263 // The full functionality of string allocators is tested
4264 // elsewhere with the MemPool internal allocator.
4265 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4266
4267 QCBORItem Item;
4268 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4269 return -1;
4270 }
4271
4272 if(Item.uDataAlloc == 0 ||
4273 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4274 Item.val.string.ptr != pAllocatorBuffer) {
4275 return -2;
4276 }
4277
4278 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4279 return -3;
4280 }
4281
4282 return 0;
4283}
4284
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004285#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004286
Laurence Lundbladea826c502020-05-10 21:07:00 -07004287/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004288 [
4289 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004290 4([-20, 4759477275222530853136]),
4291 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004292 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004293 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004294 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004295 5([ 9223372036854775806, -4759477275222530853137])
4296 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004297 ]
4298 */
4299
4300static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004301 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004302 0xC4, 0x82, 0x20,
4303 0x03,
4304 0xC4, 0x82, 0x33,
4305 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4306 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4307 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4308 0xC5, 0x82, 0x19, 0x01, 0x2C,
4309 0x18, 0x64,
4310 0xC5, 0x82, 0x33,
4311 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4312 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4313 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004314 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4315 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004316 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4317 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4318};
4319
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004320
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004321int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004322{
4323 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004324 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004325 QCBORItem item;
4326
Laurence Lundblade17af4902020-01-07 19:11:55 -08004327 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4328 0x06, 0x07, 0x08, 0x09, 0x010};
4329 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004330
4331
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004332 QCBORDecode_Init(&DC,
4333 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4334 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004335
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004336 uErr = QCBORDecode_GetNext(&DC, &item);
4337 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004338 return 1;
4339 }
4340
4341 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4342 return 2;
4343 }
4344
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004345 uErr = QCBORDecode_GetNext(&DC, &item);
4346 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004347 return 3;
4348 }
4349
4350 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4351 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4352 item.val.expAndMantissa.nExponent != -1) {
4353 return 4;
4354 }
4355
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004356 uErr = QCBORDecode_GetNext(&DC, &item);
4357 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004358 return 5;
4359 }
4360
4361 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4362 item.val.expAndMantissa.nExponent != -20 ||
4363 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4364 return 6;
4365 }
4366
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004367 uErr = QCBORDecode_GetNext(&DC, &item);
4368 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004369 return 7;
4370 }
4371
4372 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4373 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4374 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4375 return 8;
4376 }
4377
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004378 uErr = QCBORDecode_GetNext(&DC, &item);
4379 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004380 return 9;
4381 }
4382
4383 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4384 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4385 item.val.expAndMantissa.nExponent != 300) {
4386 return 10;
4387 }
4388
Laurence Lundbladea826c502020-05-10 21:07:00 -07004389 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004390 uErr = QCBORDecode_GetNext(&DC, &item);
4391 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004392 return 11;
4393 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004394 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4395 item.val.expAndMantissa.nExponent != -20 ||
4396 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4397 return 12;
4398 }
4399
Laurence Lundbladea826c502020-05-10 21:07:00 -07004400 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004401 uErr = QCBORDecode_GetNext(&DC, &item);
4402 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004403 return 13;
4404 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004405 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4406 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4407 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4408 return 14;
4409 }
4410
Laurence Lundbladea826c502020-05-10 21:07:00 -07004411 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004412 uErr = QCBORDecode_GetNext(&DC, &item);
4413 if(uErr != QCBOR_SUCCESS) {
4414 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004415 }
4416 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4417 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4418 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004419 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004420 }
4421
Laurence Lundbladea826c502020-05-10 21:07:00 -07004422 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004423 uErr = QCBORDecode_GetNext(&DC, &item);
4424 if(uErr != QCBOR_SUCCESS) {
4425 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004426 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004427 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4428 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4429 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004430 return 18;
4431 }
4432
4433 uErr = QCBORDecode_Finish(&DC);
4434 if(uErr != QCBOR_SUCCESS) {
4435 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004436 }
4437
4438 /* Now encode some stuff and then decode it */
4439 uint8_t pBuf[40];
4440 QCBOREncodeContext EC;
4441 UsefulBufC Encoded;
4442
4443 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4444 QCBOREncode_OpenArray(&EC);
4445 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4446 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4447 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4448 QCBOREncode_CloseArray(&EC);
4449 QCBOREncode_Finish(&EC, &Encoded);
4450
4451
4452 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004453 uErr = QCBORDecode_GetNext(&DC, &item);
4454 if(uErr != QCBOR_SUCCESS) {
4455 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004456 }
4457
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004458 uErr = QCBORDecode_GetNext(&DC, &item);
4459 if(uErr != QCBOR_SUCCESS) {
4460 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004461 }
4462
4463 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4464 item.val.expAndMantissa.nExponent != 1000 ||
4465 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004466 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004467 }
4468
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004469 uErr = QCBORDecode_GetNext(&DC, &item);
4470 if(uErr != QCBOR_SUCCESS) {
4471 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004472 }
4473
4474 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4475 item.val.expAndMantissa.nExponent != INT32_MIN ||
4476 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004477 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004478 }
4479
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004480 uErr = QCBORDecode_GetNext(&DC, &item);
4481 if(uErr != QCBOR_SUCCESS) {
4482 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004483 }
4484
4485 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4486 item.val.expAndMantissa.nExponent != INT32_MAX ||
4487 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004488 return 106;
4489 }
4490
4491
4492 int64_t nExp, nMant;
4493 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4494 UsefulBufC Mant;
4495 bool bIsNeg;
4496
4497 QCBORDecode_Init(&DC,
4498 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4499 QCBOR_DECODE_MODE_NORMAL);
4500 QCBORDecode_EnterArray(&DC);
4501
4502 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004503 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004504
4505 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004506 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4507 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004508
4509 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004510 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4511 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004512
4513 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004514 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004515
4516 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004517 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4518 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004519
4520 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004521 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4522 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004523
4524 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004525 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4526 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004527
4528 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004529 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4530 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004531
4532 QCBORDecode_ExitArray(&DC);
4533
4534 uErr = QCBORDecode_Finish(&DC);
4535 if(uErr != QCBOR_SUCCESS) {
4536 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004537 }
4538
4539 return 0;
4540}
4541
4542
4543static struct FailInput ExponentAndMantissaFailures[] = {
4544 // Exponent > INT64_MAX
4545 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4546 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4547 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4548 // Mantissa > INT64_MAX
4549 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4550 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4551 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4552 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004553 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004554 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004555 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004556 // bad content for big num
4557 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4558 // bad content for big num
4559 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4560 // Bad integer for exponent
4561 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4562 // Bad integer for mantissa
4563 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4564 // 3 items in array
4565 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4566 // unterminated indefinite length array
4567 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4568 // Empty array
4569 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4570 // Second is not an integer
4571 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4572 // First is not an integer
4573 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4574 // Not an array
4575 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4576};
4577
4578
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004579int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004580{
4581 return ProcessFailures(ExponentAndMantissaFailures,
4582 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4583}
4584
4585#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004586
4587
4588
4589/*
4590 Some basic CBOR with map and array used in a lot of tests.
4591 The map labels are all strings
4592
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004593 {
4594 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004595 "an array of two strings": [
4596 "string1", "string2"
4597 ],
4598 "map in a map": {
4599 "bytes 1": h'78787878',
4600 "bytes 2": h'79797979',
4601 "another int": 98,
4602 "text 2": "lies, damn lies and statistics"
4603 }
4604 }
4605 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004606
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004607int32_t EMap(UsefulBufC input)
4608{
4609 QCBORItem Item1, Item2, Item3;
4610 int64_t nDecodedInt1, nDecodedInt2;
4611 UsefulBufC B1, B2, S1, S2, S3;
4612
4613 QCBORDecodeContext DCtx;
4614 QCBORError nCBORError;
4615
4616 QCBORDecode_Init(&DCtx, input, 0);
4617
4618 QCBORDecode_EnterMap(&DCtx);
4619
4620 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4621
4622 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4623 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004624 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4625 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4626 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004627 QCBORDecode_ExitMap(&DCtx);
4628
4629 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4630 QCBORDecode_GetNext(&DCtx, &Item1);
4631 QCBORDecode_GetNext(&DCtx, &Item2);
4632 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4633 return -400;
4634 }
4635 QCBORDecode_ExitArray(&DCtx);
4636
4637 // Parse the same array again using GetText() instead of GetItem()
4638 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004639 QCBORDecode_GetTextString(&DCtx, &S2);
4640 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004641 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4642 return 5000;
4643 }
4644 /* QCBORDecode_GetText(&DCtx, &S3);
4645 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4646 return 5001;
4647 } */
4648
4649 QCBORDecode_ExitArray(&DCtx);
4650
4651 QCBORDecode_ExitMap(&DCtx);
4652
4653 nCBORError = QCBORDecode_Finish(&DCtx);
4654
4655 if(nCBORError) {
4656 return (int32_t)nCBORError;
4657 }
4658
4659 if(nDecodedInt1 != 42) {
4660 return 1001;
4661 }
4662
4663 if(nDecodedInt2 != 98) {
4664 return 1002;
4665 }
4666
4667 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004668 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004669 return 1003;
4670 }
4671
4672 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004673 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004674 return 1004;
4675 }
4676
Laurence Lundblade9b334962020-08-27 10:55:53 -07004677 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004678 return 1005;
4679 }
4680
4681 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4682 return 1006;
4683 }
4684
4685 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4686 return 1007;
4687 }
4688
4689 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4690 return 1008;
4691 }
4692
4693 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4694 return 1009;
4695 }
4696
4697 return 0;
4698}
4699
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004700
4701/*
4702 [23,
4703 6000,
4704 h'67616C6163746963',
4705 h'686176656E20746F6B656E'
4706 ]
4707 */
4708static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004709 0x84,
4710 0x17,
4711 0x19, 0x17, 0x70,
4712 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4713 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004714
4715
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004716static const uint8_t spEmptyMap[] = {0xa0};
4717
4718static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004719
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004720static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4721
Laurence Lundbladef0499502020-08-01 11:55:57 -07004722/*
4723 {
4724 0: [],
4725 9: [
4726 [],
4727 []
4728 ],
4729 8: {
4730 1: [],
4731 2: {},
4732 3: []
4733 },
4734 4: {},
4735 5: [],
4736 6: [
4737 [],
4738 []
4739 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004740 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004741 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004742
Laurence Lundbladef0499502020-08-01 11:55:57 -07004743static const uint8_t spMapOfEmpty[] = {
4744 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4745 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4746 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004747
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004748/*
4749 Too many tags
4750 Invalid tag content
4751 Duplicate label
4752 Integer overflow
4753 Date overflow
4754
4755 {1: 224(225(226(227(4(0))))),
4756 2: 1(h''),
4757 3: -18446744073709551616,
4758 4: 1(1.0e+300),
4759 5: 0, 8: 8}
4760 */
4761static const uint8_t spRecoverableMapErrors[] = {
4762 0xbf,
4763 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
4764 0x02, 0xc1, 0x40,
4765 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4766 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
4767 0x05, 0x00,
4768 0x05, 0x00,
4769 0x08, 0x08,
4770 0xff
4771};
4772
4773// Bad break
4774static const uint8_t spUnRecoverableMapError1[] = {
4775 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
4776};
4777
4778// No more items
4779static const uint8_t spUnRecoverableMapError2[] = {
4780 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
4781};
4782
4783// Hit end because string is too long
4784static const uint8_t spUnRecoverableMapError3[] = {
4785 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
4786};
4787
4788// Hit end because string is too long
4789static const uint8_t spUnRecoverableMapError4[] = {
4790 0xbf,
4791 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4792 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4793 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4794 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4795 0xff
4796};
4797
4798
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004799int32_t EnterMapTest()
4800{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004801 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004802 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004803 int32_t nReturn;
4804 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004805
4806
4807 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4808 QCBORDecode_EnterMap(&DCtx);
4809
Laurence Lundbladef0499502020-08-01 11:55:57 -07004810
4811 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004812 QCBORDecode_ExitArray(&DCtx);
4813
Laurence Lundbladef0499502020-08-01 11:55:57 -07004814 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004815 QCBORDecode_EnterArray(&DCtx);
4816 QCBORDecode_ExitArray(&DCtx);
4817 QCBORDecode_EnterArray(&DCtx);
4818 QCBORDecode_ExitArray(&DCtx);
4819 QCBORDecode_ExitArray(&DCtx);
4820
Laurence Lundbladef0499502020-08-01 11:55:57 -07004821 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004822 QCBORDecode_EnterArray(&DCtx);
4823 QCBORDecode_ExitArray(&DCtx);
4824 QCBORDecode_EnterMap(&DCtx);
4825 QCBORDecode_ExitMap(&DCtx);
4826 QCBORDecode_EnterArray(&DCtx);
4827 QCBORDecode_ExitArray(&DCtx);
4828 QCBORDecode_ExitMap(&DCtx);
4829
Laurence Lundbladef0499502020-08-01 11:55:57 -07004830 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004831 QCBORDecode_ExitMap(&DCtx);
4832
Laurence Lundbladef0499502020-08-01 11:55:57 -07004833 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004834 QCBORDecode_ExitArray(&DCtx);
4835
Laurence Lundbladef0499502020-08-01 11:55:57 -07004836 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004837 QCBORDecode_EnterArray(&DCtx);
4838 QCBORDecode_ExitArray(&DCtx);
4839 QCBORDecode_EnterArray(&DCtx);
4840 QCBORDecode_ExitArray(&DCtx);
4841 QCBORDecode_ExitArray(&DCtx);
4842
4843 QCBORDecode_ExitMap(&DCtx);
4844
4845 uErr = QCBORDecode_Finish(&DCtx);
4846 if(uErr != QCBOR_SUCCESS){
4847 return 3011;
4848 }
4849
4850
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004851 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004852 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004853 if(nReturn) {
4854 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004855 }
4856
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004857 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004858 if(nReturn) {
4859 return nReturn;
4860 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004861
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004862
Laurence Lundblade937ea812020-05-08 11:38:23 -07004863
4864 // These tests confirm the cursor is at the right place after entering a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004865 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004866
4867 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004868 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004869 QCBORDecode_EnterMap(&DCtx);
4870 QCBORDecode_GetNext(&DCtx, &Item1);
4871 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4872 return 2001;
4873 }
4874
4875
Laurence Lundblade9b334962020-08-27 10:55:53 -07004876 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004877 QCBORDecode_VGetNext(&DCtx, &Item1);
4878 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004879 QCBORDecode_EnterArray(&DCtx);
4880 QCBORDecode_GetNext(&DCtx, &Item1);
4881 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4882 return 2002;
4883 }
4884
Laurence Lundblade9b334962020-08-27 10:55:53 -07004885 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004886 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004887 QCBORDecode_GetNext(&DCtx, &Item1);
4888 QCBORDecode_GetNext(&DCtx, &Item1);
4889 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004890 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4891 QCBORDecode_GetNext(&DCtx, &Item1);
4892 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4893 return 2003;
4894 }
4895
Laurence Lundblade9b334962020-08-27 10:55:53 -07004896 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004897 QCBORDecode_EnterMap(&DCtx);
4898 QCBORDecode_GetNext(&DCtx, &Item1);
4899 QCBORDecode_GetNext(&DCtx, &Item1);
4900 QCBORDecode_GetNext(&DCtx, &Item1);
4901 QCBORDecode_GetNext(&DCtx, &Item1);
4902 QCBORDecode_GetNext(&DCtx, &Item1);
4903 QCBORDecode_GetNext(&DCtx, &Item1);
4904 QCBORDecode_GetNext(&DCtx, &Item1);
4905 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4906 QCBORDecode_GetNext(&DCtx, &Item1);
4907 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004908 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004909 }
4910
Laurence Lundblade9b334962020-08-27 10:55:53 -07004911 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004912 QCBORDecode_EnterMap(&DCtx);
4913 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4914 QCBORDecode_ExitArray(&DCtx);
4915 QCBORDecode_GetNext(&DCtx, &Item1);
4916 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4917 return 2006;
4918 }
4919 QCBORDecode_ExitMap(&DCtx);
4920 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4921 return 2007;
4922 }
4923
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004924 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4925 QCBORDecode_EnterArray(&DCtx);
4926 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004927 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4928 uErr = QCBORDecode_GetAndResetError(&DCtx);
4929 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004930 return 2008;
4931 }
4932 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004933 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004934 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004935 return 2009;
4936 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004937
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004938
4939 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4940 QCBORDecode_EnterMap(&DCtx);
4941 // This will fail because the map is empty.
4942 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4943 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004944 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004945 return 2010;
4946 }
4947 QCBORDecode_ExitMap(&DCtx);
4948 uErr = QCBORDecode_Finish(&DCtx);
4949 if(uErr != QCBOR_SUCCESS){
4950 return 2011;
4951 }
4952
4953
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004954 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4955 QCBORDecode_EnterMap(&DCtx);
4956 // This will fail because the map is empty.
4957 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4958 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004959 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004960 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004961 }
4962 QCBORDecode_ExitMap(&DCtx);
4963 uErr = QCBORDecode_Finish(&DCtx);
4964 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004965 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004966 }
4967
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004968
4969 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
4970 QCBORDecode_EnterArray(&DCtx);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004971 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004972 QCBORDecode_EnterMap(&DCtx);
4973 QCBORDecode_ExitMap(&DCtx);
4974 QCBORDecode_EnterArray(&DCtx);
4975 QCBORDecode_ExitArray(&DCtx);
4976 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
4977 QCBORDecode_ExitArray(&DCtx);
4978 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004979 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004980 return 2014;
4981 }
4982
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004983 int64_t nInt;
4984 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
4985 QCBORDecode_EnterMap(&DCtx);
4986 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
4987 uErr = QCBORDecode_GetAndResetError(&DCtx);
4988 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
4989 return 2021;
4990 }
4991
4992 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
4993 uErr = QCBORDecode_GetAndResetError(&DCtx);
4994 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
4995 return 2022;
4996 }
4997
4998 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
4999 uErr = QCBORDecode_GetAndResetError(&DCtx);
5000 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5001 return 2023;
5002 }
5003
5004 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5005 uErr = QCBORDecode_GetAndResetError(&DCtx);
5006#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5007 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5008 return 2024;
5009 }
5010#else
5011 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5012 return 2027;
5013 }
5014#endif
5015
5016 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5017 uErr = QCBORDecode_GetAndResetError(&DCtx);
5018 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5019 return 2025;
5020 }
5021
5022 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5023
5024 QCBORDecode_ExitMap(&DCtx);
5025 uErr = QCBORDecode_Finish(&DCtx);
5026 if(uErr != QCBOR_SUCCESS) {
5027 return 2026;
5028 }
5029
5030 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
5031 QCBORDecode_EnterMap(&DCtx);
5032 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5033 uErr = QCBORDecode_GetAndResetError(&DCtx);
5034 if(uErr != QCBOR_ERR_BAD_BREAK) {
5035 return 2030;
5036 }
5037
5038 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
5039 QCBORDecode_EnterMap(&DCtx);
5040 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5041 uErr = QCBORDecode_GetAndResetError(&DCtx);
5042 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5043 return 2031;
5044 }
5045
5046 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
5047 QCBORDecode_EnterMap(&DCtx);
5048 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5049 uErr = QCBORDecode_GetAndResetError(&DCtx);
5050 if(uErr != QCBOR_ERR_HIT_END) {
5051 return 2032;
5052 }
5053
5054 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
5055 QCBORDecode_EnterMap(&DCtx);
5056 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5057 uErr = QCBORDecode_GetAndResetError(&DCtx);
5058 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5059 return 2033;
5060 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005061
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005062 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005063}
5064
5065
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005066struct NumberConversion {
5067 char *szDescription;
5068 UsefulBufC CBOR;
5069 int64_t nConvertedToInt64;
5070 QCBORError uErrorInt64;
5071 uint64_t uConvertToUInt64;
5072 QCBORError uErrorUint64;
5073 double dConvertToDouble;
5074 QCBORError uErrorDouble;
5075};
5076
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005077static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005078 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005079 "too large to fit into int64_t",
5080 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5081 0,
5082 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5083 0,
5084 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5085 ((double)INT64_MIN) + 1 ,
5086 QCBOR_SUCCESS
5087 },
5088 {
5089 "largest negative int that fits in int64_t",
5090 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5091 INT64_MIN,
5092 QCBOR_SUCCESS,
5093 0,
5094 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5095 (double)INT64_MIN,
5096 QCBOR_SUCCESS
5097 },
5098 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005099 "negative bignum -1",
5100 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5101 -1,
5102 QCBOR_SUCCESS,
5103 0,
5104 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5105 -1.0,
5106 QCBOR_SUCCESS
5107 },
5108 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005109 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005110 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5111 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005112#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005113 257000,
5114 QCBOR_SUCCESS,
5115 257000,
5116 QCBOR_SUCCESS,
5117 257000.0,
5118 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005119#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5120 0,
5121 QCBOR_ERR_UNEXPECTED_TYPE,
5122 0,
5123 QCBOR_ERR_UNEXPECTED_TYPE,
5124 0.0,
5125 QCBOR_ERR_UNEXPECTED_TYPE
5126#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005127 },
5128 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005129 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005130 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5131 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005132#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005133 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005134 QCBOR_SUCCESS,
5135 0,
5136 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005137 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005138 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005139#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5140 0,
5141 QCBOR_ERR_UNEXPECTED_TYPE,
5142 0,
5143 QCBOR_ERR_UNEXPECTED_TYPE,
5144 0.0,
5145 QCBOR_ERR_UNEXPECTED_TYPE
5146#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005147 },
5148 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005149 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005150 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5151 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005152#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005153 2056,
5154 QCBOR_SUCCESS,
5155 2056,
5156 QCBOR_SUCCESS,
5157 2056.0,
5158 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005159#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5160 0,
5161 QCBOR_ERR_UNEXPECTED_TYPE,
5162 0,
5163 QCBOR_ERR_UNEXPECTED_TYPE,
5164 0.0,
5165 QCBOR_ERR_UNEXPECTED_TYPE
5166#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005167 },
5168 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005169 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005170 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5171 0,
5172 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5173 0,
5174 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5175 -18446744073709551617.0,
5176 QCBOR_SUCCESS
5177 },
5178 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005179 "Positive bignum 0x01020304 indefinite length string",
5180 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5181 0x01020304,
5182 QCBOR_SUCCESS,
5183 0x01020304,
5184 QCBOR_SUCCESS,
5185 16909060.0,
5186 QCBOR_SUCCESS
5187 },
5188 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005189 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005190 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5191 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005192#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005193 0,
5194 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5195 0,
5196 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5197 -INFINITY,
5198 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005199#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5200 0,
5201 QCBOR_ERR_UNEXPECTED_TYPE,
5202 0,
5203 QCBOR_ERR_UNEXPECTED_TYPE,
5204 0.0,
5205 QCBOR_ERR_UNEXPECTED_TYPE
5206#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005207 },
5208 {
5209 "big float [9223372036854775806, 9223372036854775806]",
5210 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5211 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005212#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005213 0,
5214 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5215 0,
5216 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5217 INFINITY,
5218 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005219#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5220 0,
5221 QCBOR_ERR_UNEXPECTED_TYPE,
5222 0,
5223 QCBOR_ERR_UNEXPECTED_TYPE,
5224 0.0,
5225 QCBOR_ERR_UNEXPECTED_TYPE
5226#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005227 },
5228 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005229 "Big float 3 * 2^^2",
5230 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005231#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005232 12,
5233 QCBOR_SUCCESS,
5234 12,
5235 QCBOR_SUCCESS,
5236 12.0,
5237 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005238#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5239 0,
5240 QCBOR_ERR_UNEXPECTED_TYPE,
5241 0,
5242 QCBOR_ERR_UNEXPECTED_TYPE,
5243 0.0,
5244 QCBOR_ERR_UNEXPECTED_TYPE
5245#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005246 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005247 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005248 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005249 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5250 0,
5251 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5252 18446744073709551615ULL,
5253 QCBOR_SUCCESS,
5254 18446744073709551615.0,
5255 QCBOR_SUCCESS
5256 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005257 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005258 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005259 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5260 65536-1,
5261 QCBOR_SUCCESS,
5262 0xffff,
5263 QCBOR_SUCCESS,
5264 65535.0,
5265 QCBOR_SUCCESS
5266 },
5267 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005268 "Postive integer 0",
5269 {(uint8_t[]){0x0}, 1},
5270 0LL,
5271 QCBOR_SUCCESS,
5272 0ULL,
5273 QCBOR_SUCCESS,
5274 0.0,
5275 QCBOR_SUCCESS
5276 },
5277 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005278 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005279 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5280 -9223372036854775807-1, // INT64_MIN
5281 QCBOR_SUCCESS,
5282 0ULL,
5283 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5284 -9223372036854775808.0,
5285 QCBOR_SUCCESS
5286 },
5287 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005288 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005289 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005290#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005291 100L,
5292 QCBOR_SUCCESS,
5293 100ULL,
5294 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005295#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5296 0,
5297 QCBOR_ERR_HW_FLOAT_DISABLED,
5298 0,
5299 QCBOR_ERR_HW_FLOAT_DISABLED,
5300#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005301 100.3,
5302 QCBOR_SUCCESS
5303 },
5304 {
5305 "Floating point value NaN 0xfa7fc00000",
5306 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005307#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005308 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005309 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005310 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005311 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005312#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5313 0,
5314 QCBOR_ERR_HW_FLOAT_DISABLED,
5315 0,
5316 QCBOR_ERR_HW_FLOAT_DISABLED,
5317#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005318 NAN,
5319 QCBOR_SUCCESS
5320 },
5321 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005322 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005323 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005324#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5325#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5326 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005327 -4,
5328 QCBOR_SUCCESS,
5329 0,
5330 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5331 -4.0,
5332 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005333#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5334 // Float HW disabled
5335 -4,
5336 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5337 0,
5338 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5339 -4.0,
5340 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5341#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5342#else
5343 // Half-precision disabled
5344 -4,
5345 QCBOR_ERR_HALF_PRECISION_DISABLED,
5346 0,
5347 QCBOR_ERR_HALF_PRECISION_DISABLED,
5348 -4.0,
5349 QCBOR_ERR_HALF_PRECISION_DISABLED
5350#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005351 },
5352 {
5353 "Decimal fraction 3/10",
5354 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005355#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005356 0,
5357 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5358 0,
5359 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5360 0.30000000000000004,
5361 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005362#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5363 0,
5364 QCBOR_ERR_UNEXPECTED_TYPE,
5365 0,
5366 QCBOR_ERR_UNEXPECTED_TYPE,
5367 0.0,
5368 QCBOR_ERR_UNEXPECTED_TYPE
5369#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005370 },
5371 {
5372 "+inifinity",
5373 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5374#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5375 0,
5376 QCBOR_ERR_FLOAT_EXCEPTION,
5377 0,
5378 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5379#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5380 0,
5381 QCBOR_ERR_HW_FLOAT_DISABLED,
5382 0,
5383 QCBOR_ERR_HW_FLOAT_DISABLED,
5384#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5385 INFINITY,
5386 QCBOR_SUCCESS
5387 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005388
5389 {
5390 "extreme pos bignum",
5391 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5392 // 50 rows of 8 is 400 digits.
5393 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5394 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5395 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5396 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5397 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5398 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5399 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5400 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5401 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5402 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5403 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5404 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5405 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5406 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5407 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5408 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5409 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5410 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5411 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5412 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5413 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5414 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5415 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5416 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5417 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5418 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5419 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5420 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5421 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5422 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5423 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5424 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5425 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5426 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5427 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5428 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5429 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5430 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5431 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5432 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5433 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5434 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5435 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5436 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5437 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5438 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5439 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5440 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5441 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5442 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5443 404},
5444 0,
5445 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5446 0,
5447 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5448#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5449 INFINITY,
5450 QCBOR_SUCCESS
5451#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5452 0,
5453 QCBOR_ERR_HW_FLOAT_DISABLED,
5454#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5455 },
5456
5457 {
5458 "extreme neg bignum",
5459 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5460 // 50 rows of 8 is 400 digits.
5461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5481 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5482 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5483 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5484 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5485 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5486 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5487 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5488 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5489 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5490 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5491 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5492 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5493 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5494 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5495 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5496 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5497 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5498 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5499 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5500 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5501 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5502 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5503 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5504 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5505 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5506 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5507 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5508 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5509 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5510 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5511 404},
5512 0,
5513 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5514 0,
5515 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5516#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5517 -INFINITY,
5518 QCBOR_SUCCESS
5519#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5520 0,
5521 QCBOR_ERR_HW_FLOAT_DISABLED,
5522#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5523 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005524
5525 {
5526 "big float underflow [9223372036854775806, -9223372036854775806]",
5527 {(uint8_t[]){
5528 0xC5, 0x82,
5529 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5530 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5531#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5532 0,
5533 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5534 0,
5535 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5536 0,
5537 QCBOR_SUCCESS
5538#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5539 0,
5540 QCBOR_ERR_UNEXPECTED_TYPE,
5541 0,
5542 QCBOR_ERR_UNEXPECTED_TYPE,
5543 0.0,
5544 QCBOR_ERR_UNEXPECTED_TYPE
5545#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5546 },
5547
5548 {
5549 "bigfloat that evaluates to -INFINITY",
5550 {(uint8_t[]){
5551 0xC5, 0x82,
5552 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5553 0xC3, 0x42, 0x01, 0x01}, 15},
5554#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5555 0,
5556 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5557 0,
5558 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5559 -INFINITY,
5560 QCBOR_SUCCESS
5561#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5562 0,
5563 QCBOR_ERR_UNEXPECTED_TYPE,
5564 0,
5565 QCBOR_ERR_UNEXPECTED_TYPE,
5566 0.0,
5567 QCBOR_ERR_UNEXPECTED_TYPE
5568#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5569 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005570};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005571
5572
5573
Laurence Lundblade313b2862020-05-16 01:23:06 -07005574int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005575{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005576 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005577
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005578 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5579 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005580
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005581 // Set up the decoding context including a memory pool so that
5582 // indefinite length items can be checked
5583 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005584 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005585
5586 /* ----- test conversion to int64_t ------ */
5587 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005588 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5589 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005590 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005591 }
5592
5593 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005594 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005595 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005596 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005597 }
5598 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005599 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005600 }
5601
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005602 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005603 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5604 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5605 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005606 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005607 }
5608 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005609 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005610 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005611 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005612 }
5613 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005614 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005615 }
5616
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005617 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005618 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5619 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5620 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005621 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005622 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005623#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005624 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005625 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005626 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005627 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005628 }
5629 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5630 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005631 // NaN's can't be compared for equality. A NaN is
5632 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005633 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005634 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005635 }
5636 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005637 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005638 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005639 }
5640 }
5641 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005642#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005643 }
5644
5645 return 0;
5646}
5647
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005648
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005649
5650
Laurence Lundbladee3553422020-05-02 11:11:17 -07005651int32_t CBORSequenceDecodeTests(void)
5652{
5653 QCBORDecodeContext DCtx;
5654 QCBORItem Item;
5655 QCBORError uCBORError;
5656
5657 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005658
Laurence Lundbladee3553422020-05-02 11:11:17 -07005659 // The input for the date test happens to be a sequence so it
5660 // is reused. It is a sequence because it doesn't start as
5661 // an array or map.
5662 QCBORDecode_Init(&DCtx,
5663 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5664 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005665
Laurence Lundbladee3553422020-05-02 11:11:17 -07005666 // Get the first item
5667 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5668 if(uCBORError != QCBOR_SUCCESS) {
5669 return 1;
5670 }
5671 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5672 return 2;
5673 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005674
Laurence Lundbladee3553422020-05-02 11:11:17 -07005675 // Get a second item
5676 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005677 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5678 return 66;
5679 }
5680
5681 // Get a third item
5682 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005683 if(uCBORError != QCBOR_SUCCESS) {
5684 return 2;
5685 }
5686 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5687 return 3;
5688 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005689
Laurence Lundbladee3553422020-05-02 11:11:17 -07005690 // A sequence can have stuff at the end that may
5691 // or may not be valid CBOR. The protocol decoder knows
5692 // when to stop by definition of the protocol, not
5693 // when the top-level map or array is ended.
5694 // Finish still has to be called to know that
5695 // maps and arrays (if there were any) were closed
5696 // off correctly. When called like this it
5697 // must return the error QCBOR_ERR_EXTRA_BYTES.
5698 uCBORError = QCBORDecode_Finish(&DCtx);
5699 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5700 return 4;
5701 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005702
5703
Laurence Lundbladee3553422020-05-02 11:11:17 -07005704 // --- Test an empty input ----
5705 uint8_t empty[1];
5706 UsefulBufC Empty = {empty, 0};
5707 QCBORDecode_Init(&DCtx,
5708 Empty,
5709 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005710
Laurence Lundbladee3553422020-05-02 11:11:17 -07005711 uCBORError = QCBORDecode_Finish(&DCtx);
5712 if(uCBORError != QCBOR_SUCCESS) {
5713 return 5;
5714 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005715
5716
Laurence Lundbladee3553422020-05-02 11:11:17 -07005717 // --- Sequence with unclosed indefinite length array ---
5718 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005719
Laurence Lundbladee3553422020-05-02 11:11:17 -07005720 QCBORDecode_Init(&DCtx,
5721 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5722 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005723
Laurence Lundbladee3553422020-05-02 11:11:17 -07005724 // Get the first item
5725 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5726 if(uCBORError != QCBOR_SUCCESS) {
5727 return 7;
5728 }
5729 if(Item.uDataType != QCBOR_TYPE_INT64) {
5730 return 8;
5731 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005732
Laurence Lundbladee3553422020-05-02 11:11:17 -07005733 // Get a second item
5734 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5735 if(uCBORError != QCBOR_SUCCESS) {
5736 return 9;
5737 }
5738 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5739 return 10;
5740 }
5741
5742 // Try to finish before consuming all bytes to confirm
5743 // that the still-open error is returned.
5744 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005745 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005746 return 11;
5747 }
5748
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005749
Laurence Lundbladee3553422020-05-02 11:11:17 -07005750 // --- Sequence with a closed indefinite length array ---
5751 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005752
Laurence Lundbladee3553422020-05-02 11:11:17 -07005753 QCBORDecode_Init(&DCtx,
5754 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5755 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005756
Laurence Lundbladee3553422020-05-02 11:11:17 -07005757 // Get the first item
5758 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5759 if(uCBORError != QCBOR_SUCCESS) {
5760 return 12;
5761 }
5762 if(Item.uDataType != QCBOR_TYPE_INT64) {
5763 return 13;
5764 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005765
Laurence Lundbladee3553422020-05-02 11:11:17 -07005766 // Get a second item
5767 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5768 if(uCBORError != QCBOR_SUCCESS) {
5769 return 14;
5770 }
5771 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5772 return 15;
5773 }
5774
5775 // Try to finish before consuming all bytes to confirm
5776 // that the still-open error is returned.
5777 uCBORError = QCBORDecode_Finish(&DCtx);
5778 if(uCBORError != QCBOR_SUCCESS) {
5779 return 16;
5780 }
5781
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005782
Laurence Lundbladee3553422020-05-02 11:11:17 -07005783 return 0;
5784}
5785
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005786
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005787
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005788int32_t IntToTests()
5789{
5790 int nErrCode;
5791 int32_t n32;
5792 int16_t n16;
5793 int8_t n8;
5794 uint32_t u32;
5795 uint16_t u16;
5796 uint8_t u8;
5797 uint64_t u64;
5798
5799 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5800 if(nErrCode == -1 || n32 != 1) {
5801 return 1;
5802 }
5803
5804 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5805 if(nErrCode == -1 || n32 != INT32_MAX) {
5806 return 2;
5807 }
5808
5809 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5810 if(nErrCode == -1 || n32 != INT32_MIN) {
5811 return 3;
5812 }
5813
5814 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5815 if(nErrCode != -1) {
5816 return 4;
5817 }
5818
5819 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5820 if(nErrCode != -1) {
5821 return 5;
5822 }
5823
5824
5825 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5826 if(nErrCode == -1 || n16 != INT16_MAX) {
5827 return 6;
5828 }
5829
5830 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5831 if(nErrCode == -1 || n16 != INT16_MIN) {
5832 return 7;
5833 }
5834
5835 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5836 if(nErrCode == -1 || n16 != 1) {
5837 return 8;
5838 }
5839
5840 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5841 if(nErrCode != -1) {
5842 return 9;
5843 }
5844
5845 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5846 if(nErrCode != -1) {
5847 return 10;
5848 }
5849
5850
5851 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5852 if(nErrCode == -1 || n8 != 1) {
5853 return 11;
5854 }
5855
5856 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5857 if(nErrCode == -1 || n8 != INT8_MAX) {
5858 return 12;
5859 }
5860
5861 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5862 if(nErrCode == -1 || n8 != INT8_MIN) {
5863 return 13;
5864 }
5865
5866 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5867 if(nErrCode != -1) {
5868 return 14;
5869 }
5870
5871 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5872 if(nErrCode != -1) {
5873 return 15;
5874 }
5875
5876
5877 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5878 if(nErrCode == -1 || u32 != 1) {
5879 return 16;
5880 }
5881
5882 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5883 if(nErrCode == -1 || u32 != UINT32_MAX) {
5884 return 17;
5885 }
5886
5887 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5888 if(nErrCode == -1 || u32 != 0) {
5889 return 18;
5890 }
5891
5892 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5893 if(nErrCode != -1) {
5894 return 19;
5895 }
5896
5897 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5898 if(nErrCode != -1) {
5899 return 20;
5900 }
5901
5902
5903 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5904 if(nErrCode == -1 || u16 != UINT16_MAX) {
5905 return 21;
5906 }
5907
5908 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5909 if(nErrCode == -1 || u16 != 0) {
5910 return 22;
5911 }
5912
5913 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5914 if(nErrCode == -1 || u16 != 1) {
5915 return 23;
5916 }
5917
5918 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5919 if(nErrCode != -1) {
5920 return 24;
5921 }
5922
5923 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5924 if(nErrCode != -1) {
5925 return 25;
5926 }
5927
5928
5929 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5930 if(nErrCode == -1 || u8 != UINT8_MAX) {
5931 return 26;
5932 }
5933
5934 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5935 if(nErrCode == -1 || u8 != 0) {
5936 return 27;
5937 }
5938
5939 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5940 if(nErrCode == -1 || u8 != 1) {
5941 return 28;
5942 }
5943
5944 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5945 if(nErrCode != -1) {
5946 return 29;
5947 }
5948
5949 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5950 if(nErrCode != -1) {
5951 return 30;
5952 }
5953
5954
5955 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5956 if(nErrCode == -1 || u64 != 1) {
5957 return 31;
5958 }
5959
5960 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
5961 if(nErrCode == -1 || u64 != INT64_MAX) {
5962 return 32;
5963 }
5964
5965 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
5966 if(nErrCode == -1 || u64 != 0) {
5967 return 33;
5968 }
5969
5970 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
5971 if(nErrCode != -1) {
5972 return 34;
5973 }
5974
5975 return 0;
5976}
5977
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005978
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005979
5980
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005981/*
5982A sequence with
5983 A wrapping bstr
5984 containing a map
5985 1
5986 2
5987 A wrapping bstr
5988 containing an array
5989 3
5990 wrapping bstr
5991 4
5992 5
5993 6
5994 array
5995 7
5996 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005997 */
5998
Laurence Lundblade55013642020-09-23 05:39:22 -07005999static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006000{
Laurence Lundblade55013642020-09-23 05:39:22 -07006001 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006002 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006003 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006004
Laurence Lundblade55013642020-09-23 05:39:22 -07006005 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006006
6007 QCBOREncode_BstrWrap(&EC);
6008 QCBOREncode_OpenMap(&EC);
6009 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6010 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6011 QCBOREncode_CloseMap(&EC);
6012 QCBOREncode_BstrWrap(&EC);
6013 QCBOREncode_OpenArray(&EC);
6014 QCBOREncode_AddInt64(&EC, 3);
6015 QCBOREncode_BstrWrap(&EC);
6016 QCBOREncode_AddInt64(&EC, 4);
6017 QCBOREncode_CloseBstrWrap(&EC, NULL);
6018 QCBOREncode_AddInt64(&EC, 5);
6019 QCBOREncode_CloseArray(&EC);
6020 QCBOREncode_CloseBstrWrap(&EC, NULL);
6021 QCBOREncode_AddInt64(&EC, 6);
6022 QCBOREncode_CloseBstrWrap(&EC, NULL);
6023 QCBOREncode_OpenArray(&EC);
6024 QCBOREncode_AddInt64(&EC, 7);
6025 QCBOREncode_AddInt64(&EC, 8);
6026 QCBOREncode_CloseArray(&EC);
6027
6028 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006029 if(uErr) {
6030 Encoded = NULLUsefulBufC;
6031 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006032
6033 return Encoded;
6034}
6035
6036
6037int32_t EnterBstrTest()
6038{
Laurence Lundblade55013642020-09-23 05:39:22 -07006039 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006040
6041 QCBORDecodeContext DC;
6042
Laurence Lundblade55013642020-09-23 05:39:22 -07006043 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006044
Laurence Lundblade55013642020-09-23 05:39:22 -07006045 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006046
6047
Laurence Lundblade9b334962020-08-27 10:55:53 -07006048 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006049 QCBORDecode_EnterMap(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006050 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6051 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006052 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006053 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006054 QCBORDecode_EnterArray(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006055 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006056 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006057 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006058 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006059 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006060 QCBORDecode_ExitArray(&DC);
6061 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006062 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006063 QCBORDecode_ExitBstrWrapped(&DC);
6064 QCBORDecode_EnterArray(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006065 QCBORDecode_GetInt64(&DC, &n7);
6066 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006067 QCBORDecode_ExitArray(&DC);
6068
6069 QCBORError uErr = QCBORDecode_Finish(&DC);
6070
6071 return (int32_t)uErr;
6072}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006073
6074
6075
6076
6077static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006078 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006079
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006080 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006081 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006082 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6083 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6084 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006085
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006086 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006087 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6088 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6089 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006090
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006091 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006092 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006093 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6094 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006095
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006096 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006097 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6098 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006099
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006100 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006101 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006102 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6103 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006104
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006105 21,
6106 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6107 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6108
6109 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006110 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006111 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6112 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006113
6114 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006115 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6116 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006117
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006118 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006119 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006120 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6121 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006122
6123 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006124 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6125 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006126
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006127 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006128 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006129 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6130 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006131
6132 0x18, 0x33,
6133 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6134
6135 // MIME
6136 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006137 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6138 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6139 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006140
6141 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006142 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6143 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006144
6145 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006146 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6147 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6148 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006149
6150 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006151 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6152 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006153
6154 // UUID
6155 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006156 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6157 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006158
6159 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006160 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6161 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006162};
6163
6164int32_t DecodeTaggedTypeTests()
6165{
6166 QCBORDecodeContext DC;
6167 QCBORError uErr;
6168
6169 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6170
6171 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006172 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006173
6174 QCBORDecode_EnterMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006175 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006176 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006177 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6178 return 1;
6179 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006180 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006181 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6182 return 2;
6183 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006184 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006185 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6186 return 3;
6187 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006188 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006189 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006190 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6191 return 4;
6192 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006193 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006194 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006195 return 5;
6196 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006197
Laurence Lundblade9b334962020-08-27 10:55:53 -07006198 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006199 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6200 bNeg != false) {
6201 return 10;
6202 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006203 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006204 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6205 bNeg != true) {
6206 return 11;
6207 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006208 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006209 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6210 return 12;
6211 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006212 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006213 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006214 return 13;
6215 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006216 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006217 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006218 return 14;
6219 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006220
Laurence Lundblade9b334962020-08-27 10:55:53 -07006221 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006222 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6223 return 20;
6224 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006225 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006226 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6227 return 21;
6228 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006229 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006230 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006231 return 22;
6232 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006233 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006234 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006235 return 23;
6236 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006237
Laurence Lundblade9b334962020-08-27 10:55:53 -07006238 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006239 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6240 return 30;
6241 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006242 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006243 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6244 return 31;
6245 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006246 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006247 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006248 return 32;
6249 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006250 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006251 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006252 return 33;
6253 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006254
Laurence Lundblade9b334962020-08-27 10:55:53 -07006255 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006256 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6257 return 40;
6258 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006259 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006260 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6261 return 41;
6262 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006263 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006264 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006265 return 42;
6266 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006267 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006268 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006269 return 43;
6270 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006271
Laurence Lundblade9b334962020-08-27 10:55:53 -07006272 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006273 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6274 return 50;
6275 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006276 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006277 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6278 return 51;
6279 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006280 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006281 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006282 return 52;
6283 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006284 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006285 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006286 return 53;
6287 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006288
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006289 // MIME
6290 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006291 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006292 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6293 bIsNot7Bit == true) {
6294 return 60;
6295 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006296 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006297 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6298 bIsNot7Bit == true) {
6299 return 61;
6300 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006301 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006302 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6303 bIsNot7Bit == false) {
6304 return 62;
6305 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006306 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006307 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6308 bIsNot7Bit == false) {
6309 return 63;
6310 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006311 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006312 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006313 return 64;
6314 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006315 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006316 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006317 return 65;
6318 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006319
Laurence Lundblade9b334962020-08-27 10:55:53 -07006320 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006321 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6322 return 70;
6323 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006324 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006325 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6326 return 71;
6327 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006328 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006329 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006330 return 72;
6331 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006332 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006333 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006334 return 73;
6335 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006336
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006337 // Improvement: add some more error test cases
6338
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006339 QCBORDecode_ExitMap(&DC);
6340
6341 uErr = QCBORDecode_Finish(&DC);
6342 if(uErr != QCBOR_SUCCESS) {
6343 return 100;
6344 }
6345
6346 return 0;
6347}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006348
6349
6350
6351
6352/*
6353 [
6354 "aaaaaaaaaa",
6355 {}
6356 ]
6357 */
6358static const uint8_t spTooLarge1[] = {
6359 0x9f,
6360 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6361 0xa0,
6362 0xff
6363};
6364
6365/*
6366 [
6367 {
6368 0: "aaaaaaaaaa"
6369 }
6370 ]
6371 */
6372static const uint8_t spTooLarge2[] = {
6373 0x9f,
6374 0xa1,
6375 0x00,
6376 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6377 0xff
6378};
6379
6380/*
6381 h'A1006A61616161616161616161'
6382
6383 {
6384 0: "aaaaaaaaaa"
6385 }
6386 */
6387static const uint8_t spTooLarge3[] = {
6388 0x4d,
6389 0xa1,
6390 0x00,
6391 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6392};
6393
6394int32_t TooLargeInputTest(void)
6395{
6396 QCBORDecodeContext DC;
6397 QCBORError uErr;
6398 UsefulBufC String;
6399
6400 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6401 // to 10 There's not really any way to test this error
6402 // condition. The error condition is not complex, so setting
6403 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6404
6405 // The input CBOR is only too large because the
6406 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6407 //
6408 // This test is disabled for the normal test runs because of the
6409 // special build requirement.
6410
6411
6412 // Tests the start of a map being too large
6413 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
6414 QCBORDecode_EnterArray(&DC);
6415 QCBORDecode_GetTextString(&DC, &String);
6416 uErr = QCBORDecode_GetError(&DC);
6417 if(uErr != QCBOR_SUCCESS) {
6418 return 1;
6419 }
6420 QCBORDecode_EnterMap(&DC);
6421 uErr = QCBORDecode_GetError(&DC);
6422 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6423 return 2;
6424 }
6425
6426 // Tests the end of a map being too large
6427 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
6428 QCBORDecode_EnterArray(&DC);
6429 QCBORDecode_EnterMap(&DC);
6430 uErr = QCBORDecode_GetError(&DC);
6431 if(uErr != QCBOR_SUCCESS) {
6432 return 3;
6433 }
6434 QCBORDecode_ExitMap(&DC);
6435 uErr = QCBORDecode_GetError(&DC);
6436 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6437 return 4;
6438 }
6439
6440 // Tests the entire input CBOR being too large when processing bstr wrapping
6441 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6442 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6443 uErr = QCBORDecode_GetError(&DC);
6444 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6445 return 5;
6446 }
6447
6448 return 0;
6449}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006450
6451
6452static const uint8_t spMapWithIndefLenStrings[] = {
6453 0xbf,
6454 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6455 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6456 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6457 0x03,
6458 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6459 0xc3,
6460 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6461 0xff
6462};
6463
6464int32_t SpiffyIndefiniteLengthStringsTests()
6465{
6466 QCBORDecodeContext DCtx;
6467
6468 QCBORDecode_Init(&DCtx,
6469 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6470 QCBOR_DECODE_MODE_NORMAL);
6471
6472 MakeUsefulBufOnStack(StringBuf, 200);
6473 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6474
6475 UsefulBufC ByteString;
6476 QCBORDecode_EnterMap(&DCtx);
6477 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6478 if(QCBORDecode_GetAndResetError(&DCtx)) {
6479 return 1;
6480 }
6481
6482 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6483 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6484 return 2;
6485 }
6486
6487 uint64_t uInt;
6488 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6489 if(QCBORDecode_GetAndResetError(&DCtx)) {
6490 return 3;
6491 }
6492 if(uInt != 3) {
6493 return 4;
6494 }
6495
6496 double uDouble;
6497 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6498 "label2",
6499 0xff,
6500 &uDouble);
6501 if(QCBORDecode_GetAndResetError(&DCtx)) {
6502 return 5;
6503 }
6504 if(uDouble != -16777474) {
6505 return 6;
6506 }
6507
6508 QCBORDecode_ExitMap(&DCtx);
6509
6510 if(QCBORDecode_Finish(&DCtx)) {
6511 return 99;
6512 }
6513
6514 return 0;
6515}