blob: 6ee22fdf20cbce2259bf52548ec95569f65645ec [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
Laurence Lundblade88e9db22020-11-02 03:56:33 -08002893 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
2894 return -106;
2895 }
2896
2897
Laurence Lundblade9b334962020-08-27 10:55:53 -07002898 /* tag 10489608748473423768(
2899 2442302356(
2900 21590(
2901 240(
2902 []))))
2903 */
2904 uError = QCBORDecode_GetNext(&DCtx, &Item);
2905 if(uError != QCBOR_SUCCESS ||
2906 Item.uDataType != QCBOR_TYPE_ARRAY ||
2907 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2908 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2909 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2910 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002911 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002912 }
2913
2914 /* tag 21590(
2915 10489608748473423768(
2916 2442302357(
2917 21591(
2918 []))))
2919 */
2920 uError = QCBORDecode_GetNext(&DCtx, &Item);
2921 if(uError != QCBOR_SUCCESS ||
2922 Item.uDataType != QCBOR_TYPE_ARRAY ||
2923 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2924 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2925 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2926 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2927 return -8;
2928 }
2929
2930 /* Make sure to blow past the limit of tags that must be mapped.
2931 works in conjuntion with entries above.
2932 269488144(269488145(269488146(269488147([]))))
2933 */
2934 uError = QCBORDecode_GetNext(&DCtx, &Item);
2935 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2936 return -9;
2937 }
2938
2939 uError = QCBORDecode_GetNext(&DCtx, &Item);
2940 if(uError == QCBOR_SUCCESS) {
2941 return -10;
2942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002943
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002944 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002945 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002946 // tage and then matches it. Caller-config lists are no longer
2947 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002948 QCBORDecode_Init(&DCtx,
2949 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2950 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002951 const uint64_t puList[] = {0x9192939495969798, 257};
2952 const QCBORTagListIn TL = {2, puList};
2953 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002954
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002955 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2956 return -8;
2957 }
2958 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2959 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2960 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2961 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2962 Item.val.uCount != 0) {
2963 return -9;
2964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002965
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002966 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002967 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002968 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002969 const uint64_t puLongList[17] = {1,2,1};
2970 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002971 QCBORDecode_Init(&DCtx,
2972 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2973 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002974 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2975 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2976 return -11;
2977 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002978
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07002979 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07002980 QCBORTagListOut Out = {0, 4, puTags};
2981
2982
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002983 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002984 QCBORDecode_Init(&DCtx,
2985 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2986 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002987 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2988 return -12;
2989 }
2990 if(puTags[0] != 0x9192939495969798 ||
2991 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002992 puTags[2] != 0x06 ||
2993 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002994 return -13;
2995 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002996
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002997 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002998 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002999 QCBORDecode_Init(&DCtx,
3000 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3001 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003002 QCBORTagListOut OutSmall = {0, 3, puTags};
3003 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3004 return -14;
3005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundblade9b334962020-08-27 10:55:53 -07003007
3008
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003009 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003010 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3011 // It is a bit of a messy test and maybe could be improved, but
3012 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003013 QCBORDecode_Init(&DCtx,
3014 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3015 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003016 int n = CheckCSRMaps(&DCtx);
3017 if(n) {
3018 return n-2000;
3019 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003020
Laurence Lundblade59289e52019-12-30 13:44:37 -08003021 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003022 QCBORDecode_Init(&DCtx,
3023 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3024 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003025
Laurence Lundblade9b334962020-08-27 10:55:53 -07003026 /* With the spiffy decode revision, this tag list is not used.
3027 It doesn't matter if a tag is in this list or not so some
3028 tests that couldn't process a tag because it isn't in this list
3029 now can process these unlisted tags. The tests have been
3030 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003031 const uint64_t puTagList[] = {773, 1, 90599561};
3032 const QCBORTagListIn TagList = {3, puTagList};
3033 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003034
3035
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003036 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3037 return -100;
3038 }
3039 if(Item.uDataType != QCBOR_TYPE_MAP ||
3040 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3041 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3042 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3043 Item.val.uCount != 2 ||
3044 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3045 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3046 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3047 Out.uNumUsed != 3) {
3048 return -101;
3049 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003050
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003051 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3052 return -102;
3053 }
3054 if(Item.uDataType != QCBOR_TYPE_MAP ||
3055 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3056 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003057 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003058 Item.val.uCount != 2 ||
3059 puTags[0] != 5859837686836516696 ||
3060 puTags[1] != 7 ||
3061 Out.uNumUsed != 2) {
3062 return -103;
3063 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003064
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003065 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3066 return -104;
3067 }
3068 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003069 Item.val.uCount != 5 ||
3070 puTags[0] != 0x0b ||
3071 Out.uNumUsed != 1) {
3072 return -105;
3073 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003074
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003075 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3076 return -106;
3077 }
3078 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3079 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3080 Item.val.string.len != 12 ||
3081 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3082 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3083 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3084 Out.uNumUsed != 3) {
3085 return -105;
3086 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003087
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003088 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3089 return -107;
3090 }
3091 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3092 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3093 Item.val.string.len != 3 ||
3094 puTags[0] != 773 ||
3095 Out.uNumUsed != 1) {
3096 return -108;
3097 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003098
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003099 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3100 return -109;
3101 }
3102 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003103 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003104 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003105 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003106 puTags[3] != 7 ||
3107 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003108 return -110;
3109 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003110
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003111 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3112 return -111;
3113 }
3114 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3115 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3116 Item.val.string.len != 9 ||
3117 puTags[0] != 17 ||
3118 Out.uNumUsed != 1) {
3119 return -112;
3120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003121
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003122 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3123 return -111;
3124 }
3125 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3126 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3127 Item.val.string.len != 2 ||
3128 puTags[0] != 17 ||
3129 Out.uNumUsed != 1) {
3130 return -112;
3131 }
3132
3133 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3134 return -113;
3135 }
3136 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003137 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003138 Item.val.uCount != 2 ||
3139 puTags[0] != 19 ||
3140 Out.uNumUsed != 1) {
3141 return -114;
3142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003143
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003144 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3145 return -115;
3146 }
3147 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003148 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003149 Item.val.uCount != 1 ||
3150 puTags[0] != 9 ||
3151 Out.uNumUsed != 1) {
3152 return -116;
3153 }
3154
3155 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3156 return -116;
3157 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003158 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003159 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003160 Out.uNumUsed != 0) {
3161 return -117;
3162 }
3163
3164 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3165 return -118;
3166 }
3167 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3168 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003169 puTags[0] != 12 ||
3170 Out.uNumUsed != 1) {
3171 return -119;
3172 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003173
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003174 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3175 return -120;
3176 }
3177 if(Item.uDataType != QCBOR_TYPE_MAP ||
3178 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3179 Item.val.uCount != 1 ||
3180 puTags[0] != 0x17 ||
3181 Out.uNumUsed != 1) {
3182 return -121;
3183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003184
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003185 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3186 return -122;
3187 }
3188 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003189 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003190 Item.val.int64 != -3 ||
3191 puTags[0] != 8 ||
3192 Out.uNumUsed != 1) {
3193 return -123;
3194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003195
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003196 if(QCBORDecode_Finish(&DCtx)) {
3197 return -124;
3198 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003199
3200 UsefulBufC DateString;
3201 QCBORDecode_Init(&DCtx,
3202 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3203 QCBOR_DECODE_MODE_NORMAL);
3204
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003205 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003206 // tagged date string
3207 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3208 // untagged date string
3209 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3210 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3211 return 100;
3212 }
3213 // untagged byte string
3214 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3215 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3216 return 101;
3217 }
3218 // tagged regex
3219 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3220 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3221 return 102;
3222 }
3223 // tagged date string with a byte string
3224 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3225 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3226 return 103;
3227 }
3228 QCBORDecode_ExitArray(&DCtx);
3229 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3230 return 104;
3231 }
3232
3233
3234 QCBORDecode_Init(&DCtx,
3235 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3236 QCBOR_DECODE_MODE_NORMAL);
3237
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003238 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003239 // tagged date string
3240 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3241 // untagged date string
3242 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3243 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3244 return 200;
3245 }
3246 // untagged byte string
3247 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3248 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3249 return 201;
3250 }
3251 // tagged regex
3252 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3253 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3254 return 202;
3255 }
3256 // tagged date string with a byte string
3257 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3258 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3259 return 203;
3260 }
3261 QCBORDecode_ExitArray(&DCtx);
3262 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3263 return 204;
3264 }
3265
3266 QCBORDecode_Init(&DCtx,
3267 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3268 QCBOR_DECODE_MODE_NORMAL);
3269
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003270 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003271 // tagged date string
3272 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3273 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3274 return 300;
3275 }
3276 // untagged date string
3277 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3278 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3279 return 301;
3280 }
3281 // untagged byte string
3282 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3283 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3284 return 302;
3285 }
3286 // tagged regex
3287 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3288 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3289 return 303;
3290 }
3291 // tagged date string with a byte string
3292 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3293 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3294 return 304;
3295 }
3296 QCBORDecode_ExitArray(&DCtx);
3297 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3298 return 305;
3299 }
3300
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003301 return 0;
3302}
3303
3304
3305
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003306
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003307static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003308 0x83,
3309 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3310 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3311 0xA4,
3312 0x63, 0x42, 0x4E, 0x2B,
3313 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3314 0x18, 0x40,
3315 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3316 0x63, 0x42, 0x4E, 0x2D,
3317 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3318 0x38, 0x3F,
3319 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3320
3321
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003322static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003323
3324
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003325int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003326{
3327 QCBORDecodeContext DCtx;
3328 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003329 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003330
Laurence Lundbladeee851742020-01-08 08:37:05 -08003331 QCBORDecode_Init(&DCtx,
3332 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3333 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003334
3335
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003336 //
3337 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3338 return -1;
3339 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003340 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003342
3343 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003344 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003345 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003346 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003347 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003348 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003349 }
3350
3351 //
3352 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003353 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003354 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003355 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003356 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003357 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003359 //
3360 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003361 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003362 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003363 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003365
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003366 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003367 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003368 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3369 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003370 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003371 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003372 }
3373
3374 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003375 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003376 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3377 Item.uLabelType != QCBOR_TYPE_INT64 ||
3378 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003379 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003380 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003382
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003383 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003384 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003385 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3386 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003387 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003388 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003389 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003390
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003391 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003392 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003393 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3394 Item.uLabelType != QCBOR_TYPE_INT64 ||
3395 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003396 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003397 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003399
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003400 return 0;
3401}
3402
3403
3404
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003405static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003406 uint8_t uDataType,
3407 uint8_t uNestingLevel,
3408 uint8_t uNextNest,
3409 int64_t nLabel,
3410 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003411{
3412 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003413 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003414
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003415 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3416 if(Item.uDataType != uDataType) return -1;
3417 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003418 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3419 Item.uLabelType != QCBOR_TYPE_UINT64) {
3420 return -1;
3421 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003422 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3423 if(Item.label.int64 != nLabel) return -1;
3424 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003425 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003426 }
3427 }
3428 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303429 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003431 if(pItem) {
3432 *pItem = Item;
3433 }
3434 return 0;
3435}
3436
3437
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003438// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003439static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003440{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303441 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003442
Laurence Lundblade9b334962020-08-27 10:55:53 -07003443 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003444
Laurence Lundblade9b334962020-08-27 10:55:53 -07003445 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003446
Laurence Lundblade9b334962020-08-27 10:55:53 -07003447 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3448 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3449 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3450 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3451 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003452
Laurence Lundblade9b334962020-08-27 10:55:53 -07003453 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3454 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003455
Laurence Lundblade9b334962020-08-27 10:55:53 -07003456 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3457 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003458
Laurence Lundblade9b334962020-08-27 10:55:53 -07003459 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3460 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003461
Laurence Lundblade9b334962020-08-27 10:55:53 -07003462 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003463
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003464 return 0;
3465}
3466
3467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003468/*
3469// cbor.me decoded output
3470{
3471 -23: {
3472 -20: {
3473 -18: "Organization",
3474 -17: "SSG",
3475 -15: "Confusion",
3476 -16: "San Diego",
3477 -14: "US"
3478 },
3479 -19: {
3480 -11: {
3481 -9: -7
3482 },
3483 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3484 }
3485 },
3486 -22: {
3487 -5: -3
3488 }
3489}
3490 */
3491
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003492
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003493static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003494 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3495 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3496 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3497 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3498 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3499 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3500 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3501 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3502 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3503
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003504int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003505{
3506 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003507
Laurence Lundbladeee851742020-01-08 08:37:05 -08003508 QCBORDecode_Init(&DCtx,
3509 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3510 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003511
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003512 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003513}
3514
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003515
3516
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003517int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003518{
3519 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003520
Laurence Lundbladeee851742020-01-08 08:37:05 -08003521 QCBORDecode_Init(&DCtx,
3522 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3523 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003524
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003525 QCBORItem Item;
3526 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003527
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003528 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3529 return -1;
3530 }
3531 if(Item.uDataType != QCBOR_TYPE_MAP) {
3532 return -2;
3533 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003534
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003535 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3536 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3537 return -3;
3538 }
3539
3540 return 0;
3541}
3542
3543
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003544// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003545static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003546 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3547 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3548 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3549 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3550 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3551 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003552 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3553 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3554 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3555 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003556
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003557int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003558{
3559 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003560
Laurence Lundbladeee851742020-01-08 08:37:05 -08003561 QCBORDecode_Init(&DCtx,
3562 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3563 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003564
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003565 return CheckCSRMaps(&DCtx);
3566}
3567
3568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003569
Laurence Lundblade17ede402018-10-13 11:43:07 +08003570static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3571{
3572 UsefulOutBuf UOB;
3573 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003574
Laurence Lundblade17ede402018-10-13 11:43:07 +08003575 int i;
3576 for(i = 0; i < n; i++) {
3577 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3578 }
3579
3580 for(i = 0; i < n; i++) {
3581 UsefulOutBuf_AppendByte(&UOB, 0xff);
3582 }
3583 return UsefulOutBuf_OutUBuf(&UOB);
3584}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003585
3586
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003587static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003588{
3589 QCBORDecodeContext DC;
3590 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003591
Laurence Lundblade17ede402018-10-13 11:43:07 +08003592 int j;
3593 for(j = 0; j < nNestLevel; j++) {
3594 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003595 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003596 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3597 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003598 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003599 return -4;
3600 } else {
3601 return 0; // Decoding doesn't recover after an error
3602 }
3603 } else {
3604 // Should be no error
3605 if(nReturn) {
3606 return -9; // Should not have got an error
3607 }
3608 }
3609 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3610 return -7;
3611 }
3612 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003613 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003614 if(nReturn) {
3615 return -3;
3616 }
3617 return 0;
3618}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003619
3620
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003621int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003622{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303623 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003624 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003625 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003626 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003627 int nReturn = parse_indeflen_nested(Nested, i);
3628 if(nReturn) {
3629 return nReturn;
3630 }
3631 }
3632 return 0;
3633}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003634
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003635
Laurence Lundbladeee851742020-01-08 08:37:05 -08003636// [1, [2, 3]]
3637static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3638// No closing break
3639static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3640// Not enough closing breaks
3641static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3642// Too many closing breaks
3643static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3644// Unclosed indeflen inside def len
3645static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3646// confused tag
3647static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003648
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003649int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003650{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003651 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003652 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003653 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003654
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003655 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303656 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003657 QCBORDecodeContext DC;
3658 QCBORItem Item;
3659 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003660
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003661 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003662
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003663 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303664
3665 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3666 Item.uNestingLevel != 0 ||
3667 Item.uNextNestLevel != 1) {
3668 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003670
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003671 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303672 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3673 Item.uNestingLevel != 1 ||
3674 Item.uNextNestLevel != 1) {
3675 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003676 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003677
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003678 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303679 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3680 Item.uNestingLevel != 1 ||
3681 Item.uNextNestLevel != 2) {
3682 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003683 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003684
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003685 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003686 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303687 Item.uNestingLevel != 2 ||
3688 Item.uNextNestLevel != 2) {
3689 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003690 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003691
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003692 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003693 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303694 Item.uNestingLevel != 2 ||
3695 Item.uNextNestLevel != 0) {
3696 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003698
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003699 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303700 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003701 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003702
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003703 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003704 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003705
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003706 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003707
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003708 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003709
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003710 nResult = QCBORDecode_GetNext(&DC, &Item);
3711 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303712 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003713 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003714
Laurence Lundblade570fab52018-10-13 18:28:27 +08003715 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003716 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303717 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003718 }
3719
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003720
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003721 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003722 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003723
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003724 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003725
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003726 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003727
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003728 nResult = QCBORDecode_GetNext(&DC, &Item);
3729 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303730 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003731 }
3732
3733 nResult = QCBORDecode_GetNext(&DC, &Item);
3734 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303735 return -10;
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_GetNext(&DC, &Item);
3739 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303740 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003741 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003742
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003743 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003744 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303745 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003746 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003747
3748
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003749 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003750 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003751
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003752 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003753
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003754 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003755
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003756 nResult = QCBORDecode_GetNext(&DC, &Item);
3757 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303758 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003759 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003760
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003761 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003762 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303763 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003764 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303765
Laurence Lundblade642282a2020-06-23 12:00:33 -07003766 nResult = QCBORDecode_GetNext(&DC, &Item);
3767 if(nResult != QCBOR_ERR_BAD_BREAK) {
3768 return -140;
3769 }
3770
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003771
Laurence Lundblade570fab52018-10-13 18:28:27 +08003772 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003773 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003774
Laurence Lundblade570fab52018-10-13 18:28:27 +08003775 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003776
Laurence Lundblade570fab52018-10-13 18:28:27 +08003777 QCBORDecode_SetMemPool(&DC, MemPool, false);
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 -15;
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_GetNext(&DC, &Item);
3785 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303786 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003787 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003788
Laurence Lundblade570fab52018-10-13 18:28:27 +08003789 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003790 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303791 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003792 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003793
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303794 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003795 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003796
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303797 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003798
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303799 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003800
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303801 nResult = QCBORDecode_GetNext(&DC, &Item);
3802 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303803 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303804 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003805
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303806 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303807 if(nResult != QCBOR_ERR_BAD_BREAK) {
3808 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303809 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003810
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003811 return 0;
3812}
3813
Laurence Lundblade17ede402018-10-13 11:43:07 +08003814
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003815static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003816 0x81, // Array of length one
3817 0x7f, // text string marked with indefinite length
3818 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3819 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3820 0xff // ending break
3821};
3822
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003823static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303824 0x81, // Array of length one
3825 0x7f, // text string marked with indefinite length
3826 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3827 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3828 0xff // ending break
3829};
3830
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003831static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303832 0x81, // Array of length one
3833 0x7f, // text string marked with indefinite length
3834 0x01, 0x02, // Not a string
3835 0xff // ending break
3836};
3837
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003838static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303839 0x81, // Array of length one
3840 0x7f, // text string marked with indefinite length
3841 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3842 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3843 // missing end of string
3844};
3845
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003846static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303847 0xa1, // Array of length one
3848 0x7f, // text string marked with indefinite length
3849 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3850 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3851 0xff, // ending break
3852 0x01 // integer being labeled.
3853};
3854
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003855/**
3856 Make an indefinite length string
3857
3858 @param Storage Storage for string, must be 144 bytes in size
3859 @return The indefinite length string
3860
3861 This makes an array with one indefinite length string that has 7 chunks
3862 from size of 1 byte up to 64 bytes.
3863 */
3864static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303865{
3866 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003867
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303868 UsefulOutBuf_Init(&UOB, Storage);
3869 UsefulOutBuf_AppendByte(&UOB, 0x81);
3870 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003871
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003872 uint8_t uStringByte = 0;
3873 // Use of type int is intentional
3874 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3875 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303876 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003877 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3878 for(int j = 0; j < uChunkSize; j++) {
3879 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3880 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303881 }
3882 }
3883 UsefulOutBuf_AppendByte(&UOB, 0xff);
3884
3885 return UsefulOutBuf_OutUBuf(&UOB);
3886}
3887
3888static int CheckBigString(UsefulBufC BigString)
3889{
3890 if(BigString.len != 255) {
3891 return 1;
3892 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003893
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303894 for(uint8_t i = 0; i < 255; i++){
3895 if(((const uint8_t *)BigString.ptr)[i] != i) {
3896 return 1;
3897 }
3898 }
3899 return 0;
3900}
3901
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303902
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003903int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303904{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303905 QCBORDecodeContext DC;
3906 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303907 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003908 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003909
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303910 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003911 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303912 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003913
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303914 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303915 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303916 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003917
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303918 if(QCBORDecode_GetNext(&DC, &Item)) {
3919 return -2;
3920 }
3921 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3922 return -3;
3923 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003924
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303925 if(QCBORDecode_GetNext(&DC, &Item)) {
3926 return -4;
3927 }
3928 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3929 return -5;
3930 }
3931 if(QCBORDecode_Finish(&DC)) {
3932 return -6;
3933 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303934
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303935 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003936 QCBORDecode_Init(&DC,
3937 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3938 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003939
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303940 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3941 return -7;
3942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003943
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303944 if(QCBORDecode_GetNext(&DC, &Item)) {
3945 return -8;
3946 }
3947 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3948 return -9;
3949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003950
Laurence Lundblade30816f22018-11-10 13:40:22 +07003951 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303952 return -10;
3953 }
3954
3955 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003956 QCBORDecode_Init(&DC,
3957 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3958 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003959
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303960 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3961 return -11;
3962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003963
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303964 if(QCBORDecode_GetNext(&DC, &Item)) {
3965 return -12;
3966 }
3967 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3968 return -13;
3969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003970
Laurence Lundblade30816f22018-11-10 13:40:22 +07003971 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303972 return -14;
3973 }
3974
3975 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003976 QCBORDecode_Init(&DC,
3977 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3978 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003979
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303980 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3981 return -15;
3982 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003983
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303984 if(QCBORDecode_GetNext(&DC, &Item)) {
3985 return -16;
3986 }
3987 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3988 return -17;
3989 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303991 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3992 return -18;
3993 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003994
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303995 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303996 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003997
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303998 QCBORDecode_GetNext(&DC, &Item);
3999 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304000 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004002
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304003 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304004 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004006
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304007 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004008 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304009
4010 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4011 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304012 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304013 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004014
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304015 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304016 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004017 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004018
Laurence Lundbladeee851742020-01-08 08:37:05 -08004019 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4020 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004021
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304022 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304023 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304024 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004026
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304027 QCBORDecode_GetNext(&DC, &Item);
4028 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304029 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304030 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004031 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304032 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004034
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304035 // ---- big bstr -----
4036 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004037
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304038 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4039 return -25;
4040 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004041
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304042 if(QCBORDecode_GetNext(&DC, &Item)) {
4043 return -26;
4044 }
4045 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304046 return -26;
4047 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004048
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304049 if(QCBORDecode_GetNext(&DC, &Item)) {
4050 return -27;
4051 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304052 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304053 return -28;
4054 }
4055 if(CheckBigString(Item.val.string)) {
4056 return -3;
4057 }
4058 if(QCBORDecode_Finish(&DC)) {
4059 return -29;
4060 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004061
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304062 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004063 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004064
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304065 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4066 return -30;
4067 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004068
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304069 QCBORDecode_GetNext(&DC, &Item);
4070 if(Item.uDataType != QCBOR_TYPE_MAP) {
4071 return -31;
4072 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004073
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304074 if(QCBORDecode_GetNext(&DC, &Item)){
4075 return -32;
4076 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004077 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4078 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304079 Item.uDataAlloc || !Item.uLabelAlloc ||
4080 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4081 return -33;
4082 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004083
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304084 if(QCBORDecode_Finish(&DC)) {
4085 return -34;
4086 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004087
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004088 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004089}
4090
4091
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004092int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304093{
4094 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004095 QCBORError nCBORError;
4096
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004097
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304098 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004099 QCBORDecode_Init(&DC,
4100 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4101 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004102
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004103 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004104
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004105 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4106 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304107 return -1;
4108 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004109
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004110 if(CheckCSRMaps(&DC)) {
4111 return -2;
4112 }
4113
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004114 // Next parse, save pointers to a few strings, destroy original and
4115 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004116 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004117 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004118
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304119 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004120 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304121 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004122
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304123 QCBORItem Item1, Item2, Item3, Item4;
4124 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(Item1.uDataType != QCBOR_TYPE_MAP ||
4127 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004128 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304129 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004130 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304131 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004132 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304133 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004134 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304135 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004136 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004137
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304138 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004139
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304140 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304141 Item1.uDataType != QCBOR_TYPE_INT64 ||
4142 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004143 Item1.uDataAlloc != 0 ||
4144 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004145 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004146 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004147 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004148
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304149
4150 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004151 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304152 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004153 Item2.uDataAlloc != 0 ||
4154 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304155 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004156 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004157
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304158 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004159 Item3.uDataAlloc == 0 ||
4160 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004161 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004162 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004164
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304165 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004166 Item4.uDataAlloc == 0 ||
4167 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004168 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004169 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004171
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304172 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004173 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004174 QCBORDecode_Init(&DC,
4175 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4176 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304177 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4178 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004179 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304180 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004181 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004182 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004183 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304184 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4185 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4186 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4187 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4188 }
4189 }
4190 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004191 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004192 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304193 }
4194
4195 return 0;
4196}
4197
Laurence Lundbladef6531662018-12-04 10:42:22 +09004198
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304199
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004200int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004201{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004202 // Set up the decoder with a tiny bit of CBOR to parse because
4203 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004204 QCBORDecodeContext DC;
4205 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4206 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004207
Laurence Lundbladef6531662018-12-04 10:42:22 +09004208 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004209 // Then fish into the internals of the decode context
4210 // to get the allocator function so it can be called directly.
4211 // Also figure out how much pool is available for use
4212 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004213 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004214 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4215 if(nError) {
4216 return -9;
4217 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004218 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4219 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4220 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004221
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004222 // First test -- ask for one more byte than available and see failure
4223 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004224 if(!UsefulBuf_IsNULL(Allocated)) {
4225 return -1;
4226 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004227
Laurence Lundbladef6531662018-12-04 10:42:22 +09004228 // Re do the set up for the next test that will do a successful alloc,
4229 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004230 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004231 pAlloc = DC.StringAllocator.pfAllocator;
4232 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4233 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004234
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004235 // Allocate one byte less than available and see success
4236 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004237 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4238 return -2;
4239 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004240 // Ask for some more and see failure
4241 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004242 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4243 return -3;
4244 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004245 // Free the first allocate, retry the second and see success
4246 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4247 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004248 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4249 return -4;
4250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004251
Laurence Lundbladef6531662018-12-04 10:42:22 +09004252 // Re do set up for next test that involves a successful alloc,
4253 // and a successful realloc and a failed realloc
4254 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004255 pAlloc = DC.StringAllocator.pfAllocator;
4256 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004257
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004258 // Allocate half the pool and see success
4259 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004260 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4261 return -5;
4262 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004263 // Reallocate to take up the whole pool and see success
4264 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004265 if(UsefulBuf_IsNULL(Allocated2)) {
4266 return -6;
4267 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004268 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004269 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4270 return -7;
4271 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004272 // Try to allocate more to be sure there is failure after a realloc
4273 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4274 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004275 return -8;
4276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004277
Laurence Lundbladef6531662018-12-04 10:42:22 +09004278 return 0;
4279}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004280
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004281
4282/* Just enough of an allocator to test configuration of one */
4283static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4284{
4285 (void)pOldMem; // unused variable
4286
4287 if(uNewSize) {
4288 // Assumes the context pointer is the buffer and
4289 // nothing too big will ever be asked for.
4290 // This is only good for this basic test!
4291 return (UsefulBuf) {pCtx, uNewSize};
4292 } else {
4293 return NULLUsefulBuf;
4294 }
4295}
4296
4297
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004298int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004299{
4300 // Set up the decoder with a tiny bit of CBOR to parse because
4301 // nothing can be done with it unless that is set up.
4302 QCBORDecodeContext DC;
4303 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4304 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4305
4306 uint8_t pAllocatorBuffer[50];
4307
4308 // This is really just to test that this call works.
4309 // The full functionality of string allocators is tested
4310 // elsewhere with the MemPool internal allocator.
4311 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4312
4313 QCBORItem Item;
4314 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4315 return -1;
4316 }
4317
4318 if(Item.uDataAlloc == 0 ||
4319 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4320 Item.val.string.ptr != pAllocatorBuffer) {
4321 return -2;
4322 }
4323
4324 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4325 return -3;
4326 }
4327
4328 return 0;
4329}
4330
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004331#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004332
Laurence Lundbladea826c502020-05-10 21:07:00 -07004333/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004334 [
4335 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004336 4([-20, 4759477275222530853136]),
4337 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004338 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004339 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004340 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004341 5([ 9223372036854775806, -4759477275222530853137])
4342 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004343 ]
4344 */
4345
4346static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004347 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004348 0xC4, 0x82, 0x20,
4349 0x03,
4350 0xC4, 0x82, 0x33,
4351 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4352 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4353 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4354 0xC5, 0x82, 0x19, 0x01, 0x2C,
4355 0x18, 0x64,
4356 0xC5, 0x82, 0x33,
4357 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4358 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4359 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004360 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4361 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004362 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4363 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4364};
4365
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004366
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004367int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004368{
4369 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004370 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004371 QCBORItem item;
4372
Laurence Lundblade17af4902020-01-07 19:11:55 -08004373 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4374 0x06, 0x07, 0x08, 0x09, 0x010};
4375 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004376
4377
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004378 QCBORDecode_Init(&DC,
4379 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4380 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004381
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004382 uErr = QCBORDecode_GetNext(&DC, &item);
4383 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004384 return 1;
4385 }
4386
4387 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4388 return 2;
4389 }
4390
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004391 uErr = QCBORDecode_GetNext(&DC, &item);
4392 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004393 return 3;
4394 }
4395
4396 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4397 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4398 item.val.expAndMantissa.nExponent != -1) {
4399 return 4;
4400 }
4401
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004402 uErr = QCBORDecode_GetNext(&DC, &item);
4403 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004404 return 5;
4405 }
4406
4407 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4408 item.val.expAndMantissa.nExponent != -20 ||
4409 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4410 return 6;
4411 }
4412
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004413 uErr = QCBORDecode_GetNext(&DC, &item);
4414 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004415 return 7;
4416 }
4417
4418 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4419 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4420 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4421 return 8;
4422 }
4423
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004424 uErr = QCBORDecode_GetNext(&DC, &item);
4425 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004426 return 9;
4427 }
4428
4429 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4430 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4431 item.val.expAndMantissa.nExponent != 300) {
4432 return 10;
4433 }
4434
Laurence Lundbladea826c502020-05-10 21:07:00 -07004435 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004436 uErr = QCBORDecode_GetNext(&DC, &item);
4437 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004438 return 11;
4439 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004440 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4441 item.val.expAndMantissa.nExponent != -20 ||
4442 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4443 return 12;
4444 }
4445
Laurence Lundbladea826c502020-05-10 21:07:00 -07004446 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004447 uErr = QCBORDecode_GetNext(&DC, &item);
4448 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004449 return 13;
4450 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004451 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4452 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4453 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4454 return 14;
4455 }
4456
Laurence Lundbladea826c502020-05-10 21:07:00 -07004457 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004458 uErr = QCBORDecode_GetNext(&DC, &item);
4459 if(uErr != QCBOR_SUCCESS) {
4460 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004461 }
4462 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4463 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4464 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004465 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004466 }
4467
Laurence Lundbladea826c502020-05-10 21:07:00 -07004468 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004469 uErr = QCBORDecode_GetNext(&DC, &item);
4470 if(uErr != QCBOR_SUCCESS) {
4471 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004472 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004473 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4474 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4475 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004476 return 18;
4477 }
4478
4479 uErr = QCBORDecode_Finish(&DC);
4480 if(uErr != QCBOR_SUCCESS) {
4481 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004482 }
4483
4484 /* Now encode some stuff and then decode it */
4485 uint8_t pBuf[40];
4486 QCBOREncodeContext EC;
4487 UsefulBufC Encoded;
4488
4489 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4490 QCBOREncode_OpenArray(&EC);
4491 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4492 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4493 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4494 QCBOREncode_CloseArray(&EC);
4495 QCBOREncode_Finish(&EC, &Encoded);
4496
4497
4498 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004499 uErr = QCBORDecode_GetNext(&DC, &item);
4500 if(uErr != QCBOR_SUCCESS) {
4501 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004502 }
4503
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004504 uErr = QCBORDecode_GetNext(&DC, &item);
4505 if(uErr != QCBOR_SUCCESS) {
4506 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004507 }
4508
4509 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4510 item.val.expAndMantissa.nExponent != 1000 ||
4511 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004512 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004513 }
4514
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004515 uErr = QCBORDecode_GetNext(&DC, &item);
4516 if(uErr != QCBOR_SUCCESS) {
4517 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004518 }
4519
4520 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4521 item.val.expAndMantissa.nExponent != INT32_MIN ||
4522 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004523 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004524 }
4525
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004526 uErr = QCBORDecode_GetNext(&DC, &item);
4527 if(uErr != QCBOR_SUCCESS) {
4528 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004529 }
4530
4531 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4532 item.val.expAndMantissa.nExponent != INT32_MAX ||
4533 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004534 return 106;
4535 }
4536
4537
4538 int64_t nExp, nMant;
4539 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4540 UsefulBufC Mant;
4541 bool bIsNeg;
4542
4543 QCBORDecode_Init(&DC,
4544 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4545 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004546 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004547
4548 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004549 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004550
4551 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004552 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4553 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004554
4555 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004556 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4557 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004558
4559 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004560 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004561
4562 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004563 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4564 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004565
4566 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004567 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4568 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004569
4570 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004571 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4572 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004573
4574 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004575 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4576 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004577
4578 QCBORDecode_ExitArray(&DC);
4579
4580 uErr = QCBORDecode_Finish(&DC);
4581 if(uErr != QCBOR_SUCCESS) {
4582 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004583 }
4584
4585 return 0;
4586}
4587
4588
4589static struct FailInput ExponentAndMantissaFailures[] = {
4590 // Exponent > INT64_MAX
4591 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4592 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4593 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4594 // Mantissa > INT64_MAX
4595 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4596 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4597 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4598 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004599 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004600 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004601 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004602 // bad content for big num
4603 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4604 // bad content for big num
4605 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4606 // Bad integer for exponent
4607 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4608 // Bad integer for mantissa
4609 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4610 // 3 items in array
4611 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4612 // unterminated indefinite length array
4613 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4614 // Empty array
4615 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4616 // Second is not an integer
4617 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4618 // First is not an integer
4619 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4620 // Not an array
4621 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4622};
4623
4624
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004625int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004626{
4627 return ProcessFailures(ExponentAndMantissaFailures,
4628 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4629}
4630
4631#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004632
4633
4634
4635/*
4636 Some basic CBOR with map and array used in a lot of tests.
4637 The map labels are all strings
4638
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004639 {
4640 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004641 "an array of two strings": [
4642 "string1", "string2"
4643 ],
4644 "map in a map": {
4645 "bytes 1": h'78787878',
4646 "bytes 2": h'79797979',
4647 "another int": 98,
4648 "text 2": "lies, damn lies and statistics"
4649 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004650 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004651 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004652
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004653int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004654{
4655 QCBORItem Item1, Item2, Item3;
4656 int64_t nDecodedInt1, nDecodedInt2;
4657 UsefulBufC B1, B2, S1, S2, S3;
4658
4659 QCBORDecodeContext DCtx;
4660 QCBORError nCBORError;
4661
4662 QCBORDecode_Init(&DCtx, input, 0);
4663
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004664 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004665
4666 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4667
4668 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4669 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004670 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4671 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4672 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004673 QCBORDecode_ExitMap(&DCtx);
4674
4675 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4676 QCBORDecode_GetNext(&DCtx, &Item1);
4677 QCBORDecode_GetNext(&DCtx, &Item2);
4678 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4679 return -400;
4680 }
4681 QCBORDecode_ExitArray(&DCtx);
4682
4683 // Parse the same array again using GetText() instead of GetItem()
4684 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004685 QCBORDecode_GetTextString(&DCtx, &S2);
4686 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004687 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4688 return 5000;
4689 }
4690 /* QCBORDecode_GetText(&DCtx, &S3);
4691 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4692 return 5001;
4693 } */
4694
4695 QCBORDecode_ExitArray(&DCtx);
4696
4697 QCBORDecode_ExitMap(&DCtx);
4698
4699 nCBORError = QCBORDecode_Finish(&DCtx);
4700
4701 if(nCBORError) {
4702 return (int32_t)nCBORError;
4703 }
4704
4705 if(nDecodedInt1 != 42) {
4706 return 1001;
4707 }
4708
4709 if(nDecodedInt2 != 98) {
4710 return 1002;
4711 }
4712
4713 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004714 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004715 return 1003;
4716 }
4717
4718 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004719 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004720 return 1004;
4721 }
4722
Laurence Lundblade9b334962020-08-27 10:55:53 -07004723 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004724 return 1005;
4725 }
4726
4727 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4728 return 1006;
4729 }
4730
4731 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4732 return 1007;
4733 }
4734
4735 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4736 return 1008;
4737 }
4738
4739 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4740 return 1009;
4741 }
4742
4743 return 0;
4744}
4745
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004746/*
4747 {
4748 -75008: h'05083399',
4749 88: [
4750 ],
4751 100100: {
4752 "sub1": {
4753 10: [
4754 0
4755 ],
4756 -75009: h'A46823990001',
4757 100100: {
4758 "json": "{ \"ueid\", \"xyz\"}",
4759 "subsub": {
4760 100002: h'141813191001'
4761 }
4762 }
4763 }
4764 }
4765 }
4766 */
4767
4768static const uint8_t spNestedCBOR[] = {
47690xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
47700x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
47710x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
47720x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
47730x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
47740x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, 0x01
4775};
4776
4777/* Get item in multi-level nesting in spNestedCBOR */
4778static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4779{
4780 UsefulBufC String;
4781
4782 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4783 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4784
4785 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4786 QCBORDecode_EnterMap(pDCtx, NULL);
4787 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4788 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4789 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4790 if(QCBORDecode_GetError(pDCtx)) {
4791 return 4001;
4792 }
4793 if(UsefulBuf_Compare(String, test_oemid)) {
4794 return 4002;
4795 }
4796 QCBORDecode_ExitMap(pDCtx);
4797 QCBORDecode_ExitMap(pDCtx);
4798 QCBORDecode_ExitMap(pDCtx);
4799 QCBORDecode_ExitMap(pDCtx);
4800
4801 return 0;
4802}
4803
4804/* Iterations on the zero-length array in spNestedCBOR */
4805static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4806{
4807 QCBORItem Item;
4808 QCBORError uErr;
4809
4810 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4811 for(int x = 0; x < 20; x++) {
4812 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4813 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4814 return 4100;
4815
4816 }
4817 }
4818 QCBORDecode_ExitArray(pDCtx);
4819 if(QCBORDecode_GetError(pDCtx)) {
4820 return 4101;
4821 }
4822
4823 return 0;
4824}
4825
4826/* Various iterations on the array that contains a zero in spNestedCBOR */
4827static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
4828{
4829 QCBORError uErr;
4830
4831 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4832 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
4833 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4834 int64_t nInt = 99;
4835 QCBORDecode_GetInt64(pDCtx, &nInt);
4836 if(nInt != 0) {
4837 return 4200;
4838 }
4839 for(int x = 0; x < 20; x++) {
4840 QCBORItem Item;
4841 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4842 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4843 return 4201;
4844
4845 }
4846 }
4847 QCBORDecode_ExitArray(pDCtx);
4848 if(QCBORDecode_GetAndResetError(pDCtx)) {
4849 return 4202;
4850 }
4851 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4852 UsefulBufC dD;
4853 QCBORDecode_GetByteString(pDCtx, &dD);
4854 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4855 return 4203;
4856 }
4857 for(int x = 0; x < 20; x++) {
4858 QCBORDecode_GetByteString(pDCtx, &dD);
4859 uErr = QCBORDecode_GetAndResetError(pDCtx);
4860 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4861 return 4204;
4862 }
4863 }
4864 QCBORDecode_ExitArray(pDCtx);
4865 QCBORDecode_ExitMap(pDCtx);
4866 QCBORDecode_ExitMap(pDCtx);
4867
4868 return 0;
4869}
4870
4871/* Repeatedly enter and exit maps and arrays, go off the end of maps
4872 and arrays and such. */
4873static int32_t DecodeNestedIterate()
4874{
4875 QCBORDecodeContext DCtx;
4876 int32_t nReturn;
4877 QCBORError uErr;
4878
4879 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
4880 QCBORDecode_EnterMap(&DCtx, NULL);
4881
4882 for(int j = 0; j < 5; j++) {
4883 for(int i = 0; i < 20; i++) {
4884 nReturn = DecodeNestedGetSubSub(&DCtx);
4885 if(nReturn) {
4886 return nReturn;
4887 }
4888 }
4889
4890 for(int i = 0; i < 20; i++) {
4891 nReturn = DecodeNestedGetEmpty(&DCtx);
4892 if(nReturn ) {
4893 return nReturn;
4894 }
4895 }
4896
4897 for(int i = 0; i < 20; i++) {
4898 nReturn = DecodeNestedGetZero(&DCtx);
4899 if(nReturn ) {
4900 return nReturn;
4901 }
4902 }
4903 }
4904
4905 QCBORDecode_ExitMap(&DCtx);
4906 uErr = QCBORDecode_Finish(&DCtx);
4907 if(uErr) {
4908 return (int32_t)uErr + 4100;
4909 }
4910
4911 return 0;
4912}
4913
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004914
4915/*
4916 [23,
4917 6000,
4918 h'67616C6163746963',
4919 h'686176656E20746F6B656E'
4920 ]
4921 */
4922static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004923 0x84,
4924 0x17,
4925 0x19, 0x17, 0x70,
4926 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4927 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004928
4929
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004930static const uint8_t spEmptyMap[] = {0xa0};
4931
4932static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004933
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004934static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4935
Laurence Lundbladef0499502020-08-01 11:55:57 -07004936/*
4937 {
4938 0: [],
4939 9: [
4940 [],
4941 []
4942 ],
4943 8: {
4944 1: [],
4945 2: {},
4946 3: []
4947 },
4948 4: {},
4949 5: [],
4950 6: [
4951 [],
4952 []
4953 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004954 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004955 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004956
Laurence Lundbladef0499502020-08-01 11:55:57 -07004957static const uint8_t spMapOfEmpty[] = {
4958 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4959 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4960 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004961
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004962/*
4963 Too many tags
4964 Invalid tag content
4965 Duplicate label
4966 Integer overflow
4967 Date overflow
4968
4969 {1: 224(225(226(227(4(0))))),
4970 2: 1(h''),
4971 3: -18446744073709551616,
4972 4: 1(1.0e+300),
4973 5: 0, 8: 8}
4974 */
4975static const uint8_t spRecoverableMapErrors[] = {
4976 0xbf,
4977 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
4978 0x02, 0xc1, 0x40,
4979 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4980 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
4981 0x05, 0x00,
4982 0x05, 0x00,
4983 0x08, 0x08,
4984 0xff
4985};
4986
4987// Bad break
4988static const uint8_t spUnRecoverableMapError1[] = {
4989 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
4990};
4991
4992// No more items
4993static const uint8_t spUnRecoverableMapError2[] = {
4994 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
4995};
4996
4997// Hit end because string is too long
4998static const uint8_t spUnRecoverableMapError3[] = {
4999 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5000};
5001
5002// Hit end because string is too long
5003static const uint8_t spUnRecoverableMapError4[] = {
5004 0xbf,
5005 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5006 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5007 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5008 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5009 0xff
5010};
5011
5012
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005013int32_t EnterMapTest()
5014{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005015 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005016 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005017 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005018 int32_t nReturn;
5019 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005020
5021
5022 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005023 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005024
Laurence Lundbladef0499502020-08-01 11:55:57 -07005025
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005026 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005027 QCBORDecode_ExitArray(&DCtx);
5028
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005029 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5030 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005031 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005032 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005033 QCBORDecode_ExitArray(&DCtx);
5034 QCBORDecode_ExitArray(&DCtx);
5035
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005036 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5037 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005038 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005039 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005040 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005041 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005042 QCBORDecode_ExitArray(&DCtx);
5043 QCBORDecode_ExitMap(&DCtx);
5044
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005045 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005046 QCBORDecode_ExitMap(&DCtx);
5047
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005048 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005049 QCBORDecode_ExitArray(&DCtx);
5050
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005051 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5052 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005053 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005054 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005055 QCBORDecode_ExitArray(&DCtx);
5056 QCBORDecode_ExitArray(&DCtx);
5057
5058 QCBORDecode_ExitMap(&DCtx);
5059
5060 uErr = QCBORDecode_Finish(&DCtx);
5061 if(uErr != QCBOR_SUCCESS){
5062 return 3011;
5063 }
5064
5065
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005066 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005067 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005068 if(nReturn) {
5069 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005070 }
5071
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005072 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005073 if(nReturn) {
5074 return nReturn;
5075 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005076
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005077
Laurence Lundblade937ea812020-05-08 11:38:23 -07005078
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005079 // These tests confirm the cursor is at the right place after entering
5080 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005081 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005082
5083 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005084 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005085 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005086 QCBORDecode_GetNext(&DCtx, &Item1);
5087 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5088 return 2001;
5089 }
5090
5091
Laurence Lundblade9b334962020-08-27 10:55:53 -07005092 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005093 QCBORDecode_VGetNext(&DCtx, &Item1);
5094 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005095 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5096 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5097 UsefulBuf_Compare(ArrayItem.label.string,
5098 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5099 return 2051;
5100 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005101 QCBORDecode_GetNext(&DCtx, &Item1);
5102 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5103 return 2002;
5104 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005105 QCBORDecode_ExitArray(&DCtx);
5106 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5107 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5108 UsefulBuf_Compare(ArrayItem.label.string,
5109 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5110 return 2052;
5111 }
5112
Laurence Lundblade937ea812020-05-08 11:38:23 -07005113
Laurence Lundblade9b334962020-08-27 10:55:53 -07005114 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005115 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005116 QCBORDecode_GetNext(&DCtx, &Item1);
5117 QCBORDecode_GetNext(&DCtx, &Item1);
5118 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005119 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5120 QCBORDecode_GetNext(&DCtx, &Item1);
5121 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5122 return 2003;
5123 }
5124
Laurence Lundblade9b334962020-08-27 10:55:53 -07005125 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005126 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005127 QCBORDecode_GetNext(&DCtx, &Item1);
5128 QCBORDecode_GetNext(&DCtx, &Item1);
5129 QCBORDecode_GetNext(&DCtx, &Item1);
5130 QCBORDecode_GetNext(&DCtx, &Item1);
5131 QCBORDecode_GetNext(&DCtx, &Item1);
5132 QCBORDecode_GetNext(&DCtx, &Item1);
5133 QCBORDecode_GetNext(&DCtx, &Item1);
5134 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5135 QCBORDecode_GetNext(&DCtx, &Item1);
5136 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005137 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005138 }
5139
Laurence Lundblade9b334962020-08-27 10:55:53 -07005140 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005141 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005142 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5143 QCBORDecode_ExitArray(&DCtx);
5144 QCBORDecode_GetNext(&DCtx, &Item1);
5145 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5146 return 2006;
5147 }
5148 QCBORDecode_ExitMap(&DCtx);
5149 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5150 return 2007;
5151 }
5152
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005153 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005154 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005155 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005156 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5157 uErr = QCBORDecode_GetAndResetError(&DCtx);
5158 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005159 return 2008;
5160 }
5161 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005162 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005163 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005164 return 2009;
5165 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005166
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005167
5168 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005169 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005170 // This will fail because the map is empty.
5171 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5172 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005173 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005174 return 2010;
5175 }
5176 QCBORDecode_ExitMap(&DCtx);
5177 uErr = QCBORDecode_Finish(&DCtx);
5178 if(uErr != QCBOR_SUCCESS){
5179 return 2011;
5180 }
5181
5182
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005183 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005184 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005185 // This will fail because the map is empty.
5186 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5187 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005188 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005189 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005190 }
5191 QCBORDecode_ExitMap(&DCtx);
5192 uErr = QCBORDecode_Finish(&DCtx);
5193 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005194 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005195 }
5196
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005197
5198 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005199 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005200 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005201 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005202 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005203 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005204 QCBORDecode_ExitArray(&DCtx);
5205 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5206 QCBORDecode_ExitArray(&DCtx);
5207 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005208 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005209 return 2014;
5210 }
5211
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005212 int64_t nInt;
5213 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005214 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005215 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005216 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005217 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5218 return 2021;
5219 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005220 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5221 return 2121;
5222 }
5223 (void)QCBORDecode_GetAndResetError(&DCtx);
5224
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005225
5226 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5227 uErr = QCBORDecode_GetAndResetError(&DCtx);
5228 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5229 return 2022;
5230 }
5231
5232 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5233 uErr = QCBORDecode_GetAndResetError(&DCtx);
5234 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5235 return 2023;
5236 }
5237
5238 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5239 uErr = QCBORDecode_GetAndResetError(&DCtx);
5240#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5241 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5242 return 2024;
5243 }
5244#else
5245 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5246 return 2027;
5247 }
5248#endif
5249
5250 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5251 uErr = QCBORDecode_GetAndResetError(&DCtx);
5252 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5253 return 2025;
5254 }
5255
5256 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5257
5258 QCBORDecode_ExitMap(&DCtx);
5259 uErr = QCBORDecode_Finish(&DCtx);
5260 if(uErr != QCBOR_SUCCESS) {
5261 return 2026;
5262 }
5263
5264 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005265 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005266 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5267 uErr = QCBORDecode_GetAndResetError(&DCtx);
5268 if(uErr != QCBOR_ERR_BAD_BREAK) {
5269 return 2030;
5270 }
5271
5272 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005273 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005274 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5275 uErr = QCBORDecode_GetAndResetError(&DCtx);
5276 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5277 return 2031;
5278 }
5279
5280 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005281 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005282 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5283 uErr = QCBORDecode_GetAndResetError(&DCtx);
5284 if(uErr != QCBOR_ERR_HIT_END) {
5285 return 2032;
5286 }
5287
5288 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005289 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005290 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5291 uErr = QCBORDecode_GetAndResetError(&DCtx);
5292 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5293 return 2033;
5294 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005295
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005296 nReturn = DecodeNestedIterate();
5297
5298 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005299}
5300
5301
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005302struct NumberConversion {
5303 char *szDescription;
5304 UsefulBufC CBOR;
5305 int64_t nConvertedToInt64;
5306 QCBORError uErrorInt64;
5307 uint64_t uConvertToUInt64;
5308 QCBORError uErrorUint64;
5309 double dConvertToDouble;
5310 QCBORError uErrorDouble;
5311};
5312
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005313static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005314 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005315 "too large to fit into int64_t",
5316 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5317 0,
5318 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5319 0,
5320 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5321 ((double)INT64_MIN) + 1 ,
5322 QCBOR_SUCCESS
5323 },
5324 {
5325 "largest negative int that fits in int64_t",
5326 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5327 INT64_MIN,
5328 QCBOR_SUCCESS,
5329 0,
5330 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5331 (double)INT64_MIN,
5332 QCBOR_SUCCESS
5333 },
5334 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005335 "negative bignum -1",
5336 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5337 -1,
5338 QCBOR_SUCCESS,
5339 0,
5340 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5341 -1.0,
5342 QCBOR_SUCCESS
5343 },
5344 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005345 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005346 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5347 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005348#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005349 257000,
5350 QCBOR_SUCCESS,
5351 257000,
5352 QCBOR_SUCCESS,
5353 257000.0,
5354 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005355#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5356 0,
5357 QCBOR_ERR_UNEXPECTED_TYPE,
5358 0,
5359 QCBOR_ERR_UNEXPECTED_TYPE,
5360 0.0,
5361 QCBOR_ERR_UNEXPECTED_TYPE
5362#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005363 },
5364 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005365 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005366 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5367 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005368#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005369 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005370 QCBOR_SUCCESS,
5371 0,
5372 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005373 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005374 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005375#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5376 0,
5377 QCBOR_ERR_UNEXPECTED_TYPE,
5378 0,
5379 QCBOR_ERR_UNEXPECTED_TYPE,
5380 0.0,
5381 QCBOR_ERR_UNEXPECTED_TYPE
5382#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005383 },
5384 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005385 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005386 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5387 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005388#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005389 2056,
5390 QCBOR_SUCCESS,
5391 2056,
5392 QCBOR_SUCCESS,
5393 2056.0,
5394 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005395#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5396 0,
5397 QCBOR_ERR_UNEXPECTED_TYPE,
5398 0,
5399 QCBOR_ERR_UNEXPECTED_TYPE,
5400 0.0,
5401 QCBOR_ERR_UNEXPECTED_TYPE
5402#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005403 },
5404 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005405 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005406 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5407 0,
5408 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5409 0,
5410 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5411 -18446744073709551617.0,
5412 QCBOR_SUCCESS
5413 },
5414 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005415 "Positive bignum 0x01020304 indefinite length string",
5416 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5417 0x01020304,
5418 QCBOR_SUCCESS,
5419 0x01020304,
5420 QCBOR_SUCCESS,
5421 16909060.0,
5422 QCBOR_SUCCESS
5423 },
5424 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005425 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005426 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5427 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005428#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005429 0,
5430 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5431 0,
5432 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5433 -INFINITY,
5434 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005435#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5436 0,
5437 QCBOR_ERR_UNEXPECTED_TYPE,
5438 0,
5439 QCBOR_ERR_UNEXPECTED_TYPE,
5440 0.0,
5441 QCBOR_ERR_UNEXPECTED_TYPE
5442#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005443 },
5444 {
5445 "big float [9223372036854775806, 9223372036854775806]",
5446 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5447 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005448#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005449 0,
5450 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5451 0,
5452 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5453 INFINITY,
5454 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005455#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5456 0,
5457 QCBOR_ERR_UNEXPECTED_TYPE,
5458 0,
5459 QCBOR_ERR_UNEXPECTED_TYPE,
5460 0.0,
5461 QCBOR_ERR_UNEXPECTED_TYPE
5462#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005463 },
5464 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005465 "Big float 3 * 2^^2",
5466 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005467#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005468 12,
5469 QCBOR_SUCCESS,
5470 12,
5471 QCBOR_SUCCESS,
5472 12.0,
5473 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005474#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5475 0,
5476 QCBOR_ERR_UNEXPECTED_TYPE,
5477 0,
5478 QCBOR_ERR_UNEXPECTED_TYPE,
5479 0.0,
5480 QCBOR_ERR_UNEXPECTED_TYPE
5481#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005482 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005483 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005484 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005485 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5486 0,
5487 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5488 18446744073709551615ULL,
5489 QCBOR_SUCCESS,
5490 18446744073709551615.0,
5491 QCBOR_SUCCESS
5492 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005493 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005494 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005495 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5496 65536-1,
5497 QCBOR_SUCCESS,
5498 0xffff,
5499 QCBOR_SUCCESS,
5500 65535.0,
5501 QCBOR_SUCCESS
5502 },
5503 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005504 "Postive integer 0",
5505 {(uint8_t[]){0x0}, 1},
5506 0LL,
5507 QCBOR_SUCCESS,
5508 0ULL,
5509 QCBOR_SUCCESS,
5510 0.0,
5511 QCBOR_SUCCESS
5512 },
5513 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005514 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005515 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5516 -9223372036854775807-1, // INT64_MIN
5517 QCBOR_SUCCESS,
5518 0ULL,
5519 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5520 -9223372036854775808.0,
5521 QCBOR_SUCCESS
5522 },
5523 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005524 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005525 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005526#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005527 100L,
5528 QCBOR_SUCCESS,
5529 100ULL,
5530 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005531#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5532 0,
5533 QCBOR_ERR_HW_FLOAT_DISABLED,
5534 0,
5535 QCBOR_ERR_HW_FLOAT_DISABLED,
5536#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005537 100.3,
5538 QCBOR_SUCCESS
5539 },
5540 {
5541 "Floating point value NaN 0xfa7fc00000",
5542 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005543#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005544 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005545 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005546 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005547 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005548#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5549 0,
5550 QCBOR_ERR_HW_FLOAT_DISABLED,
5551 0,
5552 QCBOR_ERR_HW_FLOAT_DISABLED,
5553#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005554 NAN,
5555 QCBOR_SUCCESS
5556 },
5557 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005558 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005559 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005560#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5561#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5562 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005563 -4,
5564 QCBOR_SUCCESS,
5565 0,
5566 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5567 -4.0,
5568 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005569#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5570 // Float HW disabled
5571 -4,
5572 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5573 0,
5574 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5575 -4.0,
5576 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5577#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5578#else
5579 // Half-precision disabled
5580 -4,
5581 QCBOR_ERR_HALF_PRECISION_DISABLED,
5582 0,
5583 QCBOR_ERR_HALF_PRECISION_DISABLED,
5584 -4.0,
5585 QCBOR_ERR_HALF_PRECISION_DISABLED
5586#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005587 },
5588 {
5589 "Decimal fraction 3/10",
5590 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005591#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005592 0,
5593 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5594 0,
5595 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5596 0.30000000000000004,
5597 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005598#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5599 0,
5600 QCBOR_ERR_UNEXPECTED_TYPE,
5601 0,
5602 QCBOR_ERR_UNEXPECTED_TYPE,
5603 0.0,
5604 QCBOR_ERR_UNEXPECTED_TYPE
5605#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005606 },
5607 {
5608 "+inifinity",
5609 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5610#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5611 0,
5612 QCBOR_ERR_FLOAT_EXCEPTION,
5613 0,
5614 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5615#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5616 0,
5617 QCBOR_ERR_HW_FLOAT_DISABLED,
5618 0,
5619 QCBOR_ERR_HW_FLOAT_DISABLED,
5620#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5621 INFINITY,
5622 QCBOR_SUCCESS
5623 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005624
5625 {
5626 "extreme pos bignum",
5627 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5628 // 50 rows of 8 is 400 digits.
5629 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5630 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5631 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5632 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5633 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5634 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5635 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5636 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5637 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5638 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5639 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5640 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5641 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5642 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5643 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5644 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5645 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5646 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5647 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5648 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5649 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5650 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5651 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5652 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5653 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5654 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5655 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5656 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5657 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5658 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5659 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5660 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5661 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5662 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5663 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5664 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5665 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5666 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5667 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5668 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5669 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5670 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5671 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5672 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5673 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5674 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5675 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5676 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5677 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5678 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5679 404},
5680 0,
5681 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5682 0,
5683 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5684#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5685 INFINITY,
5686 QCBOR_SUCCESS
5687#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5688 0,
5689 QCBOR_ERR_HW_FLOAT_DISABLED,
5690#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5691 },
5692
5693 {
5694 "extreme neg bignum",
5695 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5696 // 50 rows of 8 is 400 digits.
5697 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5698 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5699 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5700 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5701 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5702 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5703 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5704 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5705 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5706 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5707 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5708 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5709 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5710 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5711 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5712 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5713 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5714 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5715 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5716 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5717 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5718 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5719 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5720 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5721 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5722 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5723 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5724 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5725 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5726 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5727 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5728 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5729 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5730 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5731 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5732 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5733 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5734 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5735 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5736 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5737 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5738 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5739 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5740 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5741 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5742 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5743 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5744 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5745 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5746 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5747 404},
5748 0,
5749 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5750 0,
5751 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5752#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5753 -INFINITY,
5754 QCBOR_SUCCESS
5755#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5756 0,
5757 QCBOR_ERR_HW_FLOAT_DISABLED,
5758#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5759 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005760
5761 {
5762 "big float underflow [9223372036854775806, -9223372036854775806]",
5763 {(uint8_t[]){
5764 0xC5, 0x82,
5765 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5766 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5767#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5768 0,
5769 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5770 0,
5771 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5772 0,
5773 QCBOR_SUCCESS
5774#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5775 0,
5776 QCBOR_ERR_UNEXPECTED_TYPE,
5777 0,
5778 QCBOR_ERR_UNEXPECTED_TYPE,
5779 0.0,
5780 QCBOR_ERR_UNEXPECTED_TYPE
5781#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5782 },
5783
5784 {
5785 "bigfloat that evaluates to -INFINITY",
5786 {(uint8_t[]){
5787 0xC5, 0x82,
5788 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5789 0xC3, 0x42, 0x01, 0x01}, 15},
5790#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5791 0,
5792 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5793 0,
5794 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5795 -INFINITY,
5796 QCBOR_SUCCESS
5797#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5798 0,
5799 QCBOR_ERR_UNEXPECTED_TYPE,
5800 0,
5801 QCBOR_ERR_UNEXPECTED_TYPE,
5802 0.0,
5803 QCBOR_ERR_UNEXPECTED_TYPE
5804#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5805 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005806};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005807
5808
5809
Laurence Lundblade313b2862020-05-16 01:23:06 -07005810int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005811{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005812 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005813
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005814 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5815 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005816
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005817 // Set up the decoding context including a memory pool so that
5818 // indefinite length items can be checked
5819 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005820 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005821
5822 /* ----- test conversion to int64_t ------ */
5823 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005824 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5825 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005826 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005827 }
5828
5829 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005830 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005831 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005832 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005833 }
5834 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005835 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005836 }
5837
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005838 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005839 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5840 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5841 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005842 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005843 }
5844 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005845 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005846 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005847 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005848 }
5849 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005850 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005851 }
5852
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005853 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005854 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5855 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5856 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005857 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005858 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005859#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005860 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005861 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005862 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005863 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005864 }
5865 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5866 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005867 // NaN's can't be compared for equality. A NaN is
5868 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005869 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005870 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005871 }
5872 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005873 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005874 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005875 }
5876 }
5877 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005878#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005879 }
5880
5881 return 0;
5882}
5883
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005884
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005885
5886
Laurence Lundbladee3553422020-05-02 11:11:17 -07005887int32_t CBORSequenceDecodeTests(void)
5888{
5889 QCBORDecodeContext DCtx;
5890 QCBORItem Item;
5891 QCBORError uCBORError;
5892
5893 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005894
Laurence Lundbladee3553422020-05-02 11:11:17 -07005895 // The input for the date test happens to be a sequence so it
5896 // is reused. It is a sequence because it doesn't start as
5897 // an array or map.
5898 QCBORDecode_Init(&DCtx,
5899 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5900 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005901
Laurence Lundbladee3553422020-05-02 11:11:17 -07005902 // Get the first item
5903 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5904 if(uCBORError != QCBOR_SUCCESS) {
5905 return 1;
5906 }
5907 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5908 return 2;
5909 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005910
Laurence Lundbladee3553422020-05-02 11:11:17 -07005911 // Get a second item
5912 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005913 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5914 return 66;
5915 }
5916
5917 // Get a third item
5918 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005919 if(uCBORError != QCBOR_SUCCESS) {
5920 return 2;
5921 }
5922 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5923 return 3;
5924 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005925
Laurence Lundbladee3553422020-05-02 11:11:17 -07005926 // A sequence can have stuff at the end that may
5927 // or may not be valid CBOR. The protocol decoder knows
5928 // when to stop by definition of the protocol, not
5929 // when the top-level map or array is ended.
5930 // Finish still has to be called to know that
5931 // maps and arrays (if there were any) were closed
5932 // off correctly. When called like this it
5933 // must return the error QCBOR_ERR_EXTRA_BYTES.
5934 uCBORError = QCBORDecode_Finish(&DCtx);
5935 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5936 return 4;
5937 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005938
5939
Laurence Lundbladee3553422020-05-02 11:11:17 -07005940 // --- Test an empty input ----
5941 uint8_t empty[1];
5942 UsefulBufC Empty = {empty, 0};
5943 QCBORDecode_Init(&DCtx,
5944 Empty,
5945 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005946
Laurence Lundbladee3553422020-05-02 11:11:17 -07005947 uCBORError = QCBORDecode_Finish(&DCtx);
5948 if(uCBORError != QCBOR_SUCCESS) {
5949 return 5;
5950 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005951
5952
Laurence Lundbladee3553422020-05-02 11:11:17 -07005953 // --- Sequence with unclosed indefinite length array ---
5954 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005955
Laurence Lundbladee3553422020-05-02 11:11:17 -07005956 QCBORDecode_Init(&DCtx,
5957 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5958 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005959
Laurence Lundbladee3553422020-05-02 11:11:17 -07005960 // Get the first item
5961 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5962 if(uCBORError != QCBOR_SUCCESS) {
5963 return 7;
5964 }
5965 if(Item.uDataType != QCBOR_TYPE_INT64) {
5966 return 8;
5967 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005968
Laurence Lundbladee3553422020-05-02 11:11:17 -07005969 // Get a second item
5970 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5971 if(uCBORError != QCBOR_SUCCESS) {
5972 return 9;
5973 }
5974 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5975 return 10;
5976 }
5977
5978 // Try to finish before consuming all bytes to confirm
5979 // that the still-open error is returned.
5980 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005981 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005982 return 11;
5983 }
5984
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005985
Laurence Lundbladee3553422020-05-02 11:11:17 -07005986 // --- Sequence with a closed indefinite length array ---
5987 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005988
Laurence Lundbladee3553422020-05-02 11:11:17 -07005989 QCBORDecode_Init(&DCtx,
5990 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5991 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005992
Laurence Lundbladee3553422020-05-02 11:11:17 -07005993 // Get the first item
5994 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5995 if(uCBORError != QCBOR_SUCCESS) {
5996 return 12;
5997 }
5998 if(Item.uDataType != QCBOR_TYPE_INT64) {
5999 return 13;
6000 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006001
Laurence Lundbladee3553422020-05-02 11:11:17 -07006002 // Get a second item
6003 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6004 if(uCBORError != QCBOR_SUCCESS) {
6005 return 14;
6006 }
6007 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6008 return 15;
6009 }
6010
6011 // Try to finish before consuming all bytes to confirm
6012 // that the still-open error is returned.
6013 uCBORError = QCBORDecode_Finish(&DCtx);
6014 if(uCBORError != QCBOR_SUCCESS) {
6015 return 16;
6016 }
6017
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006018
Laurence Lundbladee3553422020-05-02 11:11:17 -07006019 return 0;
6020}
6021
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006022
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006023
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006024int32_t IntToTests()
6025{
6026 int nErrCode;
6027 int32_t n32;
6028 int16_t n16;
6029 int8_t n8;
6030 uint32_t u32;
6031 uint16_t u16;
6032 uint8_t u8;
6033 uint64_t u64;
6034
6035 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6036 if(nErrCode == -1 || n32 != 1) {
6037 return 1;
6038 }
6039
6040 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6041 if(nErrCode == -1 || n32 != INT32_MAX) {
6042 return 2;
6043 }
6044
6045 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6046 if(nErrCode == -1 || n32 != INT32_MIN) {
6047 return 3;
6048 }
6049
6050 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6051 if(nErrCode != -1) {
6052 return 4;
6053 }
6054
6055 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6056 if(nErrCode != -1) {
6057 return 5;
6058 }
6059
6060
6061 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6062 if(nErrCode == -1 || n16 != INT16_MAX) {
6063 return 6;
6064 }
6065
6066 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6067 if(nErrCode == -1 || n16 != INT16_MIN) {
6068 return 7;
6069 }
6070
6071 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6072 if(nErrCode == -1 || n16 != 1) {
6073 return 8;
6074 }
6075
6076 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6077 if(nErrCode != -1) {
6078 return 9;
6079 }
6080
6081 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6082 if(nErrCode != -1) {
6083 return 10;
6084 }
6085
6086
6087 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6088 if(nErrCode == -1 || n8 != 1) {
6089 return 11;
6090 }
6091
6092 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6093 if(nErrCode == -1 || n8 != INT8_MAX) {
6094 return 12;
6095 }
6096
6097 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6098 if(nErrCode == -1 || n8 != INT8_MIN) {
6099 return 13;
6100 }
6101
6102 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6103 if(nErrCode != -1) {
6104 return 14;
6105 }
6106
6107 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6108 if(nErrCode != -1) {
6109 return 15;
6110 }
6111
6112
6113 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6114 if(nErrCode == -1 || u32 != 1) {
6115 return 16;
6116 }
6117
6118 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6119 if(nErrCode == -1 || u32 != UINT32_MAX) {
6120 return 17;
6121 }
6122
6123 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6124 if(nErrCode == -1 || u32 != 0) {
6125 return 18;
6126 }
6127
6128 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6129 if(nErrCode != -1) {
6130 return 19;
6131 }
6132
6133 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6134 if(nErrCode != -1) {
6135 return 20;
6136 }
6137
6138
6139 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6140 if(nErrCode == -1 || u16 != UINT16_MAX) {
6141 return 21;
6142 }
6143
6144 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6145 if(nErrCode == -1 || u16 != 0) {
6146 return 22;
6147 }
6148
6149 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6150 if(nErrCode == -1 || u16 != 1) {
6151 return 23;
6152 }
6153
6154 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6155 if(nErrCode != -1) {
6156 return 24;
6157 }
6158
6159 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6160 if(nErrCode != -1) {
6161 return 25;
6162 }
6163
6164
6165 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6166 if(nErrCode == -1 || u8 != UINT8_MAX) {
6167 return 26;
6168 }
6169
6170 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6171 if(nErrCode == -1 || u8 != 0) {
6172 return 27;
6173 }
6174
6175 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6176 if(nErrCode == -1 || u8 != 1) {
6177 return 28;
6178 }
6179
6180 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6181 if(nErrCode != -1) {
6182 return 29;
6183 }
6184
6185 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6186 if(nErrCode != -1) {
6187 return 30;
6188 }
6189
6190
6191 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6192 if(nErrCode == -1 || u64 != 1) {
6193 return 31;
6194 }
6195
6196 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6197 if(nErrCode == -1 || u64 != INT64_MAX) {
6198 return 32;
6199 }
6200
6201 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6202 if(nErrCode == -1 || u64 != 0) {
6203 return 33;
6204 }
6205
6206 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6207 if(nErrCode != -1) {
6208 return 34;
6209 }
6210
6211 return 0;
6212}
6213
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006214
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006215
6216
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006217/*
6218A sequence with
6219 A wrapping bstr
6220 containing a map
6221 1
6222 2
6223 A wrapping bstr
6224 containing an array
6225 3
6226 wrapping bstr
6227 4
6228 5
6229 6
6230 array
6231 7
6232 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006233 */
6234
Laurence Lundblade55013642020-09-23 05:39:22 -07006235static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006236{
Laurence Lundblade55013642020-09-23 05:39:22 -07006237 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006238 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006239 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006240
Laurence Lundblade55013642020-09-23 05:39:22 -07006241 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006242
6243 QCBOREncode_BstrWrap(&EC);
6244 QCBOREncode_OpenMap(&EC);
6245 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6246 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6247 QCBOREncode_CloseMap(&EC);
6248 QCBOREncode_BstrWrap(&EC);
6249 QCBOREncode_OpenArray(&EC);
6250 QCBOREncode_AddInt64(&EC, 3);
6251 QCBOREncode_BstrWrap(&EC);
6252 QCBOREncode_AddInt64(&EC, 4);
6253 QCBOREncode_CloseBstrWrap(&EC, NULL);
6254 QCBOREncode_AddInt64(&EC, 5);
6255 QCBOREncode_CloseArray(&EC);
6256 QCBOREncode_CloseBstrWrap(&EC, NULL);
6257 QCBOREncode_AddInt64(&EC, 6);
6258 QCBOREncode_CloseBstrWrap(&EC, NULL);
6259 QCBOREncode_OpenArray(&EC);
6260 QCBOREncode_AddInt64(&EC, 7);
6261 QCBOREncode_AddInt64(&EC, 8);
6262 QCBOREncode_CloseArray(&EC);
6263
6264 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006265 if(uErr) {
6266 Encoded = NULLUsefulBufC;
6267 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006268
6269 return Encoded;
6270}
6271
6272
6273int32_t EnterBstrTest()
6274{
Laurence Lundblade55013642020-09-23 05:39:22 -07006275 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006276
6277 QCBORDecodeContext DC;
6278
Laurence Lundblade55013642020-09-23 05:39:22 -07006279 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006280
Laurence Lundblade55013642020-09-23 05:39:22 -07006281 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006282
6283
Laurence Lundblade9b334962020-08-27 10:55:53 -07006284 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006285 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006286 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6287 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006288 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006289 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006290 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006291 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006292 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006293 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006294 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006295 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006296 QCBORDecode_ExitArray(&DC);
6297 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006298 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006299 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006300 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006301 QCBORDecode_GetInt64(&DC, &n7);
6302 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006303 QCBORDecode_ExitArray(&DC);
6304
6305 QCBORError uErr = QCBORDecode_Finish(&DC);
6306
6307 return (int32_t)uErr;
6308}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006309
6310
6311
6312
6313static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006314 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006315
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006316 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006317 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006318 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6319 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6320 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006321
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006322 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006323 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6324 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6325 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006326
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006327 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006328 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006329 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6330 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006331
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006332 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006333 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6334 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006335
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006336 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006337 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006338 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6339 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006340
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006341 21,
6342 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6343 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6344
6345 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006346 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006347 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6348 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006349
6350 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006351 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6352 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006353
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006354 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006355 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006356 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6357 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006358
6359 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006360 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6361 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006362
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006363 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006364 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006365 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6366 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006367
6368 0x18, 0x33,
6369 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6370
6371 // MIME
6372 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006373 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6374 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6375 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006376
6377 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006378 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6379 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006380
6381 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006382 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6383 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6384 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006385
6386 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006387 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6388 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006389
6390 // UUID
6391 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006392 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6393 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006394
6395 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006396 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6397 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006398};
6399
6400int32_t DecodeTaggedTypeTests()
6401{
6402 QCBORDecodeContext DC;
6403 QCBORError uErr;
6404
6405 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6406
6407 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006408 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006409
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006410 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006411 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006412 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006413 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6414 return 1;
6415 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006416 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006417 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6418 return 2;
6419 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006420 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006421 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6422 return 3;
6423 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006424 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006425 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006426 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6427 return 4;
6428 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006429 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006430 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006431 return 5;
6432 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006433
Laurence Lundblade9b334962020-08-27 10:55:53 -07006434 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006435 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6436 bNeg != false) {
6437 return 10;
6438 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006439 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006440 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6441 bNeg != true) {
6442 return 11;
6443 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006444 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006445 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6446 return 12;
6447 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006448 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006449 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006450 return 13;
6451 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006452 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006453 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006454 return 14;
6455 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006456
Laurence Lundblade9b334962020-08-27 10:55:53 -07006457 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006458 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6459 return 20;
6460 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006461 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006462 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6463 return 21;
6464 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006465 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006466 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006467 return 22;
6468 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006469 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006470 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006471 return 23;
6472 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006473
Laurence Lundblade9b334962020-08-27 10:55:53 -07006474 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006475 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6476 return 30;
6477 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006478 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006479 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6480 return 31;
6481 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006482 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006483 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006484 return 32;
6485 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006486 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006487 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006488 return 33;
6489 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006490
Laurence Lundblade9b334962020-08-27 10:55:53 -07006491 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006492 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6493 return 40;
6494 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006495 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006496 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6497 return 41;
6498 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006499 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006500 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006501 return 42;
6502 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006503 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006504 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006505 return 43;
6506 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006507
Laurence Lundblade9b334962020-08-27 10:55:53 -07006508 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006509 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6510 return 50;
6511 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006512 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006513 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6514 return 51;
6515 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006516 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006517 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006518 return 52;
6519 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006520 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006521 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006522 return 53;
6523 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006524
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006525 // MIME
6526 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006527 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006528 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6529 bIsNot7Bit == true) {
6530 return 60;
6531 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006532 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006533 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6534 bIsNot7Bit == true) {
6535 return 61;
6536 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006537 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006538 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6539 bIsNot7Bit == false) {
6540 return 62;
6541 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006542 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006543 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6544 bIsNot7Bit == false) {
6545 return 63;
6546 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006547 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006548 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006549 return 64;
6550 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006551 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006552 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006553 return 65;
6554 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006555
Laurence Lundblade9b334962020-08-27 10:55:53 -07006556 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006557 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6558 return 70;
6559 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006560 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006561 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6562 return 71;
6563 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006564 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006565 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006566 return 72;
6567 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006568 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006569 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006570 return 73;
6571 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006572
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006573 // Improvement: add some more error test cases
6574
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006575 QCBORDecode_ExitMap(&DC);
6576
6577 uErr = QCBORDecode_Finish(&DC);
6578 if(uErr != QCBOR_SUCCESS) {
6579 return 100;
6580 }
6581
6582 return 0;
6583}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006584
6585
6586
6587
6588/*
6589 [
6590 "aaaaaaaaaa",
6591 {}
6592 ]
6593 */
6594static const uint8_t spTooLarge1[] = {
6595 0x9f,
6596 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6597 0xa0,
6598 0xff
6599};
6600
6601/*
6602 [
6603 {
6604 0: "aaaaaaaaaa"
6605 }
6606 ]
6607 */
6608static const uint8_t spTooLarge2[] = {
6609 0x9f,
6610 0xa1,
6611 0x00,
6612 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6613 0xff
6614};
6615
6616/*
6617 h'A1006A61616161616161616161'
6618
6619 {
6620 0: "aaaaaaaaaa"
6621 }
6622 */
6623static const uint8_t spTooLarge3[] = {
6624 0x4d,
6625 0xa1,
6626 0x00,
6627 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6628};
6629
6630int32_t TooLargeInputTest(void)
6631{
6632 QCBORDecodeContext DC;
6633 QCBORError uErr;
6634 UsefulBufC String;
6635
6636 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6637 // to 10 There's not really any way to test this error
6638 // condition. The error condition is not complex, so setting
6639 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6640
6641 // The input CBOR is only too large because the
6642 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6643 //
6644 // This test is disabled for the normal test runs because of the
6645 // special build requirement.
6646
6647
6648 // Tests the start of a map being too large
6649 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006650 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006651 QCBORDecode_GetTextString(&DC, &String);
6652 uErr = QCBORDecode_GetError(&DC);
6653 if(uErr != QCBOR_SUCCESS) {
6654 return 1;
6655 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006656 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006657 uErr = QCBORDecode_GetError(&DC);
6658 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6659 return 2;
6660 }
6661
6662 // Tests the end of a map being too large
6663 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006664 QCBORDecode_EnterArray(&DC, NULL);
6665 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006666 uErr = QCBORDecode_GetError(&DC);
6667 if(uErr != QCBOR_SUCCESS) {
6668 return 3;
6669 }
6670 QCBORDecode_ExitMap(&DC);
6671 uErr = QCBORDecode_GetError(&DC);
6672 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6673 return 4;
6674 }
6675
6676 // Tests the entire input CBOR being too large when processing bstr wrapping
6677 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6678 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6679 uErr = QCBORDecode_GetError(&DC);
6680 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6681 return 5;
6682 }
6683
6684 return 0;
6685}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006686
6687
6688static const uint8_t spMapWithIndefLenStrings[] = {
6689 0xbf,
6690 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6691 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6692 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6693 0x03,
6694 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6695 0xc3,
6696 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6697 0xff
6698};
6699
6700int32_t SpiffyIndefiniteLengthStringsTests()
6701{
6702 QCBORDecodeContext DCtx;
6703
6704 QCBORDecode_Init(&DCtx,
6705 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6706 QCBOR_DECODE_MODE_NORMAL);
6707
6708 MakeUsefulBufOnStack(StringBuf, 200);
6709 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6710
6711 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006712 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006713 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6714 if(QCBORDecode_GetAndResetError(&DCtx)) {
6715 return 1;
6716 }
6717
6718 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6719 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6720 return 2;
6721 }
6722
6723 uint64_t uInt;
6724 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6725 if(QCBORDecode_GetAndResetError(&DCtx)) {
6726 return 3;
6727 }
6728 if(uInt != 3) {
6729 return 4;
6730 }
6731
6732 double uDouble;
6733 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6734 "label2",
6735 0xff,
6736 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006737#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006738 if(QCBORDecode_GetAndResetError(&DCtx)) {
6739 return 5;
6740 }
6741 if(uDouble != -16777474) {
6742 return 6;
6743 }
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006744#else
6745 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6746 return 7;
6747 }
6748#endif
6749
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006750
6751 QCBORDecode_ExitMap(&DCtx);
6752
6753 if(QCBORDecode_Finish(&DCtx)) {
6754 return 99;
6755 }
6756
6757 return 0;
6758}
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08006759
6760
6761
6762
6763int32_t PeekTest()
6764{
6765 QCBORItem Item;
6766 QCBORError nCBORError;
6767 QCBORDecodeContext DCtx;
6768
6769 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6770
6771 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6772 return 100+(int32_t)nCBORError;
6773 }
6774 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6775 return 200;
6776 }
6777
6778 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6779 return (int32_t)nCBORError;
6780 }
6781 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6782 return 300;
6783 }
6784
6785 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6786 return 400 + (int32_t)nCBORError;
6787 }
6788 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6789 return 500;
6790 }
6791
6792 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6793 return (int32_t)nCBORError;
6794 }
6795 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6796 return 600;
6797 }
6798
6799 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6800 return 900 + (int32_t)nCBORError;
6801 }
6802 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6803 Item.uDataType != QCBOR_TYPE_INT64 ||
6804 Item.val.int64 != 42 ||
6805 Item.uDataAlloc ||
6806 Item.uLabelAlloc ||
6807 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6808 return 1000;
6809 }
6810
6811 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6812 return 1100 + (int32_t)nCBORError;
6813 }
6814
6815 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6816 Item.uDataType != QCBOR_TYPE_INT64 ||
6817 Item.val.int64 != 42 ||
6818 Item.uDataAlloc ||
6819 Item.uLabelAlloc ||
6820 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6821 return 1200;
6822 }
6823
6824
6825 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6826 return 1300 + (int32_t)nCBORError;
6827 }
6828 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6829 Item.uDataAlloc ||
6830 Item.uLabelAlloc ||
6831 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
6832 Item.uDataType != QCBOR_TYPE_ARRAY ||
6833 Item.val.uCount != 2)
6834 return 1400;
6835
6836 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6837 return 1500 + (int32_t)nCBORError;
6838 }
6839 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6840 Item.uDataAlloc ||
6841 Item.uLabelAlloc ||
6842 UsefulBufCompareToSZ(Item.val.string, "string1")) {
6843 return 1600;
6844 }
6845
6846 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6847 return 1700 + (int32_t)nCBORError;
6848 }
6849 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6850 Item.uDataAlloc ||
6851 Item.uLabelAlloc ||
6852 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6853 return 1800;
6854 }
6855
6856 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6857 return (int32_t)nCBORError;
6858 }
6859 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6860 Item.uDataAlloc ||
6861 Item.uLabelAlloc ||
6862 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6863 return 1900;
6864 }
6865
6866 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6867 return (int32_t)nCBORError;
6868 }
6869 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6870 Item.uDataAlloc ||
6871 Item.uLabelAlloc ||
6872 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6873 return 2000;
6874 }
6875
6876
6877 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6878 return 2100 + (int32_t)nCBORError;
6879 }
6880 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6881 Item.uDataAlloc ||
6882 Item.uLabelAlloc ||
6883 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
6884 Item.uDataType != QCBOR_TYPE_MAP ||
6885 Item.val.uCount != 4) {
6886 return 2100;
6887 }
6888
6889 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6890 return 2200 + (int32_t)nCBORError;
6891 }
6892 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6893 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
6894 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6895 Item.uDataAlloc ||
6896 Item.uLabelAlloc ||
6897 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
6898 return 2300;
6899 }
6900
6901 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6902 return 2400 + (int32_t)nCBORError;
6903 }
6904 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6905 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6906 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6907 Item.uDataAlloc ||
6908 Item.uLabelAlloc ||
6909 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6910 return 2500;
6911 }
6912
6913 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6914 return 2600 + (int32_t)nCBORError;
6915 }
6916 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6917 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6918 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6919 Item.uDataAlloc ||
6920 Item.uLabelAlloc ||
6921 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6922 return 2700;
6923 }
6924
6925 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6926 return 2800 + (int32_t)nCBORError;
6927 }
6928 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6929 Item.uDataAlloc ||
6930 Item.uLabelAlloc ||
6931 UsefulBufCompareToSZ(Item.label.string, "another int") ||
6932 Item.uDataType != QCBOR_TYPE_INT64 ||
6933 Item.val.int64 != 98)
6934 return 2900;
6935
6936 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6937 return 3000 + (int32_t)nCBORError;
6938 }
6939 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6940 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
6941 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6942 Item.uDataAlloc ||
6943 Item.uLabelAlloc ||
6944 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
6945 return 3100;
6946 }
6947
6948 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6949 return 3200 + (int32_t)nCBORError;
6950 }
6951 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6952 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
6953 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6954 Item.uDataAlloc ||
6955 Item.uLabelAlloc ||
6956 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
6957 return 3300;
6958 }
6959
6960 return 0;
6961}