blob: bc6530ccd087bb717ee8692398f519cae0cc6a00 [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
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001723 // All the chunks in an indefinite length string must be of the type of
1724 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001725 // indefinite length byte string with text string chunk
1726 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1727 // indefinite length text string with a byte string chunk
1728 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1729 // indefinite length byte string with an positive integer chunk
1730 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1731 // indefinite length byte string with an negative integer chunk
1732 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1733 // indefinite length byte string with an array chunk
1734 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1735 // indefinite length byte string with an map chunk
1736 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1737 // indefinite length byte string with tagged integer chunk
1738 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1739 // indefinite length byte string with an simple type chunk
1740 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1741 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1742 // indefinite length text string with indefinite string inside
1743 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1744
1745
1746 // Definte length maps and arrays must be closed by having the right number of items
1747 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001748 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001749 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001750 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001751 // A definte length array that is supposed to have 511 items, but has only 1
1752 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1753 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001754 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001755 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001756 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001757
1758
1759 // Indefinte length maps and arrays must be ended by a break
1760 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001761 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001762 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001763 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001764 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001765 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001766 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001767 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001768
1769
1770 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001771 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001772 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001773 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001774 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001775 // Unclosed indefinite map containing a closed definite length array
1776 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1777 // Definite length map containing an unclosed indefinite length array
1778 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001779 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001780 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001781 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001782 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001783 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001784 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001785 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001786 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001787 // Unclosed indefinite length map in definite length maps
1788 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1789 QCBOR_ERR_NO_MORE_ITEMS},
1790 // Unclosed definite length map in indefinite length maps
1791 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1792 // Unclosed indefinite length array in definite length maps
1793 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1794 QCBOR_ERR_NO_MORE_ITEMS},
1795 // Unclosed definite length array in indefinite length maps
1796 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1797 // Unclosed indefinite length map in definite length arrays
1798 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1799 // Unclosed definite length map in indefinite length arrays
1800 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001801
1802
1803 // The "argument" for the data item is incomplete
1804 // Positive integer missing 1 byte argument
1805 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1806 // Positive integer missing 2 byte argument
1807 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1808 // Positive integer missing 4 byte argument
1809 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1810 // Positive integer missing 8 byte argument
1811 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1812 // Positive integer missing 1 byte of 2 byte argument
1813 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1814 // Positive integer missing 2 bytes of 4 byte argument
1815 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1816 // Positive integer missing 1 bytes of 7 byte argument
1817 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1818 // Negative integer missing 1 byte argument
1819 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1820 // Binary string missing 1 byte argument
1821 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1822 // Text string missing 1 byte argument
1823 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1824 // Array missing 1 byte argument
1825 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1826 // Map missing 1 byte argument
1827 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1828 // Tag missing 1 byte argument
1829 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1830 // Simple missing 1 byte argument
1831 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001832 // half-precision with 1 byte argument
1833 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1834 // single-precision with 2 byte argument
1835 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1836 // double-precision with 3 byte argument
1837 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1838
1839
1840 // Tag with no content
1841 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001842
1843
1844 // Breaks must not occur in definite length arrays and maps
1845 // Array of length 1 with sole member replaced by a break
1846 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1847 // Array of length 2 with 2nd member replaced by a break
1848 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1849 // Map of length 1 with sole member label replaced by a break
1850 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1851 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001852 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001853 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1854 // Array of length 1 with 2nd member value replaced by a break
1855 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1856 // Map of length 2 with 2nd member replaced by a break
1857 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1858
1859
1860 // Breaks must not occur on their own out of an indefinite length data item
1861 // A bare break is not well formed
1862 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1863 // A bare break after a zero length definite length array
1864 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1865 // A bare break after a zero length indefinite length map
1866 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001867 // A break inside a definite length array inside an indefenite length array
1868 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1869 // Complicated mixed nesting with break outside indefinite length array
1870 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001871
1872
1873 // Forbidden two byte encodings of simple types
1874 // Must use 0xe0 instead
1875 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1876 // Should use 0xe1 instead
1877 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1878 // Should use 0xe2 instead
1879 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1880 // Should use 0xe3 instead
1881 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1882 // Should use 0xe4 instead
1883 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1884 // Should use 0xe5 instead
1885 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1886 // Should use 0xe6 instead
1887 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1888 // Should use 0xe7 instead
1889 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1890 // Should use 0xe8 instead
1891 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1892 // Should use 0xe9 instead
1893 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1894 // Should use 0xea instead
1895 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1896 // Should use 0xeb instead
1897 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1898 // Should use 0xec instead
1899 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1900 // Should use 0xed instead
1901 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1902 // Should use 0xee instead
1903 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1904 // Should use 0xef instead
1905 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1906 // Should use 0xf0 instead
1907 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1908 // Should use 0xf1 instead
1909 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1910 // Should use 0xf2 instead
1911 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1912 // Must use 0xf3 instead
1913 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1914 // Must use 0xf4 instead
1915 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1916 // Must use 0xf5 instead
1917 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1918 // Must use 0xf6 instead
1919 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1920 // Must use 0xf7 instead
1921 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1922 // Must use 0xf8 instead
1923 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001924 // Reserved
1925 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001926
1927 // Integers with additional info indefinite length
1928 // Positive integer with additional info indefinite length
1929 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1930 // Negative integer with additional info indefinite length
1931 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1932 // CBOR tag with "argument" an indefinite length
1933 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1934 // CBOR tag with "argument" an indefinite length alternate vector
1935 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1936
1937
1938 // Missing bytes from a deterministic length string
1939 // A byte string is of length 1 without the 1 byte
1940 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1941 // A text string is of length 1 without the 1 byte
1942 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001943 // Byte string should have 2^32-15 bytes, but has one
1944 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1945 // Byte string should have 2^32-15 bytes, but has one
1946 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001947 // Byte string should have 2^64 bytes, but has 3
1948 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1949 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
1950 // Text string should have 2^64 bytes, but has 3
1951 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1952 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001953
1954 // Use of unassigned additional information values
1955 // Major type positive integer with reserved value 28
1956 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1957 // Major type positive integer with reserved value 29
1958 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1959 // Major type positive integer with reserved value 30
1960 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1961 // Major type negative integer with reserved value 28
1962 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1963 // Major type negative integer with reserved value 29
1964 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1965 // Major type negative integer with reserved value 30
1966 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1967 // Major type byte string with reserved value 28 length
1968 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1969 // Major type byte string with reserved value 29 length
1970 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1971 // Major type byte string with reserved value 30 length
1972 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1973 // Major type text string with reserved value 28 length
1974 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1975 // Major type text string with reserved value 29 length
1976 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1977 // Major type text string with reserved value 30 length
1978 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1979 // Major type array with reserved value 28 length
1980 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1981 // Major type array with reserved value 29 length
1982 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1983 // Major type array with reserved value 30 length
1984 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1985 // Major type map with reserved value 28 length
1986 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1987 // Major type map with reserved value 29 length
1988 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1989 // Major type map with reserved value 30 length
1990 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1991 // Major type tag with reserved value 28 length
1992 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1993 // Major type tag with reserved value 29 length
1994 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1995 // Major type tag with reserved value 30 length
1996 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1997 // Major type simple with reserved value 28 length
1998 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1999 // Major type simple with reserved value 29 length
2000 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2001 // Major type simple with reserved value 30 length
2002 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2003
2004
2005 // Maps must have an even number of data items (key & value)
2006 // Map with 1 item when it should have 2
2007 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2008 // Map with 3 item when it should have 4
2009 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
2010 // Map with 1 item when it should have 2
2011 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2012 // Map with 3 item when it should have 4
2013 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
2014
2015
2016 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002017 // Text-based date, with an integer
2018 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2019 // Epoch date, with an byte string
2020 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2021 // tagged as both epoch and string dates
2022 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2023 // big num tagged an int, not a byte string
2024 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002025};
2026
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002027int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002028{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002029 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002030
Laurence Lundblade59289e52019-12-30 13:44:37 -08002031 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
2032 if(nResult) {
2033 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002035
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002036 // Corrupt the UsefulInputBuf and see that
2037 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002038 QCBORDecodeContext DCtx;
2039 QCBORItem Item;
2040 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002041
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002042 QCBORDecode_Init(&DCtx,
2043 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2044 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002045
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002046 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2047 return (int32_t)uQCBORError;
2048 }
2049 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2050 // This wasn't supposed to happen
2051 return -1;
2052 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002053
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002054 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002055
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002056 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2057 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2058 // Did not get back the error expected
2059 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002060 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002061
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002062
Laurence Lundblade98427e92020-09-28 21:33:23 -07002063 /*
2064 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2065 tests here can be performed to see that the max length
2066 error check works correctly. See DecodeBytes(). If the max
2067 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002068
Laurence Lundblade98427e92020-09-28 21:33:23 -07002069 This test will automatocally adapt the all CPU sizes
2070 through the use of SIZE_MAX.
2071 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002072
Laurence Lundblade98427e92020-09-28 21:33:23 -07002073 MakeUsefulBufOnStack( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
2074 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002075
Laurence Lundblade98427e92020-09-28 21:33:23 -07002076 // This makes a CBOR head with a text string that is very long
2077 // but doesn't fill in the bytes of the text string as that is
2078 // not needed to test this part of QCBOR.
2079 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2080
2081 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2082
2083 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2084 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002085 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002086
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002087 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002088}
2089
2090
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002091/* Try all 256 values of the byte at nLen including recursing for
2092 each of the values to try values at nLen+1 ... up to nLenMax
2093 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002094static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002095{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002096 if(nLen >= nLenMax) {
2097 return;
2098 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002099
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002100 for(int inputByte = 0; inputByte < 256; inputByte++) {
2101 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002102 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002103 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002104
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002105 // Get ready to parse
2106 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002107 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002108
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002109 // Parse by getting the next item until an error occurs
2110 // Just about every possible decoder error can occur here
2111 // The goal of this test is not to check for the correct
2112 // error since that is not really possible. It is to
2113 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002114 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002115 QCBORItem Item;
2116 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002117 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002118 break;
2119 }
2120 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002121
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002122 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002123 }
2124}
2125
2126
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002127int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002128{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002129 // Size 2 tests 64K inputs and runs quickly
2130 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002131
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002132 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002133
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002134 return 0;
2135}
2136
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002137
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002138int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002139{
2140 // size 3 tests 16 million inputs and runs OK
2141 // in seconds on fast machines. Size 4 takes
2142 // 10+ minutes and 5 half a day on fast
2143 // machines. This test is kept separate from
2144 // the others so as to no slow down the use
2145 // of them as a very frequent regression.
2146 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002147
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002148 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002149
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002150 return 0;
2151}
2152
2153
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002154static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002155 0xc0, // tag for string date
2156 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002157
Laurence Lundbladec7114722020-08-13 05:11:40 -07002158 0xc0, // tag for string date
2159 0x00, // Wrong type for a string date
2160
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161 0xc1, // tag for epoch date
2162 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2163
Laurence Lundbladec7114722020-08-13 05:11:40 -07002164 0xc1,
2165 0x62, 'h', 'i', // wrong type tagged
2166
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002167 // CBOR_TAG_B64
Laurence Lundblade9b334962020-08-27 10:55:53 -07002168 0xcf, 0xd8, 0x22, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002169 0x1a, 0x53, 0x72, 0x4E, 0x01,
2170
2171 0xc1, // tag for epoch date
2172 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002173
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002174 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002175 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002177 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002178 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002179
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002180 0xc1, // tag for epoch date
2181 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2182 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2183
2184 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002185 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2186
2187 0xc1, // tag for epoch date
2188 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2189
2190 0xc1,
2191 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2192
2193 0xc1, // tag for epoch date
2194 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002195};
2196
2197
Laurence Lundbladec7114722020-08-13 05:11:40 -07002198
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002199// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002200#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002201static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002202
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002203 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002204
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002205 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002206
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002207 return diff > 0.0000001;
2208}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002209#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002210
2211
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002212int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213{
2214 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002215 QCBORItem Item;
2216 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002217
Laurence Lundbladeee851742020-01-08 08:37:05 -08002218 QCBORDecode_Init(&DCtx,
2219 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2220 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002222 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002223 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002224 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002225 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002226 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002227 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002228 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002229 }
2230
Laurence Lundbladec7114722020-08-13 05:11:40 -07002231 // Wrong type for a string date
2232 uError = QCBORDecode_GetNext(&DCtx, &Item);
2233 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002234 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002235 }
2236
2237 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2238 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2239 return -4;
2240 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002241 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2242 Item.val.epochDate.nSeconds != 1400000000 ||
2243 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002244 return -5;
2245 }
2246
2247 // Wrong type for an epoch date
2248 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2249 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002251
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002252 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2253 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002254 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2255 return -7;
2256 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002257 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2258 Item.val.epochDate.nSeconds != 1400000001 ||
2259 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002260 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002261 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002262 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002263
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002264 // Epoch date that is too large for our representation
2265 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002266 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002268
Laurence Lundblade9682a532020-06-06 18:33:04 -07002269#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002270 // Epoch date in float format with fractional seconds
2271 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2272 return -10;
2273 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002274 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2275 Item.val.epochDate.nSeconds != 1 ||
2276 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002277 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002279
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002280 // Epoch date float that is too large for our representation
2281 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002282 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002283 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002284
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002285 // Epoch date double that is just slightly too large
2286 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002287 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002288 }
2289
2290 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002291 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2292 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2293 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2294 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002295 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002296 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002297
2298 // Nan
2299 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2300 return -15;
2301 }
2302
2303 // +Inifinity double-precision
2304 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2305 return -16;
2306 }
2307
2308#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2309 // -Inifinity half-precision
2310 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2311 return -17;
2312 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002313#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002314 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2315 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002316 }
2317#endif
2318
Laurence Lundbladec7114722020-08-13 05:11:40 -07002319#else
2320 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2321 return -19;
2322 }
2323 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2324 return -20;
2325 }
2326 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2327 return -21;
2328 }
2329 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2330 return -22;
2331 }
2332 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2333 return -23;
2334 }
2335 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2336 return -24;
2337 }
2338#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2339 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2340 return -25;
2341 }
2342#else
2343 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2344 return -26;
2345 }
2346#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002347
Laurence Lundbladec7114722020-08-13 05:11:40 -07002348#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002349
2350 return 0;
2351}
2352
Laurence Lundblade4b270642020-08-14 12:53:07 -07002353/*
2354 Test cases covered here. Some items cover more than one of these.
2355 positive integer (zero counts as a positive integer)
2356 negative integer
2357 half-precision float
2358 single-precision float
2359 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002360
Laurence Lundblade4b270642020-08-14 12:53:07 -07002361 float Overflow error
2362 Wrong type error for epoch
2363 Wrong type error for date string
2364 float disabled error
2365 half-precision disabled error
2366 -Infinity
2367 Slightly too large integer
2368 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002369
Laurence Lundblade4b270642020-08-14 12:53:07 -07002370 Get epoch by int
2371 Get string by int
2372 Get epoch by string
2373 Get string by string
2374 Fail to get epoch by wrong int label
2375 Fail to get string by wrong string label
2376 Fail to get epoch by string because it is invalid
2377 Fail to get epoch by int because it is invalid
2378
2379 Untagged values
2380 */
2381static uint8_t spSpiffyDateTestInput[] = {
2382 0x86,
2383
2384 0xc1,
2385 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2386
Laurence Lundbladec7114722020-08-13 05:11:40 -07002387 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002388 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2389
2390 0xc1, // tag for epoch date
2391 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2392
2393 0xc1, // tag for epoch date
2394 0x9f, 0xff, // Erroneous empty array as content for date
2395
2396 0xc0, // tag for string date
2397 0xbf, 0xff, // Erroneous empty map as content for date
2398
2399 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002400
2401 0x00,
2402 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002403 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002404
2405 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002406 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002407 0xc1, // tag for epoch date
2408 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2409
2410 // Untagged integer 0
2411 0x08,
2412 0x00,
2413
2414 // Utagged date string with string label y
2415 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002416 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002417
2418 // Untagged -1000 with label z
2419 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002420 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002421 0x39, 0x03, 0xe7,
2422
Laurence Lundbladec7114722020-08-13 05:11:40 -07002423 0x07,
2424 0xc1, // tag for epoch date
2425 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2426
Laurence Lundblade4b270642020-08-14 12:53:07 -07002427 0x05,
2428 0xc1,
2429 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2430
Laurence Lundbladec7114722020-08-13 05:11:40 -07002431 // Untagged single-precision float with value 3.14 with string label x
2432 0x61, 0x78,
2433 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2434
Laurence Lundbladec7114722020-08-13 05:11:40 -07002435 // Untagged half-precision float with value -2
2436 0x09,
2437 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002438
2439 0xff,
2440};
2441
2442int32_t SpiffyDateDecodeTest()
2443{
2444 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002445 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002446 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2447 nEpochDate4, nEpochDate6, nEpochDateFail,
2448 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002449 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002450 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002451
2452 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002453 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002454 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002455 QCBORDecode_EnterArray(&DC);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002456
Laurence Lundblade9b334962020-08-27 10:55:53 -07002457 // Too-negative float, -9.2233720368547748E+18
2458 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002459 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002460#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002461 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2462 return 1111;
2463 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002464#else
2465 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2466 return 1112;
2467 }
2468#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002469
2470 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002471 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002472 uError = QCBORDecode_GetAndResetError(&DC);
2473 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002474 return 1;
2475 }
2476
Laurence Lundblade4b270642020-08-14 12:53:07 -07002477 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002478 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002479 uError = QCBORDecode_GetAndResetError(&DC);
2480#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2481#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2482 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2483#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2484 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2485#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2486#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2487 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2488#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2489 if(uError != uExpectedforHalfMinusInfinity) {
2490 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002491 }
2492
Laurence Lundblade4b270642020-08-14 12:53:07 -07002493 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002494 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002495 uError = QCBORDecode_GetAndResetError(&DC);
2496 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2497 return 3;
2498 }
2499
2500 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002501 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002502 uError = QCBORDecode_GetAndResetError(&DC);
2503 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2504 return 4;
2505 }
2506
2507 QCBORDecode_EnterMap(&DC);
2508
2509 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002510 QCBORDecode_GetEpochDateInMapN(&DC,
2511 5,
2512 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2513 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2514 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002515#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2516 if(nEpochDate2 != -9223372036854773760LL) {
2517 return 101;
2518 }
2519#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2520 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002521 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002522 return 102;
2523 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002524#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002525
Laurence Lundblade4b270642020-08-14 12:53:07 -07002526 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002527 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2528 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002529#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2530 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002531 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002532 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002533#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2534 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002535 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002536 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002537 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002538#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2539
2540 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002541 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2542 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002543#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2544 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002545 return 103;
2546 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002547#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2548 uError = QCBORDecode_GetAndResetError(&DC);
2549 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2550 return 104;
2551 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002552#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2553
Laurence Lundblade9b334962020-08-27 10:55:53 -07002554 // A half-precision date with value -2 FFF
2555 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2556 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002557#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2558 if(nEpochDate4 != -2) {
2559 return 105;
2560 }
2561#else
2562 uError = QCBORDecode_GetAndResetError(&DC);
2563 if(uError == QCBOR_SUCCESS) {
2564 return 106;
2565 }
2566#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002567
Laurence Lundblade4b270642020-08-14 12:53:07 -07002568
2569 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002570 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2571 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2572 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002573 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002574 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002575 return 107;
2576 }
2577
2578 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002579 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2580 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002581 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002582 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002583 return 108;
2584 }
2585
2586 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002587 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2588 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2589 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002590 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002591 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002592 return 109;
2593 }
2594
2595 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002596 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2597 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002598 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002599 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002600 return 110;
2601 }
2602
2603 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002604
Laurence Lundblade4b270642020-08-14 12:53:07 -07002605 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002606 QCBORDecode_GetEpochDateInMapN(&DC,
2607 1,
2608 QCBOR_TAG_REQUIREMENT_TAG |
2609 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2610 &nEpochDate1400000000);
2611 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002612 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002613 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2614 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002615 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002616 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2617 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002618 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002619 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2620 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002621 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002622 QCBORDecode_GetEpochDateInMapSZ(&DC,
2623 "z",
2624 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2625 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2626 &nEpochDate6);
2627 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002628
2629 QCBORDecode_ExitMap(&DC);
2630 QCBORDecode_ExitArray(&DC);
2631 uError = QCBORDecode_Finish(&DC);
2632 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002633 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002634 }
2635
Laurence Lundblade9b334962020-08-27 10:55:53 -07002636 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002637 return 200;
2638 }
2639
Laurence Lundblade9b334962020-08-27 10:55:53 -07002640 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002641 return 201;
2642 }
2643
Laurence Lundblade9b334962020-08-27 10:55:53 -07002644 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002645 return 202;
2646 }
2647
Laurence Lundblade9b334962020-08-27 10:55:53 -07002648 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002649 return 203;
2650 }
2651
Laurence Lundblade9b334962020-08-27 10:55:53 -07002652 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002653 return 204;
2654 }
2655
Laurence Lundblade9b334962020-08-27 10:55:53 -07002656 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2657 return 205;
2658 }
2659
2660 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2661 return 206;
2662 }
2663
Laurence Lundbladec7114722020-08-13 05:11:40 -07002664 return 0;
2665}
2666
2667
2668
Laurence Lundblade9b334962020-08-27 10:55:53 -07002669// Input for one of the tagging tests
2670static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002671 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002672 0x81, // Array of one
2673 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2674 0x82, // Array of two that is the faction 1/3
2675 0x01,
2676 0x03,
2677
2678 /*
2679 More than 4 tags on an item 225(226(227(228(229([])))))
2680 */
2681 0xd8, 0xe1,
2682 0xd8, 0xe2,
2683 0xd8, 0xe3,
2684 0xd8, 0xe4,
2685 0xd8, 0xe5,
2686 0x80,
2687
2688 /* tag 10489608748473423768(
2689 2442302356(
2690 21590(
2691 240(
2692 []))))
2693 */
2694 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2695 0xda, 0x91, 0x92, 0x93, 0x94,
2696 0xd9, 0x54, 0x56,
2697 0xd8, 0xf0,
2698 0x80,
2699
2700 /* tag 21590(
2701 10489608748473423768(
2702 2442302357(
2703 65534(
2704 []))))
2705 */
2706 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2707 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2708 0xda, 0x91, 0x92, 0x93, 0x95,
2709 0xd9, 0xff, 0xfe,
2710 0x80,
2711
2712 /* Make sure to blow past the limit of tags that must be mapped.
2713 works in conjuntion with entries above.
2714 269488144(269488145(269488146(269488147([]))))
2715 */
2716 0xda, 0x10, 0x10, 0x10, 0x10,
2717 0xda, 0x10, 0x10, 0x10, 0x11,
2718 0xda, 0x10, 0x10, 0x10, 0x12,
2719 0xda, 0x10, 0x10, 0x10, 0x13,
2720 0x80,
2721
2722 /* An invalid decimal fraction with an additional tag */
2723 0xd9, 0xff, 0xfa,
2724 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2725 0x00, // the integer 0; should be a byte string
2726};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002727
Laurence Lundblade59289e52019-12-30 13:44:37 -08002728/*
2729 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002730 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002731 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002732static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2733 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002734
Laurence Lundblade59289e52019-12-30 13:44:37 -08002735/*
2736DB 9192939495969798 # tag(10489608748473423768)
2737 D8 88 # tag(136)
2738 C6 # tag(6)
2739 C7 # tag(7)
2740 80 # array(0)
2741*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002742static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2743 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002744
2745/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002746 55799(55799(55799({
2747 6(7(-23)): 5859837686836516696(7({
2748 7(-20): 11({
2749 17(-18): 17(17(17("Organization"))),
2750 9(-17): 773("SSG"),
2751 -15: 16(17(6(7("Confusion")))),
2752 17(-16): 17("San Diego"),
2753 17(-14): 17("US")
2754 }),
2755 23(-19): 19({
2756 -11: 9({
2757 -9: -7
2758 }),
2759 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2760 })
2761 })),
2762 16(-22): 23({
2763 11(8(7(-5))): 8(-3)
2764 })
2765 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002766 */
2767static uint8_t spCSRWithTags[] = {
2768 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2769 0xc6, 0xc7, 0x36,
2770 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2771 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2772 0xcb, 0xa5,
2773 0xd1, 0x31,
2774 0xd1, 0xd1, 0xd1, 0x6c,
2775 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2776 0xc9, 0x30,
2777 0xd9, 0x03, 0x05, 0x63,
2778 0x53, 0x53, 0x47,
2779 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002780 0xd0, 0xd1, 0xc6, 0xc7,
2781 0x69,
2782 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002783 0xd1, 0x2f,
2784 0xd1, 0x69,
2785 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2786 0xd1, 0x2d,
2787 0xd1, 0x62,
2788 0x55, 0x53,
2789 0xd7, 0x32,
2790 0xd3, 0xa2,
2791 0x2a,
2792 0xc9, 0xa1,
2793 0x28,
2794 0x26,
2795 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2796 0xcc, 0x4a,
2797 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2798 0xd0, 0x35,
2799 0xd7, 0xa1,
2800 0xcb, 0xc8, 0xc7, 0x24,
2801 0xc8, 0x22};
2802
Laurence Lundblade9b334962020-08-27 10:55:53 -07002803
2804static uint8_t spSpiffyTagInput[] = {
2805 0x9f, // Open indefinite array
2806
2807 0xc0, // tag for string date
2808 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2809
2810 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2811
2812 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2813
2814 0xd8, 0x23, // tag for regex
2815 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2816
2817 0xc0, // tag for string date
2818 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2819
2820 0xff
2821};
2822
2823
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002824static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002825
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002826
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002827int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002828{
2829 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002830 QCBORItem Item;
2831 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002832
Laurence Lundbladeee851742020-01-08 08:37:05 -08002833 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002834 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002835 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002836
Laurence Lundblade9b334962020-08-27 10:55:53 -07002837 /*
2838 This test matches the magic number tag and the fraction tag
2839 55799([...])
2840 */
2841 uError = QCBORDecode_GetNext(&DCtx, &Item);
2842 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002843 return -2;
2844 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002845 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002846 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2847 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002848 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002849
Laurence Lundblade9b334962020-08-27 10:55:53 -07002850 /*
2851 4([1,3])
2852 */
2853 uError = QCBORDecode_GetNext(&DCtx, &Item);
2854#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2855 if(uError != QCBOR_SUCCESS ||
2856 Item.uDataType != QCBOR_TYPE_ARRAY ||
2857 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2858 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2859 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2860 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2861 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2862 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2863 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002864 return -4;
2865 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002866 // consume the items in the array
2867 uError = QCBORDecode_GetNext(&DCtx, &Item);
2868 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002869
Laurence Lundblade59289e52019-12-30 13:44:37 -08002870#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002871 if(uError != QCBOR_SUCCESS ||
2872 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2873 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2874 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2875 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2876 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2877 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2878 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002879 }
2880#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002881
Laurence Lundblade9b334962020-08-27 10:55:53 -07002882 /*
2883 More than 4 tags on an item 225(226(227(228(229([])))))
2884 */
2885 uError = QCBORDecode_GetNext(&DCtx, &Item);
2886 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002887 return -6;
2888 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002889
2890 /* tag 10489608748473423768(
2891 2442302356(
2892 21590(
2893 240(
2894 []))))
2895 */
2896 uError = QCBORDecode_GetNext(&DCtx, &Item);
2897 if(uError != QCBOR_SUCCESS ||
2898 Item.uDataType != QCBOR_TYPE_ARRAY ||
2899 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2900 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2901 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2902 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002903 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002904 }
2905
2906 /* tag 21590(
2907 10489608748473423768(
2908 2442302357(
2909 21591(
2910 []))))
2911 */
2912 uError = QCBORDecode_GetNext(&DCtx, &Item);
2913 if(uError != QCBOR_SUCCESS ||
2914 Item.uDataType != QCBOR_TYPE_ARRAY ||
2915 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2916 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2917 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2918 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2919 return -8;
2920 }
2921
2922 /* Make sure to blow past the limit of tags that must be mapped.
2923 works in conjuntion with entries above.
2924 269488144(269488145(269488146(269488147([]))))
2925 */
2926 uError = QCBORDecode_GetNext(&DCtx, &Item);
2927 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2928 return -9;
2929 }
2930
2931 uError = QCBORDecode_GetNext(&DCtx, &Item);
2932 if(uError == QCBOR_SUCCESS) {
2933 return -10;
2934 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002935
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002936 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002937 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002938 // tage and then matches it. Caller-config lists are no longer
2939 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002940 QCBORDecode_Init(&DCtx,
2941 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2942 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002943 const uint64_t puList[] = {0x9192939495969798, 257};
2944 const QCBORTagListIn TL = {2, puList};
2945 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002946
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002947 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2948 return -8;
2949 }
2950 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2951 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2952 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2953 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2954 Item.val.uCount != 0) {
2955 return -9;
2956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002957
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002958 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002959 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002960 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002961 const uint64_t puLongList[17] = {1,2,1};
2962 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002963 QCBORDecode_Init(&DCtx,
2964 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2965 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002966 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2967 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2968 return -11;
2969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002970
Laurence Lundblade9b334962020-08-27 10:55:53 -07002971 uint64_t puTags[16];
2972 QCBORTagListOut Out = {0, 4, puTags};
2973
2974
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002975 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002976 QCBORDecode_Init(&DCtx,
2977 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2978 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002979 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2980 return -12;
2981 }
2982 if(puTags[0] != 0x9192939495969798 ||
2983 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002984 puTags[2] != 0x06 ||
2985 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002986 return -13;
2987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002988
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002989 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002990 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002991 QCBORDecode_Init(&DCtx,
2992 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2993 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002994 QCBORTagListOut OutSmall = {0, 3, puTags};
2995 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2996 return -14;
2997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002998
Laurence Lundblade9b334962020-08-27 10:55:53 -07002999
3000
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003001 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003002 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3003 // It is a bit of a messy test and maybe could be improved, but
3004 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003005 QCBORDecode_Init(&DCtx,
3006 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3007 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003008 int n = CheckCSRMaps(&DCtx);
3009 if(n) {
3010 return n-2000;
3011 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003012
Laurence Lundblade59289e52019-12-30 13:44:37 -08003013 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003014 QCBORDecode_Init(&DCtx,
3015 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3016 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003017
Laurence Lundblade9b334962020-08-27 10:55:53 -07003018 /* With the spiffy decode revision, this tag list is not used.
3019 It doesn't matter if a tag is in this list or not so some
3020 tests that couldn't process a tag because it isn't in this list
3021 now can process these unlisted tags. The tests have been
3022 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003023 const uint64_t puTagList[] = {773, 1, 90599561};
3024 const QCBORTagListIn TagList = {3, puTagList};
3025 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003026
3027
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003028 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3029 return -100;
3030 }
3031 if(Item.uDataType != QCBOR_TYPE_MAP ||
3032 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3033 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3034 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3035 Item.val.uCount != 2 ||
3036 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3037 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3038 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3039 Out.uNumUsed != 3) {
3040 return -101;
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 -102;
3045 }
3046 if(Item.uDataType != QCBOR_TYPE_MAP ||
3047 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3048 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003049 !QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003050 Item.val.uCount != 2 ||
3051 puTags[0] != 5859837686836516696 ||
3052 puTags[1] != 7 ||
3053 Out.uNumUsed != 2) {
3054 return -103;
3055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003056
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003057 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3058 return -104;
3059 }
3060 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003061 //Item.uTagBits ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003062 Item.val.uCount != 5 ||
3063 puTags[0] != 0x0b ||
3064 Out.uNumUsed != 1) {
3065 return -105;
3066 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003067
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003068 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3069 return -106;
3070 }
3071 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3072 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3073 Item.val.string.len != 12 ||
3074 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3075 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3076 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3077 Out.uNumUsed != 3) {
3078 return -105;
3079 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003080
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003081 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3082 return -107;
3083 }
3084 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3085 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3086 Item.val.string.len != 3 ||
3087 puTags[0] != 773 ||
3088 Out.uNumUsed != 1) {
3089 return -108;
3090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003091
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003092 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3093 return -109;
3094 }
3095 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003096 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003097 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003098 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003099 puTags[3] != 7 ||
3100 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003101 return -110;
3102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003103
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003104 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3105 return -111;
3106 }
3107 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3108 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3109 Item.val.string.len != 9 ||
3110 puTags[0] != 17 ||
3111 Out.uNumUsed != 1) {
3112 return -112;
3113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003114
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003115 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3116 return -111;
3117 }
3118 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3119 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3120 Item.val.string.len != 2 ||
3121 puTags[0] != 17 ||
3122 Out.uNumUsed != 1) {
3123 return -112;
3124 }
3125
3126 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3127 return -113;
3128 }
3129 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003130 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003131 Item.val.uCount != 2 ||
3132 puTags[0] != 19 ||
3133 Out.uNumUsed != 1) {
3134 return -114;
3135 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003136
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003137 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3138 return -115;
3139 }
3140 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003141 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003142 Item.val.uCount != 1 ||
3143 puTags[0] != 9 ||
3144 Out.uNumUsed != 1) {
3145 return -116;
3146 }
3147
3148 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3149 return -116;
3150 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003151 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003152 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003153 Out.uNumUsed != 0) {
3154 return -117;
3155 }
3156
3157 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3158 return -118;
3159 }
3160 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3161 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003162 puTags[0] != 12 ||
3163 Out.uNumUsed != 1) {
3164 return -119;
3165 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003166
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003167 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3168 return -120;
3169 }
3170 if(Item.uDataType != QCBOR_TYPE_MAP ||
3171 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3172 Item.val.uCount != 1 ||
3173 puTags[0] != 0x17 ||
3174 Out.uNumUsed != 1) {
3175 return -121;
3176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003177
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003178 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3179 return -122;
3180 }
3181 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003182 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003183 Item.val.int64 != -3 ||
3184 puTags[0] != 8 ||
3185 Out.uNumUsed != 1) {
3186 return -123;
3187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003188
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003189 if(QCBORDecode_Finish(&DCtx)) {
3190 return -124;
3191 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003192
3193 UsefulBufC DateString;
3194 QCBORDecode_Init(&DCtx,
3195 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3196 QCBOR_DECODE_MODE_NORMAL);
3197
3198 QCBORDecode_EnterArray(&DCtx);
3199 // tagged date string
3200 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3201 // untagged date string
3202 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3203 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3204 return 100;
3205 }
3206 // untagged byte string
3207 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3208 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3209 return 101;
3210 }
3211 // tagged regex
3212 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3213 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3214 return 102;
3215 }
3216 // tagged date string with a byte string
3217 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3218 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3219 return 103;
3220 }
3221 QCBORDecode_ExitArray(&DCtx);
3222 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3223 return 104;
3224 }
3225
3226
3227 QCBORDecode_Init(&DCtx,
3228 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3229 QCBOR_DECODE_MODE_NORMAL);
3230
3231 QCBORDecode_EnterArray(&DCtx);
3232 // tagged date string
3233 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3234 // untagged date string
3235 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3236 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3237 return 200;
3238 }
3239 // untagged byte string
3240 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3241 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3242 return 201;
3243 }
3244 // tagged regex
3245 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3246 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3247 return 202;
3248 }
3249 // tagged date string with a byte string
3250 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3251 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3252 return 203;
3253 }
3254 QCBORDecode_ExitArray(&DCtx);
3255 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3256 return 204;
3257 }
3258
3259 QCBORDecode_Init(&DCtx,
3260 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3261 QCBOR_DECODE_MODE_NORMAL);
3262
3263 QCBORDecode_EnterArray(&DCtx);
3264 // tagged date string
3265 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3266 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3267 return 300;
3268 }
3269 // untagged date string
3270 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3271 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3272 return 301;
3273 }
3274 // untagged byte string
3275 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3276 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3277 return 302;
3278 }
3279 // tagged regex
3280 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3281 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3282 return 303;
3283 }
3284 // tagged date string with a byte string
3285 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3286 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3287 return 304;
3288 }
3289 QCBORDecode_ExitArray(&DCtx);
3290 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3291 return 305;
3292 }
3293
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003294 return 0;
3295}
3296
3297
3298
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003299
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003300static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003301 0x83,
3302 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3303 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3304 0xA4,
3305 0x63, 0x42, 0x4E, 0x2B,
3306 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3307 0x18, 0x40,
3308 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3309 0x63, 0x42, 0x4E, 0x2D,
3310 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3311 0x38, 0x3F,
3312 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3313
3314
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003315static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003316
3317
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003318int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003319{
3320 QCBORDecodeContext DCtx;
3321 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003322 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003323
Laurence Lundbladeee851742020-01-08 08:37:05 -08003324 QCBORDecode_Init(&DCtx,
3325 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3326 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003327
3328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003329 //
3330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3331 return -1;
3332 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003333 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003335
3336 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003337 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003338 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003339 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003340 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003341 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003342 }
3343
3344 //
3345 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003346 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003347 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003348 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003349 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003350 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003351
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003352 //
3353 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003354 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003355 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003356 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003357 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003359 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003360 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003361 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3362 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003363 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003364 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003365 }
3366
3367 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003368 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003369 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3370 Item.uLabelType != QCBOR_TYPE_INT64 ||
3371 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003372 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003373 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003374 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003376 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003377 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003378 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3379 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003380 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003381 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003382 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003383
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003384 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003385 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003386 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3387 Item.uLabelType != QCBOR_TYPE_INT64 ||
3388 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003389 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003390 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003392
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003393 return 0;
3394}
3395
3396
3397
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003398static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003399 uint8_t uDataType,
3400 uint8_t uNestingLevel,
3401 uint8_t uNextNest,
3402 int64_t nLabel,
3403 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003404{
3405 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003406 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003407
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003408 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3409 if(Item.uDataType != uDataType) return -1;
3410 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003411 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3412 Item.uLabelType != QCBOR_TYPE_UINT64) {
3413 return -1;
3414 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003415 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3416 if(Item.label.int64 != nLabel) return -1;
3417 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003418 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003419 }
3420 }
3421 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303422 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003423
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003424 if(pItem) {
3425 *pItem = Item;
3426 }
3427 return 0;
3428}
3429
3430
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003431// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003432static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003433{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303434 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003435
Laurence Lundblade9b334962020-08-27 10:55:53 -07003436 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003437
Laurence Lundblade9b334962020-08-27 10:55:53 -07003438 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003439
Laurence Lundblade9b334962020-08-27 10:55:53 -07003440 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3441 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3442 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3443 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3444 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003445
Laurence Lundblade9b334962020-08-27 10:55:53 -07003446 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3447 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003448
Laurence Lundblade9b334962020-08-27 10:55:53 -07003449 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3450 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003451
Laurence Lundblade9b334962020-08-27 10:55:53 -07003452 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3453 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003454
Laurence Lundblade9b334962020-08-27 10:55:53 -07003455 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003456
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003457 return 0;
3458}
3459
3460
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003461/*
3462// cbor.me decoded output
3463{
3464 -23: {
3465 -20: {
3466 -18: "Organization",
3467 -17: "SSG",
3468 -15: "Confusion",
3469 -16: "San Diego",
3470 -14: "US"
3471 },
3472 -19: {
3473 -11: {
3474 -9: -7
3475 },
3476 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3477 }
3478 },
3479 -22: {
3480 -5: -3
3481 }
3482}
3483 */
3484
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003485
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003486static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003487 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3488 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3489 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3490 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3491 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3492 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3493 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3494 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3495 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3496
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003497int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003498{
3499 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003500
Laurence Lundbladeee851742020-01-08 08:37:05 -08003501 QCBORDecode_Init(&DCtx,
3502 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3503 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003504
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003505 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003506}
3507
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003508
3509
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003510int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003511{
3512 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003513
Laurence Lundbladeee851742020-01-08 08:37:05 -08003514 QCBORDecode_Init(&DCtx,
3515 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3516 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003517
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003518 QCBORItem Item;
3519 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003520
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003521 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3522 return -1;
3523 }
3524 if(Item.uDataType != QCBOR_TYPE_MAP) {
3525 return -2;
3526 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003527
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003528 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3529 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3530 return -3;
3531 }
3532
3533 return 0;
3534}
3535
3536
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003537// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003538static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003539 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3540 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3541 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3542 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3543 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3544 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003545 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3546 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3547 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3548 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003549
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003550int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003551{
3552 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003553
Laurence Lundbladeee851742020-01-08 08:37:05 -08003554 QCBORDecode_Init(&DCtx,
3555 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3556 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003557
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003558 return CheckCSRMaps(&DCtx);
3559}
3560
3561
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003562
Laurence Lundblade17ede402018-10-13 11:43:07 +08003563static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3564{
3565 UsefulOutBuf UOB;
3566 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003567
Laurence Lundblade17ede402018-10-13 11:43:07 +08003568 int i;
3569 for(i = 0; i < n; i++) {
3570 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3571 }
3572
3573 for(i = 0; i < n; i++) {
3574 UsefulOutBuf_AppendByte(&UOB, 0xff);
3575 }
3576 return UsefulOutBuf_OutUBuf(&UOB);
3577}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003578
3579
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003580static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003581{
3582 QCBORDecodeContext DC;
3583 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003584
Laurence Lundblade17ede402018-10-13 11:43:07 +08003585 int j;
3586 for(j = 0; j < nNestLevel; j++) {
3587 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003588 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003589 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3590 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003591 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003592 return -4;
3593 } else {
3594 return 0; // Decoding doesn't recover after an error
3595 }
3596 } else {
3597 // Should be no error
3598 if(nReturn) {
3599 return -9; // Should not have got an error
3600 }
3601 }
3602 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3603 return -7;
3604 }
3605 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003606 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003607 if(nReturn) {
3608 return -3;
3609 }
3610 return 0;
3611}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003612
3613
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003614int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003615{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303616 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003617 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003618 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003619 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003620 int nReturn = parse_indeflen_nested(Nested, i);
3621 if(nReturn) {
3622 return nReturn;
3623 }
3624 }
3625 return 0;
3626}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003627
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003628
Laurence Lundbladeee851742020-01-08 08:37:05 -08003629// [1, [2, 3]]
3630static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3631// No closing break
3632static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3633// Not enough closing breaks
3634static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3635// Too many closing breaks
3636static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3637// Unclosed indeflen inside def len
3638static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3639// confused tag
3640static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003641
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003642int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003643{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003644 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003645 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003646 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003647
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003648 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303649 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003650 QCBORDecodeContext DC;
3651 QCBORItem Item;
3652 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003653
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003654 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003655
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003656 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303657
3658 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3659 Item.uNestingLevel != 0 ||
3660 Item.uNextNestLevel != 1) {
3661 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003662 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003663
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003664 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303665 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3666 Item.uNestingLevel != 1 ||
3667 Item.uNextNestLevel != 1) {
3668 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003670
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003671 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303672 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3673 Item.uNestingLevel != 1 ||
3674 Item.uNextNestLevel != 2) {
3675 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003676 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003677
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003678 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003679 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303680 Item.uNestingLevel != 2 ||
3681 Item.uNextNestLevel != 2) {
3682 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003683 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003684
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003685 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003686 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303687 Item.uNestingLevel != 2 ||
3688 Item.uNextNestLevel != 0) {
3689 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003690 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003691
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003692 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303693 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003694 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003695
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003696 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003697 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003698
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003699 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003700
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003701 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003702
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003703 nResult = QCBORDecode_GetNext(&DC, &Item);
3704 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303705 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003706 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003707
Laurence Lundblade570fab52018-10-13 18:28:27 +08003708 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003709 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303710 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003711 }
3712
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003713
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003714 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003715 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003716
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003717 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003718
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003719 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003720
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003721 nResult = QCBORDecode_GetNext(&DC, &Item);
3722 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303723 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003724 }
3725
3726 nResult = QCBORDecode_GetNext(&DC, &Item);
3727 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303728 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003729 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003730
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003731 nResult = QCBORDecode_GetNext(&DC, &Item);
3732 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303733 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003734 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003735
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003736 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003737 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303738 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003739 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003740
3741
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003742 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003743 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003744
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003745 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003746
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003747 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003748
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003749 nResult = QCBORDecode_GetNext(&DC, &Item);
3750 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303751 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003752 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003753
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003754 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003755 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303756 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003757 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303758
Laurence Lundblade642282a2020-06-23 12:00:33 -07003759 nResult = QCBORDecode_GetNext(&DC, &Item);
3760 if(nResult != QCBOR_ERR_BAD_BREAK) {
3761 return -140;
3762 }
3763
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003764
Laurence Lundblade570fab52018-10-13 18:28:27 +08003765 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003766 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003767
Laurence Lundblade570fab52018-10-13 18:28:27 +08003768 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003769
Laurence Lundblade570fab52018-10-13 18:28:27 +08003770 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003771
Laurence Lundblade570fab52018-10-13 18:28:27 +08003772 nResult = QCBORDecode_GetNext(&DC, &Item);
3773 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303774 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003775 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003776
Laurence Lundblade570fab52018-10-13 18:28:27 +08003777 nResult = QCBORDecode_GetNext(&DC, &Item);
3778 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303779 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003780 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003781
Laurence Lundblade570fab52018-10-13 18:28:27 +08003782 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003783 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303784 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003785 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003786
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303787 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003788 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003789
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303790 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003791
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303792 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003793
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303794 nResult = QCBORDecode_GetNext(&DC, &Item);
3795 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303796 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303797 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003798
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303799 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303800 if(nResult != QCBOR_ERR_BAD_BREAK) {
3801 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303802 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003803
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003804 return 0;
3805}
3806
Laurence Lundblade17ede402018-10-13 11:43:07 +08003807
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003808static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003809 0x81, // Array of length one
3810 0x7f, // text string marked with indefinite length
3811 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3812 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3813 0xff // ending break
3814};
3815
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003816static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303817 0x81, // Array of length one
3818 0x7f, // text string marked with indefinite length
3819 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3820 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3821 0xff // ending break
3822};
3823
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003824static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303825 0x81, // Array of length one
3826 0x7f, // text string marked with indefinite length
3827 0x01, 0x02, // Not a string
3828 0xff // ending break
3829};
3830
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003831static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303832 0x81, // Array of length one
3833 0x7f, // text string marked with indefinite length
3834 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3835 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3836 // missing end of string
3837};
3838
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003839static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303840 0xa1, // Array of length one
3841 0x7f, // text string marked with indefinite length
3842 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3843 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3844 0xff, // ending break
3845 0x01 // integer being labeled.
3846};
3847
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003848/**
3849 Make an indefinite length string
3850
3851 @param Storage Storage for string, must be 144 bytes in size
3852 @return The indefinite length string
3853
3854 This makes an array with one indefinite length string that has 7 chunks
3855 from size of 1 byte up to 64 bytes.
3856 */
3857static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303858{
3859 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003860
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303861 UsefulOutBuf_Init(&UOB, Storage);
3862 UsefulOutBuf_AppendByte(&UOB, 0x81);
3863 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003864
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003865 uint8_t uStringByte = 0;
3866 // Use of type int is intentional
3867 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3868 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303869 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003870 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3871 for(int j = 0; j < uChunkSize; j++) {
3872 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3873 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303874 }
3875 }
3876 UsefulOutBuf_AppendByte(&UOB, 0xff);
3877
3878 return UsefulOutBuf_OutUBuf(&UOB);
3879}
3880
3881static int CheckBigString(UsefulBufC BigString)
3882{
3883 if(BigString.len != 255) {
3884 return 1;
3885 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003886
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303887 for(uint8_t i = 0; i < 255; i++){
3888 if(((const uint8_t *)BigString.ptr)[i] != i) {
3889 return 1;
3890 }
3891 }
3892 return 0;
3893}
3894
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303895
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003896int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303897{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303898 QCBORDecodeContext DC;
3899 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303900 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003901 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003902
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303903 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003904 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303905 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003906
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303907 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303908 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303909 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003910
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303911 if(QCBORDecode_GetNext(&DC, &Item)) {
3912 return -2;
3913 }
3914 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3915 return -3;
3916 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003917
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303918 if(QCBORDecode_GetNext(&DC, &Item)) {
3919 return -4;
3920 }
3921 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3922 return -5;
3923 }
3924 if(QCBORDecode_Finish(&DC)) {
3925 return -6;
3926 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303927
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303928 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003929 QCBORDecode_Init(&DC,
3930 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3931 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003932
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303933 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3934 return -7;
3935 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003936
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303937 if(QCBORDecode_GetNext(&DC, &Item)) {
3938 return -8;
3939 }
3940 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3941 return -9;
3942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003943
Laurence Lundblade30816f22018-11-10 13:40:22 +07003944 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303945 return -10;
3946 }
3947
3948 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003949 QCBORDecode_Init(&DC,
3950 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3951 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003952
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303953 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3954 return -11;
3955 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003956
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303957 if(QCBORDecode_GetNext(&DC, &Item)) {
3958 return -12;
3959 }
3960 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3961 return -13;
3962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003963
Laurence Lundblade30816f22018-11-10 13:40:22 +07003964 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303965 return -14;
3966 }
3967
3968 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003969 QCBORDecode_Init(&DC,
3970 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3971 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003972
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303973 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3974 return -15;
3975 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003976
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303977 if(QCBORDecode_GetNext(&DC, &Item)) {
3978 return -16;
3979 }
3980 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3981 return -17;
3982 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003983
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303984 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3985 return -18;
3986 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003987
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303988 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303989 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303991 QCBORDecode_GetNext(&DC, &Item);
3992 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303993 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303994 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003995
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303996 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303997 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003999
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304000 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004001 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304002
4003 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4004 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304005 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004007
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304008 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304009 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004010 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004011
Laurence Lundbladeee851742020-01-08 08:37:05 -08004012 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4013 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004014
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304015 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304016 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304017 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004019
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304020 QCBORDecode_GetNext(&DC, &Item);
4021 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304022 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304023 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004024 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304025 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304026 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004027
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304028 // ---- big bstr -----
4029 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004030
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304031 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4032 return -25;
4033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004034
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304035 if(QCBORDecode_GetNext(&DC, &Item)) {
4036 return -26;
4037 }
4038 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304039 return -26;
4040 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004041
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304042 if(QCBORDecode_GetNext(&DC, &Item)) {
4043 return -27;
4044 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304045 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304046 return -28;
4047 }
4048 if(CheckBigString(Item.val.string)) {
4049 return -3;
4050 }
4051 if(QCBORDecode_Finish(&DC)) {
4052 return -29;
4053 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004054
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304055 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004056 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004057
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304058 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4059 return -30;
4060 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004061
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304062 QCBORDecode_GetNext(&DC, &Item);
4063 if(Item.uDataType != QCBOR_TYPE_MAP) {
4064 return -31;
4065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004066
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304067 if(QCBORDecode_GetNext(&DC, &Item)){
4068 return -32;
4069 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004070 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4071 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304072 Item.uDataAlloc || !Item.uLabelAlloc ||
4073 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4074 return -33;
4075 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004076
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304077 if(QCBORDecode_Finish(&DC)) {
4078 return -34;
4079 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004080
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004081 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004082}
4083
4084
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004085int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304086{
4087 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004088 QCBORError nCBORError;
4089
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004090
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304091 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004092 QCBORDecode_Init(&DC,
4093 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4094 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004095
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004096 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004097
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004098 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4099 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304100 return -1;
4101 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004102
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004103 if(CheckCSRMaps(&DC)) {
4104 return -2;
4105 }
4106
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004107 // Next parse, save pointers to a few strings, destroy original and
4108 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004109 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004110 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004111
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304112 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004113 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304114 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004115
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304116 QCBORItem Item1, Item2, Item3, Item4;
4117 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004118 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304119 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4120 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004121 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304122 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004123 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304124 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004125 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304126 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004127 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304128 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004129 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004130
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304131 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304133 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304134 Item1.uDataType != QCBOR_TYPE_INT64 ||
4135 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004136 Item1.uDataAlloc != 0 ||
4137 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004138 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004139 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004140 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004141
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304142
4143 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004144 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304145 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004146 Item2.uDataAlloc != 0 ||
4147 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304148 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004149 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004150
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304151 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004152 Item3.uDataAlloc == 0 ||
4153 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004154 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004155 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004156 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004157
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304158 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004159 Item4.uDataAlloc == 0 ||
4160 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004161 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004162 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004164
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304165 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004166 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004167 QCBORDecode_Init(&DC,
4168 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4169 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304170 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4171 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004172 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304173 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004174 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004175 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004176 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304177 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4178 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4179 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4180 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4181 }
4182 }
4183 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004184 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004185 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304186 }
4187
4188 return 0;
4189}
4190
Laurence Lundbladef6531662018-12-04 10:42:22 +09004191
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304192
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004193int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004194{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004195 // Set up the decoder with a tiny bit of CBOR to parse because
4196 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004197 QCBORDecodeContext DC;
4198 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4199 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004200
Laurence Lundbladef6531662018-12-04 10:42:22 +09004201 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004202 // Then fish into the internals of the decode context
4203 // to get the allocator function so it can be called directly.
4204 // Also figure out how much pool is available for use
4205 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004206 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004207 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4208 if(nError) {
4209 return -9;
4210 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004211 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4212 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4213 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004214
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004215 // First test -- ask for one more byte than available and see failure
4216 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004217 if(!UsefulBuf_IsNULL(Allocated)) {
4218 return -1;
4219 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004220
Laurence Lundbladef6531662018-12-04 10:42:22 +09004221 // Re do the set up for the next test that will do a successful alloc,
4222 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004223 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004224 pAlloc = DC.StringAllocator.pfAllocator;
4225 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4226 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004227
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004228 // Allocate one byte less than available and see success
4229 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004230 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4231 return -2;
4232 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004233 // Ask for some more and see failure
4234 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004235 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4236 return -3;
4237 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004238 // Free the first allocate, retry the second and see success
4239 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4240 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004241 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4242 return -4;
4243 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004244
Laurence Lundbladef6531662018-12-04 10:42:22 +09004245 // Re do set up for next test that involves a successful alloc,
4246 // and a successful realloc and a failed realloc
4247 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004248 pAlloc = DC.StringAllocator.pfAllocator;
4249 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004250
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004251 // Allocate half the pool and see success
4252 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004253 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4254 return -5;
4255 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004256 // Reallocate to take up the whole pool and see success
4257 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004258 if(UsefulBuf_IsNULL(Allocated2)) {
4259 return -6;
4260 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004261 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004262 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4263 return -7;
4264 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004265 // Try to allocate more to be sure there is failure after a realloc
4266 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4267 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004268 return -8;
4269 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004270
Laurence Lundbladef6531662018-12-04 10:42:22 +09004271 return 0;
4272}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004273
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004274
4275/* Just enough of an allocator to test configuration of one */
4276static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4277{
4278 (void)pOldMem; // unused variable
4279
4280 if(uNewSize) {
4281 // Assumes the context pointer is the buffer and
4282 // nothing too big will ever be asked for.
4283 // This is only good for this basic test!
4284 return (UsefulBuf) {pCtx, uNewSize};
4285 } else {
4286 return NULLUsefulBuf;
4287 }
4288}
4289
4290
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004291int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004292{
4293 // Set up the decoder with a tiny bit of CBOR to parse because
4294 // nothing can be done with it unless that is set up.
4295 QCBORDecodeContext DC;
4296 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4297 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4298
4299 uint8_t pAllocatorBuffer[50];
4300
4301 // This is really just to test that this call works.
4302 // The full functionality of string allocators is tested
4303 // elsewhere with the MemPool internal allocator.
4304 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4305
4306 QCBORItem Item;
4307 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4308 return -1;
4309 }
4310
4311 if(Item.uDataAlloc == 0 ||
4312 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4313 Item.val.string.ptr != pAllocatorBuffer) {
4314 return -2;
4315 }
4316
4317 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4318 return -3;
4319 }
4320
4321 return 0;
4322}
4323
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004324#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004325
Laurence Lundbladea826c502020-05-10 21:07:00 -07004326/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004327 [
4328 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004329 4([-20, 4759477275222530853136]),
4330 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004331 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004332 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004333 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004334 5([ 9223372036854775806, -4759477275222530853137])
4335 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004336 ]
4337 */
4338
4339static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004340 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004341 0xC4, 0x82, 0x20,
4342 0x03,
4343 0xC4, 0x82, 0x33,
4344 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4345 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4346 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4347 0xC5, 0x82, 0x19, 0x01, 0x2C,
4348 0x18, 0x64,
4349 0xC5, 0x82, 0x33,
4350 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4351 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4352 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004353 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4354 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004355 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4356 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4357};
4358
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004359
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004360int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004361{
4362 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004363 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004364 QCBORItem item;
4365
Laurence Lundblade17af4902020-01-07 19:11:55 -08004366 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4367 0x06, 0x07, 0x08, 0x09, 0x010};
4368 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004369
4370
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004371 QCBORDecode_Init(&DC,
4372 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4373 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004374
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004375 uErr = QCBORDecode_GetNext(&DC, &item);
4376 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004377 return 1;
4378 }
4379
4380 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4381 return 2;
4382 }
4383
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004384 uErr = QCBORDecode_GetNext(&DC, &item);
4385 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004386 return 3;
4387 }
4388
4389 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4390 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4391 item.val.expAndMantissa.nExponent != -1) {
4392 return 4;
4393 }
4394
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004395 uErr = QCBORDecode_GetNext(&DC, &item);
4396 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004397 return 5;
4398 }
4399
4400 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4401 item.val.expAndMantissa.nExponent != -20 ||
4402 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4403 return 6;
4404 }
4405
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004406 uErr = QCBORDecode_GetNext(&DC, &item);
4407 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004408 return 7;
4409 }
4410
4411 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4412 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4413 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4414 return 8;
4415 }
4416
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004417 uErr = QCBORDecode_GetNext(&DC, &item);
4418 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004419 return 9;
4420 }
4421
4422 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4423 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4424 item.val.expAndMantissa.nExponent != 300) {
4425 return 10;
4426 }
4427
Laurence Lundbladea826c502020-05-10 21:07:00 -07004428 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004429 uErr = QCBORDecode_GetNext(&DC, &item);
4430 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004431 return 11;
4432 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004433 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4434 item.val.expAndMantissa.nExponent != -20 ||
4435 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4436 return 12;
4437 }
4438
Laurence Lundbladea826c502020-05-10 21:07:00 -07004439 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004440 uErr = QCBORDecode_GetNext(&DC, &item);
4441 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004442 return 13;
4443 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004444 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4445 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4446 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4447 return 14;
4448 }
4449
Laurence Lundbladea826c502020-05-10 21:07:00 -07004450 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004451 uErr = QCBORDecode_GetNext(&DC, &item);
4452 if(uErr != QCBOR_SUCCESS) {
4453 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004454 }
4455 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4456 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4457 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004458 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004459 }
4460
Laurence Lundbladea826c502020-05-10 21:07:00 -07004461 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004462 uErr = QCBORDecode_GetNext(&DC, &item);
4463 if(uErr != QCBOR_SUCCESS) {
4464 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004465 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004466 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4467 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4468 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004469 return 18;
4470 }
4471
4472 uErr = QCBORDecode_Finish(&DC);
4473 if(uErr != QCBOR_SUCCESS) {
4474 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004475 }
4476
4477 /* Now encode some stuff and then decode it */
4478 uint8_t pBuf[40];
4479 QCBOREncodeContext EC;
4480 UsefulBufC Encoded;
4481
4482 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4483 QCBOREncode_OpenArray(&EC);
4484 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4485 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4486 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4487 QCBOREncode_CloseArray(&EC);
4488 QCBOREncode_Finish(&EC, &Encoded);
4489
4490
4491 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004492 uErr = QCBORDecode_GetNext(&DC, &item);
4493 if(uErr != QCBOR_SUCCESS) {
4494 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004495 }
4496
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004497 uErr = QCBORDecode_GetNext(&DC, &item);
4498 if(uErr != QCBOR_SUCCESS) {
4499 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004500 }
4501
4502 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4503 item.val.expAndMantissa.nExponent != 1000 ||
4504 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004505 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004506 }
4507
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004508 uErr = QCBORDecode_GetNext(&DC, &item);
4509 if(uErr != QCBOR_SUCCESS) {
4510 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004511 }
4512
4513 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4514 item.val.expAndMantissa.nExponent != INT32_MIN ||
4515 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004516 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004517 }
4518
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004519 uErr = QCBORDecode_GetNext(&DC, &item);
4520 if(uErr != QCBOR_SUCCESS) {
4521 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004522 }
4523
4524 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4525 item.val.expAndMantissa.nExponent != INT32_MAX ||
4526 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004527 return 106;
4528 }
4529
4530
4531 int64_t nExp, nMant;
4532 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4533 UsefulBufC Mant;
4534 bool bIsNeg;
4535
4536 QCBORDecode_Init(&DC,
4537 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4538 QCBOR_DECODE_MODE_NORMAL);
4539 QCBORDecode_EnterArray(&DC);
4540
4541 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004542 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004543
4544 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004545 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4546 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004547
4548 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004549 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4550 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004551
4552 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004553 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004554
4555 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004556 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4557 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004558
4559 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004560 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4561 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004562
4563 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004564 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4565 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004566
4567 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004568 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4569 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004570
4571 QCBORDecode_ExitArray(&DC);
4572
4573 uErr = QCBORDecode_Finish(&DC);
4574 if(uErr != QCBOR_SUCCESS) {
4575 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004576 }
4577
4578 return 0;
4579}
4580
4581
4582static struct FailInput ExponentAndMantissaFailures[] = {
4583 // Exponent > INT64_MAX
4584 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4585 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4586 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4587 // Mantissa > INT64_MAX
4588 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4589 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4590 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4591 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004592 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004593 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004594 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004595 // bad content for big num
4596 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4597 // bad content for big num
4598 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4599 // Bad integer for exponent
4600 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4601 // Bad integer for mantissa
4602 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4603 // 3 items in array
4604 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4605 // unterminated indefinite length array
4606 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4607 // Empty array
4608 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4609 // Second is not an integer
4610 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4611 // First is not an integer
4612 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4613 // Not an array
4614 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4615};
4616
4617
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004618int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004619{
4620 return ProcessFailures(ExponentAndMantissaFailures,
4621 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4622}
4623
4624#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004625
4626
4627
4628/*
4629 Some basic CBOR with map and array used in a lot of tests.
4630 The map labels are all strings
4631
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004632 {
4633 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004634 "an array of two strings": [
4635 "string1", "string2"
4636 ],
4637 "map in a map": {
4638 "bytes 1": h'78787878',
4639 "bytes 2": h'79797979',
4640 "another int": 98,
4641 "text 2": "lies, damn lies and statistics"
4642 }
4643 }
4644 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004645
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004646int32_t EMap(UsefulBufC input)
4647{
4648 QCBORItem Item1, Item2, Item3;
4649 int64_t nDecodedInt1, nDecodedInt2;
4650 UsefulBufC B1, B2, S1, S2, S3;
4651
4652 QCBORDecodeContext DCtx;
4653 QCBORError nCBORError;
4654
4655 QCBORDecode_Init(&DCtx, input, 0);
4656
4657 QCBORDecode_EnterMap(&DCtx);
4658
4659 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4660
4661 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4662 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004663 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4664 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4665 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004666 QCBORDecode_ExitMap(&DCtx);
4667
4668 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4669 QCBORDecode_GetNext(&DCtx, &Item1);
4670 QCBORDecode_GetNext(&DCtx, &Item2);
4671 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4672 return -400;
4673 }
4674 QCBORDecode_ExitArray(&DCtx);
4675
4676 // Parse the same array again using GetText() instead of GetItem()
4677 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004678 QCBORDecode_GetTextString(&DCtx, &S2);
4679 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004680 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4681 return 5000;
4682 }
4683 /* QCBORDecode_GetText(&DCtx, &S3);
4684 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4685 return 5001;
4686 } */
4687
4688 QCBORDecode_ExitArray(&DCtx);
4689
4690 QCBORDecode_ExitMap(&DCtx);
4691
4692 nCBORError = QCBORDecode_Finish(&DCtx);
4693
4694 if(nCBORError) {
4695 return (int32_t)nCBORError;
4696 }
4697
4698 if(nDecodedInt1 != 42) {
4699 return 1001;
4700 }
4701
4702 if(nDecodedInt2 != 98) {
4703 return 1002;
4704 }
4705
4706 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004707 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004708 return 1003;
4709 }
4710
4711 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004712 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004713 return 1004;
4714 }
4715
Laurence Lundblade9b334962020-08-27 10:55:53 -07004716 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004717 return 1005;
4718 }
4719
4720 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4721 return 1006;
4722 }
4723
4724 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4725 return 1007;
4726 }
4727
4728 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4729 return 1008;
4730 }
4731
4732 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4733 return 1009;
4734 }
4735
4736 return 0;
4737}
4738
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004739
4740/*
4741 [23,
4742 6000,
4743 h'67616C6163746963',
4744 h'686176656E20746F6B656E'
4745 ]
4746 */
4747static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004748 0x84,
4749 0x17,
4750 0x19, 0x17, 0x70,
4751 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4752 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004753
4754
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004755static const uint8_t spEmptyMap[] = {0xa0};
4756
4757static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004758
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004759static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4760
Laurence Lundbladef0499502020-08-01 11:55:57 -07004761/*
4762 {
4763 0: [],
4764 9: [
4765 [],
4766 []
4767 ],
4768 8: {
4769 1: [],
4770 2: {},
4771 3: []
4772 },
4773 4: {},
4774 5: [],
4775 6: [
4776 [],
4777 []
4778 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004779 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004780 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004781
Laurence Lundbladef0499502020-08-01 11:55:57 -07004782static const uint8_t spMapOfEmpty[] = {
4783 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4784 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4785 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004786
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004787/*
4788 Too many tags
4789 Invalid tag content
4790 Duplicate label
4791 Integer overflow
4792 Date overflow
4793
4794 {1: 224(225(226(227(4(0))))),
4795 2: 1(h''),
4796 3: -18446744073709551616,
4797 4: 1(1.0e+300),
4798 5: 0, 8: 8}
4799 */
4800static const uint8_t spRecoverableMapErrors[] = {
4801 0xbf,
4802 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
4803 0x02, 0xc1, 0x40,
4804 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4805 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
4806 0x05, 0x00,
4807 0x05, 0x00,
4808 0x08, 0x08,
4809 0xff
4810};
4811
4812// Bad break
4813static const uint8_t spUnRecoverableMapError1[] = {
4814 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
4815};
4816
4817// No more items
4818static const uint8_t spUnRecoverableMapError2[] = {
4819 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
4820};
4821
4822// Hit end because string is too long
4823static const uint8_t spUnRecoverableMapError3[] = {
4824 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
4825};
4826
4827// Hit end because string is too long
4828static const uint8_t spUnRecoverableMapError4[] = {
4829 0xbf,
4830 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4831 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4832 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4833 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4834 0xff
4835};
4836
4837
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004838int32_t EnterMapTest()
4839{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004840 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004841 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004842 int32_t nReturn;
4843 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004844
4845
4846 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4847 QCBORDecode_EnterMap(&DCtx);
4848
Laurence Lundbladef0499502020-08-01 11:55:57 -07004849
4850 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004851 QCBORDecode_ExitArray(&DCtx);
4852
Laurence Lundbladef0499502020-08-01 11:55:57 -07004853 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004854 QCBORDecode_EnterArray(&DCtx);
4855 QCBORDecode_ExitArray(&DCtx);
4856 QCBORDecode_EnterArray(&DCtx);
4857 QCBORDecode_ExitArray(&DCtx);
4858 QCBORDecode_ExitArray(&DCtx);
4859
Laurence Lundbladef0499502020-08-01 11:55:57 -07004860 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004861 QCBORDecode_EnterArray(&DCtx);
4862 QCBORDecode_ExitArray(&DCtx);
4863 QCBORDecode_EnterMap(&DCtx);
4864 QCBORDecode_ExitMap(&DCtx);
4865 QCBORDecode_EnterArray(&DCtx);
4866 QCBORDecode_ExitArray(&DCtx);
4867 QCBORDecode_ExitMap(&DCtx);
4868
Laurence Lundbladef0499502020-08-01 11:55:57 -07004869 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004870 QCBORDecode_ExitMap(&DCtx);
4871
Laurence Lundbladef0499502020-08-01 11:55:57 -07004872 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004873 QCBORDecode_ExitArray(&DCtx);
4874
Laurence Lundbladef0499502020-08-01 11:55:57 -07004875 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004876 QCBORDecode_EnterArray(&DCtx);
4877 QCBORDecode_ExitArray(&DCtx);
4878 QCBORDecode_EnterArray(&DCtx);
4879 QCBORDecode_ExitArray(&DCtx);
4880 QCBORDecode_ExitArray(&DCtx);
4881
4882 QCBORDecode_ExitMap(&DCtx);
4883
4884 uErr = QCBORDecode_Finish(&DCtx);
4885 if(uErr != QCBOR_SUCCESS){
4886 return 3011;
4887 }
4888
4889
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004890 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004891 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004892 if(nReturn) {
4893 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004894 }
4895
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004896 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004897 if(nReturn) {
4898 return nReturn;
4899 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004900
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004901
Laurence Lundblade937ea812020-05-08 11:38:23 -07004902
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004903 // These tests confirm the cursor is at the right place after entering
4904 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004905 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004906
4907 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004908 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004909 QCBORDecode_EnterMap(&DCtx);
4910 QCBORDecode_GetNext(&DCtx, &Item1);
4911 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4912 return 2001;
4913 }
4914
4915
Laurence Lundblade9b334962020-08-27 10:55:53 -07004916 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004917 QCBORDecode_VGetNext(&DCtx, &Item1);
4918 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004919 QCBORDecode_EnterArray(&DCtx);
4920 QCBORDecode_GetNext(&DCtx, &Item1);
4921 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4922 return 2002;
4923 }
4924
Laurence Lundblade9b334962020-08-27 10:55:53 -07004925 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004926 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004927 QCBORDecode_GetNext(&DCtx, &Item1);
4928 QCBORDecode_GetNext(&DCtx, &Item1);
4929 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004930 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4931 QCBORDecode_GetNext(&DCtx, &Item1);
4932 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4933 return 2003;
4934 }
4935
Laurence Lundblade9b334962020-08-27 10:55:53 -07004936 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004937 QCBORDecode_EnterMap(&DCtx);
4938 QCBORDecode_GetNext(&DCtx, &Item1);
4939 QCBORDecode_GetNext(&DCtx, &Item1);
4940 QCBORDecode_GetNext(&DCtx, &Item1);
4941 QCBORDecode_GetNext(&DCtx, &Item1);
4942 QCBORDecode_GetNext(&DCtx, &Item1);
4943 QCBORDecode_GetNext(&DCtx, &Item1);
4944 QCBORDecode_GetNext(&DCtx, &Item1);
4945 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4946 QCBORDecode_GetNext(&DCtx, &Item1);
4947 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004948 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004949 }
4950
Laurence Lundblade9b334962020-08-27 10:55:53 -07004951 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004952 QCBORDecode_EnterMap(&DCtx);
4953 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4954 QCBORDecode_ExitArray(&DCtx);
4955 QCBORDecode_GetNext(&DCtx, &Item1);
4956 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4957 return 2006;
4958 }
4959 QCBORDecode_ExitMap(&DCtx);
4960 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4961 return 2007;
4962 }
4963
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004964 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4965 QCBORDecode_EnterArray(&DCtx);
4966 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004967 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4968 uErr = QCBORDecode_GetAndResetError(&DCtx);
4969 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004970 return 2008;
4971 }
4972 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004973 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004974 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004975 return 2009;
4976 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004977
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004978
4979 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4980 QCBORDecode_EnterMap(&DCtx);
4981 // This will fail because the map is empty.
4982 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4983 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004984 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004985 return 2010;
4986 }
4987 QCBORDecode_ExitMap(&DCtx);
4988 uErr = QCBORDecode_Finish(&DCtx);
4989 if(uErr != QCBOR_SUCCESS){
4990 return 2011;
4991 }
4992
4993
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004994 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4995 QCBORDecode_EnterMap(&DCtx);
4996 // This will fail because the map is empty.
4997 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4998 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004999 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005000 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005001 }
5002 QCBORDecode_ExitMap(&DCtx);
5003 uErr = QCBORDecode_Finish(&DCtx);
5004 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005005 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005006 }
5007
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005008
5009 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
5010 QCBORDecode_EnterArray(&DCtx);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005011 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005012 QCBORDecode_EnterMap(&DCtx);
5013 QCBORDecode_ExitMap(&DCtx);
5014 QCBORDecode_EnterArray(&DCtx);
5015 QCBORDecode_ExitArray(&DCtx);
5016 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5017 QCBORDecode_ExitArray(&DCtx);
5018 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005019 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005020 return 2014;
5021 }
5022
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005023 int64_t nInt;
5024 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
5025 QCBORDecode_EnterMap(&DCtx);
5026 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5027 uErr = QCBORDecode_GetAndResetError(&DCtx);
5028 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5029 return 2021;
5030 }
5031
5032 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5033 uErr = QCBORDecode_GetAndResetError(&DCtx);
5034 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5035 return 2022;
5036 }
5037
5038 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5039 uErr = QCBORDecode_GetAndResetError(&DCtx);
5040 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5041 return 2023;
5042 }
5043
5044 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5045 uErr = QCBORDecode_GetAndResetError(&DCtx);
5046#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5047 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5048 return 2024;
5049 }
5050#else
5051 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5052 return 2027;
5053 }
5054#endif
5055
5056 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5057 uErr = QCBORDecode_GetAndResetError(&DCtx);
5058 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5059 return 2025;
5060 }
5061
5062 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5063
5064 QCBORDecode_ExitMap(&DCtx);
5065 uErr = QCBORDecode_Finish(&DCtx);
5066 if(uErr != QCBOR_SUCCESS) {
5067 return 2026;
5068 }
5069
5070 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
5071 QCBORDecode_EnterMap(&DCtx);
5072 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5073 uErr = QCBORDecode_GetAndResetError(&DCtx);
5074 if(uErr != QCBOR_ERR_BAD_BREAK) {
5075 return 2030;
5076 }
5077
5078 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
5079 QCBORDecode_EnterMap(&DCtx);
5080 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5081 uErr = QCBORDecode_GetAndResetError(&DCtx);
5082 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5083 return 2031;
5084 }
5085
5086 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
5087 QCBORDecode_EnterMap(&DCtx);
5088 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5089 uErr = QCBORDecode_GetAndResetError(&DCtx);
5090 if(uErr != QCBOR_ERR_HIT_END) {
5091 return 2032;
5092 }
5093
5094 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
5095 QCBORDecode_EnterMap(&DCtx);
5096 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5097 uErr = QCBORDecode_GetAndResetError(&DCtx);
5098 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5099 return 2033;
5100 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005101
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005102 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005103}
5104
5105
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005106struct NumberConversion {
5107 char *szDescription;
5108 UsefulBufC CBOR;
5109 int64_t nConvertedToInt64;
5110 QCBORError uErrorInt64;
5111 uint64_t uConvertToUInt64;
5112 QCBORError uErrorUint64;
5113 double dConvertToDouble;
5114 QCBORError uErrorDouble;
5115};
5116
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005117static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005118 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005119 "too large to fit into int64_t",
5120 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5121 0,
5122 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5123 0,
5124 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5125 ((double)INT64_MIN) + 1 ,
5126 QCBOR_SUCCESS
5127 },
5128 {
5129 "largest negative int that fits in int64_t",
5130 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5131 INT64_MIN,
5132 QCBOR_SUCCESS,
5133 0,
5134 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5135 (double)INT64_MIN,
5136 QCBOR_SUCCESS
5137 },
5138 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005139 "negative bignum -1",
5140 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5141 -1,
5142 QCBOR_SUCCESS,
5143 0,
5144 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5145 -1.0,
5146 QCBOR_SUCCESS
5147 },
5148 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005149 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005150 {(uint8_t[]){0xC4, 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 257000,
5154 QCBOR_SUCCESS,
5155 257000,
5156 QCBOR_SUCCESS,
5157 257000.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 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005170 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5171 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005172#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005173 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005174 QCBOR_SUCCESS,
5175 0,
5176 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005177 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005178 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005179#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5180 0,
5181 QCBOR_ERR_UNEXPECTED_TYPE,
5182 0,
5183 QCBOR_ERR_UNEXPECTED_TYPE,
5184 0.0,
5185 QCBOR_ERR_UNEXPECTED_TYPE
5186#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005187 },
5188 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005189 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005190 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5191 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005192#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005193 2056,
5194 QCBOR_SUCCESS,
5195 2056,
5196 QCBOR_SUCCESS,
5197 2056.0,
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 Lundblade887add82020-05-17 05:50:34 -07005207 },
5208 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005209 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005210 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5211 0,
5212 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5213 0,
5214 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5215 -18446744073709551617.0,
5216 QCBOR_SUCCESS
5217 },
5218 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005219 "Positive bignum 0x01020304 indefinite length string",
5220 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5221 0x01020304,
5222 QCBOR_SUCCESS,
5223 0x01020304,
5224 QCBOR_SUCCESS,
5225 16909060.0,
5226 QCBOR_SUCCESS
5227 },
5228 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005229 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005230 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5231 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005232#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005233 0,
5234 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5235 0,
5236 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5237 -INFINITY,
5238 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005239#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5240 0,
5241 QCBOR_ERR_UNEXPECTED_TYPE,
5242 0,
5243 QCBOR_ERR_UNEXPECTED_TYPE,
5244 0.0,
5245 QCBOR_ERR_UNEXPECTED_TYPE
5246#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005247 },
5248 {
5249 "big float [9223372036854775806, 9223372036854775806]",
5250 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5251 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005252#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005253 0,
5254 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5255 0,
5256 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5257 INFINITY,
5258 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005259#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5260 0,
5261 QCBOR_ERR_UNEXPECTED_TYPE,
5262 0,
5263 QCBOR_ERR_UNEXPECTED_TYPE,
5264 0.0,
5265 QCBOR_ERR_UNEXPECTED_TYPE
5266#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005267 },
5268 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005269 "Big float 3 * 2^^2",
5270 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005271#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005272 12,
5273 QCBOR_SUCCESS,
5274 12,
5275 QCBOR_SUCCESS,
5276 12.0,
5277 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005278#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5279 0,
5280 QCBOR_ERR_UNEXPECTED_TYPE,
5281 0,
5282 QCBOR_ERR_UNEXPECTED_TYPE,
5283 0.0,
5284 QCBOR_ERR_UNEXPECTED_TYPE
5285#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005286 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005287 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005288 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005289 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5290 0,
5291 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5292 18446744073709551615ULL,
5293 QCBOR_SUCCESS,
5294 18446744073709551615.0,
5295 QCBOR_SUCCESS
5296 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005297 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005298 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005299 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5300 65536-1,
5301 QCBOR_SUCCESS,
5302 0xffff,
5303 QCBOR_SUCCESS,
5304 65535.0,
5305 QCBOR_SUCCESS
5306 },
5307 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005308 "Postive integer 0",
5309 {(uint8_t[]){0x0}, 1},
5310 0LL,
5311 QCBOR_SUCCESS,
5312 0ULL,
5313 QCBOR_SUCCESS,
5314 0.0,
5315 QCBOR_SUCCESS
5316 },
5317 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005318 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005319 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5320 -9223372036854775807-1, // INT64_MIN
5321 QCBOR_SUCCESS,
5322 0ULL,
5323 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5324 -9223372036854775808.0,
5325 QCBOR_SUCCESS
5326 },
5327 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005328 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005329 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005330#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005331 100L,
5332 QCBOR_SUCCESS,
5333 100ULL,
5334 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005335#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5336 0,
5337 QCBOR_ERR_HW_FLOAT_DISABLED,
5338 0,
5339 QCBOR_ERR_HW_FLOAT_DISABLED,
5340#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005341 100.3,
5342 QCBOR_SUCCESS
5343 },
5344 {
5345 "Floating point value NaN 0xfa7fc00000",
5346 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005347#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005348 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005349 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005350 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005351 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005352#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5353 0,
5354 QCBOR_ERR_HW_FLOAT_DISABLED,
5355 0,
5356 QCBOR_ERR_HW_FLOAT_DISABLED,
5357#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005358 NAN,
5359 QCBOR_SUCCESS
5360 },
5361 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005362 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005363 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005364#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5365#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5366 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005367 -4,
5368 QCBOR_SUCCESS,
5369 0,
5370 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5371 -4.0,
5372 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005373#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5374 // Float HW disabled
5375 -4,
5376 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5377 0,
5378 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5379 -4.0,
5380 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5381#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5382#else
5383 // Half-precision disabled
5384 -4,
5385 QCBOR_ERR_HALF_PRECISION_DISABLED,
5386 0,
5387 QCBOR_ERR_HALF_PRECISION_DISABLED,
5388 -4.0,
5389 QCBOR_ERR_HALF_PRECISION_DISABLED
5390#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005391 },
5392 {
5393 "Decimal fraction 3/10",
5394 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005395#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005396 0,
5397 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5398 0,
5399 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5400 0.30000000000000004,
5401 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005402#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5403 0,
5404 QCBOR_ERR_UNEXPECTED_TYPE,
5405 0,
5406 QCBOR_ERR_UNEXPECTED_TYPE,
5407 0.0,
5408 QCBOR_ERR_UNEXPECTED_TYPE
5409#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005410 },
5411 {
5412 "+inifinity",
5413 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5414#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5415 0,
5416 QCBOR_ERR_FLOAT_EXCEPTION,
5417 0,
5418 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5419#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5420 0,
5421 QCBOR_ERR_HW_FLOAT_DISABLED,
5422 0,
5423 QCBOR_ERR_HW_FLOAT_DISABLED,
5424#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5425 INFINITY,
5426 QCBOR_SUCCESS
5427 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005428
5429 {
5430 "extreme pos bignum",
5431 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5432 // 50 rows of 8 is 400 digits.
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, 0xf0,
5443 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5444 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5445 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5446 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5447 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5448 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5449 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5450 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5451 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5452 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5453 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5454 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5455 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5456 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5457 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5458 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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},
5483 404},
5484 0,
5485 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5486 0,
5487 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5488#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5489 INFINITY,
5490 QCBOR_SUCCESS
5491#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5492 0,
5493 QCBOR_ERR_HW_FLOAT_DISABLED,
5494#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5495 },
5496
5497 {
5498 "extreme neg bignum",
5499 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5500 // 50 rows of 8 is 400 digits.
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, 0xf0,
5511 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5512 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5513 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5514 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5515 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5516 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5517 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5518 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5519 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5520 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5521 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5522 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5523 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5524 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5525 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5526 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5527 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5528 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5529 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5530 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5531 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5532 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5533 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5534 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5535 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5536 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5537 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5538 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5539 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5540 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5541 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5542 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5543 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5544 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5545 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5546 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5547 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5548 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5549 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5550 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5551 404},
5552 0,
5553 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5554 0,
5555 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5556#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5557 -INFINITY,
5558 QCBOR_SUCCESS
5559#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5560 0,
5561 QCBOR_ERR_HW_FLOAT_DISABLED,
5562#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5563 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005564
5565 {
5566 "big float underflow [9223372036854775806, -9223372036854775806]",
5567 {(uint8_t[]){
5568 0xC5, 0x82,
5569 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5570 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5571#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5572 0,
5573 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5574 0,
5575 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5576 0,
5577 QCBOR_SUCCESS
5578#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5579 0,
5580 QCBOR_ERR_UNEXPECTED_TYPE,
5581 0,
5582 QCBOR_ERR_UNEXPECTED_TYPE,
5583 0.0,
5584 QCBOR_ERR_UNEXPECTED_TYPE
5585#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5586 },
5587
5588 {
5589 "bigfloat that evaluates to -INFINITY",
5590 {(uint8_t[]){
5591 0xC5, 0x82,
5592 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5593 0xC3, 0x42, 0x01, 0x01}, 15},
5594#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5595 0,
5596 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5597 0,
5598 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5599 -INFINITY,
5600 QCBOR_SUCCESS
5601#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5602 0,
5603 QCBOR_ERR_UNEXPECTED_TYPE,
5604 0,
5605 QCBOR_ERR_UNEXPECTED_TYPE,
5606 0.0,
5607 QCBOR_ERR_UNEXPECTED_TYPE
5608#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5609 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005610};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005611
5612
5613
Laurence Lundblade313b2862020-05-16 01:23:06 -07005614int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005615{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005616 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005617
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005618 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5619 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005620
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005621 // Set up the decoding context including a memory pool so that
5622 // indefinite length items can be checked
5623 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005624 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005625
5626 /* ----- test conversion to int64_t ------ */
5627 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005628 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5629 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005630 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005631 }
5632
5633 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005634 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005635 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005636 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005637 }
5638 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005639 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005640 }
5641
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005642 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005643 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5644 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5645 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005646 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005647 }
5648 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005649 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005650 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005651 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005652 }
5653 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005654 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005655 }
5656
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005657 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005658 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5659 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5660 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005661 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005662 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005663#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005664 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005665 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005666 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005667 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005668 }
5669 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5670 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005671 // NaN's can't be compared for equality. A NaN is
5672 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005673 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005674 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005675 }
5676 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005677 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005678 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005679 }
5680 }
5681 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005682#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005683 }
5684
5685 return 0;
5686}
5687
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005688
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005689
5690
Laurence Lundbladee3553422020-05-02 11:11:17 -07005691int32_t CBORSequenceDecodeTests(void)
5692{
5693 QCBORDecodeContext DCtx;
5694 QCBORItem Item;
5695 QCBORError uCBORError;
5696
5697 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005698
Laurence Lundbladee3553422020-05-02 11:11:17 -07005699 // The input for the date test happens to be a sequence so it
5700 // is reused. It is a sequence because it doesn't start as
5701 // an array or map.
5702 QCBORDecode_Init(&DCtx,
5703 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5704 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005705
Laurence Lundbladee3553422020-05-02 11:11:17 -07005706 // Get the first item
5707 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5708 if(uCBORError != QCBOR_SUCCESS) {
5709 return 1;
5710 }
5711 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5712 return 2;
5713 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005714
Laurence Lundbladee3553422020-05-02 11:11:17 -07005715 // Get a second item
5716 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005717 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5718 return 66;
5719 }
5720
5721 // Get a third item
5722 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005723 if(uCBORError != QCBOR_SUCCESS) {
5724 return 2;
5725 }
5726 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5727 return 3;
5728 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005729
Laurence Lundbladee3553422020-05-02 11:11:17 -07005730 // A sequence can have stuff at the end that may
5731 // or may not be valid CBOR. The protocol decoder knows
5732 // when to stop by definition of the protocol, not
5733 // when the top-level map or array is ended.
5734 // Finish still has to be called to know that
5735 // maps and arrays (if there were any) were closed
5736 // off correctly. When called like this it
5737 // must return the error QCBOR_ERR_EXTRA_BYTES.
5738 uCBORError = QCBORDecode_Finish(&DCtx);
5739 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5740 return 4;
5741 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005742
5743
Laurence Lundbladee3553422020-05-02 11:11:17 -07005744 // --- Test an empty input ----
5745 uint8_t empty[1];
5746 UsefulBufC Empty = {empty, 0};
5747 QCBORDecode_Init(&DCtx,
5748 Empty,
5749 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005750
Laurence Lundbladee3553422020-05-02 11:11:17 -07005751 uCBORError = QCBORDecode_Finish(&DCtx);
5752 if(uCBORError != QCBOR_SUCCESS) {
5753 return 5;
5754 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005755
5756
Laurence Lundbladee3553422020-05-02 11:11:17 -07005757 // --- Sequence with unclosed indefinite length array ---
5758 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005759
Laurence Lundbladee3553422020-05-02 11:11:17 -07005760 QCBORDecode_Init(&DCtx,
5761 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5762 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005763
Laurence Lundbladee3553422020-05-02 11:11:17 -07005764 // Get the first item
5765 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5766 if(uCBORError != QCBOR_SUCCESS) {
5767 return 7;
5768 }
5769 if(Item.uDataType != QCBOR_TYPE_INT64) {
5770 return 8;
5771 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005772
Laurence Lundbladee3553422020-05-02 11:11:17 -07005773 // Get a second item
5774 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5775 if(uCBORError != QCBOR_SUCCESS) {
5776 return 9;
5777 }
5778 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5779 return 10;
5780 }
5781
5782 // Try to finish before consuming all bytes to confirm
5783 // that the still-open error is returned.
5784 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005785 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005786 return 11;
5787 }
5788
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005789
Laurence Lundbladee3553422020-05-02 11:11:17 -07005790 // --- Sequence with a closed indefinite length array ---
5791 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005792
Laurence Lundbladee3553422020-05-02 11:11:17 -07005793 QCBORDecode_Init(&DCtx,
5794 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5795 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005796
Laurence Lundbladee3553422020-05-02 11:11:17 -07005797 // Get the first item
5798 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5799 if(uCBORError != QCBOR_SUCCESS) {
5800 return 12;
5801 }
5802 if(Item.uDataType != QCBOR_TYPE_INT64) {
5803 return 13;
5804 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005805
Laurence Lundbladee3553422020-05-02 11:11:17 -07005806 // Get a second item
5807 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5808 if(uCBORError != QCBOR_SUCCESS) {
5809 return 14;
5810 }
5811 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5812 return 15;
5813 }
5814
5815 // Try to finish before consuming all bytes to confirm
5816 // that the still-open error is returned.
5817 uCBORError = QCBORDecode_Finish(&DCtx);
5818 if(uCBORError != QCBOR_SUCCESS) {
5819 return 16;
5820 }
5821
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005822
Laurence Lundbladee3553422020-05-02 11:11:17 -07005823 return 0;
5824}
5825
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005826
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005827
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005828int32_t IntToTests()
5829{
5830 int nErrCode;
5831 int32_t n32;
5832 int16_t n16;
5833 int8_t n8;
5834 uint32_t u32;
5835 uint16_t u16;
5836 uint8_t u8;
5837 uint64_t u64;
5838
5839 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5840 if(nErrCode == -1 || n32 != 1) {
5841 return 1;
5842 }
5843
5844 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5845 if(nErrCode == -1 || n32 != INT32_MAX) {
5846 return 2;
5847 }
5848
5849 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5850 if(nErrCode == -1 || n32 != INT32_MIN) {
5851 return 3;
5852 }
5853
5854 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5855 if(nErrCode != -1) {
5856 return 4;
5857 }
5858
5859 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5860 if(nErrCode != -1) {
5861 return 5;
5862 }
5863
5864
5865 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5866 if(nErrCode == -1 || n16 != INT16_MAX) {
5867 return 6;
5868 }
5869
5870 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5871 if(nErrCode == -1 || n16 != INT16_MIN) {
5872 return 7;
5873 }
5874
5875 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5876 if(nErrCode == -1 || n16 != 1) {
5877 return 8;
5878 }
5879
5880 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5881 if(nErrCode != -1) {
5882 return 9;
5883 }
5884
5885 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5886 if(nErrCode != -1) {
5887 return 10;
5888 }
5889
5890
5891 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5892 if(nErrCode == -1 || n8 != 1) {
5893 return 11;
5894 }
5895
5896 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5897 if(nErrCode == -1 || n8 != INT8_MAX) {
5898 return 12;
5899 }
5900
5901 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5902 if(nErrCode == -1 || n8 != INT8_MIN) {
5903 return 13;
5904 }
5905
5906 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5907 if(nErrCode != -1) {
5908 return 14;
5909 }
5910
5911 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5912 if(nErrCode != -1) {
5913 return 15;
5914 }
5915
5916
5917 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5918 if(nErrCode == -1 || u32 != 1) {
5919 return 16;
5920 }
5921
5922 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5923 if(nErrCode == -1 || u32 != UINT32_MAX) {
5924 return 17;
5925 }
5926
5927 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5928 if(nErrCode == -1 || u32 != 0) {
5929 return 18;
5930 }
5931
5932 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5933 if(nErrCode != -1) {
5934 return 19;
5935 }
5936
5937 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5938 if(nErrCode != -1) {
5939 return 20;
5940 }
5941
5942
5943 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5944 if(nErrCode == -1 || u16 != UINT16_MAX) {
5945 return 21;
5946 }
5947
5948 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5949 if(nErrCode == -1 || u16 != 0) {
5950 return 22;
5951 }
5952
5953 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5954 if(nErrCode == -1 || u16 != 1) {
5955 return 23;
5956 }
5957
5958 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5959 if(nErrCode != -1) {
5960 return 24;
5961 }
5962
5963 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5964 if(nErrCode != -1) {
5965 return 25;
5966 }
5967
5968
5969 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5970 if(nErrCode == -1 || u8 != UINT8_MAX) {
5971 return 26;
5972 }
5973
5974 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5975 if(nErrCode == -1 || u8 != 0) {
5976 return 27;
5977 }
5978
5979 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5980 if(nErrCode == -1 || u8 != 1) {
5981 return 28;
5982 }
5983
5984 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5985 if(nErrCode != -1) {
5986 return 29;
5987 }
5988
5989 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5990 if(nErrCode != -1) {
5991 return 30;
5992 }
5993
5994
5995 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5996 if(nErrCode == -1 || u64 != 1) {
5997 return 31;
5998 }
5999
6000 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6001 if(nErrCode == -1 || u64 != INT64_MAX) {
6002 return 32;
6003 }
6004
6005 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6006 if(nErrCode == -1 || u64 != 0) {
6007 return 33;
6008 }
6009
6010 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6011 if(nErrCode != -1) {
6012 return 34;
6013 }
6014
6015 return 0;
6016}
6017
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006018
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006019
6020
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006021/*
6022A sequence with
6023 A wrapping bstr
6024 containing a map
6025 1
6026 2
6027 A wrapping bstr
6028 containing an array
6029 3
6030 wrapping bstr
6031 4
6032 5
6033 6
6034 array
6035 7
6036 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006037 */
6038
Laurence Lundblade55013642020-09-23 05:39:22 -07006039static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006040{
Laurence Lundblade55013642020-09-23 05:39:22 -07006041 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006042 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006043 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006044
Laurence Lundblade55013642020-09-23 05:39:22 -07006045 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006046
6047 QCBOREncode_BstrWrap(&EC);
6048 QCBOREncode_OpenMap(&EC);
6049 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6050 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6051 QCBOREncode_CloseMap(&EC);
6052 QCBOREncode_BstrWrap(&EC);
6053 QCBOREncode_OpenArray(&EC);
6054 QCBOREncode_AddInt64(&EC, 3);
6055 QCBOREncode_BstrWrap(&EC);
6056 QCBOREncode_AddInt64(&EC, 4);
6057 QCBOREncode_CloseBstrWrap(&EC, NULL);
6058 QCBOREncode_AddInt64(&EC, 5);
6059 QCBOREncode_CloseArray(&EC);
6060 QCBOREncode_CloseBstrWrap(&EC, NULL);
6061 QCBOREncode_AddInt64(&EC, 6);
6062 QCBOREncode_CloseBstrWrap(&EC, NULL);
6063 QCBOREncode_OpenArray(&EC);
6064 QCBOREncode_AddInt64(&EC, 7);
6065 QCBOREncode_AddInt64(&EC, 8);
6066 QCBOREncode_CloseArray(&EC);
6067
6068 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006069 if(uErr) {
6070 Encoded = NULLUsefulBufC;
6071 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006072
6073 return Encoded;
6074}
6075
6076
6077int32_t EnterBstrTest()
6078{
Laurence Lundblade55013642020-09-23 05:39:22 -07006079 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006080
6081 QCBORDecodeContext DC;
6082
Laurence Lundblade55013642020-09-23 05:39:22 -07006083 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006084
Laurence Lundblade55013642020-09-23 05:39:22 -07006085 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006086
6087
Laurence Lundblade9b334962020-08-27 10:55:53 -07006088 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006089 QCBORDecode_EnterMap(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006090 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6091 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006092 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006093 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006094 QCBORDecode_EnterArray(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006095 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006096 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006097 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006098 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006099 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006100 QCBORDecode_ExitArray(&DC);
6101 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006102 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006103 QCBORDecode_ExitBstrWrapped(&DC);
6104 QCBORDecode_EnterArray(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006105 QCBORDecode_GetInt64(&DC, &n7);
6106 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006107 QCBORDecode_ExitArray(&DC);
6108
6109 QCBORError uErr = QCBORDecode_Finish(&DC);
6110
6111 return (int32_t)uErr;
6112}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006113
6114
6115
6116
6117static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006118 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006119
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006120 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006121 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006122 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6123 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6124 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006125
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006126 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006127 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6128 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6129 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006130
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006131 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006132 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006133 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6134 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006135
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006136 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006137 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6138 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006139
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006140 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006141 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006142 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6143 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006144
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006145 21,
6146 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6147 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6148
6149 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006150 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006151 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6152 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006153
6154 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006155 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6156 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006157
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006158 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006159 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006160 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6161 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006162
6163 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006164 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6165 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006166
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006167 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006168 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006169 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6170 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006171
6172 0x18, 0x33,
6173 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6174
6175 // MIME
6176 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006177 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6178 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6179 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006180
6181 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006182 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6183 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006184
6185 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006186 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6187 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6188 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006189
6190 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006191 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6192 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006193
6194 // UUID
6195 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006196 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6197 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006198
6199 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006200 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6201 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006202};
6203
6204int32_t DecodeTaggedTypeTests()
6205{
6206 QCBORDecodeContext DC;
6207 QCBORError uErr;
6208
6209 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6210
6211 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006212 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006213
6214 QCBORDecode_EnterMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006215 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006216 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006217 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6218 return 1;
6219 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006220 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006221 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6222 return 2;
6223 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006224 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006225 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6226 return 3;
6227 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006228 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006229 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006230 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6231 return 4;
6232 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006233 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_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 5;
6236 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006237
Laurence Lundblade9b334962020-08-27 10:55:53 -07006238 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006239 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6240 bNeg != false) {
6241 return 10;
6242 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006243 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006244 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6245 bNeg != true) {
6246 return 11;
6247 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006248 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006249 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6250 return 12;
6251 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006252 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006253 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006254 return 13;
6255 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006256 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006257 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006258 return 14;
6259 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006260
Laurence Lundblade9b334962020-08-27 10:55:53 -07006261 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006262 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6263 return 20;
6264 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006265 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006266 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6267 return 21;
6268 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006269 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006270 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006271 return 22;
6272 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006273 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006274 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006275 return 23;
6276 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006277
Laurence Lundblade9b334962020-08-27 10:55:53 -07006278 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006279 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6280 return 30;
6281 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006282 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006283 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6284 return 31;
6285 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006286 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006287 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006288 return 32;
6289 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006290 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006291 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006292 return 33;
6293 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006294
Laurence Lundblade9b334962020-08-27 10:55:53 -07006295 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006296 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6297 return 40;
6298 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006299 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006300 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6301 return 41;
6302 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006303 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006304 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006305 return 42;
6306 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006307 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006308 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006309 return 43;
6310 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006311
Laurence Lundblade9b334962020-08-27 10:55:53 -07006312 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006313 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6314 return 50;
6315 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006316 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006317 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6318 return 51;
6319 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006320 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006321 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006322 return 52;
6323 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006324 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006325 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006326 return 53;
6327 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006328
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006329 // MIME
6330 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006331 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006332 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6333 bIsNot7Bit == true) {
6334 return 60;
6335 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006336 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006337 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6338 bIsNot7Bit == true) {
6339 return 61;
6340 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006341 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006342 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6343 bIsNot7Bit == false) {
6344 return 62;
6345 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006346 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006347 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6348 bIsNot7Bit == false) {
6349 return 63;
6350 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006351 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006352 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006353 return 64;
6354 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006355 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006356 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006357 return 65;
6358 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006359
Laurence Lundblade9b334962020-08-27 10:55:53 -07006360 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006361 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6362 return 70;
6363 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006364 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006365 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6366 return 71;
6367 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006368 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006369 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006370 return 72;
6371 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006372 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006373 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006374 return 73;
6375 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006376
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006377 // Improvement: add some more error test cases
6378
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006379 QCBORDecode_ExitMap(&DC);
6380
6381 uErr = QCBORDecode_Finish(&DC);
6382 if(uErr != QCBOR_SUCCESS) {
6383 return 100;
6384 }
6385
6386 return 0;
6387}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006388
6389
6390
6391
6392/*
6393 [
6394 "aaaaaaaaaa",
6395 {}
6396 ]
6397 */
6398static const uint8_t spTooLarge1[] = {
6399 0x9f,
6400 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6401 0xa0,
6402 0xff
6403};
6404
6405/*
6406 [
6407 {
6408 0: "aaaaaaaaaa"
6409 }
6410 ]
6411 */
6412static const uint8_t spTooLarge2[] = {
6413 0x9f,
6414 0xa1,
6415 0x00,
6416 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6417 0xff
6418};
6419
6420/*
6421 h'A1006A61616161616161616161'
6422
6423 {
6424 0: "aaaaaaaaaa"
6425 }
6426 */
6427static const uint8_t spTooLarge3[] = {
6428 0x4d,
6429 0xa1,
6430 0x00,
6431 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6432};
6433
6434int32_t TooLargeInputTest(void)
6435{
6436 QCBORDecodeContext DC;
6437 QCBORError uErr;
6438 UsefulBufC String;
6439
6440 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6441 // to 10 There's not really any way to test this error
6442 // condition. The error condition is not complex, so setting
6443 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6444
6445 // The input CBOR is only too large because the
6446 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6447 //
6448 // This test is disabled for the normal test runs because of the
6449 // special build requirement.
6450
6451
6452 // Tests the start of a map being too large
6453 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
6454 QCBORDecode_EnterArray(&DC);
6455 QCBORDecode_GetTextString(&DC, &String);
6456 uErr = QCBORDecode_GetError(&DC);
6457 if(uErr != QCBOR_SUCCESS) {
6458 return 1;
6459 }
6460 QCBORDecode_EnterMap(&DC);
6461 uErr = QCBORDecode_GetError(&DC);
6462 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6463 return 2;
6464 }
6465
6466 // Tests the end of a map being too large
6467 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
6468 QCBORDecode_EnterArray(&DC);
6469 QCBORDecode_EnterMap(&DC);
6470 uErr = QCBORDecode_GetError(&DC);
6471 if(uErr != QCBOR_SUCCESS) {
6472 return 3;
6473 }
6474 QCBORDecode_ExitMap(&DC);
6475 uErr = QCBORDecode_GetError(&DC);
6476 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6477 return 4;
6478 }
6479
6480 // Tests the entire input CBOR being too large when processing bstr wrapping
6481 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6482 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6483 uErr = QCBORDecode_GetError(&DC);
6484 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6485 return 5;
6486 }
6487
6488 return 0;
6489}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006490
6491
6492static const uint8_t spMapWithIndefLenStrings[] = {
6493 0xbf,
6494 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6495 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6496 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6497 0x03,
6498 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6499 0xc3,
6500 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6501 0xff
6502};
6503
6504int32_t SpiffyIndefiniteLengthStringsTests()
6505{
6506 QCBORDecodeContext DCtx;
6507
6508 QCBORDecode_Init(&DCtx,
6509 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6510 QCBOR_DECODE_MODE_NORMAL);
6511
6512 MakeUsefulBufOnStack(StringBuf, 200);
6513 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6514
6515 UsefulBufC ByteString;
6516 QCBORDecode_EnterMap(&DCtx);
6517 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6518 if(QCBORDecode_GetAndResetError(&DCtx)) {
6519 return 1;
6520 }
6521
6522 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6523 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6524 return 2;
6525 }
6526
6527 uint64_t uInt;
6528 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6529 if(QCBORDecode_GetAndResetError(&DCtx)) {
6530 return 3;
6531 }
6532 if(uInt != 3) {
6533 return 4;
6534 }
6535
6536 double uDouble;
6537 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6538 "label2",
6539 0xff,
6540 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006541#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006542 if(QCBORDecode_GetAndResetError(&DCtx)) {
6543 return 5;
6544 }
6545 if(uDouble != -16777474) {
6546 return 6;
6547 }
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006548#else
6549 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6550 return 7;
6551 }
6552#endif
6553
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006554
6555 QCBORDecode_ExitMap(&DCtx);
6556
6557 if(QCBORDecode_Finish(&DCtx)) {
6558 return 99;
6559 }
6560
6561 return 0;
6562}