blob: cfa145099446f472d1339471a3ee8df2ec6d105f [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070036#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080037#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080038#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070039#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080040
Laurence Lundblade9b334962020-08-27 10:55:53 -070041// Handy macro to compare a UsefulBuf to a C string
42#define UsefulBufCompareToSZ(x, y) \
43 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044
Laurence Lundbladea2e29072018-12-30 09:20:06 -080045#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080046#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080047
48static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080049{
50 if(szLabel) {
51 printf("%s ", szLabel);
52 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080053
Laurence Lundblade570fab52018-10-13 18:28:27 +080054 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080055 for(i = 0; i < Buf.len; i++) {
56 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 printf("%02x ", Z);
58 }
59 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080060
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 fflush(stdout);
62}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080063#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064
65
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070066static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080067 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
69 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
70 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
71 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
72 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
73 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
74 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
75 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
76 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
77 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
78 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
79 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
80 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
81 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
82 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
83 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
84 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
85 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
86 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
87 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
88 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 0xff, 0xff};
91
92
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080093// return CBOR error or -1 if type of value doesn't match
94
Laurence Lundbladec5fef682020-01-25 11:38:45 -080095static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080096{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070097 QCBORItem Item;
98 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080099
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800100 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700101 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102 if(Item.uDataType != QCBOR_TYPE_ARRAY)
103 return -1;
104
105 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700106 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800107 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800108 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 return -1;
110
111 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700112 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800114 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800116
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700118 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800120 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700124 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800126 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800128
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700130 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800131 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800132 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800134
135
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800136 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700137 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 if(Item.uDataType != QCBOR_TYPE_INT64 ||
139 Item.val.int64 != -2147483648)
140 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800141
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800142 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700143 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if(Item.uDataType != QCBOR_TYPE_INT64 ||
145 Item.val.int64 != -2147483647)
146 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800147
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700149 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if(Item.uDataType != QCBOR_TYPE_INT64 ||
151 Item.val.int64 != -65538)
152 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800153
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800154 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700155 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if(Item.uDataType != QCBOR_TYPE_INT64 ||
157 Item.val.int64 != -65537)
158 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800159
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800160 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700161 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800162 if(Item.uDataType != QCBOR_TYPE_INT64 ||
163 Item.val.int64 != -65536)
164 return -1;
165
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700168 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if(Item.uDataType != QCBOR_TYPE_INT64 ||
170 Item.val.int64 != -65535)
171 return -1;
172
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800173
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800174 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700175 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 if(Item.uDataType != QCBOR_TYPE_INT64 ||
177 Item.val.int64 != -65534)
178 return -1;
179
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800180
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700182 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
184 Item.val.int64 != -257)
185 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800186
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800187 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700188 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if(Item.uDataType != QCBOR_TYPE_INT64 ||
190 Item.val.int64 != -256)
191 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800192
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800193 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700194 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(Item.uDataType != QCBOR_TYPE_INT64 ||
196 Item.val.int64 != -255)
197 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800198
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700200 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(Item.uDataType != QCBOR_TYPE_INT64 ||
202 Item.val.int64 != -254)
203 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800204
205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700207 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
209 Item.val.int64 != -25)
210 return -1;
211
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800212
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800213 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700214 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 if(Item.uDataType != QCBOR_TYPE_INT64 ||
216 Item.val.int64 != -24)
217 return -1;
218
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700221 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if(Item.uDataType != QCBOR_TYPE_INT64 ||
223 Item.val.int64 != -23)
224 return -1;
225
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800227 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700228 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800229 if(Item.uDataType != QCBOR_TYPE_INT64 ||
230 Item.val.int64 != -1)
231 return -1;
232
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800233
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800234 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700235 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 if(Item.uDataType != QCBOR_TYPE_INT64 ||
237 Item.val.int64 != 0)
238 return -1;
239
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800241 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700242 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 if(Item.uDataType != QCBOR_TYPE_INT64 ||
244 Item.val.int64 != 0)
245 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700248 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(Item.uDataType != QCBOR_TYPE_INT64 ||
250 Item.val.int64 != 1)
251 return -1;
252
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800254 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700255 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 if(Item.uDataType != QCBOR_TYPE_INT64 ||
257 Item.val.int64 != 22)
258 return -1;
259
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700262 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
264 Item.val.int64 != 23)
265 return -1;
266
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800268 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700269 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800270 if(Item.uDataType != QCBOR_TYPE_INT64 ||
271 Item.val.int64 != 24)
272 return -1;
273
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800274
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800275 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700276 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800277 if(Item.uDataType != QCBOR_TYPE_INT64 ||
278 Item.val.int64 != 25)
279 return -1;
280
281 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700282 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
284 Item.val.int64 != 26)
285 return -1;
286
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700289 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(Item.uDataType != QCBOR_TYPE_INT64 ||
291 Item.val.int64 != 254)
292 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800293
294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700296 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(Item.uDataType != QCBOR_TYPE_INT64 ||
298 Item.val.int64 != 255)
299 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800300
301
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700303 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(Item.uDataType != QCBOR_TYPE_INT64 ||
305 Item.val.int64 != 256)
306 return -1;
307
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800308
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800309 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700310 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(Item.uDataType != QCBOR_TYPE_INT64 ||
312 Item.val.int64 != 257)
313 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800315 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700316 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(Item.uDataType != QCBOR_TYPE_INT64 ||
318 Item.val.int64 != 65534)
319 return -1;
320
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700323 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(Item.uDataType != QCBOR_TYPE_INT64 ||
325 Item.val.int64 != 65535)
326 return -1;
327
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700330 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
332 Item.val.int64 != 65536)
333 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800334
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800335 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700336 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 if(Item.uDataType != QCBOR_TYPE_INT64 ||
338 Item.val.int64 != 65537)
339 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800341 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700342 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(Item.uDataType != QCBOR_TYPE_INT64 ||
344 Item.val.int64 != 65538)
345 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800347 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700348 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(Item.uDataType != QCBOR_TYPE_INT64 ||
350 Item.val.int64 != 2147483647)
351 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800352
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800353 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700354 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 if(Item.uDataType != QCBOR_TYPE_INT64 ||
356 Item.val.int64 != 2147483647)
357 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800359 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700360 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(Item.uDataType != QCBOR_TYPE_INT64 ||
362 Item.val.int64 != 2147483648)
363 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800364
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800365 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700366 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(Item.uDataType != QCBOR_TYPE_INT64 ||
368 Item.val.int64 != 2147483649)
369 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800370
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800371 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700372 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 if(Item.uDataType != QCBOR_TYPE_INT64 ||
374 Item.val.int64 != 4294967294)
375 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800378 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700379 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 if(Item.uDataType != QCBOR_TYPE_INT64 ||
381 Item.val.int64 != 4294967295)
382 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800383
384
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800385 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700386 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(Item.uDataType != QCBOR_TYPE_INT64 ||
388 Item.val.int64 != 4294967296)
389 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800390
391
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800392 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700393 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800394 if(Item.uDataType != QCBOR_TYPE_INT64 ||
395 Item.val.int64 != 4294967297)
396 return -1;
397
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800398
399
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800400 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700401 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800402 if(Item.uDataType != QCBOR_TYPE_INT64 ||
403 Item.val.int64 != 9223372036854775807LL)
404 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800405
406
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800407 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700408 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
410 Item.val.uint64 != 18446744073709551615ULL)
411 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800412
413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
415 return -1;
416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800418 return 0;
419}
420
421
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800422// One less than the smallest negative integer allowed in C. Decoding
423// this should fail.
424static const uint8_t spTooSmallNegative[] = {
425 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000426};
427
428
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800429/*
430 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800431 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800433int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000435 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800436 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000438 QCBORDecode_Init(&DCtx,
439 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
440 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800441
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000442 // The really big test of all successes
443 nReturn = IntegerValuesParseTestInternal(&DCtx);
444 if(nReturn) {
445 return nReturn;
446 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800447
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000448 // The one large negative integer that can be parsed
449 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800450 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000451 QCBOR_DECODE_MODE_NORMAL);
452
453 QCBORItem item;
454 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
455 nReturn = -4000;
456 }
457
458 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459}
460
461
462/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800463 Creates a simple CBOR array and returns it in *pEncoded. The array is
464 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 */
469
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800470static uint8_t spSimpleArrayBuffer[50];
471
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800472static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473{
474 QCBOREncodeContext ECtx;
475 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800476
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 *pEncoded = NULL;
478 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800479
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800481 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 // and last with the buffer to do the actual encoding
483 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700484 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800485 QCBOREncode_OpenArray(&ECtx);
486 QCBOREncode_AddInt64(&ECtx, nInt1);
487 QCBOREncode_AddInt64(&ECtx, nInt2);
488 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
489 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
490 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800491
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800492 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800493 goto Done;
494
495 if(*pEncoded != NULL) {
496 nReturn = 0;
497 goto Done;
498 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800499
500 // Use static buffer to avoid dependency on malloc()
501 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 goto Done;
503 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800504 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800505
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800508Done:
509 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800510}
511
512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800514 Some basic CBOR with map and array used in a lot of tests.
515 The map labels are all strings
516
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900518 "an array of two strings": [
519 "string1", "string2"
520 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521 "map in a map": {
522 "bytes 1": h'78787878',
523 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900524 "another int": 98,
525 "text 2": "lies, damn lies and statistics"
526 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800527 }
528 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800529static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700530 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
531 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
532 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
533 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
534 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
535 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
536 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
537 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
538 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
539 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
540 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
541 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
542 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
543 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
544 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700545 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800546
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700547// Same as above, but with indefinite lengths.
548static const uint8_t pValidMapIndefEncoded[] = {
5490xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5500x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5510x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5520x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5530x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5540x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5550x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5560x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5570x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5580x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5590x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5600x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5610x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5620x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5630x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5640x73, 0xff, 0xff};
565
566
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800567static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700568 size_t nLen,
569 int64_t *pInt1,
570 int64_t *pInt2,
571 const uint8_t **pBuf3,
572 size_t *pBuf3Len,
573 const uint8_t **pBuf4,
574 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800575{
576 QCBORDecodeContext DCtx;
577 QCBORItem Item;
578 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundbladeee851742020-01-08 08:37:05 -0800580 QCBORDecode_Init(&DCtx,
581 (UsefulBufC){pEncoded, nLen},
582 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800583
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800584 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700585 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
586 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800590 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700591 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
592 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800593 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700594 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800596
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700598 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
599 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800600 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700601 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800602 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700605 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
606 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700608 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 *pBuf3 = Item.val.string.ptr;
610 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700613 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
614 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800615 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700616 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 *pBuf4 = Item.val.string.ptr;
618 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800621
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800622Done:
623 return(nReturn);
624}
625
626
627
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800628
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800629int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800630{
631 uint8_t *pEncoded;
632 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800633
Laurence Lundblade5e390822019-01-06 12:35:01 -0800634 int64_t i1=0, i2=0;
635 size_t i3=0, i4=0;
636 const uint8_t *s3= (uint8_t *)"";
637 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800638
639
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800640 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
641 return(-1);
642 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800644 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800646 if(i1 != 23 ||
647 i2 != 6000 ||
648 i3 != 8 ||
649 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530650 memcmp("galactic", s3, 8) !=0 ||
651 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800652 return(-1);
653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655 return(0);
656}
657
658
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700659/*
660 [
661 0,
662 [],
663 [
664 [],
665 [
666 0
667 ],
668 {},
669 {
670 1: {},
671 2: {},
672 3: []
673 }
674 ]
675 ]
676 */
677static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
678 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
679
Laurence Lundblade02625d42020-06-25 14:41:41 -0700680/* Same as above, but with indefinte lengths */
681static uint8_t sEmptiesIndef[] = {
6820x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700683 0x00,
684 0x9F,
685 0xFF,
686 0x9F,
687 0x9F,
688 0xFF,
689 0x9F,
690 0x00,
691 0xFF,
692 0xBF,
693 0xFF,
694 0xBF,
695 0x01,
696 0xBF,
697 0xFF,
698 0x02,
699 0xBF,
700 0xFF,
701 0x03,
702 0x9F,
703 0xFF,
704 0xFF,
705 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700706 0xFF};
707
708
709
710static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700711{
712 QCBORDecodeContext DCtx;
713 QCBORItem Item;
714
Laurence Lundbladeee851742020-01-08 08:37:05 -0800715 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700716 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800717 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700718
719 // Array with 3 items
720 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
721 Item.uDataType != QCBOR_TYPE_ARRAY ||
722 Item.uNestingLevel != 0 ||
723 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700724 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700725 return -1;
726 }
727
728 // An integer 0
729 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
730 Item.uDataType != QCBOR_TYPE_INT64 ||
731 Item.uNestingLevel != 1 ||
732 Item.uNextNestLevel != 1 ||
733 Item.val.uint64 != 0) {
734 return -2;
735 }
736
737 // An empty array
738 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
739 Item.uDataType != QCBOR_TYPE_ARRAY ||
740 Item.uNestingLevel != 1 ||
741 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700742 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700743 return -3;
744 }
745
746 // An array with 4 items
747 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
748 Item.uDataType != QCBOR_TYPE_ARRAY ||
749 Item.uNestingLevel != 1 ||
750 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700751 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700752 return -4;
753 }
754
755 // An empty array
756 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
757 Item.uDataType != QCBOR_TYPE_ARRAY ||
758 Item.uNestingLevel != 2 ||
759 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700760 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700761 return -5;
762 }
763
764 // An array with 1 item
765 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
766 Item.uDataType != QCBOR_TYPE_ARRAY ||
767 Item.uNestingLevel != 2 ||
768 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700769 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700770 return -6;
771 }
772
773 // An integer 0
774 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
775 Item.uDataType != QCBOR_TYPE_INT64 ||
776 Item.uNestingLevel != 3 ||
777 Item.uNextNestLevel != 2 ||
778 Item.val.uint64 != 0) {
779 return -7;
780 }
781
782 // An empty map
783 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
784 Item.uDataType != QCBOR_TYPE_MAP ||
785 Item.uNestingLevel != 2 ||
786 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700787 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700788 return -8;
789 }
790
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700791 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700792 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
793 Item.uDataType != QCBOR_TYPE_MAP ||
794 Item.uNestingLevel != 2 ||
795 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700796 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700797 return -9;
798 }
799
800 // An empty map
801 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
802 Item.uDataType != QCBOR_TYPE_MAP ||
803 Item.uNestingLevel != 3 ||
804 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700805 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700806 return -10;
807 }
808
809 // An empty map
810 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
811 Item.uDataType != QCBOR_TYPE_MAP ||
812 Item.uNestingLevel != 3 ||
813 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700814 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700815 return -11;
816 }
817
818 // An empty array
819 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
820 Item.uDataType != QCBOR_TYPE_ARRAY ||
821 Item.uNestingLevel != 3 ||
822 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700823 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700824 return -12;
825 }
826
827 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
828 return -13;
829 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700830 return 0;
831}
832
833
834int32_t EmptyMapsAndArraysTest()
835{
836 int nResult;
837 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
838 true);
839 if(nResult) {
840 return nResult;
841 }
842
843 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
844 false);
845
846 if(nResult) {
847 return nResult -100;
848 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700849
850 return 0;
851}
852
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800853
Laurence Lundbladeee851742020-01-08 08:37:05 -0800854static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
855 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800856
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800857int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800858{
859 QCBORDecodeContext DCtx;
860 int nReturn = 0;
861 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800862
Laurence Lundbladeee851742020-01-08 08:37:05 -0800863 QCBORDecode_Init(&DCtx,
864 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
865 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800866
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800867 for(i = 0; i < 10; i++) {
868 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
871 Item.uDataType != QCBOR_TYPE_ARRAY ||
872 Item.uNestingLevel != i) {
873 nReturn = -1;
874 break;
875 }
876 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800877
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800878 return(nReturn);
879}
880
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700881// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800882static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
883 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
884 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
885 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800886
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800887int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888{
889 QCBORDecodeContext DCtx;
890 int nReturn = 0;
891 int i;
892 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800893
894
Laurence Lundbladeee851742020-01-08 08:37:05 -0800895 QCBORDecode_Init(&DCtx,
896 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
897 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700899 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800900
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
902 Item.uDataType != QCBOR_TYPE_ARRAY ||
903 Item.uNestingLevel != i) {
904 nReturn = -1;
905 break;
906 }
907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800908
Laurence Lundbladea9489f82020-09-12 13:50:56 -0700909 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800911
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800912 return(nReturn);
913}
914
915
916
917
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800918int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700920 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800921
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800922 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700923 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800924
Laurence Lundbladeee851742020-01-08 08:37:05 -0800925 QCBORDecode_Init(&DCtx,
926 (UsefulBufC){spExpectedEncodedInts, nNum},
927 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800928
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800929 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700931 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800932 nResult = -1;
933 goto Done;
934 }
935 }
936Done:
937 return nResult;
938}
939
940
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800941
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800942int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800943{
944 uint8_t *pEncoded;
945 int nReturn;
946 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800947
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800948 int64_t i1, i2;
949 size_t i3, i4;
950 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800951
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800953
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
955 return(-1);
956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800957
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -0700959 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
960 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800961 if(nResult == 0) {
962 nReturn = -1;
963 }
964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800965
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800966 return(nReturn);
967}
968
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530969/*
970 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800971 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
972 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530973 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800974static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975{
976 QCBORDecodeContext DCtx;
977 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700978 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800979
Laurence Lundbladeee851742020-01-08 08:37:05 -0800980 QCBORDecode_Init(&DCtx,
981 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
982 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800983
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900984 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700985 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800987 if(Item.uDataType != QCBOR_TYPE_MAP ||
988 Item.val.uCount != 3)
989 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800990
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700992 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900993 }
Laurence Lundblade9b334962020-08-27 10:55:53 -0700994
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800995 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800996 Item.uDataType != QCBOR_TYPE_INT64 ||
997 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530998 Item.uDataAlloc ||
999 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001000 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001002 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001003
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001005 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001007 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301008 Item.uDataAlloc ||
1009 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001010 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001011 Item.uDataType != QCBOR_TYPE_ARRAY ||
1012 Item.val.uCount != 2)
1013 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001014
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001015 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001016 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001017 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001018 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301019 Item.uDataAlloc ||
1020 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001021 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001022 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001024
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001025 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001026 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301029 Item.uDataAlloc ||
1030 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001031 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001032 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001034
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001036 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001037 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301039 Item.uDataAlloc ||
1040 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001041 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001042 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001043 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001045 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001046
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001047 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001048 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001049 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001050 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001051 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001052 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301053 Item.uDataAlloc ||
1054 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001055 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001056 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001058
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001060 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001061 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001062 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001063 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001064 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301065 Item.uDataAlloc ||
1066 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001067 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001068 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001069 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001070
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001071 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001072 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001073 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301075 Item.uDataAlloc ||
1076 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001077 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001078 Item.uDataType != QCBOR_TYPE_INT64 ||
1079 Item.val.int64 != 98)
1080 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001081
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001082 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001083 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001084 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001085 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001086 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001087 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301088 Item.uDataAlloc ||
1089 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001090 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001093
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001094 return 0;
1095}
1096
1097
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098/*
1099 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001100 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001102int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001103{
1104 QCBORDecodeContext DCtx;
1105 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001106 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001107
Laurence Lundbladeee851742020-01-08 08:37:05 -08001108 QCBORDecode_Init(&DCtx,
1109 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1110 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001111
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001113 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001115 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1116 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 return -1;
1118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001119
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001120 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001121 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 }
1123 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1124 Item.uDataAlloc ||
1125 Item.uLabelAlloc ||
1126 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001127 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 return -2;
1129 }
1130
1131 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001132 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 }
1134 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1135 Item.uDataType != QCBOR_TYPE_INT64 ||
1136 Item.val.int64 != 42 ||
1137 Item.uDataAlloc ||
1138 Item.uLabelAlloc) {
1139 return -3;
1140 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001141
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001143 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 }
1145 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1146 Item.uDataAlloc ||
1147 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001148 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001149 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1150 return -4;
1151 }
1152
1153 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001154 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 }
1156 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1157 Item.uDataAlloc ||
1158 Item.uLabelAlloc ||
1159 Item.uDataType != QCBOR_TYPE_ARRAY ||
1160 Item.val.uCount != 2) {
1161 return -5;
1162 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001163
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001164 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001165 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 }
1167 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1168 Item.val.string.len != 7 ||
1169 Item.uDataAlloc ||
1170 Item.uLabelAlloc ||
1171 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1172 return -6;
1173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001174
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001176 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 }
1178 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1179 Item.uDataAlloc ||
1180 Item.uLabelAlloc ||
1181 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1182 return -7;
1183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001184
1185
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001186 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001187 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 }
1189 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1190 Item.uDataAlloc ||
1191 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001192 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 return -8;
1194 }
1195
1196 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001197 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001198 }
1199 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1200 Item.uDataAlloc ||
1201 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001202 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1203 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001204 return -9;
1205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001206
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001207 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001208 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 }
1210 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001211 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1213 Item.uDataAlloc ||
1214 Item.uLabelAlloc) {
1215 return -10;
1216 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001217
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001219 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 }
1221 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1222 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1223 Item.uDataAlloc ||
1224 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001225 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 return -11;
1227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001228
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001230 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 }
1232 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001233 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001234 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1235 Item.uDataAlloc ||
1236 Item.uLabelAlloc) {
1237 return -12;
1238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001239
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001240 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001241 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001242 }
1243 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1244 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1245 Item.uDataAlloc ||
1246 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001247 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001248 return -13;
1249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001250
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001251 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001252 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001253 }
1254 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1255 Item.uDataAlloc ||
1256 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001257 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1259 return -14;
1260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001261
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001262 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001263 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001264 }
1265 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1266 Item.uDataAlloc ||
1267 Item.uLabelAlloc ||
1268 Item.uDataType != QCBOR_TYPE_INT64 ||
1269 Item.val.int64 != 98) {
1270 return -15;
1271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001272
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001273 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001274 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001275 }
1276 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001277 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001278 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1279 Item.uDataAlloc ||
1280 Item.uLabelAlloc) {
1281 return -16;
1282 }
1283
1284 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001285 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001286 }
1287 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1288 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1289 Item.uDataAlloc ||
1290 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001291 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001292 return -17;
1293 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001294
1295
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001296 /*
1297 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1298 map that when interpreted as an array will be too many. Test
1299 data just has the start of the map, not all the items in the map.
1300 */
1301 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001302
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001303 QCBORDecode_Init(&DCtx,
1304 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1305 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001306
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001307 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001308 return -50;
1309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001310
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001311 // TODO: test decoding of labels that are arrays or such
1312 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1313
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001314 return 0;
1315}
1316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001317
1318/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301319 Fully or partially decode pValidMapEncoded. When
1320 partially decoding check for the right error code.
1321 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301323 The partial decodes test error conditions of
1324 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001325
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301326 This could be combined with the above test
1327 and made prettier and maybe a little more
1328 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001330static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001331{
1332 QCBORDecodeContext DCtx;
1333 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001334 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001335
Laurence Lundbladeee851742020-01-08 08:37:05 -08001336 QCBORDecode_Init(&DCtx,
1337 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1338 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001339
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001340 if(nLevel < 1) {
1341 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1342 return -1;
1343 } else {
1344 return 0;
1345 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001346 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301347
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001348
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001350 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 if(Item.uDataType != QCBOR_TYPE_MAP ||
1353 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001354 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001355
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001356 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001357 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1358 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001359 } else {
1360 return 0;
1361 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001363
1364
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001366 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001367 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 Item.uDataType != QCBOR_TYPE_INT64 ||
1370 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001371 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001372 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001374
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001375 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001376 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1377 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001378 } else {
1379 return 0;
1380 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001382
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001383 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001384 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001385 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001386 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001387 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001389 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001390 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001392
1393
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001394 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001395 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1396 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001397 } else {
1398 return 0;
1399 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001401
1402
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001403 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001404 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001405 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001407 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001408 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001409 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001410
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001411 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001412 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1413 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001414 } else {
1415 return 0;
1416 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001417 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001418
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001420 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001421 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001423 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001424 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001426
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001427 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001428 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1429 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001430 } else {
1431 return 0;
1432 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001433 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001434
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001435 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001436 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001437 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001439 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 Item.uDataType != QCBOR_TYPE_MAP ||
1441 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001442 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001443
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001445 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1446 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001447 } else {
1448 return 0;
1449 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001451
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001452 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001453 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001454 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001455 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001456 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001458 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001459 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001461
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001462 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001463 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1464 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001465 } else {
1466 return 0;
1467 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001469
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001470 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001471 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001472 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001474 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001475 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001476 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001477 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001479
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001480 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001481 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1482 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001483 } else {
1484 return 0;
1485 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001486 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001489 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001490 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001491 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001492 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001493 Item.uDataType != QCBOR_TYPE_INT64 ||
1494 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001495 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001496
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001497 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001498 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1499 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001500 } else {
1501 return 0;
1502 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001504
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001505 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001506 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001507 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001509 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001511 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001512 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001514
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301515 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001516 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001517 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001518
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001519 return 0;
1520}
1521
1522
1523
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001524
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001525int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001527 // Parse a moderatly complex map structure very thoroughly
1528 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1529 if(nResult) {
1530 return nResult;
1531 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001532
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001533 // Again, but in strings-only mode. It should succeed since the input
1534 // map has only string labels.
1535 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1536 if(nResult) {
1537 return nResult;
1538 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001539
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001540 // Again, but try to finish the decoding before the end of the
1541 // input at 10 different place and see that the right error code
1542 // is returned.
1543 for(int i = 0; i < 10; i++) {
1544 nResult = ExtraBytesTest(i);
1545 if(nResult) {
1546 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001547 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001549
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001550 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551}
1552
1553
Laurence Lundbladeee851742020-01-08 08:37:05 -08001554static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1555 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1556 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001558int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001559{
1560 QCBORDecodeContext DCtx;
1561 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001562 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001563
1564
Laurence Lundbladeee851742020-01-08 08:37:05 -08001565 QCBORDecode_Init(&DCtx,
1566 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1567 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001568
1569
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001570 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001571 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001572 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1573 Item.val.uCount != 10)
1574 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001577 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001578 if(Item.uDataType != QCBOR_TYPE_FALSE)
1579 return -1;
1580
1581 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001582 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583 if(Item.uDataType != QCBOR_TYPE_TRUE)
1584 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001586 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001587 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588 if(Item.uDataType != QCBOR_TYPE_NULL)
1589 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001591 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001592 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001593 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1594 return -1;
1595
1596 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001597 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001598 return -1;
1599
1600 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001601 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001602 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1603 return -1;
1604
1605 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001606 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001607 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1608 return -1;
1609
Laurence Lundblade077475f2019-04-26 09:06:33 -07001610 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001611 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001612
Laurence Lundblade077475f2019-04-26 09:06:33 -07001613 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001614 return -1;
1615
Laurence Lundblade077475f2019-04-26 09:06:33 -07001616 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001617 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001618
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001619 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001620 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001621 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1622 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001624 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001625 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001626 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1627 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001628
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001629 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001630
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001631}
1632
1633
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001634int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001635{
1636 // Loop over all the not-well-formed instance of CBOR
1637 // that are test vectors in not_well_formed_cbor.h
1638 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1639 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1640 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1641 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1642
Laurence Lundbladeee851742020-01-08 08:37:05 -08001643 // Set up decoder context. String allocator needed for indefinite
1644 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001645 QCBORDecodeContext DCtx;
1646 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1647 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1648 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1649
1650 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001651 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001652 do {
1653 QCBORItem Item;
1654
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001655 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1656 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001657
1658 // Every test vector must fail with
1659 // a not-well-formed error. If not
1660 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001661 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001662 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001663 // Return index of failure in the error code
1664 return 2000 + nIterate;
1665 }
1666 }
1667 return 0;
1668}
1669
1670
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001671struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001672 UsefulBufC Input;
1673 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001674};
1675
Laurence Lundblade59289e52019-12-30 13:44:37 -08001676
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001677static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001678{
1679 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1680 // Set up the decoding context including a memory pool so that
1681 // indefinite length items can be checked
1682 QCBORDecodeContext DCtx;
1683 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1684 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001685
Laurence Lundblade59289e52019-12-30 13:44:37 -08001686 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1687 if(nCBORError) {
1688 return -9;
1689 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001690
Laurence Lundblade59289e52019-12-30 13:44:37 -08001691 // Iterate until there is an error of some sort error
1692 QCBORItem Item;
1693 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001694 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001695 memset(&Item, 0x33, sizeof(Item));
1696
1697 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1698 } while(nCBORError == QCBOR_SUCCESS);
1699
1700 // Must get the expected error or the this test fails
1701 // The data and label type must also be QCBOR_TYPE_NONE
1702 if(nCBORError != pF->nError ||
1703 Item.uDataType != QCBOR_TYPE_NONE ||
1704 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001705 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001706 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001707 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001708 }
1709 }
1710
1711 return 0;
1712}
1713
1714
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001715struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001716 // Most of this is copied from not_well_formed.h. Here the error code
1717 // returned is also checked.
1718
1719 // Indefinite length strings must be closed off
1720 // An indefinite length byte string not closed off
1721 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1722 // An indefinite length text string not closed off
1723 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1724
1725
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001726 // All the chunks in an indefinite length string must be of the type of
1727 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001728 // indefinite length byte string with text string chunk
1729 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1730 // indefinite length text string with a byte string chunk
1731 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1732 // indefinite length byte string with an positive integer chunk
1733 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1734 // indefinite length byte string with an negative integer chunk
1735 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1736 // indefinite length byte string with an array chunk
1737 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1738 // indefinite length byte string with an map chunk
1739 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1740 // indefinite length byte string with tagged integer chunk
1741 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1742 // indefinite length byte string with an simple type chunk
1743 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1744 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1745 // indefinite length text string with indefinite string inside
1746 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1747
1748
1749 // Definte length maps and arrays must be closed by having the right number of items
1750 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001751 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001753 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001754 // A definte length array that is supposed to have 511 items, but has only 1
1755 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1756 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001757 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001758 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001759 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001760
1761
1762 // Indefinte length maps and arrays must be ended by a break
1763 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001764 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001766 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001767 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001768 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001769 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001770 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001771
1772
1773 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001774 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001775 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001776 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001777 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001778 // Unclosed indefinite map containing a closed definite length array
1779 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1780 // Definite length map containing an unclosed indefinite length array
1781 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001782 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001783 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001784 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001785 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001786 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001787 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001788 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001789 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001790 // Unclosed indefinite length map in definite length maps
1791 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1792 QCBOR_ERR_NO_MORE_ITEMS},
1793 // Unclosed definite length map in indefinite length maps
1794 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1795 // Unclosed indefinite length array in definite length maps
1796 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1797 QCBOR_ERR_NO_MORE_ITEMS},
1798 // Unclosed definite length array in indefinite length maps
1799 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1800 // Unclosed indefinite length map in definite length arrays
1801 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1802 // Unclosed definite length map in indefinite length arrays
1803 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001804
1805
1806 // The "argument" for the data item is incomplete
1807 // Positive integer missing 1 byte argument
1808 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1809 // Positive integer missing 2 byte argument
1810 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1811 // Positive integer missing 4 byte argument
1812 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1813 // Positive integer missing 8 byte argument
1814 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1815 // Positive integer missing 1 byte of 2 byte argument
1816 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1817 // Positive integer missing 2 bytes of 4 byte argument
1818 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1819 // Positive integer missing 1 bytes of 7 byte argument
1820 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1821 // Negative integer missing 1 byte argument
1822 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1823 // Binary string missing 1 byte argument
1824 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1825 // Text string missing 1 byte argument
1826 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1827 // Array missing 1 byte argument
1828 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1829 // Map missing 1 byte argument
1830 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1831 // Tag missing 1 byte argument
1832 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1833 // Simple missing 1 byte argument
1834 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001835 // half-precision with 1 byte argument
1836 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1837 // single-precision with 2 byte argument
1838 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1839 // double-precision with 3 byte argument
1840 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1841
1842
1843 // Tag with no content
1844 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001845
1846
1847 // Breaks must not occur in definite length arrays and maps
1848 // Array of length 1 with sole member replaced by a break
1849 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1850 // Array of length 2 with 2nd member replaced by a break
1851 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1852 // Map of length 1 with sole member label replaced by a break
1853 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1854 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001855 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001856 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1857 // Array of length 1 with 2nd member value replaced by a break
1858 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1859 // Map of length 2 with 2nd member replaced by a break
1860 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1861
1862
1863 // Breaks must not occur on their own out of an indefinite length data item
1864 // A bare break is not well formed
1865 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1866 // A bare break after a zero length definite length array
1867 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1868 // A bare break after a zero length indefinite length map
1869 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001870 // A break inside a definite length array inside an indefenite length array
1871 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1872 // Complicated mixed nesting with break outside indefinite length array
1873 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001874
1875
1876 // Forbidden two byte encodings of simple types
1877 // Must use 0xe0 instead
1878 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1879 // Should use 0xe1 instead
1880 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1881 // Should use 0xe2 instead
1882 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1883 // Should use 0xe3 instead
1884 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1885 // Should use 0xe4 instead
1886 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1887 // Should use 0xe5 instead
1888 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1889 // Should use 0xe6 instead
1890 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1891 // Should use 0xe7 instead
1892 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1893 // Should use 0xe8 instead
1894 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1895 // Should use 0xe9 instead
1896 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1897 // Should use 0xea instead
1898 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1899 // Should use 0xeb instead
1900 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1901 // Should use 0xec instead
1902 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1903 // Should use 0xed instead
1904 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1905 // Should use 0xee instead
1906 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1907 // Should use 0xef instead
1908 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1909 // Should use 0xf0 instead
1910 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1911 // Should use 0xf1 instead
1912 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1913 // Should use 0xf2 instead
1914 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1915 // Must use 0xf3 instead
1916 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1917 // Must use 0xf4 instead
1918 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1919 // Must use 0xf5 instead
1920 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1921 // Must use 0xf6 instead
1922 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1923 // Must use 0xf7 instead
1924 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1925 // Must use 0xf8 instead
1926 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001927 // Reserved
1928 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001929
1930 // Integers with additional info indefinite length
1931 // Positive integer with additional info indefinite length
1932 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1933 // Negative integer with additional info indefinite length
1934 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1935 // CBOR tag with "argument" an indefinite length
1936 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1937 // CBOR tag with "argument" an indefinite length alternate vector
1938 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1939
1940
1941 // Missing bytes from a deterministic length string
1942 // A byte string is of length 1 without the 1 byte
1943 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1944 // A text string is of length 1 without the 1 byte
1945 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001946 // Byte string should have 2^32-15 bytes, but has one
1947 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1948 // Byte string should have 2^32-15 bytes, but has one
1949 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001950 // Byte string should have 2^64 bytes, but has 3
1951 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1952 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
1953 // Text string should have 2^64 bytes, but has 3
1954 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1955 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001956
1957 // Use of unassigned additional information values
1958 // Major type positive integer with reserved value 28
1959 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1960 // Major type positive integer with reserved value 29
1961 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1962 // Major type positive integer with reserved value 30
1963 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1964 // Major type negative integer with reserved value 28
1965 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1966 // Major type negative integer with reserved value 29
1967 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1968 // Major type negative integer with reserved value 30
1969 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1970 // Major type byte string with reserved value 28 length
1971 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1972 // Major type byte string with reserved value 29 length
1973 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1974 // Major type byte string with reserved value 30 length
1975 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1976 // Major type text string with reserved value 28 length
1977 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1978 // Major type text string with reserved value 29 length
1979 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1980 // Major type text string with reserved value 30 length
1981 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1982 // Major type array with reserved value 28 length
1983 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1984 // Major type array with reserved value 29 length
1985 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1986 // Major type array with reserved value 30 length
1987 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1988 // Major type map with reserved value 28 length
1989 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1990 // Major type map with reserved value 29 length
1991 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1992 // Major type map with reserved value 30 length
1993 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1994 // Major type tag with reserved value 28 length
1995 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1996 // Major type tag with reserved value 29 length
1997 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1998 // Major type tag with reserved value 30 length
1999 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2000 // Major type simple with reserved value 28 length
2001 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2002 // Major type simple with reserved value 29 length
2003 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2004 // Major type simple with reserved value 30 length
2005 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2006
2007
2008 // Maps must have an even number of data items (key & value)
2009 // Map with 1 item when it should have 2
2010 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2011 // Map with 3 item when it should have 4
2012 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
2013 // Map with 1 item when it should have 2
2014 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2015 // Map with 3 item when it should have 4
2016 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
2017
2018
2019 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002020 // Text-based date, with an integer
2021 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2022 // Epoch date, with an byte string
2023 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2024 // tagged as both epoch and string dates
2025 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2026 // big num tagged an int, not a byte string
2027 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002028};
2029
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002030int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002031{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002032 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002033
Laurence Lundblade59289e52019-12-30 13:44:37 -08002034 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
2035 if(nResult) {
2036 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002037 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002038
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002039 // Corrupt the UsefulInputBuf and see that
2040 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002041 QCBORDecodeContext DCtx;
2042 QCBORItem Item;
2043 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002044
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002045 QCBORDecode_Init(&DCtx,
2046 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2047 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002048
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002049 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2050 return (int32_t)uQCBORError;
2051 }
2052 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2053 // This wasn't supposed to happen
2054 return -1;
2055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002056
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002057 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002058
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002059 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2060 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2061 // Did not get back the error expected
2062 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002063 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002064
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002065
Laurence Lundblade98427e92020-09-28 21:33:23 -07002066 /*
2067 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2068 tests here can be performed to see that the max length
2069 error check works correctly. See DecodeBytes(). If the max
2070 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002071
Laurence Lundblade98427e92020-09-28 21:33:23 -07002072 This test will automatocally adapt the all CPU sizes
2073 through the use of SIZE_MAX.
2074 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002075
Laurence Lundblade98427e92020-09-28 21:33:23 -07002076 MakeUsefulBufOnStack( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
2077 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002078
Laurence Lundblade98427e92020-09-28 21:33:23 -07002079 // This makes a CBOR head with a text string that is very long
2080 // but doesn't fill in the bytes of the text string as that is
2081 // not needed to test this part of QCBOR.
2082 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2083
2084 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2085
2086 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2087 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002088 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002089
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002090 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091}
2092
2093
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002094/* Try all 256 values of the byte at nLen including recursing for
2095 each of the values to try values at nLen+1 ... up to nLenMax
2096 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002097static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002098{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002099 if(nLen >= nLenMax) {
2100 return;
2101 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002102
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002103 for(int inputByte = 0; inputByte < 256; inputByte++) {
2104 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002105 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002106 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002107
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002108 // Get ready to parse
2109 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002110 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002111
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002112 // Parse by getting the next item until an error occurs
2113 // Just about every possible decoder error can occur here
2114 // The goal of this test is not to check for the correct
2115 // error since that is not really possible. It is to
2116 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002117 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002118 QCBORItem Item;
2119 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002120 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002121 break;
2122 }
2123 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002124
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002125 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002126 }
2127}
2128
2129
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002130int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002132 // Size 2 tests 64K inputs and runs quickly
2133 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002134
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002135 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002136
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002137 return 0;
2138}
2139
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002140
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002141int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002142{
2143 // size 3 tests 16 million inputs and runs OK
2144 // in seconds on fast machines. Size 4 takes
2145 // 10+ minutes and 5 half a day on fast
2146 // machines. This test is kept separate from
2147 // the others so as to no slow down the use
2148 // of them as a very frequent regression.
2149 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002150
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002151 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002152
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002153 return 0;
2154}
2155
2156
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002157static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158 0xc0, // tag for string date
2159 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002160
Laurence Lundbladec7114722020-08-13 05:11:40 -07002161 0xc0, // tag for string date
2162 0x00, // Wrong type for a string date
2163
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164 0xc1, // tag for epoch date
2165 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2166
Laurence Lundbladec7114722020-08-13 05:11:40 -07002167 0xc1,
2168 0x62, 'h', 'i', // wrong type tagged
2169
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002170 // CBOR_TAG_B64
Laurence Lundblade9b334962020-08-27 10:55:53 -07002171 0xcf, 0xd8, 0x22, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002172 0x1a, 0x53, 0x72, 0x4E, 0x01,
2173
2174 0xc1, // tag for epoch date
2175 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002177 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002178 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002179
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002180 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002181 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002182
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002183 0xc1, // tag for epoch date
2184 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2185 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2186
2187 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002188 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2189
2190 0xc1, // tag for epoch date
2191 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2192
2193 0xc1,
2194 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2195
2196 0xc1, // tag for epoch date
2197 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002198};
2199
2200
Laurence Lundbladec7114722020-08-13 05:11:40 -07002201
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002202// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002203#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002204static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002206 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002208 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002209
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002210 return diff > 0.0000001;
2211}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002212#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213
2214
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002215int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002216{
2217 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002218 QCBORItem Item;
2219 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002220
Laurence Lundbladeee851742020-01-08 08:37:05 -08002221 QCBORDecode_Init(&DCtx,
2222 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2223 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002224
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002225 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002226 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002228 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002229 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002230 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002231 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002232 }
2233
Laurence Lundbladec7114722020-08-13 05:11:40 -07002234 // Wrong type for a string date
2235 uError = QCBORDecode_GetNext(&DCtx, &Item);
2236 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002237 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002238 }
2239
2240 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2241 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2242 return -4;
2243 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002244 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2245 Item.val.epochDate.nSeconds != 1400000000 ||
2246 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002247 return -5;
2248 }
2249
2250 // Wrong type for an epoch date
2251 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2252 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002254
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002255 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2256 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002257 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2258 return -7;
2259 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002260 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2261 Item.val.epochDate.nSeconds != 1400000001 ||
2262 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002263 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002264 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002267 // Epoch date that is too large for our representation
2268 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002269 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002271
Laurence Lundblade9682a532020-06-06 18:33:04 -07002272#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002273 // Epoch date in float format with fractional seconds
2274 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2275 return -10;
2276 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002277 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2278 Item.val.epochDate.nSeconds != 1 ||
2279 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002280 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002283 // Epoch date float that is too large for our representation
2284 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002285 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002287
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002288 // Epoch date double that is just slightly too large
2289 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002290 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002291 }
2292
2293 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002294 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2295 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2296 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2297 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002298 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002299 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002300
2301 // Nan
2302 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2303 return -15;
2304 }
2305
2306 // +Inifinity double-precision
2307 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2308 return -16;
2309 }
2310
2311#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2312 // -Inifinity half-precision
2313 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2314 return -17;
2315 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002316#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002317 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2318 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002319 }
2320#endif
2321
Laurence Lundbladec7114722020-08-13 05:11:40 -07002322#else
2323 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2324 return -19;
2325 }
2326 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2327 return -20;
2328 }
2329 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2330 return -21;
2331 }
2332 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2333 return -22;
2334 }
2335 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2336 return -23;
2337 }
2338 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2339 return -24;
2340 }
2341#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2342 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2343 return -25;
2344 }
2345#else
2346 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2347 return -26;
2348 }
2349#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002350
Laurence Lundbladec7114722020-08-13 05:11:40 -07002351#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002352
2353 return 0;
2354}
2355
Laurence Lundblade4b270642020-08-14 12:53:07 -07002356/*
2357 Test cases covered here. Some items cover more than one of these.
2358 positive integer (zero counts as a positive integer)
2359 negative integer
2360 half-precision float
2361 single-precision float
2362 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002363
Laurence Lundblade4b270642020-08-14 12:53:07 -07002364 float Overflow error
2365 Wrong type error for epoch
2366 Wrong type error for date string
2367 float disabled error
2368 half-precision disabled error
2369 -Infinity
2370 Slightly too large integer
2371 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002372
Laurence Lundblade4b270642020-08-14 12:53:07 -07002373 Get epoch by int
2374 Get string by int
2375 Get epoch by string
2376 Get string by string
2377 Fail to get epoch by wrong int label
2378 Fail to get string by wrong string label
2379 Fail to get epoch by string because it is invalid
2380 Fail to get epoch by int because it is invalid
2381
2382 Untagged values
2383 */
2384static uint8_t spSpiffyDateTestInput[] = {
2385 0x86,
2386
2387 0xc1,
2388 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2389
Laurence Lundbladec7114722020-08-13 05:11:40 -07002390 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002391 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2392
2393 0xc1, // tag for epoch date
2394 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2395
2396 0xc1, // tag for epoch date
2397 0x9f, 0xff, // Erroneous empty array as content for date
2398
2399 0xc0, // tag for string date
2400 0xbf, 0xff, // Erroneous empty map as content for date
2401
2402 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002403
2404 0x00,
2405 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002406 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002407
2408 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002409 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002410 0xc1, // tag for epoch date
2411 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2412
2413 // Untagged integer 0
2414 0x08,
2415 0x00,
2416
2417 // Utagged date string with string label y
2418 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002419 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002420
2421 // Untagged -1000 with label z
2422 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002423 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002424 0x39, 0x03, 0xe7,
2425
Laurence Lundbladec7114722020-08-13 05:11:40 -07002426 0x07,
2427 0xc1, // tag for epoch date
2428 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2429
Laurence Lundblade4b270642020-08-14 12:53:07 -07002430 0x05,
2431 0xc1,
2432 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2433
Laurence Lundbladec7114722020-08-13 05:11:40 -07002434 // Untagged single-precision float with value 3.14 with string label x
2435 0x61, 0x78,
2436 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2437
Laurence Lundbladec7114722020-08-13 05:11:40 -07002438 // Untagged half-precision float with value -2
2439 0x09,
2440 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002441
2442 0xff,
2443};
2444
2445int32_t SpiffyDateDecodeTest()
2446{
2447 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002448 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002449 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2450 nEpochDate4, nEpochDate6, nEpochDateFail,
2451 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002452 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002453 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002454
2455 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002456 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002457 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002458 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002459
Laurence Lundblade9b334962020-08-27 10:55:53 -07002460 // Too-negative float, -9.2233720368547748E+18
2461 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002462 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002463#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002464 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2465 return 1111;
2466 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002467#else
2468 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2469 return 1112;
2470 }
2471#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002472
2473 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002474 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002475 uError = QCBORDecode_GetAndResetError(&DC);
2476 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002477 return 1;
2478 }
2479
Laurence Lundblade4b270642020-08-14 12:53:07 -07002480 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002481 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002482 uError = QCBORDecode_GetAndResetError(&DC);
2483#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2484#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2485 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2486#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2487 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2488#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2489#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2490 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2491#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2492 if(uError != uExpectedforHalfMinusInfinity) {
2493 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002494 }
2495
Laurence Lundblade4b270642020-08-14 12:53:07 -07002496 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002497 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002498 uError = QCBORDecode_GetAndResetError(&DC);
2499 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2500 return 3;
2501 }
2502
2503 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002504 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002505 uError = QCBORDecode_GetAndResetError(&DC);
2506 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2507 return 4;
2508 }
2509
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002510 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002511
2512 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002513 QCBORDecode_GetEpochDateInMapN(&DC,
2514 5,
2515 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2516 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2517 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002518#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2519 if(nEpochDate2 != -9223372036854773760LL) {
2520 return 101;
2521 }
2522#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2523 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002524 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002525 return 102;
2526 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002527#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002528
Laurence Lundblade4b270642020-08-14 12:53:07 -07002529 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002530 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2531 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002532#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2533 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002534 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002535 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002536#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2537 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002538 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002539 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002540 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002541#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2542
2543 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002544 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2545 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002546#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2547 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002548 return 103;
2549 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002550#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2551 uError = QCBORDecode_GetAndResetError(&DC);
2552 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2553 return 104;
2554 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002555#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2556
Laurence Lundblade9b334962020-08-27 10:55:53 -07002557 // A half-precision date with value -2 FFF
2558 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2559 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002560#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2561 if(nEpochDate4 != -2) {
2562 return 105;
2563 }
2564#else
2565 uError = QCBORDecode_GetAndResetError(&DC);
2566 if(uError == QCBOR_SUCCESS) {
2567 return 106;
2568 }
2569#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002570
Laurence Lundblade4b270642020-08-14 12:53:07 -07002571
2572 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002573 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2574 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2575 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002576 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002577 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002578 return 107;
2579 }
2580
2581 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002582 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2583 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002584 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002585 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002586 return 108;
2587 }
2588
2589 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002590 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2591 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2592 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002593 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002594 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002595 return 109;
2596 }
2597
2598 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002599 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2600 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002601 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002602 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002603 return 110;
2604 }
2605
2606 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002607
Laurence Lundblade4b270642020-08-14 12:53:07 -07002608 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002609 QCBORDecode_GetEpochDateInMapN(&DC,
2610 1,
2611 QCBOR_TAG_REQUIREMENT_TAG |
2612 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2613 &nEpochDate1400000000);
2614 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002615 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002616 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2617 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002618 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002619 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2620 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002621 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002622 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2623 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002624 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002625 QCBORDecode_GetEpochDateInMapSZ(&DC,
2626 "z",
2627 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2628 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2629 &nEpochDate6);
2630 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002631
2632 QCBORDecode_ExitMap(&DC);
2633 QCBORDecode_ExitArray(&DC);
2634 uError = QCBORDecode_Finish(&DC);
2635 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002636 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002637 }
2638
Laurence Lundblade9b334962020-08-27 10:55:53 -07002639 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002640 return 200;
2641 }
2642
Laurence Lundblade9b334962020-08-27 10:55:53 -07002643 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002644 return 201;
2645 }
2646
Laurence Lundblade9b334962020-08-27 10:55:53 -07002647 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002648 return 202;
2649 }
2650
Laurence Lundblade9b334962020-08-27 10:55:53 -07002651 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002652 return 203;
2653 }
2654
Laurence Lundblade9b334962020-08-27 10:55:53 -07002655 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002656 return 204;
2657 }
2658
Laurence Lundblade9b334962020-08-27 10:55:53 -07002659 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2660 return 205;
2661 }
2662
2663 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2664 return 206;
2665 }
2666
Laurence Lundbladec7114722020-08-13 05:11:40 -07002667 return 0;
2668}
2669
2670
2671
Laurence Lundblade9b334962020-08-27 10:55:53 -07002672// Input for one of the tagging tests
2673static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002674 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002675 0x81, // Array of one
2676 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2677 0x82, // Array of two that is the faction 1/3
2678 0x01,
2679 0x03,
2680
2681 /*
2682 More than 4 tags on an item 225(226(227(228(229([])))))
2683 */
2684 0xd8, 0xe1,
2685 0xd8, 0xe2,
2686 0xd8, 0xe3,
2687 0xd8, 0xe4,
2688 0xd8, 0xe5,
2689 0x80,
2690
2691 /* tag 10489608748473423768(
2692 2442302356(
2693 21590(
2694 240(
2695 []))))
2696 */
2697 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2698 0xda, 0x91, 0x92, 0x93, 0x94,
2699 0xd9, 0x54, 0x56,
2700 0xd8, 0xf0,
2701 0x80,
2702
2703 /* tag 21590(
2704 10489608748473423768(
2705 2442302357(
2706 65534(
2707 []))))
2708 */
2709 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2710 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2711 0xda, 0x91, 0x92, 0x93, 0x95,
2712 0xd9, 0xff, 0xfe,
2713 0x80,
2714
2715 /* Make sure to blow past the limit of tags that must be mapped.
2716 works in conjuntion with entries above.
2717 269488144(269488145(269488146(269488147([]))))
2718 */
2719 0xda, 0x10, 0x10, 0x10, 0x10,
2720 0xda, 0x10, 0x10, 0x10, 0x11,
2721 0xda, 0x10, 0x10, 0x10, 0x12,
2722 0xda, 0x10, 0x10, 0x10, 0x13,
2723 0x80,
2724
2725 /* An invalid decimal fraction with an additional tag */
2726 0xd9, 0xff, 0xfa,
2727 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2728 0x00, // the integer 0; should be a byte string
2729};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002730
Laurence Lundblade59289e52019-12-30 13:44:37 -08002731/*
2732 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002733 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002734 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002735static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2736 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002737
Laurence Lundblade59289e52019-12-30 13:44:37 -08002738/*
2739DB 9192939495969798 # tag(10489608748473423768)
2740 D8 88 # tag(136)
2741 C6 # tag(6)
2742 C7 # tag(7)
2743 80 # array(0)
2744*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002745static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2746 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002747
2748/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002749 55799(55799(55799({
2750 6(7(-23)): 5859837686836516696(7({
2751 7(-20): 11({
2752 17(-18): 17(17(17("Organization"))),
2753 9(-17): 773("SSG"),
2754 -15: 16(17(6(7("Confusion")))),
2755 17(-16): 17("San Diego"),
2756 17(-14): 17("US")
2757 }),
2758 23(-19): 19({
2759 -11: 9({
2760 -9: -7
2761 }),
2762 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2763 })
2764 })),
2765 16(-22): 23({
2766 11(8(7(-5))): 8(-3)
2767 })
2768 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002769 */
2770static uint8_t spCSRWithTags[] = {
2771 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2772 0xc6, 0xc7, 0x36,
2773 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2774 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2775 0xcb, 0xa5,
2776 0xd1, 0x31,
2777 0xd1, 0xd1, 0xd1, 0x6c,
2778 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2779 0xc9, 0x30,
2780 0xd9, 0x03, 0x05, 0x63,
2781 0x53, 0x53, 0x47,
2782 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002783 0xd0, 0xd1, 0xc6, 0xc7,
2784 0x69,
2785 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002786 0xd1, 0x2f,
2787 0xd1, 0x69,
2788 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2789 0xd1, 0x2d,
2790 0xd1, 0x62,
2791 0x55, 0x53,
2792 0xd7, 0x32,
2793 0xd3, 0xa2,
2794 0x2a,
2795 0xc9, 0xa1,
2796 0x28,
2797 0x26,
2798 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2799 0xcc, 0x4a,
2800 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2801 0xd0, 0x35,
2802 0xd7, 0xa1,
2803 0xcb, 0xc8, 0xc7, 0x24,
2804 0xc8, 0x22};
2805
Laurence Lundblade9b334962020-08-27 10:55:53 -07002806
2807static uint8_t spSpiffyTagInput[] = {
2808 0x9f, // Open indefinite array
2809
2810 0xc0, // tag for string date
2811 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2812
2813 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2814
2815 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2816
2817 0xd8, 0x23, // tag for regex
2818 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2819
2820 0xc0, // tag for string date
2821 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2822
2823 0xff
2824};
2825
2826
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002827static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002828
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002829
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002830int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002831{
2832 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002833 QCBORItem Item;
2834 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002835
Laurence Lundbladeee851742020-01-08 08:37:05 -08002836 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002837 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002838 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002839
Laurence Lundblade9b334962020-08-27 10:55:53 -07002840 /*
2841 This test matches the magic number tag and the fraction tag
2842 55799([...])
2843 */
2844 uError = QCBORDecode_GetNext(&DCtx, &Item);
2845 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002846 return -2;
2847 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002848 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002849 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2850 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002851 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002852
Laurence Lundblade9b334962020-08-27 10:55:53 -07002853 /*
2854 4([1,3])
2855 */
2856 uError = QCBORDecode_GetNext(&DCtx, &Item);
2857#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2858 if(uError != QCBOR_SUCCESS ||
2859 Item.uDataType != QCBOR_TYPE_ARRAY ||
2860 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2861 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2862 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2863 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2864 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2865 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2866 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002867 return -4;
2868 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002869 // consume the items in the array
2870 uError = QCBORDecode_GetNext(&DCtx, &Item);
2871 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002872
Laurence Lundblade59289e52019-12-30 13:44:37 -08002873#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002874 if(uError != QCBOR_SUCCESS ||
2875 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2876 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2877 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2878 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2879 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2880 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2881 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002882 }
2883#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002884
Laurence Lundblade9b334962020-08-27 10:55:53 -07002885 /*
2886 More than 4 tags on an item 225(226(227(228(229([])))))
2887 */
2888 uError = QCBORDecode_GetNext(&DCtx, &Item);
2889 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002890 return -6;
2891 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002892
2893 /* tag 10489608748473423768(
2894 2442302356(
2895 21590(
2896 240(
2897 []))))
2898 */
2899 uError = QCBORDecode_GetNext(&DCtx, &Item);
2900 if(uError != QCBOR_SUCCESS ||
2901 Item.uDataType != QCBOR_TYPE_ARRAY ||
2902 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2903 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2904 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2905 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002906 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002907 }
2908
2909 /* tag 21590(
2910 10489608748473423768(
2911 2442302357(
2912 21591(
2913 []))))
2914 */
2915 uError = QCBORDecode_GetNext(&DCtx, &Item);
2916 if(uError != QCBOR_SUCCESS ||
2917 Item.uDataType != QCBOR_TYPE_ARRAY ||
2918 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2919 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2920 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2921 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2922 return -8;
2923 }
2924
2925 /* Make sure to blow past the limit of tags that must be mapped.
2926 works in conjuntion with entries above.
2927 269488144(269488145(269488146(269488147([]))))
2928 */
2929 uError = QCBORDecode_GetNext(&DCtx, &Item);
2930 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2931 return -9;
2932 }
2933
2934 uError = QCBORDecode_GetNext(&DCtx, &Item);
2935 if(uError == QCBOR_SUCCESS) {
2936 return -10;
2937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002938
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002939 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002940 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002941 // tage and then matches it. Caller-config lists are no longer
2942 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002943 QCBORDecode_Init(&DCtx,
2944 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2945 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002946 const uint64_t puList[] = {0x9192939495969798, 257};
2947 const QCBORTagListIn TL = {2, puList};
2948 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002949
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002950 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2951 return -8;
2952 }
2953 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2954 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2955 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2956 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2957 Item.val.uCount != 0) {
2958 return -9;
2959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002960
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002961 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002962 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002963 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002964 const uint64_t puLongList[17] = {1,2,1};
2965 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002966 QCBORDecode_Init(&DCtx,
2967 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2968 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002969 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2970 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2971 return -11;
2972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002973
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07002974 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07002975 QCBORTagListOut Out = {0, 4, puTags};
2976
2977
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002978 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002979 QCBORDecode_Init(&DCtx,
2980 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2981 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002982 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2983 return -12;
2984 }
2985 if(puTags[0] != 0x9192939495969798 ||
2986 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002987 puTags[2] != 0x06 ||
2988 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002989 return -13;
2990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002991
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002992 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002993 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002994 QCBORDecode_Init(&DCtx,
2995 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2996 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002997 QCBORTagListOut OutSmall = {0, 3, puTags};
2998 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2999 return -14;
3000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003001
Laurence Lundblade9b334962020-08-27 10:55:53 -07003002
3003
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003004 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003005 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3006 // It is a bit of a messy test and maybe could be improved, but
3007 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003008 QCBORDecode_Init(&DCtx,
3009 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3010 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003011 int n = CheckCSRMaps(&DCtx);
3012 if(n) {
3013 return n-2000;
3014 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003015
Laurence Lundblade59289e52019-12-30 13:44:37 -08003016 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003017 QCBORDecode_Init(&DCtx,
3018 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3019 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003020
Laurence Lundblade9b334962020-08-27 10:55:53 -07003021 /* With the spiffy decode revision, this tag list is not used.
3022 It doesn't matter if a tag is in this list or not so some
3023 tests that couldn't process a tag because it isn't in this list
3024 now can process these unlisted tags. The tests have been
3025 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003026 const uint64_t puTagList[] = {773, 1, 90599561};
3027 const QCBORTagListIn TagList = {3, puTagList};
3028 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003029
3030
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003031 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3032 return -100;
3033 }
3034 if(Item.uDataType != QCBOR_TYPE_MAP ||
3035 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3036 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3037 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3038 Item.val.uCount != 2 ||
3039 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3040 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3041 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3042 Out.uNumUsed != 3) {
3043 return -101;
3044 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003045
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003046 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3047 return -102;
3048 }
3049 if(Item.uDataType != QCBOR_TYPE_MAP ||
3050 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3051 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003052 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003053 Item.val.uCount != 2 ||
3054 puTags[0] != 5859837686836516696 ||
3055 puTags[1] != 7 ||
3056 Out.uNumUsed != 2) {
3057 return -103;
3058 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003059
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003060 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3061 return -104;
3062 }
3063 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003064 Item.val.uCount != 5 ||
3065 puTags[0] != 0x0b ||
3066 Out.uNumUsed != 1) {
3067 return -105;
3068 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003069
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003070 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3071 return -106;
3072 }
3073 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3074 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3075 Item.val.string.len != 12 ||
3076 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3077 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3078 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3079 Out.uNumUsed != 3) {
3080 return -105;
3081 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003082
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003083 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3084 return -107;
3085 }
3086 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3087 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3088 Item.val.string.len != 3 ||
3089 puTags[0] != 773 ||
3090 Out.uNumUsed != 1) {
3091 return -108;
3092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003093
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003094 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3095 return -109;
3096 }
3097 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003098 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003099 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003100 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003101 puTags[3] != 7 ||
3102 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003103 return -110;
3104 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003105
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003106 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3107 return -111;
3108 }
3109 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3110 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3111 Item.val.string.len != 9 ||
3112 puTags[0] != 17 ||
3113 Out.uNumUsed != 1) {
3114 return -112;
3115 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003116
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003117 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3118 return -111;
3119 }
3120 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3121 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3122 Item.val.string.len != 2 ||
3123 puTags[0] != 17 ||
3124 Out.uNumUsed != 1) {
3125 return -112;
3126 }
3127
3128 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3129 return -113;
3130 }
3131 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003132 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003133 Item.val.uCount != 2 ||
3134 puTags[0] != 19 ||
3135 Out.uNumUsed != 1) {
3136 return -114;
3137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003138
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003139 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3140 return -115;
3141 }
3142 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003143 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003144 Item.val.uCount != 1 ||
3145 puTags[0] != 9 ||
3146 Out.uNumUsed != 1) {
3147 return -116;
3148 }
3149
3150 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3151 return -116;
3152 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003153 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003154 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003155 Out.uNumUsed != 0) {
3156 return -117;
3157 }
3158
3159 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3160 return -118;
3161 }
3162 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3163 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003164 puTags[0] != 12 ||
3165 Out.uNumUsed != 1) {
3166 return -119;
3167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003168
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003169 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3170 return -120;
3171 }
3172 if(Item.uDataType != QCBOR_TYPE_MAP ||
3173 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3174 Item.val.uCount != 1 ||
3175 puTags[0] != 0x17 ||
3176 Out.uNumUsed != 1) {
3177 return -121;
3178 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003179
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003180 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3181 return -122;
3182 }
3183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003184 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003185 Item.val.int64 != -3 ||
3186 puTags[0] != 8 ||
3187 Out.uNumUsed != 1) {
3188 return -123;
3189 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003190
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003191 if(QCBORDecode_Finish(&DCtx)) {
3192 return -124;
3193 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003194
3195 UsefulBufC DateString;
3196 QCBORDecode_Init(&DCtx,
3197 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3198 QCBOR_DECODE_MODE_NORMAL);
3199
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003200 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003201 // tagged date string
3202 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3203 // untagged date string
3204 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3205 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3206 return 100;
3207 }
3208 // untagged byte string
3209 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3210 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3211 return 101;
3212 }
3213 // tagged regex
3214 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3215 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3216 return 102;
3217 }
3218 // tagged date string with a byte string
3219 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3220 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3221 return 103;
3222 }
3223 QCBORDecode_ExitArray(&DCtx);
3224 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3225 return 104;
3226 }
3227
3228
3229 QCBORDecode_Init(&DCtx,
3230 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3231 QCBOR_DECODE_MODE_NORMAL);
3232
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003233 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003234 // tagged date string
3235 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3236 // untagged date string
3237 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3238 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3239 return 200;
3240 }
3241 // untagged byte string
3242 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3243 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3244 return 201;
3245 }
3246 // tagged regex
3247 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3248 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3249 return 202;
3250 }
3251 // tagged date string with a byte string
3252 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3253 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3254 return 203;
3255 }
3256 QCBORDecode_ExitArray(&DCtx);
3257 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3258 return 204;
3259 }
3260
3261 QCBORDecode_Init(&DCtx,
3262 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3263 QCBOR_DECODE_MODE_NORMAL);
3264
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003265 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003266 // tagged date string
3267 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3268 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3269 return 300;
3270 }
3271 // untagged date string
3272 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3273 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3274 return 301;
3275 }
3276 // untagged byte string
3277 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3278 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3279 return 302;
3280 }
3281 // tagged regex
3282 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3283 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3284 return 303;
3285 }
3286 // tagged date string with a byte string
3287 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3288 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3289 return 304;
3290 }
3291 QCBORDecode_ExitArray(&DCtx);
3292 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3293 return 305;
3294 }
3295
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003296 return 0;
3297}
3298
3299
3300
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003301
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003302static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003303 0x83,
3304 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3305 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3306 0xA4,
3307 0x63, 0x42, 0x4E, 0x2B,
3308 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3309 0x18, 0x40,
3310 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3311 0x63, 0x42, 0x4E, 0x2D,
3312 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3313 0x38, 0x3F,
3314 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3315
3316
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003317static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003318
3319
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003320int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003321{
3322 QCBORDecodeContext DCtx;
3323 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003324 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003325
Laurence Lundbladeee851742020-01-08 08:37:05 -08003326 QCBORDecode_Init(&DCtx,
3327 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3328 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003329
3330
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003331 //
3332 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3333 return -1;
3334 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003335 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003337
3338 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003339 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003340 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003341 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003342 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003343 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003344 }
3345
3346 //
3347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003348 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003349 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003350 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003351 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003352 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003353
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003354 //
3355 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003356 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003357 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003358 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003359 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003361 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003362 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003363 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3364 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003365 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003366 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003367 }
3368
3369 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003370 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003371 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3372 Item.uLabelType != QCBOR_TYPE_INT64 ||
3373 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003374 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003375 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003378 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003379 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003380 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3381 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003382 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003383 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003384 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003385
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003386 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003387 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003388 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3389 Item.uLabelType != QCBOR_TYPE_INT64 ||
3390 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003391 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003392 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003393 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003394
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003395 return 0;
3396}
3397
3398
3399
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003400static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003401 uint8_t uDataType,
3402 uint8_t uNestingLevel,
3403 uint8_t uNextNest,
3404 int64_t nLabel,
3405 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003406{
3407 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003408 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003410 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3411 if(Item.uDataType != uDataType) return -1;
3412 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003413 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3414 Item.uLabelType != QCBOR_TYPE_UINT64) {
3415 return -1;
3416 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003417 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3418 if(Item.label.int64 != nLabel) return -1;
3419 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003420 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003421 }
3422 }
3423 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303424 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003425
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003426 if(pItem) {
3427 *pItem = Item;
3428 }
3429 return 0;
3430}
3431
3432
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003433// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003434static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003435{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303436 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003437
Laurence Lundblade9b334962020-08-27 10:55:53 -07003438 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003439
Laurence Lundblade9b334962020-08-27 10:55:53 -07003440 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003441
Laurence Lundblade9b334962020-08-27 10:55:53 -07003442 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3443 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3444 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3445 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3446 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003447
Laurence Lundblade9b334962020-08-27 10:55:53 -07003448 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3449 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003450
Laurence Lundblade9b334962020-08-27 10:55:53 -07003451 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3452 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003453
Laurence Lundblade9b334962020-08-27 10:55:53 -07003454 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3455 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003456
Laurence Lundblade9b334962020-08-27 10:55:53 -07003457 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003458
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003459 return 0;
3460}
3461
3462
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003463/*
3464// cbor.me decoded output
3465{
3466 -23: {
3467 -20: {
3468 -18: "Organization",
3469 -17: "SSG",
3470 -15: "Confusion",
3471 -16: "San Diego",
3472 -14: "US"
3473 },
3474 -19: {
3475 -11: {
3476 -9: -7
3477 },
3478 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3479 }
3480 },
3481 -22: {
3482 -5: -3
3483 }
3484}
3485 */
3486
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003487
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003488static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003489 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3490 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3491 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3492 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3493 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3494 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3495 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3496 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3497 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3498
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003499int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003500{
3501 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003502
Laurence Lundbladeee851742020-01-08 08:37:05 -08003503 QCBORDecode_Init(&DCtx,
3504 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3505 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003506
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003507 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003508}
3509
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003510
3511
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003512int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003513{
3514 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003515
Laurence Lundbladeee851742020-01-08 08:37:05 -08003516 QCBORDecode_Init(&DCtx,
3517 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3518 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003519
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003520 QCBORItem Item;
3521 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003522
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003523 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3524 return -1;
3525 }
3526 if(Item.uDataType != QCBOR_TYPE_MAP) {
3527 return -2;
3528 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003529
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003530 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3531 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3532 return -3;
3533 }
3534
3535 return 0;
3536}
3537
3538
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003539// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003540static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003541 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3542 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3543 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3544 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3545 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3546 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003547 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3548 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3549 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3550 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003551
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003552int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003553{
3554 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003555
Laurence Lundbladeee851742020-01-08 08:37:05 -08003556 QCBORDecode_Init(&DCtx,
3557 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3558 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003559
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003560 return CheckCSRMaps(&DCtx);
3561}
3562
3563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003564
Laurence Lundblade17ede402018-10-13 11:43:07 +08003565static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3566{
3567 UsefulOutBuf UOB;
3568 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003569
Laurence Lundblade17ede402018-10-13 11:43:07 +08003570 int i;
3571 for(i = 0; i < n; i++) {
3572 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3573 }
3574
3575 for(i = 0; i < n; i++) {
3576 UsefulOutBuf_AppendByte(&UOB, 0xff);
3577 }
3578 return UsefulOutBuf_OutUBuf(&UOB);
3579}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003580
3581
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003582static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003583{
3584 QCBORDecodeContext DC;
3585 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003586
Laurence Lundblade17ede402018-10-13 11:43:07 +08003587 int j;
3588 for(j = 0; j < nNestLevel; j++) {
3589 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003590 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003591 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3592 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003593 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003594 return -4;
3595 } else {
3596 return 0; // Decoding doesn't recover after an error
3597 }
3598 } else {
3599 // Should be no error
3600 if(nReturn) {
3601 return -9; // Should not have got an error
3602 }
3603 }
3604 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3605 return -7;
3606 }
3607 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003608 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003609 if(nReturn) {
3610 return -3;
3611 }
3612 return 0;
3613}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003614
3615
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003616int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003617{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303618 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003619 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003620 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003621 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003622 int nReturn = parse_indeflen_nested(Nested, i);
3623 if(nReturn) {
3624 return nReturn;
3625 }
3626 }
3627 return 0;
3628}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003629
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003630
Laurence Lundbladeee851742020-01-08 08:37:05 -08003631// [1, [2, 3]]
3632static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3633// No closing break
3634static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3635// Not enough closing breaks
3636static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3637// Too many closing breaks
3638static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3639// Unclosed indeflen inside def len
3640static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3641// confused tag
3642static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003643
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003644int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003645{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003646 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003647 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003648 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003649
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003650 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303651 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003652 QCBORDecodeContext DC;
3653 QCBORItem Item;
3654 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003655
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003656 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003657
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003658 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303659
3660 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3661 Item.uNestingLevel != 0 ||
3662 Item.uNextNestLevel != 1) {
3663 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003664 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003665
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003666 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303667 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3668 Item.uNestingLevel != 1 ||
3669 Item.uNextNestLevel != 1) {
3670 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003671 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003672
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003673 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303674 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3675 Item.uNestingLevel != 1 ||
3676 Item.uNextNestLevel != 2) {
3677 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003678 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003679
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003680 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003681 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303682 Item.uNestingLevel != 2 ||
3683 Item.uNextNestLevel != 2) {
3684 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003685 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003686
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003687 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003688 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303689 Item.uNestingLevel != 2 ||
3690 Item.uNextNestLevel != 0) {
3691 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003692 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003693
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003694 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303695 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003696 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003697
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003698 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003699 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003700
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003701 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003702
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003703 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003704
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003705 nResult = QCBORDecode_GetNext(&DC, &Item);
3706 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303707 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003708 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003709
Laurence Lundblade570fab52018-10-13 18:28:27 +08003710 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003711 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303712 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003713 }
3714
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003715
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003716 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003717 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003718
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003719 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003720
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003721 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003722
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003723 nResult = QCBORDecode_GetNext(&DC, &Item);
3724 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303725 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003726 }
3727
3728 nResult = QCBORDecode_GetNext(&DC, &Item);
3729 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303730 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003731 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003732
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003733 nResult = QCBORDecode_GetNext(&DC, &Item);
3734 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303735 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003736 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003737
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003738 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003739 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303740 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003741 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003742
3743
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003744 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003745 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003746
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003747 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003748
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003749 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003750
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003751 nResult = QCBORDecode_GetNext(&DC, &Item);
3752 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303753 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003754 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003755
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003756 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003757 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303758 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003759 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303760
Laurence Lundblade642282a2020-06-23 12:00:33 -07003761 nResult = QCBORDecode_GetNext(&DC, &Item);
3762 if(nResult != QCBOR_ERR_BAD_BREAK) {
3763 return -140;
3764 }
3765
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003766
Laurence Lundblade570fab52018-10-13 18:28:27 +08003767 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003768 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003769
Laurence Lundblade570fab52018-10-13 18:28:27 +08003770 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003771
Laurence Lundblade570fab52018-10-13 18:28:27 +08003772 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003773
Laurence Lundblade570fab52018-10-13 18:28:27 +08003774 nResult = QCBORDecode_GetNext(&DC, &Item);
3775 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303776 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003777 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003778
Laurence Lundblade570fab52018-10-13 18:28:27 +08003779 nResult = QCBORDecode_GetNext(&DC, &Item);
3780 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303781 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003782 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003783
Laurence Lundblade570fab52018-10-13 18:28:27 +08003784 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003785 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303786 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003787 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003788
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303789 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003790 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003791
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303792 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003793
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303794 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003795
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303796 nResult = QCBORDecode_GetNext(&DC, &Item);
3797 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303798 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303799 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003800
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303801 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303802 if(nResult != QCBOR_ERR_BAD_BREAK) {
3803 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303804 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003805
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003806 return 0;
3807}
3808
Laurence Lundblade17ede402018-10-13 11:43:07 +08003809
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003810static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003811 0x81, // Array of length one
3812 0x7f, // text string marked with indefinite length
3813 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3814 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3815 0xff // ending break
3816};
3817
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003818static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303819 0x81, // Array of length one
3820 0x7f, // text string marked with indefinite length
3821 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3822 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3823 0xff // ending break
3824};
3825
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003826static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303827 0x81, // Array of length one
3828 0x7f, // text string marked with indefinite length
3829 0x01, 0x02, // Not a string
3830 0xff // ending break
3831};
3832
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003833static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303834 0x81, // Array of length one
3835 0x7f, // text string marked with indefinite length
3836 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3837 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3838 // missing end of string
3839};
3840
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003841static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303842 0xa1, // Array of length one
3843 0x7f, // text string marked with indefinite length
3844 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3845 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3846 0xff, // ending break
3847 0x01 // integer being labeled.
3848};
3849
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003850/**
3851 Make an indefinite length string
3852
3853 @param Storage Storage for string, must be 144 bytes in size
3854 @return The indefinite length string
3855
3856 This makes an array with one indefinite length string that has 7 chunks
3857 from size of 1 byte up to 64 bytes.
3858 */
3859static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303860{
3861 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003862
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303863 UsefulOutBuf_Init(&UOB, Storage);
3864 UsefulOutBuf_AppendByte(&UOB, 0x81);
3865 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003866
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003867 uint8_t uStringByte = 0;
3868 // Use of type int is intentional
3869 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3870 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303871 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003872 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3873 for(int j = 0; j < uChunkSize; j++) {
3874 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3875 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303876 }
3877 }
3878 UsefulOutBuf_AppendByte(&UOB, 0xff);
3879
3880 return UsefulOutBuf_OutUBuf(&UOB);
3881}
3882
3883static int CheckBigString(UsefulBufC BigString)
3884{
3885 if(BigString.len != 255) {
3886 return 1;
3887 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003888
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303889 for(uint8_t i = 0; i < 255; i++){
3890 if(((const uint8_t *)BigString.ptr)[i] != i) {
3891 return 1;
3892 }
3893 }
3894 return 0;
3895}
3896
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303897
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003898int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303899{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303900 QCBORDecodeContext DC;
3901 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303902 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003903 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003904
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303905 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003906 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303907 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003908
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303909 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303910 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303911 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003912
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303913 if(QCBORDecode_GetNext(&DC, &Item)) {
3914 return -2;
3915 }
3916 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3917 return -3;
3918 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003919
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303920 if(QCBORDecode_GetNext(&DC, &Item)) {
3921 return -4;
3922 }
3923 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3924 return -5;
3925 }
3926 if(QCBORDecode_Finish(&DC)) {
3927 return -6;
3928 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303929
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303930 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003931 QCBORDecode_Init(&DC,
3932 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3933 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003934
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303935 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3936 return -7;
3937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003938
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303939 if(QCBORDecode_GetNext(&DC, &Item)) {
3940 return -8;
3941 }
3942 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3943 return -9;
3944 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003945
Laurence Lundblade30816f22018-11-10 13:40:22 +07003946 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303947 return -10;
3948 }
3949
3950 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003951 QCBORDecode_Init(&DC,
3952 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3953 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003954
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303955 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3956 return -11;
3957 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303959 if(QCBORDecode_GetNext(&DC, &Item)) {
3960 return -12;
3961 }
3962 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3963 return -13;
3964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003965
Laurence Lundblade30816f22018-11-10 13:40:22 +07003966 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303967 return -14;
3968 }
3969
3970 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003971 QCBORDecode_Init(&DC,
3972 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3973 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003974
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303975 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3976 return -15;
3977 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003978
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303979 if(QCBORDecode_GetNext(&DC, &Item)) {
3980 return -16;
3981 }
3982 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3983 return -17;
3984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003985
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303986 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3987 return -18;
3988 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003989
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303990 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303991 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003992
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303993 QCBORDecode_GetNext(&DC, &Item);
3994 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303995 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303996 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003997
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303998 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303999 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004001
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304002 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004003 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304004
4005 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4006 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304007 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004009
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304010 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304011 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004012 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004013
Laurence Lundbladeee851742020-01-08 08:37:05 -08004014 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4015 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004016
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304017 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304018 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304019 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304020 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004021
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304022 QCBORDecode_GetNext(&DC, &Item);
4023 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304024 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304025 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004026 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304027 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304028 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004029
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304030 // ---- big bstr -----
4031 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004032
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304033 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4034 return -25;
4035 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004036
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304037 if(QCBORDecode_GetNext(&DC, &Item)) {
4038 return -26;
4039 }
4040 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304041 return -26;
4042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004043
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304044 if(QCBORDecode_GetNext(&DC, &Item)) {
4045 return -27;
4046 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304047 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304048 return -28;
4049 }
4050 if(CheckBigString(Item.val.string)) {
4051 return -3;
4052 }
4053 if(QCBORDecode_Finish(&DC)) {
4054 return -29;
4055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004056
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304057 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004058 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004059
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304060 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4061 return -30;
4062 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004063
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304064 QCBORDecode_GetNext(&DC, &Item);
4065 if(Item.uDataType != QCBOR_TYPE_MAP) {
4066 return -31;
4067 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004068
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304069 if(QCBORDecode_GetNext(&DC, &Item)){
4070 return -32;
4071 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004072 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4073 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304074 Item.uDataAlloc || !Item.uLabelAlloc ||
4075 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4076 return -33;
4077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004078
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304079 if(QCBORDecode_Finish(&DC)) {
4080 return -34;
4081 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004082
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004083 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004084}
4085
4086
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004087int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304088{
4089 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004090 QCBORError nCBORError;
4091
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004092
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304093 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004094 QCBORDecode_Init(&DC,
4095 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4096 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004097
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004098 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004099
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004100 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4101 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304102 return -1;
4103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004104
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004105 if(CheckCSRMaps(&DC)) {
4106 return -2;
4107 }
4108
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004109 // Next parse, save pointers to a few strings, destroy original and
4110 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004111 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004112 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004113
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304114 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004115 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304116 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004117
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304118 QCBORItem Item1, Item2, Item3, Item4;
4119 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004120 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304121 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4122 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004123 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304124 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
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, &Item2)))
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, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004129 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304130 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004131 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304133 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004134
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304135 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304136 Item1.uDataType != QCBOR_TYPE_INT64 ||
4137 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004138 Item1.uDataAlloc != 0 ||
4139 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004140 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004141 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004143
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304144
4145 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004146 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304147 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004148 Item2.uDataAlloc != 0 ||
4149 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304150 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004151 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004152
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304153 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004154 Item3.uDataAlloc == 0 ||
4155 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004156 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004157 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004158 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004159
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304160 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004161 Item4.uDataAlloc == 0 ||
4162 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004163 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004164 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004165 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004166
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304167 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004168 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004169 QCBORDecode_Init(&DC,
4170 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4171 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304172 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4173 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004174 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304175 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004176 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004177 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004178 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304179 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4180 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4181 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4182 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4183 }
4184 }
4185 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004186 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004187 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304188 }
4189
4190 return 0;
4191}
4192
Laurence Lundbladef6531662018-12-04 10:42:22 +09004193
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304194
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004195int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004196{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004197 // Set up the decoder with a tiny bit of CBOR to parse because
4198 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004199 QCBORDecodeContext DC;
4200 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4201 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004202
Laurence Lundbladef6531662018-12-04 10:42:22 +09004203 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004204 // Then fish into the internals of the decode context
4205 // to get the allocator function so it can be called directly.
4206 // Also figure out how much pool is available for use
4207 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004208 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004209 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4210 if(nError) {
4211 return -9;
4212 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004213 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4214 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4215 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004216
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004217 // First test -- ask for one more byte than available and see failure
4218 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004219 if(!UsefulBuf_IsNULL(Allocated)) {
4220 return -1;
4221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004222
Laurence Lundbladef6531662018-12-04 10:42:22 +09004223 // Re do the set up for the next test that will do a successful alloc,
4224 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004225 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004226 pAlloc = DC.StringAllocator.pfAllocator;
4227 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4228 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004229
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004230 // Allocate one byte less than available and see success
4231 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004232 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4233 return -2;
4234 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004235 // Ask for some more and see failure
4236 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004237 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4238 return -3;
4239 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004240 // Free the first allocate, retry the second and see success
4241 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4242 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004243 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4244 return -4;
4245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004246
Laurence Lundbladef6531662018-12-04 10:42:22 +09004247 // Re do set up for next test that involves a successful alloc,
4248 // and a successful realloc and a failed realloc
4249 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004250 pAlloc = DC.StringAllocator.pfAllocator;
4251 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004252
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004253 // Allocate half the pool and see success
4254 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004255 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4256 return -5;
4257 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004258 // Reallocate to take up the whole pool and see success
4259 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004260 if(UsefulBuf_IsNULL(Allocated2)) {
4261 return -6;
4262 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004263 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004264 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4265 return -7;
4266 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004267 // Try to allocate more to be sure there is failure after a realloc
4268 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4269 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004270 return -8;
4271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004272
Laurence Lundbladef6531662018-12-04 10:42:22 +09004273 return 0;
4274}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004275
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004276
4277/* Just enough of an allocator to test configuration of one */
4278static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4279{
4280 (void)pOldMem; // unused variable
4281
4282 if(uNewSize) {
4283 // Assumes the context pointer is the buffer and
4284 // nothing too big will ever be asked for.
4285 // This is only good for this basic test!
4286 return (UsefulBuf) {pCtx, uNewSize};
4287 } else {
4288 return NULLUsefulBuf;
4289 }
4290}
4291
4292
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004293int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004294{
4295 // Set up the decoder with a tiny bit of CBOR to parse because
4296 // nothing can be done with it unless that is set up.
4297 QCBORDecodeContext DC;
4298 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4299 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4300
4301 uint8_t pAllocatorBuffer[50];
4302
4303 // This is really just to test that this call works.
4304 // The full functionality of string allocators is tested
4305 // elsewhere with the MemPool internal allocator.
4306 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4307
4308 QCBORItem Item;
4309 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4310 return -1;
4311 }
4312
4313 if(Item.uDataAlloc == 0 ||
4314 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4315 Item.val.string.ptr != pAllocatorBuffer) {
4316 return -2;
4317 }
4318
4319 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4320 return -3;
4321 }
4322
4323 return 0;
4324}
4325
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004326#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004327
Laurence Lundbladea826c502020-05-10 21:07:00 -07004328/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004329 [
4330 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004331 4([-20, 4759477275222530853136]),
4332 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004333 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004334 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004335 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004336 5([ 9223372036854775806, -4759477275222530853137])
4337 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004338 ]
4339 */
4340
4341static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004342 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004343 0xC4, 0x82, 0x20,
4344 0x03,
4345 0xC4, 0x82, 0x33,
4346 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4347 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4348 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4349 0xC5, 0x82, 0x19, 0x01, 0x2C,
4350 0x18, 0x64,
4351 0xC5, 0x82, 0x33,
4352 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4353 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4354 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004355 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4356 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004357 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4358 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4359};
4360
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004361
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004362int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004363{
4364 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004365 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004366 QCBORItem item;
4367
Laurence Lundblade17af4902020-01-07 19:11:55 -08004368 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4369 0x06, 0x07, 0x08, 0x09, 0x010};
4370 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004371
4372
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004373 QCBORDecode_Init(&DC,
4374 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4375 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004376
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004377 uErr = QCBORDecode_GetNext(&DC, &item);
4378 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004379 return 1;
4380 }
4381
4382 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4383 return 2;
4384 }
4385
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004386 uErr = QCBORDecode_GetNext(&DC, &item);
4387 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004388 return 3;
4389 }
4390
4391 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4392 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4393 item.val.expAndMantissa.nExponent != -1) {
4394 return 4;
4395 }
4396
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004397 uErr = QCBORDecode_GetNext(&DC, &item);
4398 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004399 return 5;
4400 }
4401
4402 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4403 item.val.expAndMantissa.nExponent != -20 ||
4404 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4405 return 6;
4406 }
4407
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004408 uErr = QCBORDecode_GetNext(&DC, &item);
4409 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004410 return 7;
4411 }
4412
4413 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4414 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4415 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4416 return 8;
4417 }
4418
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004419 uErr = QCBORDecode_GetNext(&DC, &item);
4420 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004421 return 9;
4422 }
4423
4424 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4425 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4426 item.val.expAndMantissa.nExponent != 300) {
4427 return 10;
4428 }
4429
Laurence Lundbladea826c502020-05-10 21:07:00 -07004430 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004431 uErr = QCBORDecode_GetNext(&DC, &item);
4432 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004433 return 11;
4434 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004435 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4436 item.val.expAndMantissa.nExponent != -20 ||
4437 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4438 return 12;
4439 }
4440
Laurence Lundbladea826c502020-05-10 21:07:00 -07004441 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004442 uErr = QCBORDecode_GetNext(&DC, &item);
4443 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004444 return 13;
4445 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004446 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4447 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4448 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4449 return 14;
4450 }
4451
Laurence Lundbladea826c502020-05-10 21:07:00 -07004452 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004453 uErr = QCBORDecode_GetNext(&DC, &item);
4454 if(uErr != QCBOR_SUCCESS) {
4455 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004456 }
4457 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4458 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4459 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004460 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004461 }
4462
Laurence Lundbladea826c502020-05-10 21:07:00 -07004463 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004464 uErr = QCBORDecode_GetNext(&DC, &item);
4465 if(uErr != QCBOR_SUCCESS) {
4466 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004467 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004468 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4469 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4470 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004471 return 18;
4472 }
4473
4474 uErr = QCBORDecode_Finish(&DC);
4475 if(uErr != QCBOR_SUCCESS) {
4476 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004477 }
4478
4479 /* Now encode some stuff and then decode it */
4480 uint8_t pBuf[40];
4481 QCBOREncodeContext EC;
4482 UsefulBufC Encoded;
4483
4484 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4485 QCBOREncode_OpenArray(&EC);
4486 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4487 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4488 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4489 QCBOREncode_CloseArray(&EC);
4490 QCBOREncode_Finish(&EC, &Encoded);
4491
4492
4493 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004494 uErr = QCBORDecode_GetNext(&DC, &item);
4495 if(uErr != QCBOR_SUCCESS) {
4496 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004497 }
4498
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004499 uErr = QCBORDecode_GetNext(&DC, &item);
4500 if(uErr != QCBOR_SUCCESS) {
4501 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004502 }
4503
4504 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4505 item.val.expAndMantissa.nExponent != 1000 ||
4506 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004507 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004508 }
4509
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004510 uErr = QCBORDecode_GetNext(&DC, &item);
4511 if(uErr != QCBOR_SUCCESS) {
4512 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004513 }
4514
4515 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4516 item.val.expAndMantissa.nExponent != INT32_MIN ||
4517 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004518 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004519 }
4520
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004521 uErr = QCBORDecode_GetNext(&DC, &item);
4522 if(uErr != QCBOR_SUCCESS) {
4523 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004524 }
4525
4526 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4527 item.val.expAndMantissa.nExponent != INT32_MAX ||
4528 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004529 return 106;
4530 }
4531
4532
4533 int64_t nExp, nMant;
4534 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4535 UsefulBufC Mant;
4536 bool bIsNeg;
4537
4538 QCBORDecode_Init(&DC,
4539 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4540 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004541 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004542
4543 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004544 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004545
4546 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004547 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4548 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004549
4550 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004551 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4552 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004553
4554 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004555 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004556
4557 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004558 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4559 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004560
4561 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004562 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4563 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004564
4565 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004566 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4567 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004568
4569 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004570 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4571 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004572
4573 QCBORDecode_ExitArray(&DC);
4574
4575 uErr = QCBORDecode_Finish(&DC);
4576 if(uErr != QCBOR_SUCCESS) {
4577 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004578 }
4579
4580 return 0;
4581}
4582
4583
4584static struct FailInput ExponentAndMantissaFailures[] = {
4585 // Exponent > INT64_MAX
4586 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4587 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4588 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4589 // Mantissa > INT64_MAX
4590 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4591 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4592 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4593 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004594 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004595 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004596 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004597 // bad content for big num
4598 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4599 // bad content for big num
4600 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4601 // Bad integer for exponent
4602 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4603 // Bad integer for mantissa
4604 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4605 // 3 items in array
4606 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4607 // unterminated indefinite length array
4608 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4609 // Empty array
4610 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4611 // Second is not an integer
4612 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4613 // First is not an integer
4614 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4615 // Not an array
4616 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4617};
4618
4619
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004620int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004621{
4622 return ProcessFailures(ExponentAndMantissaFailures,
4623 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4624}
4625
4626#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004627
4628
4629
4630/*
4631 Some basic CBOR with map and array used in a lot of tests.
4632 The map labels are all strings
4633
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004634 {
4635 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004636 "an array of two strings": [
4637 "string1", "string2"
4638 ],
4639 "map in a map": {
4640 "bytes 1": h'78787878',
4641 "bytes 2": h'79797979',
4642 "another int": 98,
4643 "text 2": "lies, damn lies and statistics"
4644 }
4645 }
4646 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004647
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004648int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004649{
4650 QCBORItem Item1, Item2, Item3;
4651 int64_t nDecodedInt1, nDecodedInt2;
4652 UsefulBufC B1, B2, S1, S2, S3;
4653
4654 QCBORDecodeContext DCtx;
4655 QCBORError nCBORError;
4656
4657 QCBORDecode_Init(&DCtx, input, 0);
4658
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004659 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004660
4661 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4662
4663 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4664 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004665 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4666 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4667 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004668 QCBORDecode_ExitMap(&DCtx);
4669
4670 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4671 QCBORDecode_GetNext(&DCtx, &Item1);
4672 QCBORDecode_GetNext(&DCtx, &Item2);
4673 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4674 return -400;
4675 }
4676 QCBORDecode_ExitArray(&DCtx);
4677
4678 // Parse the same array again using GetText() instead of GetItem()
4679 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004680 QCBORDecode_GetTextString(&DCtx, &S2);
4681 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004682 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4683 return 5000;
4684 }
4685 /* QCBORDecode_GetText(&DCtx, &S3);
4686 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4687 return 5001;
4688 } */
4689
4690 QCBORDecode_ExitArray(&DCtx);
4691
4692 QCBORDecode_ExitMap(&DCtx);
4693
4694 nCBORError = QCBORDecode_Finish(&DCtx);
4695
4696 if(nCBORError) {
4697 return (int32_t)nCBORError;
4698 }
4699
4700 if(nDecodedInt1 != 42) {
4701 return 1001;
4702 }
4703
4704 if(nDecodedInt2 != 98) {
4705 return 1002;
4706 }
4707
4708 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004709 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004710 return 1003;
4711 }
4712
4713 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004714 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004715 return 1004;
4716 }
4717
Laurence Lundblade9b334962020-08-27 10:55:53 -07004718 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004719 return 1005;
4720 }
4721
4722 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4723 return 1006;
4724 }
4725
4726 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4727 return 1007;
4728 }
4729
4730 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4731 return 1008;
4732 }
4733
4734 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4735 return 1009;
4736 }
4737
4738 return 0;
4739}
4740
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004741
4742/*
4743 [23,
4744 6000,
4745 h'67616C6163746963',
4746 h'686176656E20746F6B656E'
4747 ]
4748 */
4749static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004750 0x84,
4751 0x17,
4752 0x19, 0x17, 0x70,
4753 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4754 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004755
4756
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004757static const uint8_t spEmptyMap[] = {0xa0};
4758
4759static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004760
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004761static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4762
Laurence Lundbladef0499502020-08-01 11:55:57 -07004763/*
4764 {
4765 0: [],
4766 9: [
4767 [],
4768 []
4769 ],
4770 8: {
4771 1: [],
4772 2: {},
4773 3: []
4774 },
4775 4: {},
4776 5: [],
4777 6: [
4778 [],
4779 []
4780 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004781 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004782 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004783
Laurence Lundbladef0499502020-08-01 11:55:57 -07004784static const uint8_t spMapOfEmpty[] = {
4785 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4786 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4787 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004788
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004789/*
4790 Too many tags
4791 Invalid tag content
4792 Duplicate label
4793 Integer overflow
4794 Date overflow
4795
4796 {1: 224(225(226(227(4(0))))),
4797 2: 1(h''),
4798 3: -18446744073709551616,
4799 4: 1(1.0e+300),
4800 5: 0, 8: 8}
4801 */
4802static const uint8_t spRecoverableMapErrors[] = {
4803 0xbf,
4804 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
4805 0x02, 0xc1, 0x40,
4806 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4807 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
4808 0x05, 0x00,
4809 0x05, 0x00,
4810 0x08, 0x08,
4811 0xff
4812};
4813
4814// Bad break
4815static const uint8_t spUnRecoverableMapError1[] = {
4816 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
4817};
4818
4819// No more items
4820static const uint8_t spUnRecoverableMapError2[] = {
4821 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
4822};
4823
4824// Hit end because string is too long
4825static const uint8_t spUnRecoverableMapError3[] = {
4826 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
4827};
4828
4829// Hit end because string is too long
4830static const uint8_t spUnRecoverableMapError4[] = {
4831 0xbf,
4832 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4833 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4834 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4835 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4836 0xff
4837};
4838
4839
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004840int32_t EnterMapTest()
4841{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004842 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004843 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004844 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004845 int32_t nReturn;
4846 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004847
4848
4849 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004850 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004851
Laurence Lundbladef0499502020-08-01 11:55:57 -07004852
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004853 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004854 QCBORDecode_ExitArray(&DCtx);
4855
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004856 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
4857 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004858 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004859 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004860 QCBORDecode_ExitArray(&DCtx);
4861 QCBORDecode_ExitArray(&DCtx);
4862
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004863 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
4864 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004865 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004866 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004867 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004868 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004869 QCBORDecode_ExitArray(&DCtx);
4870 QCBORDecode_ExitMap(&DCtx);
4871
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004872 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004873 QCBORDecode_ExitMap(&DCtx);
4874
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004875 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004876 QCBORDecode_ExitArray(&DCtx);
4877
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004878 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
4879 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004880 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004881 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004882 QCBORDecode_ExitArray(&DCtx);
4883 QCBORDecode_ExitArray(&DCtx);
4884
4885 QCBORDecode_ExitMap(&DCtx);
4886
4887 uErr = QCBORDecode_Finish(&DCtx);
4888 if(uErr != QCBOR_SUCCESS){
4889 return 3011;
4890 }
4891
4892
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004893 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004894 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004895 if(nReturn) {
4896 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004897 }
4898
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004899 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004900 if(nReturn) {
4901 return nReturn;
4902 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004903
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004904
Laurence Lundblade937ea812020-05-08 11:38:23 -07004905
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004906 // These tests confirm the cursor is at the right place after entering
4907 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004908 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004909
4910 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004911 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004912 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004913 QCBORDecode_GetNext(&DCtx, &Item1);
4914 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4915 return 2001;
4916 }
4917
4918
Laurence Lundblade9b334962020-08-27 10:55:53 -07004919 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004920 QCBORDecode_VGetNext(&DCtx, &Item1);
4921 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004922 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
4923 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4924 UsefulBuf_Compare(ArrayItem.label.string,
4925 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
4926 return 2051;
4927 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004928 QCBORDecode_GetNext(&DCtx, &Item1);
4929 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4930 return 2002;
4931 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004932 QCBORDecode_ExitArray(&DCtx);
4933 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
4934 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4935 UsefulBuf_Compare(ArrayItem.label.string,
4936 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
4937 return 2052;
4938 }
4939
Laurence Lundblade937ea812020-05-08 11:38:23 -07004940
Laurence Lundblade9b334962020-08-27 10:55:53 -07004941 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004942 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004943 QCBORDecode_GetNext(&DCtx, &Item1);
4944 QCBORDecode_GetNext(&DCtx, &Item1);
4945 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004946 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4947 QCBORDecode_GetNext(&DCtx, &Item1);
4948 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4949 return 2003;
4950 }
4951
Laurence Lundblade9b334962020-08-27 10:55:53 -07004952 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004953 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004954 QCBORDecode_GetNext(&DCtx, &Item1);
4955 QCBORDecode_GetNext(&DCtx, &Item1);
4956 QCBORDecode_GetNext(&DCtx, &Item1);
4957 QCBORDecode_GetNext(&DCtx, &Item1);
4958 QCBORDecode_GetNext(&DCtx, &Item1);
4959 QCBORDecode_GetNext(&DCtx, &Item1);
4960 QCBORDecode_GetNext(&DCtx, &Item1);
4961 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4962 QCBORDecode_GetNext(&DCtx, &Item1);
4963 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004964 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004965 }
4966
Laurence Lundblade9b334962020-08-27 10:55:53 -07004967 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004968 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004969 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4970 QCBORDecode_ExitArray(&DCtx);
4971 QCBORDecode_GetNext(&DCtx, &Item1);
4972 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4973 return 2006;
4974 }
4975 QCBORDecode_ExitMap(&DCtx);
4976 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4977 return 2007;
4978 }
4979
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004980 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004981 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004982 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004983 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4984 uErr = QCBORDecode_GetAndResetError(&DCtx);
4985 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004986 return 2008;
4987 }
4988 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004989 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004990 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004991 return 2009;
4992 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004993
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004994
4995 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004996 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004997 // This will fail because the map is empty.
4998 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4999 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005000 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005001 return 2010;
5002 }
5003 QCBORDecode_ExitMap(&DCtx);
5004 uErr = QCBORDecode_Finish(&DCtx);
5005 if(uErr != QCBOR_SUCCESS){
5006 return 2011;
5007 }
5008
5009
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005010 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005011 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005012 // This will fail because the map is empty.
5013 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5014 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005015 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005016 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005017 }
5018 QCBORDecode_ExitMap(&DCtx);
5019 uErr = QCBORDecode_Finish(&DCtx);
5020 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005021 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005022 }
5023
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005024
5025 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005026 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005027 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005028 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005029 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005030 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005031 QCBORDecode_ExitArray(&DCtx);
5032 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5033 QCBORDecode_ExitArray(&DCtx);
5034 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005035 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005036 return 2014;
5037 }
5038
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005039 int64_t nInt;
5040 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005041 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005042 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5043 uErr = QCBORDecode_GetAndResetError(&DCtx);
5044 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5045 return 2021;
5046 }
5047
5048 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5049 uErr = QCBORDecode_GetAndResetError(&DCtx);
5050 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5051 return 2022;
5052 }
5053
5054 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5055 uErr = QCBORDecode_GetAndResetError(&DCtx);
5056 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5057 return 2023;
5058 }
5059
5060 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5061 uErr = QCBORDecode_GetAndResetError(&DCtx);
5062#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5063 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5064 return 2024;
5065 }
5066#else
5067 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5068 return 2027;
5069 }
5070#endif
5071
5072 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5073 uErr = QCBORDecode_GetAndResetError(&DCtx);
5074 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5075 return 2025;
5076 }
5077
5078 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5079
5080 QCBORDecode_ExitMap(&DCtx);
5081 uErr = QCBORDecode_Finish(&DCtx);
5082 if(uErr != QCBOR_SUCCESS) {
5083 return 2026;
5084 }
5085
5086 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005087 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005088 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5089 uErr = QCBORDecode_GetAndResetError(&DCtx);
5090 if(uErr != QCBOR_ERR_BAD_BREAK) {
5091 return 2030;
5092 }
5093
5094 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005095 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005096 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5097 uErr = QCBORDecode_GetAndResetError(&DCtx);
5098 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5099 return 2031;
5100 }
5101
5102 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005103 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005104 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5105 uErr = QCBORDecode_GetAndResetError(&DCtx);
5106 if(uErr != QCBOR_ERR_HIT_END) {
5107 return 2032;
5108 }
5109
5110 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005111 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005112 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5113 uErr = QCBORDecode_GetAndResetError(&DCtx);
5114 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5115 return 2033;
5116 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005117
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005118 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005119}
5120
5121
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005122struct NumberConversion {
5123 char *szDescription;
5124 UsefulBufC CBOR;
5125 int64_t nConvertedToInt64;
5126 QCBORError uErrorInt64;
5127 uint64_t uConvertToUInt64;
5128 QCBORError uErrorUint64;
5129 double dConvertToDouble;
5130 QCBORError uErrorDouble;
5131};
5132
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005133static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005134 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005135 "too large to fit into int64_t",
5136 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5137 0,
5138 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5139 0,
5140 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5141 ((double)INT64_MIN) + 1 ,
5142 QCBOR_SUCCESS
5143 },
5144 {
5145 "largest negative int that fits in int64_t",
5146 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5147 INT64_MIN,
5148 QCBOR_SUCCESS,
5149 0,
5150 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5151 (double)INT64_MIN,
5152 QCBOR_SUCCESS
5153 },
5154 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005155 "negative bignum -1",
5156 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5157 -1,
5158 QCBOR_SUCCESS,
5159 0,
5160 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5161 -1.0,
5162 QCBOR_SUCCESS
5163 },
5164 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005165 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005166 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5167 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005168#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005169 257000,
5170 QCBOR_SUCCESS,
5171 257000,
5172 QCBOR_SUCCESS,
5173 257000.0,
5174 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005175#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5176 0,
5177 QCBOR_ERR_UNEXPECTED_TYPE,
5178 0,
5179 QCBOR_ERR_UNEXPECTED_TYPE,
5180 0.0,
5181 QCBOR_ERR_UNEXPECTED_TYPE
5182#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005183 },
5184 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005185 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005186 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5187 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005188#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005189 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005190 QCBOR_SUCCESS,
5191 0,
5192 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005193 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005194 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005195#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5196 0,
5197 QCBOR_ERR_UNEXPECTED_TYPE,
5198 0,
5199 QCBOR_ERR_UNEXPECTED_TYPE,
5200 0.0,
5201 QCBOR_ERR_UNEXPECTED_TYPE
5202#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005203 },
5204 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005205 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005206 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5207 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005208#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005209 2056,
5210 QCBOR_SUCCESS,
5211 2056,
5212 QCBOR_SUCCESS,
5213 2056.0,
5214 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005215#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5216 0,
5217 QCBOR_ERR_UNEXPECTED_TYPE,
5218 0,
5219 QCBOR_ERR_UNEXPECTED_TYPE,
5220 0.0,
5221 QCBOR_ERR_UNEXPECTED_TYPE
5222#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005223 },
5224 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005225 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005226 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5227 0,
5228 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5229 0,
5230 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5231 -18446744073709551617.0,
5232 QCBOR_SUCCESS
5233 },
5234 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005235 "Positive bignum 0x01020304 indefinite length string",
5236 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5237 0x01020304,
5238 QCBOR_SUCCESS,
5239 0x01020304,
5240 QCBOR_SUCCESS,
5241 16909060.0,
5242 QCBOR_SUCCESS
5243 },
5244 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005245 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005246 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5247 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005248#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005249 0,
5250 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5251 0,
5252 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5253 -INFINITY,
5254 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005255#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5256 0,
5257 QCBOR_ERR_UNEXPECTED_TYPE,
5258 0,
5259 QCBOR_ERR_UNEXPECTED_TYPE,
5260 0.0,
5261 QCBOR_ERR_UNEXPECTED_TYPE
5262#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005263 },
5264 {
5265 "big float [9223372036854775806, 9223372036854775806]",
5266 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5267 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005268#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005269 0,
5270 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5271 0,
5272 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5273 INFINITY,
5274 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005275#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5276 0,
5277 QCBOR_ERR_UNEXPECTED_TYPE,
5278 0,
5279 QCBOR_ERR_UNEXPECTED_TYPE,
5280 0.0,
5281 QCBOR_ERR_UNEXPECTED_TYPE
5282#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005283 },
5284 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005285 "Big float 3 * 2^^2",
5286 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005287#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005288 12,
5289 QCBOR_SUCCESS,
5290 12,
5291 QCBOR_SUCCESS,
5292 12.0,
5293 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005294#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5295 0,
5296 QCBOR_ERR_UNEXPECTED_TYPE,
5297 0,
5298 QCBOR_ERR_UNEXPECTED_TYPE,
5299 0.0,
5300 QCBOR_ERR_UNEXPECTED_TYPE
5301#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005302 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005303 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005304 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005305 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5306 0,
5307 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5308 18446744073709551615ULL,
5309 QCBOR_SUCCESS,
5310 18446744073709551615.0,
5311 QCBOR_SUCCESS
5312 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005313 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005314 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005315 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5316 65536-1,
5317 QCBOR_SUCCESS,
5318 0xffff,
5319 QCBOR_SUCCESS,
5320 65535.0,
5321 QCBOR_SUCCESS
5322 },
5323 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005324 "Postive integer 0",
5325 {(uint8_t[]){0x0}, 1},
5326 0LL,
5327 QCBOR_SUCCESS,
5328 0ULL,
5329 QCBOR_SUCCESS,
5330 0.0,
5331 QCBOR_SUCCESS
5332 },
5333 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005334 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005335 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5336 -9223372036854775807-1, // INT64_MIN
5337 QCBOR_SUCCESS,
5338 0ULL,
5339 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5340 -9223372036854775808.0,
5341 QCBOR_SUCCESS
5342 },
5343 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005344 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005345 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005346#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005347 100L,
5348 QCBOR_SUCCESS,
5349 100ULL,
5350 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005351#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5352 0,
5353 QCBOR_ERR_HW_FLOAT_DISABLED,
5354 0,
5355 QCBOR_ERR_HW_FLOAT_DISABLED,
5356#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005357 100.3,
5358 QCBOR_SUCCESS
5359 },
5360 {
5361 "Floating point value NaN 0xfa7fc00000",
5362 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005363#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005364 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005365 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005366 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005367 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005368#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5369 0,
5370 QCBOR_ERR_HW_FLOAT_DISABLED,
5371 0,
5372 QCBOR_ERR_HW_FLOAT_DISABLED,
5373#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005374 NAN,
5375 QCBOR_SUCCESS
5376 },
5377 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005378 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005379 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005380#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5381#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5382 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005383 -4,
5384 QCBOR_SUCCESS,
5385 0,
5386 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5387 -4.0,
5388 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005389#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5390 // Float HW disabled
5391 -4,
5392 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5393 0,
5394 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5395 -4.0,
5396 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5397#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5398#else
5399 // Half-precision disabled
5400 -4,
5401 QCBOR_ERR_HALF_PRECISION_DISABLED,
5402 0,
5403 QCBOR_ERR_HALF_PRECISION_DISABLED,
5404 -4.0,
5405 QCBOR_ERR_HALF_PRECISION_DISABLED
5406#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005407 },
5408 {
5409 "Decimal fraction 3/10",
5410 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005411#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005412 0,
5413 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5414 0,
5415 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5416 0.30000000000000004,
5417 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005418#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5419 0,
5420 QCBOR_ERR_UNEXPECTED_TYPE,
5421 0,
5422 QCBOR_ERR_UNEXPECTED_TYPE,
5423 0.0,
5424 QCBOR_ERR_UNEXPECTED_TYPE
5425#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005426 },
5427 {
5428 "+inifinity",
5429 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5430#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5431 0,
5432 QCBOR_ERR_FLOAT_EXCEPTION,
5433 0,
5434 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5435#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5436 0,
5437 QCBOR_ERR_HW_FLOAT_DISABLED,
5438 0,
5439 QCBOR_ERR_HW_FLOAT_DISABLED,
5440#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5441 INFINITY,
5442 QCBOR_SUCCESS
5443 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005444
5445 {
5446 "extreme pos bignum",
5447 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5448 // 50 rows of 8 is 400 digits.
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, 0xf0,
5483 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5484 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5485 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5486 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5487 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5488 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5489 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5490 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5491 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5492 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5493 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5494 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5495 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5496 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5497 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5498 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5499 404},
5500 0,
5501 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5502 0,
5503 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5504#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5505 INFINITY,
5506 QCBOR_SUCCESS
5507#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5508 0,
5509 QCBOR_ERR_HW_FLOAT_DISABLED,
5510#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5511 },
5512
5513 {
5514 "extreme neg bignum",
5515 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5516 // 50 rows of 8 is 400 digits.
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, 0xf0,
5551 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5552 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5553 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5554 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5555 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5556 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5557 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5558 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5559 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5560 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5561 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5562 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5563 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5564 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5565 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5566 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5567 404},
5568 0,
5569 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5570 0,
5571 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5572#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5573 -INFINITY,
5574 QCBOR_SUCCESS
5575#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5576 0,
5577 QCBOR_ERR_HW_FLOAT_DISABLED,
5578#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5579 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005580
5581 {
5582 "big float underflow [9223372036854775806, -9223372036854775806]",
5583 {(uint8_t[]){
5584 0xC5, 0x82,
5585 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5586 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5587#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5588 0,
5589 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5590 0,
5591 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5592 0,
5593 QCBOR_SUCCESS
5594#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5595 0,
5596 QCBOR_ERR_UNEXPECTED_TYPE,
5597 0,
5598 QCBOR_ERR_UNEXPECTED_TYPE,
5599 0.0,
5600 QCBOR_ERR_UNEXPECTED_TYPE
5601#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5602 },
5603
5604 {
5605 "bigfloat that evaluates to -INFINITY",
5606 {(uint8_t[]){
5607 0xC5, 0x82,
5608 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5609 0xC3, 0x42, 0x01, 0x01}, 15},
5610#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5611 0,
5612 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5613 0,
5614 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5615 -INFINITY,
5616 QCBOR_SUCCESS
5617#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5618 0,
5619 QCBOR_ERR_UNEXPECTED_TYPE,
5620 0,
5621 QCBOR_ERR_UNEXPECTED_TYPE,
5622 0.0,
5623 QCBOR_ERR_UNEXPECTED_TYPE
5624#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5625 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005626};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005627
5628
5629
Laurence Lundblade313b2862020-05-16 01:23:06 -07005630int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005631{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005632 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005633
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005634 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5635 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005636
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005637 // Set up the decoding context including a memory pool so that
5638 // indefinite length items can be checked
5639 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005640 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005641
5642 /* ----- test conversion to int64_t ------ */
5643 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005644 QCBORError 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
5649 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005650 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005651 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005652 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005653 }
5654 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005655 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005656 }
5657
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005658 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005659 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5660 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5661 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005662 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005663 }
5664 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005665 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005666 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005667 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005668 }
5669 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005670 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005671 }
5672
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005673 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005674 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5675 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5676 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005677 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005678 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005679#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005680 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005681 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005682 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005683 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005684 }
5685 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5686 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005687 // NaN's can't be compared for equality. A NaN is
5688 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005689 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005690 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005691 }
5692 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005693 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005694 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005695 }
5696 }
5697 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005698#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005699 }
5700
5701 return 0;
5702}
5703
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005704
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005705
5706
Laurence Lundbladee3553422020-05-02 11:11:17 -07005707int32_t CBORSequenceDecodeTests(void)
5708{
5709 QCBORDecodeContext DCtx;
5710 QCBORItem Item;
5711 QCBORError uCBORError;
5712
5713 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005714
Laurence Lundbladee3553422020-05-02 11:11:17 -07005715 // The input for the date test happens to be a sequence so it
5716 // is reused. It is a sequence because it doesn't start as
5717 // an array or map.
5718 QCBORDecode_Init(&DCtx,
5719 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5720 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005721
Laurence Lundbladee3553422020-05-02 11:11:17 -07005722 // Get the first item
5723 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5724 if(uCBORError != QCBOR_SUCCESS) {
5725 return 1;
5726 }
5727 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5728 return 2;
5729 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005730
Laurence Lundbladee3553422020-05-02 11:11:17 -07005731 // Get a second item
5732 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005733 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5734 return 66;
5735 }
5736
5737 // Get a third item
5738 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005739 if(uCBORError != QCBOR_SUCCESS) {
5740 return 2;
5741 }
5742 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5743 return 3;
5744 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005745
Laurence Lundbladee3553422020-05-02 11:11:17 -07005746 // A sequence can have stuff at the end that may
5747 // or may not be valid CBOR. The protocol decoder knows
5748 // when to stop by definition of the protocol, not
5749 // when the top-level map or array is ended.
5750 // Finish still has to be called to know that
5751 // maps and arrays (if there were any) were closed
5752 // off correctly. When called like this it
5753 // must return the error QCBOR_ERR_EXTRA_BYTES.
5754 uCBORError = QCBORDecode_Finish(&DCtx);
5755 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5756 return 4;
5757 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005758
5759
Laurence Lundbladee3553422020-05-02 11:11:17 -07005760 // --- Test an empty input ----
5761 uint8_t empty[1];
5762 UsefulBufC Empty = {empty, 0};
5763 QCBORDecode_Init(&DCtx,
5764 Empty,
5765 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005766
Laurence Lundbladee3553422020-05-02 11:11:17 -07005767 uCBORError = QCBORDecode_Finish(&DCtx);
5768 if(uCBORError != QCBOR_SUCCESS) {
5769 return 5;
5770 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005771
5772
Laurence Lundbladee3553422020-05-02 11:11:17 -07005773 // --- Sequence with unclosed indefinite length array ---
5774 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005775
Laurence Lundbladee3553422020-05-02 11:11:17 -07005776 QCBORDecode_Init(&DCtx,
5777 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5778 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005779
Laurence Lundbladee3553422020-05-02 11:11:17 -07005780 // Get the first item
5781 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5782 if(uCBORError != QCBOR_SUCCESS) {
5783 return 7;
5784 }
5785 if(Item.uDataType != QCBOR_TYPE_INT64) {
5786 return 8;
5787 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005788
Laurence Lundbladee3553422020-05-02 11:11:17 -07005789 // Get a second item
5790 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5791 if(uCBORError != QCBOR_SUCCESS) {
5792 return 9;
5793 }
5794 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5795 return 10;
5796 }
5797
5798 // Try to finish before consuming all bytes to confirm
5799 // that the still-open error is returned.
5800 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005801 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005802 return 11;
5803 }
5804
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005805
Laurence Lundbladee3553422020-05-02 11:11:17 -07005806 // --- Sequence with a closed indefinite length array ---
5807 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005808
Laurence Lundbladee3553422020-05-02 11:11:17 -07005809 QCBORDecode_Init(&DCtx,
5810 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5811 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005812
Laurence Lundbladee3553422020-05-02 11:11:17 -07005813 // Get the first item
5814 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5815 if(uCBORError != QCBOR_SUCCESS) {
5816 return 12;
5817 }
5818 if(Item.uDataType != QCBOR_TYPE_INT64) {
5819 return 13;
5820 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005821
Laurence Lundbladee3553422020-05-02 11:11:17 -07005822 // Get a second item
5823 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5824 if(uCBORError != QCBOR_SUCCESS) {
5825 return 14;
5826 }
5827 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5828 return 15;
5829 }
5830
5831 // Try to finish before consuming all bytes to confirm
5832 // that the still-open error is returned.
5833 uCBORError = QCBORDecode_Finish(&DCtx);
5834 if(uCBORError != QCBOR_SUCCESS) {
5835 return 16;
5836 }
5837
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005838
Laurence Lundbladee3553422020-05-02 11:11:17 -07005839 return 0;
5840}
5841
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005842
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005843
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005844int32_t IntToTests()
5845{
5846 int nErrCode;
5847 int32_t n32;
5848 int16_t n16;
5849 int8_t n8;
5850 uint32_t u32;
5851 uint16_t u16;
5852 uint8_t u8;
5853 uint64_t u64;
5854
5855 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5856 if(nErrCode == -1 || n32 != 1) {
5857 return 1;
5858 }
5859
5860 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5861 if(nErrCode == -1 || n32 != INT32_MAX) {
5862 return 2;
5863 }
5864
5865 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5866 if(nErrCode == -1 || n32 != INT32_MIN) {
5867 return 3;
5868 }
5869
5870 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5871 if(nErrCode != -1) {
5872 return 4;
5873 }
5874
5875 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5876 if(nErrCode != -1) {
5877 return 5;
5878 }
5879
5880
5881 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5882 if(nErrCode == -1 || n16 != INT16_MAX) {
5883 return 6;
5884 }
5885
5886 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5887 if(nErrCode == -1 || n16 != INT16_MIN) {
5888 return 7;
5889 }
5890
5891 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5892 if(nErrCode == -1 || n16 != 1) {
5893 return 8;
5894 }
5895
5896 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5897 if(nErrCode != -1) {
5898 return 9;
5899 }
5900
5901 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5902 if(nErrCode != -1) {
5903 return 10;
5904 }
5905
5906
5907 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5908 if(nErrCode == -1 || n8 != 1) {
5909 return 11;
5910 }
5911
5912 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5913 if(nErrCode == -1 || n8 != INT8_MAX) {
5914 return 12;
5915 }
5916
5917 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5918 if(nErrCode == -1 || n8 != INT8_MIN) {
5919 return 13;
5920 }
5921
5922 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5923 if(nErrCode != -1) {
5924 return 14;
5925 }
5926
5927 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5928 if(nErrCode != -1) {
5929 return 15;
5930 }
5931
5932
5933 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5934 if(nErrCode == -1 || u32 != 1) {
5935 return 16;
5936 }
5937
5938 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5939 if(nErrCode == -1 || u32 != UINT32_MAX) {
5940 return 17;
5941 }
5942
5943 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5944 if(nErrCode == -1 || u32 != 0) {
5945 return 18;
5946 }
5947
5948 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5949 if(nErrCode != -1) {
5950 return 19;
5951 }
5952
5953 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5954 if(nErrCode != -1) {
5955 return 20;
5956 }
5957
5958
5959 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5960 if(nErrCode == -1 || u16 != UINT16_MAX) {
5961 return 21;
5962 }
5963
5964 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5965 if(nErrCode == -1 || u16 != 0) {
5966 return 22;
5967 }
5968
5969 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5970 if(nErrCode == -1 || u16 != 1) {
5971 return 23;
5972 }
5973
5974 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5975 if(nErrCode != -1) {
5976 return 24;
5977 }
5978
5979 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5980 if(nErrCode != -1) {
5981 return 25;
5982 }
5983
5984
5985 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5986 if(nErrCode == -1 || u8 != UINT8_MAX) {
5987 return 26;
5988 }
5989
5990 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5991 if(nErrCode == -1 || u8 != 0) {
5992 return 27;
5993 }
5994
5995 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5996 if(nErrCode == -1 || u8 != 1) {
5997 return 28;
5998 }
5999
6000 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6001 if(nErrCode != -1) {
6002 return 29;
6003 }
6004
6005 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6006 if(nErrCode != -1) {
6007 return 30;
6008 }
6009
6010
6011 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6012 if(nErrCode == -1 || u64 != 1) {
6013 return 31;
6014 }
6015
6016 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6017 if(nErrCode == -1 || u64 != INT64_MAX) {
6018 return 32;
6019 }
6020
6021 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6022 if(nErrCode == -1 || u64 != 0) {
6023 return 33;
6024 }
6025
6026 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6027 if(nErrCode != -1) {
6028 return 34;
6029 }
6030
6031 return 0;
6032}
6033
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006034
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006035
6036
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006037/*
6038A sequence with
6039 A wrapping bstr
6040 containing a map
6041 1
6042 2
6043 A wrapping bstr
6044 containing an array
6045 3
6046 wrapping bstr
6047 4
6048 5
6049 6
6050 array
6051 7
6052 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006053 */
6054
Laurence Lundblade55013642020-09-23 05:39:22 -07006055static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006056{
Laurence Lundblade55013642020-09-23 05:39:22 -07006057 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006058 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006059 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006060
Laurence Lundblade55013642020-09-23 05:39:22 -07006061 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006062
6063 QCBOREncode_BstrWrap(&EC);
6064 QCBOREncode_OpenMap(&EC);
6065 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6066 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6067 QCBOREncode_CloseMap(&EC);
6068 QCBOREncode_BstrWrap(&EC);
6069 QCBOREncode_OpenArray(&EC);
6070 QCBOREncode_AddInt64(&EC, 3);
6071 QCBOREncode_BstrWrap(&EC);
6072 QCBOREncode_AddInt64(&EC, 4);
6073 QCBOREncode_CloseBstrWrap(&EC, NULL);
6074 QCBOREncode_AddInt64(&EC, 5);
6075 QCBOREncode_CloseArray(&EC);
6076 QCBOREncode_CloseBstrWrap(&EC, NULL);
6077 QCBOREncode_AddInt64(&EC, 6);
6078 QCBOREncode_CloseBstrWrap(&EC, NULL);
6079 QCBOREncode_OpenArray(&EC);
6080 QCBOREncode_AddInt64(&EC, 7);
6081 QCBOREncode_AddInt64(&EC, 8);
6082 QCBOREncode_CloseArray(&EC);
6083
6084 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006085 if(uErr) {
6086 Encoded = NULLUsefulBufC;
6087 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006088
6089 return Encoded;
6090}
6091
6092
6093int32_t EnterBstrTest()
6094{
Laurence Lundblade55013642020-09-23 05:39:22 -07006095 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006096
6097 QCBORDecodeContext DC;
6098
Laurence Lundblade55013642020-09-23 05:39:22 -07006099 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006100
Laurence Lundblade55013642020-09-23 05:39:22 -07006101 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006102
6103
Laurence Lundblade9b334962020-08-27 10:55:53 -07006104 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006105 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006106 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6107 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006108 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006109 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006110 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006111 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006112 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006113 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006114 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006115 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006116 QCBORDecode_ExitArray(&DC);
6117 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006118 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006119 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006120 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006121 QCBORDecode_GetInt64(&DC, &n7);
6122 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006123 QCBORDecode_ExitArray(&DC);
6124
6125 QCBORError uErr = QCBORDecode_Finish(&DC);
6126
6127 return (int32_t)uErr;
6128}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006129
6130
6131
6132
6133static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006134 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006135
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006136 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006137 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006138 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6139 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6140 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006141
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006142 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006143 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6144 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6145 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006146
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006147 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006148 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006149 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6150 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006151
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006152 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006153 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6154 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006155
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006156 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006157 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006158 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6159 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006160
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006161 21,
6162 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6163 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6164
6165 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006166 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006167 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6168 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006169
6170 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006171 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6172 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006173
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006174 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006175 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006176 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6177 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006178
6179 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006180 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6181 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006182
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006183 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006184 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006185 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6186 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006187
6188 0x18, 0x33,
6189 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6190
6191 // MIME
6192 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006193 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6194 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6195 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006196
6197 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006198 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6199 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006200
6201 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006202 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6203 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6204 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006205
6206 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006207 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6208 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006209
6210 // UUID
6211 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006212 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6213 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006214
6215 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006216 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6217 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006218};
6219
6220int32_t DecodeTaggedTypeTests()
6221{
6222 QCBORDecodeContext DC;
6223 QCBORError uErr;
6224
6225 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6226
6227 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006228 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006229
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006230 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006231 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006232 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006233 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6234 return 1;
6235 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006236 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006237 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6238 return 2;
6239 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006240 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006241 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6242 return 3;
6243 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006244 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006245 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006246 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6247 return 4;
6248 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006249 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006250 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006251 return 5;
6252 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006253
Laurence Lundblade9b334962020-08-27 10:55:53 -07006254 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006255 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6256 bNeg != false) {
6257 return 10;
6258 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006259 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006260 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6261 bNeg != true) {
6262 return 11;
6263 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006264 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006265 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6266 return 12;
6267 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006268 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006269 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006270 return 13;
6271 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006272 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006273 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006274 return 14;
6275 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006276
Laurence Lundblade9b334962020-08-27 10:55:53 -07006277 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006278 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6279 return 20;
6280 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006281 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006282 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6283 return 21;
6284 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006285 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006286 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006287 return 22;
6288 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006289 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006290 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006291 return 23;
6292 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006293
Laurence Lundblade9b334962020-08-27 10:55:53 -07006294 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006295 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6296 return 30;
6297 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006298 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006299 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6300 return 31;
6301 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006302 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006303 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006304 return 32;
6305 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006306 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006307 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006308 return 33;
6309 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006310
Laurence Lundblade9b334962020-08-27 10:55:53 -07006311 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006312 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6313 return 40;
6314 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006315 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006316 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6317 return 41;
6318 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006319 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006320 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006321 return 42;
6322 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006323 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006324 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006325 return 43;
6326 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006327
Laurence Lundblade9b334962020-08-27 10:55:53 -07006328 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006329 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6330 return 50;
6331 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006332 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006333 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6334 return 51;
6335 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006336 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006337 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006338 return 52;
6339 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006340 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006341 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006342 return 53;
6343 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006344
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006345 // MIME
6346 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006347 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006348 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6349 bIsNot7Bit == true) {
6350 return 60;
6351 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006352 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006353 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6354 bIsNot7Bit == true) {
6355 return 61;
6356 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006357 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006358 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6359 bIsNot7Bit == false) {
6360 return 62;
6361 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006362 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006363 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6364 bIsNot7Bit == false) {
6365 return 63;
6366 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006367 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006368 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006369 return 64;
6370 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006371 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006372 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006373 return 65;
6374 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006375
Laurence Lundblade9b334962020-08-27 10:55:53 -07006376 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006377 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6378 return 70;
6379 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006380 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006381 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6382 return 71;
6383 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006384 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006385 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006386 return 72;
6387 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006388 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006389 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006390 return 73;
6391 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006392
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006393 // Improvement: add some more error test cases
6394
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006395 QCBORDecode_ExitMap(&DC);
6396
6397 uErr = QCBORDecode_Finish(&DC);
6398 if(uErr != QCBOR_SUCCESS) {
6399 return 100;
6400 }
6401
6402 return 0;
6403}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006404
6405
6406
6407
6408/*
6409 [
6410 "aaaaaaaaaa",
6411 {}
6412 ]
6413 */
6414static const uint8_t spTooLarge1[] = {
6415 0x9f,
6416 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6417 0xa0,
6418 0xff
6419};
6420
6421/*
6422 [
6423 {
6424 0: "aaaaaaaaaa"
6425 }
6426 ]
6427 */
6428static const uint8_t spTooLarge2[] = {
6429 0x9f,
6430 0xa1,
6431 0x00,
6432 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6433 0xff
6434};
6435
6436/*
6437 h'A1006A61616161616161616161'
6438
6439 {
6440 0: "aaaaaaaaaa"
6441 }
6442 */
6443static const uint8_t spTooLarge3[] = {
6444 0x4d,
6445 0xa1,
6446 0x00,
6447 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6448};
6449
6450int32_t TooLargeInputTest(void)
6451{
6452 QCBORDecodeContext DC;
6453 QCBORError uErr;
6454 UsefulBufC String;
6455
6456 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6457 // to 10 There's not really any way to test this error
6458 // condition. The error condition is not complex, so setting
6459 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6460
6461 // The input CBOR is only too large because the
6462 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6463 //
6464 // This test is disabled for the normal test runs because of the
6465 // special build requirement.
6466
6467
6468 // Tests the start of a map being too large
6469 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006470 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006471 QCBORDecode_GetTextString(&DC, &String);
6472 uErr = QCBORDecode_GetError(&DC);
6473 if(uErr != QCBOR_SUCCESS) {
6474 return 1;
6475 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006476 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006477 uErr = QCBORDecode_GetError(&DC);
6478 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6479 return 2;
6480 }
6481
6482 // Tests the end of a map being too large
6483 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006484 QCBORDecode_EnterArray(&DC, NULL);
6485 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006486 uErr = QCBORDecode_GetError(&DC);
6487 if(uErr != QCBOR_SUCCESS) {
6488 return 3;
6489 }
6490 QCBORDecode_ExitMap(&DC);
6491 uErr = QCBORDecode_GetError(&DC);
6492 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6493 return 4;
6494 }
6495
6496 // Tests the entire input CBOR being too large when processing bstr wrapping
6497 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6498 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6499 uErr = QCBORDecode_GetError(&DC);
6500 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6501 return 5;
6502 }
6503
6504 return 0;
6505}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006506
6507
6508static const uint8_t spMapWithIndefLenStrings[] = {
6509 0xbf,
6510 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6511 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6512 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6513 0x03,
6514 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6515 0xc3,
6516 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6517 0xff
6518};
6519
6520int32_t SpiffyIndefiniteLengthStringsTests()
6521{
6522 QCBORDecodeContext DCtx;
6523
6524 QCBORDecode_Init(&DCtx,
6525 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6526 QCBOR_DECODE_MODE_NORMAL);
6527
6528 MakeUsefulBufOnStack(StringBuf, 200);
6529 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6530
6531 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006532 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006533 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6534 if(QCBORDecode_GetAndResetError(&DCtx)) {
6535 return 1;
6536 }
6537
6538 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6539 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6540 return 2;
6541 }
6542
6543 uint64_t uInt;
6544 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6545 if(QCBORDecode_GetAndResetError(&DCtx)) {
6546 return 3;
6547 }
6548 if(uInt != 3) {
6549 return 4;
6550 }
6551
6552 double uDouble;
6553 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6554 "label2",
6555 0xff,
6556 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006557#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006558 if(QCBORDecode_GetAndResetError(&DCtx)) {
6559 return 5;
6560 }
6561 if(uDouble != -16777474) {
6562 return 6;
6563 }
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006564#else
6565 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6566 return 7;
6567 }
6568#endif
6569
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006570
6571 QCBORDecode_ExitMap(&DCtx);
6572
6573 if(QCBORDecode_Finish(&DCtx)) {
6574 return 99;
6575 }
6576
6577 return 0;
6578}