blob: 70fd3b196ba37e5d0983ca16526085264a1d0b3c [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 }
4650 }
4651 */
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 Lundbladeabf5c572020-06-29 21:21:29 -07004746
4747/*
4748 [23,
4749 6000,
4750 h'67616C6163746963',
4751 h'686176656E20746F6B656E'
4752 ]
4753 */
4754static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004755 0x84,
4756 0x17,
4757 0x19, 0x17, 0x70,
4758 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4759 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004760
4761
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004762static const uint8_t spEmptyMap[] = {0xa0};
4763
4764static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004765
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004766static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4767
Laurence Lundbladef0499502020-08-01 11:55:57 -07004768/*
4769 {
4770 0: [],
4771 9: [
4772 [],
4773 []
4774 ],
4775 8: {
4776 1: [],
4777 2: {},
4778 3: []
4779 },
4780 4: {},
4781 5: [],
4782 6: [
4783 [],
4784 []
4785 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004786 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004787 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004788
Laurence Lundbladef0499502020-08-01 11:55:57 -07004789static const uint8_t spMapOfEmpty[] = {
4790 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4791 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4792 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004793
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004794/*
4795 Too many tags
4796 Invalid tag content
4797 Duplicate label
4798 Integer overflow
4799 Date overflow
4800
4801 {1: 224(225(226(227(4(0))))),
4802 2: 1(h''),
4803 3: -18446744073709551616,
4804 4: 1(1.0e+300),
4805 5: 0, 8: 8}
4806 */
4807static const uint8_t spRecoverableMapErrors[] = {
4808 0xbf,
4809 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
4810 0x02, 0xc1, 0x40,
4811 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4812 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
4813 0x05, 0x00,
4814 0x05, 0x00,
4815 0x08, 0x08,
4816 0xff
4817};
4818
4819// Bad break
4820static const uint8_t spUnRecoverableMapError1[] = {
4821 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
4822};
4823
4824// No more items
4825static const uint8_t spUnRecoverableMapError2[] = {
4826 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
4827};
4828
4829// Hit end because string is too long
4830static const uint8_t spUnRecoverableMapError3[] = {
4831 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
4832};
4833
4834// Hit end because string is too long
4835static const uint8_t spUnRecoverableMapError4[] = {
4836 0xbf,
4837 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4838 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4839 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4840 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4841 0xff
4842};
4843
4844
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004845int32_t EnterMapTest()
4846{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004847 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004848 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004849 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004850 int32_t nReturn;
4851 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004852
4853
4854 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004855 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004856
Laurence Lundbladef0499502020-08-01 11:55:57 -07004857
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004858 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004859 QCBORDecode_ExitArray(&DCtx);
4860
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004861 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
4862 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004863 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004864 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004865 QCBORDecode_ExitArray(&DCtx);
4866 QCBORDecode_ExitArray(&DCtx);
4867
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004868 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
4869 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004870 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004871 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004872 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004873 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004874 QCBORDecode_ExitArray(&DCtx);
4875 QCBORDecode_ExitMap(&DCtx);
4876
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004877 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004878 QCBORDecode_ExitMap(&DCtx);
4879
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004880 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004881 QCBORDecode_ExitArray(&DCtx);
4882
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004883 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
4884 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004885 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004886 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004887 QCBORDecode_ExitArray(&DCtx);
4888 QCBORDecode_ExitArray(&DCtx);
4889
4890 QCBORDecode_ExitMap(&DCtx);
4891
4892 uErr = QCBORDecode_Finish(&DCtx);
4893 if(uErr != QCBOR_SUCCESS){
4894 return 3011;
4895 }
4896
4897
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004898 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004899 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004900 if(nReturn) {
4901 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004902 }
4903
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004904 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004905 if(nReturn) {
4906 return nReturn;
4907 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004908
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004909
Laurence Lundblade937ea812020-05-08 11:38:23 -07004910
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004911 // These tests confirm the cursor is at the right place after entering
4912 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004913 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004914
4915 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004916 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004917 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004918 QCBORDecode_GetNext(&DCtx, &Item1);
4919 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4920 return 2001;
4921 }
4922
4923
Laurence Lundblade9b334962020-08-27 10:55:53 -07004924 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004925 QCBORDecode_VGetNext(&DCtx, &Item1);
4926 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004927 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
4928 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4929 UsefulBuf_Compare(ArrayItem.label.string,
4930 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
4931 return 2051;
4932 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004933 QCBORDecode_GetNext(&DCtx, &Item1);
4934 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4935 return 2002;
4936 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004937 QCBORDecode_ExitArray(&DCtx);
4938 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
4939 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4940 UsefulBuf_Compare(ArrayItem.label.string,
4941 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
4942 return 2052;
4943 }
4944
Laurence Lundblade937ea812020-05-08 11:38:23 -07004945
Laurence Lundblade9b334962020-08-27 10:55:53 -07004946 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004947 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004948 QCBORDecode_GetNext(&DCtx, &Item1);
4949 QCBORDecode_GetNext(&DCtx, &Item1);
4950 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004951 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4952 QCBORDecode_GetNext(&DCtx, &Item1);
4953 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4954 return 2003;
4955 }
4956
Laurence Lundblade9b334962020-08-27 10:55:53 -07004957 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004958 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004959 QCBORDecode_GetNext(&DCtx, &Item1);
4960 QCBORDecode_GetNext(&DCtx, &Item1);
4961 QCBORDecode_GetNext(&DCtx, &Item1);
4962 QCBORDecode_GetNext(&DCtx, &Item1);
4963 QCBORDecode_GetNext(&DCtx, &Item1);
4964 QCBORDecode_GetNext(&DCtx, &Item1);
4965 QCBORDecode_GetNext(&DCtx, &Item1);
4966 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4967 QCBORDecode_GetNext(&DCtx, &Item1);
4968 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004969 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004970 }
4971
Laurence Lundblade9b334962020-08-27 10:55:53 -07004972 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004973 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004974 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4975 QCBORDecode_ExitArray(&DCtx);
4976 QCBORDecode_GetNext(&DCtx, &Item1);
4977 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4978 return 2006;
4979 }
4980 QCBORDecode_ExitMap(&DCtx);
4981 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4982 return 2007;
4983 }
4984
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004985 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004986 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004987 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004988 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4989 uErr = QCBORDecode_GetAndResetError(&DCtx);
4990 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004991 return 2008;
4992 }
4993 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004994 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004995 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004996 return 2009;
4997 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004998
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004999
5000 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005001 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005002 // This will fail because the map is empty.
5003 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5004 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005005 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005006 return 2010;
5007 }
5008 QCBORDecode_ExitMap(&DCtx);
5009 uErr = QCBORDecode_Finish(&DCtx);
5010 if(uErr != QCBOR_SUCCESS){
5011 return 2011;
5012 }
5013
5014
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005015 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005016 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005017 // This will fail because the map is empty.
5018 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5019 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005020 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005021 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005022 }
5023 QCBORDecode_ExitMap(&DCtx);
5024 uErr = QCBORDecode_Finish(&DCtx);
5025 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005026 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005027 }
5028
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005029
5030 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005031 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005032 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005033 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005034 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005035 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005036 QCBORDecode_ExitArray(&DCtx);
5037 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5038 QCBORDecode_ExitArray(&DCtx);
5039 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005040 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005041 return 2014;
5042 }
5043
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005044 int64_t nInt;
5045 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005046 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005047 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005048 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005049 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5050 return 2021;
5051 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005052 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5053 return 2121;
5054 }
5055 (void)QCBORDecode_GetAndResetError(&DCtx);
5056
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005057
5058 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5059 uErr = QCBORDecode_GetAndResetError(&DCtx);
5060 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5061 return 2022;
5062 }
5063
5064 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5065 uErr = QCBORDecode_GetAndResetError(&DCtx);
5066 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5067 return 2023;
5068 }
5069
5070 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5071 uErr = QCBORDecode_GetAndResetError(&DCtx);
5072#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5073 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5074 return 2024;
5075 }
5076#else
5077 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5078 return 2027;
5079 }
5080#endif
5081
5082 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5083 uErr = QCBORDecode_GetAndResetError(&DCtx);
5084 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5085 return 2025;
5086 }
5087
5088 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5089
5090 QCBORDecode_ExitMap(&DCtx);
5091 uErr = QCBORDecode_Finish(&DCtx);
5092 if(uErr != QCBOR_SUCCESS) {
5093 return 2026;
5094 }
5095
5096 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005097 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005098 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5099 uErr = QCBORDecode_GetAndResetError(&DCtx);
5100 if(uErr != QCBOR_ERR_BAD_BREAK) {
5101 return 2030;
5102 }
5103
5104 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005105 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005106 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5107 uErr = QCBORDecode_GetAndResetError(&DCtx);
5108 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5109 return 2031;
5110 }
5111
5112 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005113 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005114 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5115 uErr = QCBORDecode_GetAndResetError(&DCtx);
5116 if(uErr != QCBOR_ERR_HIT_END) {
5117 return 2032;
5118 }
5119
5120 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005121 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005122 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5123 uErr = QCBORDecode_GetAndResetError(&DCtx);
5124 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5125 return 2033;
5126 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005127
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005128 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005129}
5130
5131
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005132struct NumberConversion {
5133 char *szDescription;
5134 UsefulBufC CBOR;
5135 int64_t nConvertedToInt64;
5136 QCBORError uErrorInt64;
5137 uint64_t uConvertToUInt64;
5138 QCBORError uErrorUint64;
5139 double dConvertToDouble;
5140 QCBORError uErrorDouble;
5141};
5142
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005143static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005144 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005145 "too large to fit into int64_t",
5146 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5147 0,
5148 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5149 0,
5150 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5151 ((double)INT64_MIN) + 1 ,
5152 QCBOR_SUCCESS
5153 },
5154 {
5155 "largest negative int that fits in int64_t",
5156 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5157 INT64_MIN,
5158 QCBOR_SUCCESS,
5159 0,
5160 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5161 (double)INT64_MIN,
5162 QCBOR_SUCCESS
5163 },
5164 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005165 "negative bignum -1",
5166 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5167 -1,
5168 QCBOR_SUCCESS,
5169 0,
5170 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5171 -1.0,
5172 QCBOR_SUCCESS
5173 },
5174 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005175 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005176 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5177 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005178#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005179 257000,
5180 QCBOR_SUCCESS,
5181 257000,
5182 QCBOR_SUCCESS,
5183 257000.0,
5184 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005185#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5186 0,
5187 QCBOR_ERR_UNEXPECTED_TYPE,
5188 0,
5189 QCBOR_ERR_UNEXPECTED_TYPE,
5190 0.0,
5191 QCBOR_ERR_UNEXPECTED_TYPE
5192#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005193 },
5194 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005195 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005196 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5197 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005198#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005199 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005200 QCBOR_SUCCESS,
5201 0,
5202 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005203 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005204 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005205#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5206 0,
5207 QCBOR_ERR_UNEXPECTED_TYPE,
5208 0,
5209 QCBOR_ERR_UNEXPECTED_TYPE,
5210 0.0,
5211 QCBOR_ERR_UNEXPECTED_TYPE
5212#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005213 },
5214 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005215 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005216 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5217 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005218#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005219 2056,
5220 QCBOR_SUCCESS,
5221 2056,
5222 QCBOR_SUCCESS,
5223 2056.0,
5224 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005225#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5226 0,
5227 QCBOR_ERR_UNEXPECTED_TYPE,
5228 0,
5229 QCBOR_ERR_UNEXPECTED_TYPE,
5230 0.0,
5231 QCBOR_ERR_UNEXPECTED_TYPE
5232#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005233 },
5234 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005235 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005236 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5237 0,
5238 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5239 0,
5240 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5241 -18446744073709551617.0,
5242 QCBOR_SUCCESS
5243 },
5244 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005245 "Positive bignum 0x01020304 indefinite length string",
5246 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5247 0x01020304,
5248 QCBOR_SUCCESS,
5249 0x01020304,
5250 QCBOR_SUCCESS,
5251 16909060.0,
5252 QCBOR_SUCCESS
5253 },
5254 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005255 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005256 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5257 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005258#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005259 0,
5260 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5261 0,
5262 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5263 -INFINITY,
5264 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005265#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5266 0,
5267 QCBOR_ERR_UNEXPECTED_TYPE,
5268 0,
5269 QCBOR_ERR_UNEXPECTED_TYPE,
5270 0.0,
5271 QCBOR_ERR_UNEXPECTED_TYPE
5272#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005273 },
5274 {
5275 "big float [9223372036854775806, 9223372036854775806]",
5276 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5277 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005278#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005279 0,
5280 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5281 0,
5282 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5283 INFINITY,
5284 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005285#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5286 0,
5287 QCBOR_ERR_UNEXPECTED_TYPE,
5288 0,
5289 QCBOR_ERR_UNEXPECTED_TYPE,
5290 0.0,
5291 QCBOR_ERR_UNEXPECTED_TYPE
5292#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005293 },
5294 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005295 "Big float 3 * 2^^2",
5296 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005297#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005298 12,
5299 QCBOR_SUCCESS,
5300 12,
5301 QCBOR_SUCCESS,
5302 12.0,
5303 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005304#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5305 0,
5306 QCBOR_ERR_UNEXPECTED_TYPE,
5307 0,
5308 QCBOR_ERR_UNEXPECTED_TYPE,
5309 0.0,
5310 QCBOR_ERR_UNEXPECTED_TYPE
5311#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005312 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005313 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005314 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005315 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5316 0,
5317 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5318 18446744073709551615ULL,
5319 QCBOR_SUCCESS,
5320 18446744073709551615.0,
5321 QCBOR_SUCCESS
5322 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005323 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005324 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005325 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5326 65536-1,
5327 QCBOR_SUCCESS,
5328 0xffff,
5329 QCBOR_SUCCESS,
5330 65535.0,
5331 QCBOR_SUCCESS
5332 },
5333 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005334 "Postive integer 0",
5335 {(uint8_t[]){0x0}, 1},
5336 0LL,
5337 QCBOR_SUCCESS,
5338 0ULL,
5339 QCBOR_SUCCESS,
5340 0.0,
5341 QCBOR_SUCCESS
5342 },
5343 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005344 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005345 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5346 -9223372036854775807-1, // INT64_MIN
5347 QCBOR_SUCCESS,
5348 0ULL,
5349 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5350 -9223372036854775808.0,
5351 QCBOR_SUCCESS
5352 },
5353 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005354 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005355 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005356#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005357 100L,
5358 QCBOR_SUCCESS,
5359 100ULL,
5360 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005361#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5362 0,
5363 QCBOR_ERR_HW_FLOAT_DISABLED,
5364 0,
5365 QCBOR_ERR_HW_FLOAT_DISABLED,
5366#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005367 100.3,
5368 QCBOR_SUCCESS
5369 },
5370 {
5371 "Floating point value NaN 0xfa7fc00000",
5372 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005373#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005374 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005375 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005376 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005377 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005378#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5379 0,
5380 QCBOR_ERR_HW_FLOAT_DISABLED,
5381 0,
5382 QCBOR_ERR_HW_FLOAT_DISABLED,
5383#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005384 NAN,
5385 QCBOR_SUCCESS
5386 },
5387 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005388 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005389 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005390#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5391#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5392 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005393 -4,
5394 QCBOR_SUCCESS,
5395 0,
5396 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5397 -4.0,
5398 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005399#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5400 // Float HW disabled
5401 -4,
5402 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5403 0,
5404 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5405 -4.0,
5406 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5407#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5408#else
5409 // Half-precision disabled
5410 -4,
5411 QCBOR_ERR_HALF_PRECISION_DISABLED,
5412 0,
5413 QCBOR_ERR_HALF_PRECISION_DISABLED,
5414 -4.0,
5415 QCBOR_ERR_HALF_PRECISION_DISABLED
5416#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005417 },
5418 {
5419 "Decimal fraction 3/10",
5420 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005421#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005422 0,
5423 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5424 0,
5425 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5426 0.30000000000000004,
5427 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005428#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5429 0,
5430 QCBOR_ERR_UNEXPECTED_TYPE,
5431 0,
5432 QCBOR_ERR_UNEXPECTED_TYPE,
5433 0.0,
5434 QCBOR_ERR_UNEXPECTED_TYPE
5435#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005436 },
5437 {
5438 "+inifinity",
5439 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5440#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5441 0,
5442 QCBOR_ERR_FLOAT_EXCEPTION,
5443 0,
5444 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5445#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5446 0,
5447 QCBOR_ERR_HW_FLOAT_DISABLED,
5448 0,
5449 QCBOR_ERR_HW_FLOAT_DISABLED,
5450#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5451 INFINITY,
5452 QCBOR_SUCCESS
5453 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005454
5455 {
5456 "extreme pos bignum",
5457 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5458 // 50 rows of 8 is 400 digits.
5459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5481 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5482 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5483 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5484 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5485 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5486 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5487 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5488 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5489 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5490 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5491 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5492 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5493 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5494 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5495 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5496 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5497 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5498 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5499 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5500 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5501 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5502 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5503 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5504 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5505 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5506 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5507 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5508 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5509 404},
5510 0,
5511 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5512 0,
5513 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5514#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5515 INFINITY,
5516 QCBOR_SUCCESS
5517#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5518 0,
5519 QCBOR_ERR_HW_FLOAT_DISABLED,
5520#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5521 },
5522
5523 {
5524 "extreme neg bignum",
5525 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5526 // 50 rows of 8 is 400 digits.
5527 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5528 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5529 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5530 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5531 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5532 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5533 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5534 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5535 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5536 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5537 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5538 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5539 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5540 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5541 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5542 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5543 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5544 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5545 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5546 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5547 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5548 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5549 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5550 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5551 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5552 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5553 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5554 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5555 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5556 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5557 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5558 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5559 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5560 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5561 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5562 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5563 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5564 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5565 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5566 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5567 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5568 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5569 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5570 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5571 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5572 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5573 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5574 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5575 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5576 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5577 404},
5578 0,
5579 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5580 0,
5581 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5582#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5583 -INFINITY,
5584 QCBOR_SUCCESS
5585#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5586 0,
5587 QCBOR_ERR_HW_FLOAT_DISABLED,
5588#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5589 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005590
5591 {
5592 "big float underflow [9223372036854775806, -9223372036854775806]",
5593 {(uint8_t[]){
5594 0xC5, 0x82,
5595 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5596 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5597#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5598 0,
5599 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5600 0,
5601 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5602 0,
5603 QCBOR_SUCCESS
5604#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5605 0,
5606 QCBOR_ERR_UNEXPECTED_TYPE,
5607 0,
5608 QCBOR_ERR_UNEXPECTED_TYPE,
5609 0.0,
5610 QCBOR_ERR_UNEXPECTED_TYPE
5611#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5612 },
5613
5614 {
5615 "bigfloat that evaluates to -INFINITY",
5616 {(uint8_t[]){
5617 0xC5, 0x82,
5618 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5619 0xC3, 0x42, 0x01, 0x01}, 15},
5620#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5621 0,
5622 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5623 0,
5624 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5625 -INFINITY,
5626 QCBOR_SUCCESS
5627#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5628 0,
5629 QCBOR_ERR_UNEXPECTED_TYPE,
5630 0,
5631 QCBOR_ERR_UNEXPECTED_TYPE,
5632 0.0,
5633 QCBOR_ERR_UNEXPECTED_TYPE
5634#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5635 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005636};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005637
5638
5639
Laurence Lundblade313b2862020-05-16 01:23:06 -07005640int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005641{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005642 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005643
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005644 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5645 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005646
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005647 // Set up the decoding context including a memory pool so that
5648 // indefinite length items can be checked
5649 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005650 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005651
5652 /* ----- test conversion to int64_t ------ */
5653 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005654 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5655 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005656 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005657 }
5658
5659 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005660 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005661 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005662 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005663 }
5664 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005665 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005666 }
5667
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005668 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005669 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5670 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5671 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005672 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005673 }
5674 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005675 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005676 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005677 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005678 }
5679 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005680 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005681 }
5682
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005683 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005684 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5685 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5686 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005687 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005688 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005689#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005690 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005691 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005692 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005693 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005694 }
5695 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5696 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005697 // NaN's can't be compared for equality. A NaN is
5698 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005699 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005700 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005701 }
5702 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005703 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005704 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005705 }
5706 }
5707 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005708#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005709 }
5710
5711 return 0;
5712}
5713
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005714
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005715
5716
Laurence Lundbladee3553422020-05-02 11:11:17 -07005717int32_t CBORSequenceDecodeTests(void)
5718{
5719 QCBORDecodeContext DCtx;
5720 QCBORItem Item;
5721 QCBORError uCBORError;
5722
5723 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005724
Laurence Lundbladee3553422020-05-02 11:11:17 -07005725 // The input for the date test happens to be a sequence so it
5726 // is reused. It is a sequence because it doesn't start as
5727 // an array or map.
5728 QCBORDecode_Init(&DCtx,
5729 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5730 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005731
Laurence Lundbladee3553422020-05-02 11:11:17 -07005732 // Get the first item
5733 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5734 if(uCBORError != QCBOR_SUCCESS) {
5735 return 1;
5736 }
5737 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5738 return 2;
5739 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005740
Laurence Lundbladee3553422020-05-02 11:11:17 -07005741 // Get a second item
5742 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005743 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5744 return 66;
5745 }
5746
5747 // Get a third item
5748 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005749 if(uCBORError != QCBOR_SUCCESS) {
5750 return 2;
5751 }
5752 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5753 return 3;
5754 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005755
Laurence Lundbladee3553422020-05-02 11:11:17 -07005756 // A sequence can have stuff at the end that may
5757 // or may not be valid CBOR. The protocol decoder knows
5758 // when to stop by definition of the protocol, not
5759 // when the top-level map or array is ended.
5760 // Finish still has to be called to know that
5761 // maps and arrays (if there were any) were closed
5762 // off correctly. When called like this it
5763 // must return the error QCBOR_ERR_EXTRA_BYTES.
5764 uCBORError = QCBORDecode_Finish(&DCtx);
5765 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5766 return 4;
5767 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005768
5769
Laurence Lundbladee3553422020-05-02 11:11:17 -07005770 // --- Test an empty input ----
5771 uint8_t empty[1];
5772 UsefulBufC Empty = {empty, 0};
5773 QCBORDecode_Init(&DCtx,
5774 Empty,
5775 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005776
Laurence Lundbladee3553422020-05-02 11:11:17 -07005777 uCBORError = QCBORDecode_Finish(&DCtx);
5778 if(uCBORError != QCBOR_SUCCESS) {
5779 return 5;
5780 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005781
5782
Laurence Lundbladee3553422020-05-02 11:11:17 -07005783 // --- Sequence with unclosed indefinite length array ---
5784 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005785
Laurence Lundbladee3553422020-05-02 11:11:17 -07005786 QCBORDecode_Init(&DCtx,
5787 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5788 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005789
Laurence Lundbladee3553422020-05-02 11:11:17 -07005790 // Get the first item
5791 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5792 if(uCBORError != QCBOR_SUCCESS) {
5793 return 7;
5794 }
5795 if(Item.uDataType != QCBOR_TYPE_INT64) {
5796 return 8;
5797 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005798
Laurence Lundbladee3553422020-05-02 11:11:17 -07005799 // Get a second item
5800 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5801 if(uCBORError != QCBOR_SUCCESS) {
5802 return 9;
5803 }
5804 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5805 return 10;
5806 }
5807
5808 // Try to finish before consuming all bytes to confirm
5809 // that the still-open error is returned.
5810 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005811 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005812 return 11;
5813 }
5814
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005815
Laurence Lundbladee3553422020-05-02 11:11:17 -07005816 // --- Sequence with a closed indefinite length array ---
5817 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005818
Laurence Lundbladee3553422020-05-02 11:11:17 -07005819 QCBORDecode_Init(&DCtx,
5820 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5821 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005822
Laurence Lundbladee3553422020-05-02 11:11:17 -07005823 // Get the first item
5824 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5825 if(uCBORError != QCBOR_SUCCESS) {
5826 return 12;
5827 }
5828 if(Item.uDataType != QCBOR_TYPE_INT64) {
5829 return 13;
5830 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005831
Laurence Lundbladee3553422020-05-02 11:11:17 -07005832 // Get a second item
5833 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5834 if(uCBORError != QCBOR_SUCCESS) {
5835 return 14;
5836 }
5837 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5838 return 15;
5839 }
5840
5841 // Try to finish before consuming all bytes to confirm
5842 // that the still-open error is returned.
5843 uCBORError = QCBORDecode_Finish(&DCtx);
5844 if(uCBORError != QCBOR_SUCCESS) {
5845 return 16;
5846 }
5847
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005848
Laurence Lundbladee3553422020-05-02 11:11:17 -07005849 return 0;
5850}
5851
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005852
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005853
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005854int32_t IntToTests()
5855{
5856 int nErrCode;
5857 int32_t n32;
5858 int16_t n16;
5859 int8_t n8;
5860 uint32_t u32;
5861 uint16_t u16;
5862 uint8_t u8;
5863 uint64_t u64;
5864
5865 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5866 if(nErrCode == -1 || n32 != 1) {
5867 return 1;
5868 }
5869
5870 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5871 if(nErrCode == -1 || n32 != INT32_MAX) {
5872 return 2;
5873 }
5874
5875 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5876 if(nErrCode == -1 || n32 != INT32_MIN) {
5877 return 3;
5878 }
5879
5880 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5881 if(nErrCode != -1) {
5882 return 4;
5883 }
5884
5885 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5886 if(nErrCode != -1) {
5887 return 5;
5888 }
5889
5890
5891 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5892 if(nErrCode == -1 || n16 != INT16_MAX) {
5893 return 6;
5894 }
5895
5896 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5897 if(nErrCode == -1 || n16 != INT16_MIN) {
5898 return 7;
5899 }
5900
5901 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5902 if(nErrCode == -1 || n16 != 1) {
5903 return 8;
5904 }
5905
5906 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5907 if(nErrCode != -1) {
5908 return 9;
5909 }
5910
5911 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5912 if(nErrCode != -1) {
5913 return 10;
5914 }
5915
5916
5917 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5918 if(nErrCode == -1 || n8 != 1) {
5919 return 11;
5920 }
5921
5922 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5923 if(nErrCode == -1 || n8 != INT8_MAX) {
5924 return 12;
5925 }
5926
5927 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5928 if(nErrCode == -1 || n8 != INT8_MIN) {
5929 return 13;
5930 }
5931
5932 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5933 if(nErrCode != -1) {
5934 return 14;
5935 }
5936
5937 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5938 if(nErrCode != -1) {
5939 return 15;
5940 }
5941
5942
5943 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5944 if(nErrCode == -1 || u32 != 1) {
5945 return 16;
5946 }
5947
5948 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5949 if(nErrCode == -1 || u32 != UINT32_MAX) {
5950 return 17;
5951 }
5952
5953 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5954 if(nErrCode == -1 || u32 != 0) {
5955 return 18;
5956 }
5957
5958 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5959 if(nErrCode != -1) {
5960 return 19;
5961 }
5962
5963 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5964 if(nErrCode != -1) {
5965 return 20;
5966 }
5967
5968
5969 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5970 if(nErrCode == -1 || u16 != UINT16_MAX) {
5971 return 21;
5972 }
5973
5974 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5975 if(nErrCode == -1 || u16 != 0) {
5976 return 22;
5977 }
5978
5979 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5980 if(nErrCode == -1 || u16 != 1) {
5981 return 23;
5982 }
5983
5984 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5985 if(nErrCode != -1) {
5986 return 24;
5987 }
5988
5989 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5990 if(nErrCode != -1) {
5991 return 25;
5992 }
5993
5994
5995 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5996 if(nErrCode == -1 || u8 != UINT8_MAX) {
5997 return 26;
5998 }
5999
6000 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6001 if(nErrCode == -1 || u8 != 0) {
6002 return 27;
6003 }
6004
6005 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6006 if(nErrCode == -1 || u8 != 1) {
6007 return 28;
6008 }
6009
6010 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6011 if(nErrCode != -1) {
6012 return 29;
6013 }
6014
6015 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6016 if(nErrCode != -1) {
6017 return 30;
6018 }
6019
6020
6021 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6022 if(nErrCode == -1 || u64 != 1) {
6023 return 31;
6024 }
6025
6026 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6027 if(nErrCode == -1 || u64 != INT64_MAX) {
6028 return 32;
6029 }
6030
6031 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6032 if(nErrCode == -1 || u64 != 0) {
6033 return 33;
6034 }
6035
6036 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6037 if(nErrCode != -1) {
6038 return 34;
6039 }
6040
6041 return 0;
6042}
6043
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006044
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006045
6046
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006047/*
6048A sequence with
6049 A wrapping bstr
6050 containing a map
6051 1
6052 2
6053 A wrapping bstr
6054 containing an array
6055 3
6056 wrapping bstr
6057 4
6058 5
6059 6
6060 array
6061 7
6062 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006063 */
6064
Laurence Lundblade55013642020-09-23 05:39:22 -07006065static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006066{
Laurence Lundblade55013642020-09-23 05:39:22 -07006067 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006068 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006069 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006070
Laurence Lundblade55013642020-09-23 05:39:22 -07006071 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006072
6073 QCBOREncode_BstrWrap(&EC);
6074 QCBOREncode_OpenMap(&EC);
6075 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6076 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6077 QCBOREncode_CloseMap(&EC);
6078 QCBOREncode_BstrWrap(&EC);
6079 QCBOREncode_OpenArray(&EC);
6080 QCBOREncode_AddInt64(&EC, 3);
6081 QCBOREncode_BstrWrap(&EC);
6082 QCBOREncode_AddInt64(&EC, 4);
6083 QCBOREncode_CloseBstrWrap(&EC, NULL);
6084 QCBOREncode_AddInt64(&EC, 5);
6085 QCBOREncode_CloseArray(&EC);
6086 QCBOREncode_CloseBstrWrap(&EC, NULL);
6087 QCBOREncode_AddInt64(&EC, 6);
6088 QCBOREncode_CloseBstrWrap(&EC, NULL);
6089 QCBOREncode_OpenArray(&EC);
6090 QCBOREncode_AddInt64(&EC, 7);
6091 QCBOREncode_AddInt64(&EC, 8);
6092 QCBOREncode_CloseArray(&EC);
6093
6094 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006095 if(uErr) {
6096 Encoded = NULLUsefulBufC;
6097 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006098
6099 return Encoded;
6100}
6101
6102
6103int32_t EnterBstrTest()
6104{
Laurence Lundblade55013642020-09-23 05:39:22 -07006105 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006106
6107 QCBORDecodeContext DC;
6108
Laurence Lundblade55013642020-09-23 05:39:22 -07006109 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006110
Laurence Lundblade55013642020-09-23 05:39:22 -07006111 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006112
6113
Laurence Lundblade9b334962020-08-27 10:55:53 -07006114 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006115 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006116 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6117 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006118 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006119 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006120 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006121 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006122 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006123 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006124 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006125 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006126 QCBORDecode_ExitArray(&DC);
6127 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006128 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006129 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006130 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006131 QCBORDecode_GetInt64(&DC, &n7);
6132 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006133 QCBORDecode_ExitArray(&DC);
6134
6135 QCBORError uErr = QCBORDecode_Finish(&DC);
6136
6137 return (int32_t)uErr;
6138}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006139
6140
6141
6142
6143static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006144 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006145
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006146 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006147 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006148 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6149 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6150 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006151
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006152 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006153 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6154 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6155 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006156
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006157 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006158 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006159 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6160 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006161
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006162 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006163 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6164 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006165
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006166 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006167 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006168 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6169 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006170
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006171 21,
6172 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6173 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6174
6175 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006176 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006177 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6178 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006179
6180 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006181 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6182 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006183
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006184 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006185 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006186 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6187 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006188
6189 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006190 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6191 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006192
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006193 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006194 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006195 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6196 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006197
6198 0x18, 0x33,
6199 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6200
6201 // MIME
6202 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006203 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6204 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6205 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006206
6207 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006208 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6209 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006210
6211 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006212 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6213 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6214 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006215
6216 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006217 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6218 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006219
6220 // UUID
6221 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006222 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6223 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006224
6225 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006226 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6227 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006228};
6229
6230int32_t DecodeTaggedTypeTests()
6231{
6232 QCBORDecodeContext DC;
6233 QCBORError uErr;
6234
6235 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6236
6237 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006238 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006239
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006240 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006241 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006242 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006243 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6244 return 1;
6245 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006246 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006247 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6248 return 2;
6249 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006250 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006251 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6252 return 3;
6253 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006254 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006255 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006256 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6257 return 4;
6258 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006259 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006260 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006261 return 5;
6262 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006263
Laurence Lundblade9b334962020-08-27 10:55:53 -07006264 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006265 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6266 bNeg != false) {
6267 return 10;
6268 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006269 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006270 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6271 bNeg != true) {
6272 return 11;
6273 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006274 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006275 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6276 return 12;
6277 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006278 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006279 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006280 return 13;
6281 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006282 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006283 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006284 return 14;
6285 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006286
Laurence Lundblade9b334962020-08-27 10:55:53 -07006287 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006288 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6289 return 20;
6290 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006291 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006292 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6293 return 21;
6294 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006295 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006296 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006297 return 22;
6298 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006299 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006300 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006301 return 23;
6302 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006303
Laurence Lundblade9b334962020-08-27 10:55:53 -07006304 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006305 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6306 return 30;
6307 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006308 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006309 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6310 return 31;
6311 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006312 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006313 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006314 return 32;
6315 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006316 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006317 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006318 return 33;
6319 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006320
Laurence Lundblade9b334962020-08-27 10:55:53 -07006321 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006322 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6323 return 40;
6324 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006325 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006326 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6327 return 41;
6328 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006329 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006330 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006331 return 42;
6332 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006333 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006334 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006335 return 43;
6336 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006337
Laurence Lundblade9b334962020-08-27 10:55:53 -07006338 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006339 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6340 return 50;
6341 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006342 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006343 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6344 return 51;
6345 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006346 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006347 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006348 return 52;
6349 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006350 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006351 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006352 return 53;
6353 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006354
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006355 // MIME
6356 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006357 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006358 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6359 bIsNot7Bit == true) {
6360 return 60;
6361 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006362 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006363 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6364 bIsNot7Bit == true) {
6365 return 61;
6366 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006367 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006368 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6369 bIsNot7Bit == false) {
6370 return 62;
6371 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006372 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006373 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6374 bIsNot7Bit == false) {
6375 return 63;
6376 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006377 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006378 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006379 return 64;
6380 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006381 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006382 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006383 return 65;
6384 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006385
Laurence Lundblade9b334962020-08-27 10:55:53 -07006386 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006387 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6388 return 70;
6389 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006390 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006391 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6392 return 71;
6393 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006394 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006395 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006396 return 72;
6397 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006398 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006399 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006400 return 73;
6401 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006402
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006403 // Improvement: add some more error test cases
6404
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006405 QCBORDecode_ExitMap(&DC);
6406
6407 uErr = QCBORDecode_Finish(&DC);
6408 if(uErr != QCBOR_SUCCESS) {
6409 return 100;
6410 }
6411
6412 return 0;
6413}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006414
6415
6416
6417
6418/*
6419 [
6420 "aaaaaaaaaa",
6421 {}
6422 ]
6423 */
6424static const uint8_t spTooLarge1[] = {
6425 0x9f,
6426 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6427 0xa0,
6428 0xff
6429};
6430
6431/*
6432 [
6433 {
6434 0: "aaaaaaaaaa"
6435 }
6436 ]
6437 */
6438static const uint8_t spTooLarge2[] = {
6439 0x9f,
6440 0xa1,
6441 0x00,
6442 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6443 0xff
6444};
6445
6446/*
6447 h'A1006A61616161616161616161'
6448
6449 {
6450 0: "aaaaaaaaaa"
6451 }
6452 */
6453static const uint8_t spTooLarge3[] = {
6454 0x4d,
6455 0xa1,
6456 0x00,
6457 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6458};
6459
6460int32_t TooLargeInputTest(void)
6461{
6462 QCBORDecodeContext DC;
6463 QCBORError uErr;
6464 UsefulBufC String;
6465
6466 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6467 // to 10 There's not really any way to test this error
6468 // condition. The error condition is not complex, so setting
6469 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6470
6471 // The input CBOR is only too large because the
6472 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6473 //
6474 // This test is disabled for the normal test runs because of the
6475 // special build requirement.
6476
6477
6478 // Tests the start of a map being too large
6479 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006480 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006481 QCBORDecode_GetTextString(&DC, &String);
6482 uErr = QCBORDecode_GetError(&DC);
6483 if(uErr != QCBOR_SUCCESS) {
6484 return 1;
6485 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006486 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006487 uErr = QCBORDecode_GetError(&DC);
6488 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6489 return 2;
6490 }
6491
6492 // Tests the end of a map being too large
6493 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006494 QCBORDecode_EnterArray(&DC, NULL);
6495 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006496 uErr = QCBORDecode_GetError(&DC);
6497 if(uErr != QCBOR_SUCCESS) {
6498 return 3;
6499 }
6500 QCBORDecode_ExitMap(&DC);
6501 uErr = QCBORDecode_GetError(&DC);
6502 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6503 return 4;
6504 }
6505
6506 // Tests the entire input CBOR being too large when processing bstr wrapping
6507 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6508 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6509 uErr = QCBORDecode_GetError(&DC);
6510 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6511 return 5;
6512 }
6513
6514 return 0;
6515}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006516
6517
6518static const uint8_t spMapWithIndefLenStrings[] = {
6519 0xbf,
6520 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6521 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6522 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6523 0x03,
6524 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6525 0xc3,
6526 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6527 0xff
6528};
6529
6530int32_t SpiffyIndefiniteLengthStringsTests()
6531{
6532 QCBORDecodeContext DCtx;
6533
6534 QCBORDecode_Init(&DCtx,
6535 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6536 QCBOR_DECODE_MODE_NORMAL);
6537
6538 MakeUsefulBufOnStack(StringBuf, 200);
6539 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6540
6541 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006542 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006543 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6544 if(QCBORDecode_GetAndResetError(&DCtx)) {
6545 return 1;
6546 }
6547
6548 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6549 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6550 return 2;
6551 }
6552
6553 uint64_t uInt;
6554 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6555 if(QCBORDecode_GetAndResetError(&DCtx)) {
6556 return 3;
6557 }
6558 if(uInt != 3) {
6559 return 4;
6560 }
6561
6562 double uDouble;
6563 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6564 "label2",
6565 0xff,
6566 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006567#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006568 if(QCBORDecode_GetAndResetError(&DCtx)) {
6569 return 5;
6570 }
6571 if(uDouble != -16777474) {
6572 return 6;
6573 }
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006574#else
6575 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6576 return 7;
6577 }
6578#endif
6579
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006580
6581 QCBORDecode_ExitMap(&DCtx);
6582
6583 if(QCBORDecode_Finish(&DCtx)) {
6584 return 99;
6585 }
6586
6587 return 0;
6588}
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08006589
6590
6591
6592
6593int32_t PeekTest()
6594{
6595 QCBORItem Item;
6596 QCBORError nCBORError;
6597 QCBORDecodeContext DCtx;
6598
6599 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6600
6601 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6602 return 100+(int32_t)nCBORError;
6603 }
6604 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6605 return 200;
6606 }
6607
6608 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6609 return (int32_t)nCBORError;
6610 }
6611 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6612 return 300;
6613 }
6614
6615 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6616 return 400 + (int32_t)nCBORError;
6617 }
6618 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6619 return 500;
6620 }
6621
6622 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6623 return (int32_t)nCBORError;
6624 }
6625 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6626 return 600;
6627 }
6628
6629 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6630 return 900 + (int32_t)nCBORError;
6631 }
6632 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6633 Item.uDataType != QCBOR_TYPE_INT64 ||
6634 Item.val.int64 != 42 ||
6635 Item.uDataAlloc ||
6636 Item.uLabelAlloc ||
6637 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6638 return 1000;
6639 }
6640
6641 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6642 return 1100 + (int32_t)nCBORError;
6643 }
6644
6645 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6646 Item.uDataType != QCBOR_TYPE_INT64 ||
6647 Item.val.int64 != 42 ||
6648 Item.uDataAlloc ||
6649 Item.uLabelAlloc ||
6650 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6651 return 1200;
6652 }
6653
6654
6655 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6656 return 1300 + (int32_t)nCBORError;
6657 }
6658 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6659 Item.uDataAlloc ||
6660 Item.uLabelAlloc ||
6661 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
6662 Item.uDataType != QCBOR_TYPE_ARRAY ||
6663 Item.val.uCount != 2)
6664 return 1400;
6665
6666 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6667 return 1500 + (int32_t)nCBORError;
6668 }
6669 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6670 Item.uDataAlloc ||
6671 Item.uLabelAlloc ||
6672 UsefulBufCompareToSZ(Item.val.string, "string1")) {
6673 return 1600;
6674 }
6675
6676 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6677 return 1700 + (int32_t)nCBORError;
6678 }
6679 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6680 Item.uDataAlloc ||
6681 Item.uLabelAlloc ||
6682 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6683 return 1800;
6684 }
6685
6686 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6687 return (int32_t)nCBORError;
6688 }
6689 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6690 Item.uDataAlloc ||
6691 Item.uLabelAlloc ||
6692 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6693 return 1900;
6694 }
6695
6696 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6697 return (int32_t)nCBORError;
6698 }
6699 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6700 Item.uDataAlloc ||
6701 Item.uLabelAlloc ||
6702 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6703 return 2000;
6704 }
6705
6706
6707 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6708 return 2100 + (int32_t)nCBORError;
6709 }
6710 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6711 Item.uDataAlloc ||
6712 Item.uLabelAlloc ||
6713 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
6714 Item.uDataType != QCBOR_TYPE_MAP ||
6715 Item.val.uCount != 4) {
6716 return 2100;
6717 }
6718
6719 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6720 return 2200 + (int32_t)nCBORError;
6721 }
6722 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6723 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
6724 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6725 Item.uDataAlloc ||
6726 Item.uLabelAlloc ||
6727 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
6728 return 2300;
6729 }
6730
6731 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6732 return 2400 + (int32_t)nCBORError;
6733 }
6734 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6735 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6736 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6737 Item.uDataAlloc ||
6738 Item.uLabelAlloc ||
6739 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6740 return 2500;
6741 }
6742
6743 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6744 return 2600 + (int32_t)nCBORError;
6745 }
6746 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6747 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6748 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6749 Item.uDataAlloc ||
6750 Item.uLabelAlloc ||
6751 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6752 return 2700;
6753 }
6754
6755 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6756 return 2800 + (int32_t)nCBORError;
6757 }
6758 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6759 Item.uDataAlloc ||
6760 Item.uLabelAlloc ||
6761 UsefulBufCompareToSZ(Item.label.string, "another int") ||
6762 Item.uDataType != QCBOR_TYPE_INT64 ||
6763 Item.val.int64 != 98)
6764 return 2900;
6765
6766 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6767 return 3000 + (int32_t)nCBORError;
6768 }
6769 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6770 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
6771 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6772 Item.uDataAlloc ||
6773 Item.uLabelAlloc ||
6774 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
6775 return 3100;
6776 }
6777
6778 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6779 return 3200 + (int32_t)nCBORError;
6780 }
6781 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6782 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
6783 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6784 Item.uDataAlloc ||
6785 Item.uLabelAlloc ||
6786 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
6787 return 3300;
6788 }
6789
6790 return 0;
6791}