blob: e5585af65f877da7eb941dd7b664c7ab6a329177 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070036#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080037#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080038#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070039#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080040
Laurence Lundblade9b334962020-08-27 10:55:53 -070041// Handy macro to compare a UsefulBuf to a C string
42#define UsefulBufCompareToSZ(x, y) \
43 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044
Laurence Lundbladea2e29072018-12-30 09:20:06 -080045#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080046#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080047
48static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080049{
50 if(szLabel) {
51 printf("%s ", szLabel);
52 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080053
Laurence Lundblade570fab52018-10-13 18:28:27 +080054 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080055 for(i = 0; i < Buf.len; i++) {
56 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 printf("%02x ", Z);
58 }
59 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080060
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 fflush(stdout);
62}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080063#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064
65
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070066static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080067 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
69 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
70 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
71 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
72 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
73 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
74 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
75 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
76 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
77 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
78 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
79 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
80 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
81 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
82 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
83 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
84 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
85 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
86 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
87 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
88 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 0xff, 0xff};
91
92
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080093// return CBOR error or -1 if type of value doesn't match
94
Laurence Lundbladec5fef682020-01-25 11:38:45 -080095static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080096{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070097 QCBORItem Item;
98 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080099
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800100 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700101 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102 if(Item.uDataType != QCBOR_TYPE_ARRAY)
103 return -1;
104
105 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700106 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800107 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800108 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 return -1;
110
111 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700112 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800114 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800116
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700118 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800120 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700124 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800126 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800128
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700130 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800131 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800132 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800134
135
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800136 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700137 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 if(Item.uDataType != QCBOR_TYPE_INT64 ||
139 Item.val.int64 != -2147483648)
140 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800141
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800142 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700143 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if(Item.uDataType != QCBOR_TYPE_INT64 ||
145 Item.val.int64 != -2147483647)
146 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800147
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700149 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if(Item.uDataType != QCBOR_TYPE_INT64 ||
151 Item.val.int64 != -65538)
152 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800153
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800154 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700155 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if(Item.uDataType != QCBOR_TYPE_INT64 ||
157 Item.val.int64 != -65537)
158 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800159
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800160 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700161 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800162 if(Item.uDataType != QCBOR_TYPE_INT64 ||
163 Item.val.int64 != -65536)
164 return -1;
165
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700168 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if(Item.uDataType != QCBOR_TYPE_INT64 ||
170 Item.val.int64 != -65535)
171 return -1;
172
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800173
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800174 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700175 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 if(Item.uDataType != QCBOR_TYPE_INT64 ||
177 Item.val.int64 != -65534)
178 return -1;
179
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800180
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700182 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
184 Item.val.int64 != -257)
185 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800186
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800187 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700188 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if(Item.uDataType != QCBOR_TYPE_INT64 ||
190 Item.val.int64 != -256)
191 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800192
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800193 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700194 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(Item.uDataType != QCBOR_TYPE_INT64 ||
196 Item.val.int64 != -255)
197 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800198
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700200 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(Item.uDataType != QCBOR_TYPE_INT64 ||
202 Item.val.int64 != -254)
203 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800204
205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700207 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
209 Item.val.int64 != -25)
210 return -1;
211
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800212
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800213 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700214 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 if(Item.uDataType != QCBOR_TYPE_INT64 ||
216 Item.val.int64 != -24)
217 return -1;
218
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700221 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if(Item.uDataType != QCBOR_TYPE_INT64 ||
223 Item.val.int64 != -23)
224 return -1;
225
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800227 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700228 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800229 if(Item.uDataType != QCBOR_TYPE_INT64 ||
230 Item.val.int64 != -1)
231 return -1;
232
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800233
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800234 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700235 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 if(Item.uDataType != QCBOR_TYPE_INT64 ||
237 Item.val.int64 != 0)
238 return -1;
239
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800241 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700242 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 if(Item.uDataType != QCBOR_TYPE_INT64 ||
244 Item.val.int64 != 0)
245 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700248 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(Item.uDataType != QCBOR_TYPE_INT64 ||
250 Item.val.int64 != 1)
251 return -1;
252
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800254 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700255 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 if(Item.uDataType != QCBOR_TYPE_INT64 ||
257 Item.val.int64 != 22)
258 return -1;
259
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700262 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
264 Item.val.int64 != 23)
265 return -1;
266
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800268 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700269 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800270 if(Item.uDataType != QCBOR_TYPE_INT64 ||
271 Item.val.int64 != 24)
272 return -1;
273
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800274
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800275 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700276 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800277 if(Item.uDataType != QCBOR_TYPE_INT64 ||
278 Item.val.int64 != 25)
279 return -1;
280
281 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700282 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
284 Item.val.int64 != 26)
285 return -1;
286
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700289 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(Item.uDataType != QCBOR_TYPE_INT64 ||
291 Item.val.int64 != 254)
292 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800293
294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700296 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(Item.uDataType != QCBOR_TYPE_INT64 ||
298 Item.val.int64 != 255)
299 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800300
301
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700303 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(Item.uDataType != QCBOR_TYPE_INT64 ||
305 Item.val.int64 != 256)
306 return -1;
307
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800308
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800309 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700310 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(Item.uDataType != QCBOR_TYPE_INT64 ||
312 Item.val.int64 != 257)
313 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800315 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700316 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(Item.uDataType != QCBOR_TYPE_INT64 ||
318 Item.val.int64 != 65534)
319 return -1;
320
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700323 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(Item.uDataType != QCBOR_TYPE_INT64 ||
325 Item.val.int64 != 65535)
326 return -1;
327
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700330 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
332 Item.val.int64 != 65536)
333 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800334
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800335 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700336 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 if(Item.uDataType != QCBOR_TYPE_INT64 ||
338 Item.val.int64 != 65537)
339 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800341 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700342 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(Item.uDataType != QCBOR_TYPE_INT64 ||
344 Item.val.int64 != 65538)
345 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800347 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700348 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(Item.uDataType != QCBOR_TYPE_INT64 ||
350 Item.val.int64 != 2147483647)
351 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800352
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800353 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700354 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 if(Item.uDataType != QCBOR_TYPE_INT64 ||
356 Item.val.int64 != 2147483647)
357 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800359 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700360 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(Item.uDataType != QCBOR_TYPE_INT64 ||
362 Item.val.int64 != 2147483648)
363 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800364
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800365 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700366 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(Item.uDataType != QCBOR_TYPE_INT64 ||
368 Item.val.int64 != 2147483649)
369 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800370
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800371 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700372 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 if(Item.uDataType != QCBOR_TYPE_INT64 ||
374 Item.val.int64 != 4294967294)
375 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800378 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700379 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 if(Item.uDataType != QCBOR_TYPE_INT64 ||
381 Item.val.int64 != 4294967295)
382 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800383
384
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800385 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700386 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(Item.uDataType != QCBOR_TYPE_INT64 ||
388 Item.val.int64 != 4294967296)
389 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800390
391
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800392 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700393 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800394 if(Item.uDataType != QCBOR_TYPE_INT64 ||
395 Item.val.int64 != 4294967297)
396 return -1;
397
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800398
399
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800400 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700401 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800402 if(Item.uDataType != QCBOR_TYPE_INT64 ||
403 Item.val.int64 != 9223372036854775807LL)
404 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800405
406
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800407 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700408 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
410 Item.val.uint64 != 18446744073709551615ULL)
411 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800412
413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
415 return -1;
416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800418 return 0;
419}
420
421
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800422// One less than the smallest negative integer allowed in C. Decoding
423// this should fail.
424static const uint8_t spTooSmallNegative[] = {
425 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000426};
427
428
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800429/*
430 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800431 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800433int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000435 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800436 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000438 QCBORDecode_Init(&DCtx,
439 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
440 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800441
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000442 // The really big test of all successes
443 nReturn = IntegerValuesParseTestInternal(&DCtx);
444 if(nReturn) {
445 return nReturn;
446 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800447
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000448 // The one large negative integer that can be parsed
449 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800450 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000451 QCBOR_DECODE_MODE_NORMAL);
452
453 QCBORItem item;
454 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
455 nReturn = -4000;
456 }
457
458 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459}
460
461
462/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800463 Creates a simple CBOR array and returns it in *pEncoded. The array is
464 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 */
469
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800470static uint8_t spSimpleArrayBuffer[50];
471
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800472static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473{
474 QCBOREncodeContext ECtx;
475 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800476
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 *pEncoded = NULL;
478 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800479
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800481 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 // and last with the buffer to do the actual encoding
483 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700484 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800485 QCBOREncode_OpenArray(&ECtx);
486 QCBOREncode_AddInt64(&ECtx, nInt1);
487 QCBOREncode_AddInt64(&ECtx, nInt2);
488 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
489 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
490 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800491
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800492 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800493 goto Done;
494
495 if(*pEncoded != NULL) {
496 nReturn = 0;
497 goto Done;
498 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800499
500 // Use static buffer to avoid dependency on malloc()
501 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 goto Done;
503 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800504 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800505
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800508Done:
509 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800510}
511
512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800514 Some basic CBOR with map and array used in a lot of tests.
515 The map labels are all strings
516
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900518 "an array of two strings": [
519 "string1", "string2"
520 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521 "map in a map": {
522 "bytes 1": h'78787878',
523 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900524 "another int": 98,
525 "text 2": "lies, damn lies and statistics"
526 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800527 }
528 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800529static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700530 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
531 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
532 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
533 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
534 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
535 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
536 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
537 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
538 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
539 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
540 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
541 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
542 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
543 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
544 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700545 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800546
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700547// Same as above, but with indefinite lengths.
548static const uint8_t pValidMapIndefEncoded[] = {
5490xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5500x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5510x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5520x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5530x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5540x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5550x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5560x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5570x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5580x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5590x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5600x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5610x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5620x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5630x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5640x73, 0xff, 0xff};
565
566
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800567static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700568 size_t nLen,
569 int64_t *pInt1,
570 int64_t *pInt2,
571 const uint8_t **pBuf3,
572 size_t *pBuf3Len,
573 const uint8_t **pBuf4,
574 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800575{
576 QCBORDecodeContext DCtx;
577 QCBORItem Item;
578 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundbladeee851742020-01-08 08:37:05 -0800580 QCBORDecode_Init(&DCtx,
581 (UsefulBufC){pEncoded, nLen},
582 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800583
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800584 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700585 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
586 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800590 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700591 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
592 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800593 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700594 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800596
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700598 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
599 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800600 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700601 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800602 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700605 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
606 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700608 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 *pBuf3 = Item.val.string.ptr;
610 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700613 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
614 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800615 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700616 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 *pBuf4 = Item.val.string.ptr;
618 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800621
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800622Done:
623 return(nReturn);
624}
625
626
627
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800628
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800629int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800630{
631 uint8_t *pEncoded;
632 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800633
Laurence Lundblade5e390822019-01-06 12:35:01 -0800634 int64_t i1=0, i2=0;
635 size_t i3=0, i4=0;
636 const uint8_t *s3= (uint8_t *)"";
637 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800638
639
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800640 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
641 return(-1);
642 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800644 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800646 if(i1 != 23 ||
647 i2 != 6000 ||
648 i3 != 8 ||
649 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530650 memcmp("galactic", s3, 8) !=0 ||
651 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800652 return(-1);
653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655 return(0);
656}
657
658
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700659/*
660 [
661 0,
662 [],
663 [
664 [],
665 [
666 0
667 ],
668 {},
669 {
670 1: {},
671 2: {},
672 3: []
673 }
674 ]
675 ]
676 */
677static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
678 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
679
Laurence Lundblade02625d42020-06-25 14:41:41 -0700680/* Same as above, but with indefinte lengths */
681static uint8_t sEmptiesIndef[] = {
6820x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700683 0x00,
684 0x9F,
685 0xFF,
686 0x9F,
687 0x9F,
688 0xFF,
689 0x9F,
690 0x00,
691 0xFF,
692 0xBF,
693 0xFF,
694 0xBF,
695 0x01,
696 0xBF,
697 0xFF,
698 0x02,
699 0xBF,
700 0xFF,
701 0x03,
702 0x9F,
703 0xFF,
704 0xFF,
705 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700706 0xFF};
707
708
709
710static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700711{
712 QCBORDecodeContext DCtx;
713 QCBORItem Item;
714
Laurence Lundbladeee851742020-01-08 08:37:05 -0800715 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700716 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800717 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700718
719 // Array with 3 items
720 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
721 Item.uDataType != QCBOR_TYPE_ARRAY ||
722 Item.uNestingLevel != 0 ||
723 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700724 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700725 return -1;
726 }
727
728 // An integer 0
729 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
730 Item.uDataType != QCBOR_TYPE_INT64 ||
731 Item.uNestingLevel != 1 ||
732 Item.uNextNestLevel != 1 ||
733 Item.val.uint64 != 0) {
734 return -2;
735 }
736
737 // An empty array
738 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
739 Item.uDataType != QCBOR_TYPE_ARRAY ||
740 Item.uNestingLevel != 1 ||
741 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700742 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700743 return -3;
744 }
745
746 // An array with 4 items
747 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
748 Item.uDataType != QCBOR_TYPE_ARRAY ||
749 Item.uNestingLevel != 1 ||
750 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700751 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700752 return -4;
753 }
754
755 // An empty array
756 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
757 Item.uDataType != QCBOR_TYPE_ARRAY ||
758 Item.uNestingLevel != 2 ||
759 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700760 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700761 return -5;
762 }
763
764 // An array with 1 item
765 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
766 Item.uDataType != QCBOR_TYPE_ARRAY ||
767 Item.uNestingLevel != 2 ||
768 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700769 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700770 return -6;
771 }
772
773 // An integer 0
774 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
775 Item.uDataType != QCBOR_TYPE_INT64 ||
776 Item.uNestingLevel != 3 ||
777 Item.uNextNestLevel != 2 ||
778 Item.val.uint64 != 0) {
779 return -7;
780 }
781
782 // An empty map
783 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
784 Item.uDataType != QCBOR_TYPE_MAP ||
785 Item.uNestingLevel != 2 ||
786 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700787 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700788 return -8;
789 }
790
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700791 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700792 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
793 Item.uDataType != QCBOR_TYPE_MAP ||
794 Item.uNestingLevel != 2 ||
795 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700796 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700797 return -9;
798 }
799
800 // An empty map
801 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
802 Item.uDataType != QCBOR_TYPE_MAP ||
803 Item.uNestingLevel != 3 ||
804 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700805 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700806 return -10;
807 }
808
809 // An empty map
810 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
811 Item.uDataType != QCBOR_TYPE_MAP ||
812 Item.uNestingLevel != 3 ||
813 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700814 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700815 return -11;
816 }
817
818 // An empty array
819 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
820 Item.uDataType != QCBOR_TYPE_ARRAY ||
821 Item.uNestingLevel != 3 ||
822 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700823 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700824 return -12;
825 }
826
827 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
828 return -13;
829 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700830 return 0;
831}
832
833
834int32_t EmptyMapsAndArraysTest()
835{
836 int nResult;
837 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
838 true);
839 if(nResult) {
840 return nResult;
841 }
842
843 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
844 false);
845
846 if(nResult) {
847 return nResult -100;
848 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700849
850 return 0;
851}
852
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800853
Laurence Lundbladeee851742020-01-08 08:37:05 -0800854static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
855 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800856
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800857int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800858{
859 QCBORDecodeContext DCtx;
860 int nReturn = 0;
861 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800862
Laurence Lundbladeee851742020-01-08 08:37:05 -0800863 QCBORDecode_Init(&DCtx,
864 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
865 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800866
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800867 for(i = 0; i < 10; i++) {
868 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
871 Item.uDataType != QCBOR_TYPE_ARRAY ||
872 Item.uNestingLevel != i) {
873 nReturn = -1;
874 break;
875 }
876 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800877
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800878 return(nReturn);
879}
880
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700881// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800882static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
883 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
884 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
885 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800886
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800887int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888{
889 QCBORDecodeContext DCtx;
890 int nReturn = 0;
891 int i;
892 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800893
894
Laurence Lundbladeee851742020-01-08 08:37:05 -0800895 QCBORDecode_Init(&DCtx,
896 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
897 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700899 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800900
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
902 Item.uDataType != QCBOR_TYPE_ARRAY ||
903 Item.uNestingLevel != i) {
904 nReturn = -1;
905 break;
906 }
907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800908
Laurence Lundbladea9489f82020-09-12 13:50:56 -0700909 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800911
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800912 return(nReturn);
913}
914
915
916
917
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800918int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700920 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800921
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800922 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700923 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800924
Laurence Lundbladeee851742020-01-08 08:37:05 -0800925 QCBORDecode_Init(&DCtx,
926 (UsefulBufC){spExpectedEncodedInts, nNum},
927 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800928
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800929 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700931 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800932 nResult = -1;
933 goto Done;
934 }
935 }
936Done:
937 return nResult;
938}
939
940
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800941
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800942int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800943{
944 uint8_t *pEncoded;
945 int nReturn;
946 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800947
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800948 int64_t i1, i2;
949 size_t i3, i4;
950 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800951
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800953
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
955 return(-1);
956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800957
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -0700959 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
960 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800961 if(nResult == 0) {
962 nReturn = -1;
963 }
964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800965
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800966 return(nReturn);
967}
968
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530969/*
970 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800971 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
972 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530973 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800974static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975{
976 QCBORDecodeContext DCtx;
977 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700978 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800979
Laurence Lundbladeee851742020-01-08 08:37:05 -0800980 QCBORDecode_Init(&DCtx,
981 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
982 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800983
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900984 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700985 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800987 if(Item.uDataType != QCBOR_TYPE_MAP ||
988 Item.val.uCount != 3)
989 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800990
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700992 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900993 }
Laurence Lundblade9b334962020-08-27 10:55:53 -0700994
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800995 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800996 Item.uDataType != QCBOR_TYPE_INT64 ||
997 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530998 Item.uDataAlloc ||
999 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001000 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001002 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001003
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001005 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001007 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301008 Item.uDataAlloc ||
1009 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001010 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001011 Item.uDataType != QCBOR_TYPE_ARRAY ||
1012 Item.val.uCount != 2)
1013 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001014
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001015 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001016 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001017 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001018 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301019 Item.uDataAlloc ||
1020 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001021 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001022 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001024
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001025 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001026 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301029 Item.uDataAlloc ||
1030 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001031 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001032 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001034
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001036 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001037 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301039 Item.uDataAlloc ||
1040 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001041 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001042 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001043 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001045 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001046
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001047 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001048 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001049 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001050 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001051 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001052 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301053 Item.uDataAlloc ||
1054 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001055 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001056 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001058
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001060 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001061 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001062 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001063 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001064 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301065 Item.uDataAlloc ||
1066 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001067 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001068 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001069 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001070
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001071 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001072 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001073 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301075 Item.uDataAlloc ||
1076 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001077 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001078 Item.uDataType != QCBOR_TYPE_INT64 ||
1079 Item.val.int64 != 98)
1080 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001081
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001082 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001083 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001084 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001085 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001086 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001087 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301088 Item.uDataAlloc ||
1089 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001090 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001093
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001094 return 0;
1095}
1096
1097
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098/*
1099 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001100 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001102int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001103{
1104 QCBORDecodeContext DCtx;
1105 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001106 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001107
Laurence Lundbladeee851742020-01-08 08:37:05 -08001108 QCBORDecode_Init(&DCtx,
1109 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1110 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001111
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001113 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001115 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1116 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 return -1;
1118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001119
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001120 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001121 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 }
1123 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1124 Item.uDataAlloc ||
1125 Item.uLabelAlloc ||
1126 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001127 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 return -2;
1129 }
1130
1131 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001132 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 }
1134 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1135 Item.uDataType != QCBOR_TYPE_INT64 ||
1136 Item.val.int64 != 42 ||
1137 Item.uDataAlloc ||
1138 Item.uLabelAlloc) {
1139 return -3;
1140 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001141
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001143 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 }
1145 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1146 Item.uDataAlloc ||
1147 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001148 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001149 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1150 return -4;
1151 }
1152
1153 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001154 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 }
1156 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1157 Item.uDataAlloc ||
1158 Item.uLabelAlloc ||
1159 Item.uDataType != QCBOR_TYPE_ARRAY ||
1160 Item.val.uCount != 2) {
1161 return -5;
1162 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001163
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001164 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001165 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 }
1167 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1168 Item.val.string.len != 7 ||
1169 Item.uDataAlloc ||
1170 Item.uLabelAlloc ||
1171 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1172 return -6;
1173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001174
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001176 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 }
1178 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1179 Item.uDataAlloc ||
1180 Item.uLabelAlloc ||
1181 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1182 return -7;
1183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001184
1185
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001186 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001187 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 }
1189 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1190 Item.uDataAlloc ||
1191 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001192 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 return -8;
1194 }
1195
1196 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001197 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001198 }
1199 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1200 Item.uDataAlloc ||
1201 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001202 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1203 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001204 return -9;
1205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001206
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001207 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001208 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 }
1210 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001211 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1213 Item.uDataAlloc ||
1214 Item.uLabelAlloc) {
1215 return -10;
1216 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001217
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001219 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 }
1221 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1222 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1223 Item.uDataAlloc ||
1224 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001225 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 return -11;
1227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001228
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001230 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 }
1232 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001233 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001234 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1235 Item.uDataAlloc ||
1236 Item.uLabelAlloc) {
1237 return -12;
1238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001239
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001240 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001241 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001242 }
1243 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1244 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1245 Item.uDataAlloc ||
1246 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001247 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001248 return -13;
1249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001250
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001251 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001252 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001253 }
1254 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1255 Item.uDataAlloc ||
1256 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001257 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1259 return -14;
1260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001261
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001262 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001263 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001264 }
1265 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1266 Item.uDataAlloc ||
1267 Item.uLabelAlloc ||
1268 Item.uDataType != QCBOR_TYPE_INT64 ||
1269 Item.val.int64 != 98) {
1270 return -15;
1271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001272
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001273 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001274 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001275 }
1276 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001277 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001278 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1279 Item.uDataAlloc ||
1280 Item.uLabelAlloc) {
1281 return -16;
1282 }
1283
1284 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001285 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001286 }
1287 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1288 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1289 Item.uDataAlloc ||
1290 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001291 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001292 return -17;
1293 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001294
1295
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001296 /*
1297 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1298 map that when interpreted as an array will be too many. Test
1299 data just has the start of the map, not all the items in the map.
1300 */
1301 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001302
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001303 QCBORDecode_Init(&DCtx,
1304 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1305 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001306
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001307 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001308 return -50;
1309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001310
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001311 // TODO: test decoding of labels that are arrays or such
1312 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1313
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001314 return 0;
1315}
1316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001317
1318/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301319 Fully or partially decode pValidMapEncoded. When
1320 partially decoding check for the right error code.
1321 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301323 The partial decodes test error conditions of
1324 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001325
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301326 This could be combined with the above test
1327 and made prettier and maybe a little more
1328 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001330static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001331{
1332 QCBORDecodeContext DCtx;
1333 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001334 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001335
Laurence Lundbladeee851742020-01-08 08:37:05 -08001336 QCBORDecode_Init(&DCtx,
1337 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1338 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001339
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001340 if(nLevel < 1) {
1341 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1342 return -1;
1343 } else {
1344 return 0;
1345 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001346 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301347
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001348
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001350 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 if(Item.uDataType != QCBOR_TYPE_MAP ||
1353 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001354 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001355
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001356 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001357 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1358 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001359 } else {
1360 return 0;
1361 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001363
1364
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001366 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001367 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 Item.uDataType != QCBOR_TYPE_INT64 ||
1370 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001371 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001372 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001374
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001375 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001376 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1377 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001378 } else {
1379 return 0;
1380 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001382
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001383 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001384 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001385 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001386 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001387 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001389 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001390 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001392
1393
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001394 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001395 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1396 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001397 } else {
1398 return 0;
1399 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001401
1402
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001403 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001404 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001405 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001407 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001408 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001409 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001410
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001411 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001412 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1413 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001414 } else {
1415 return 0;
1416 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001417 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001418
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001420 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001421 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001423 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001424 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001426
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001427 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001428 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1429 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001430 } else {
1431 return 0;
1432 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001433 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001434
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001435 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001436 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001437 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001439 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 Item.uDataType != QCBOR_TYPE_MAP ||
1441 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001442 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001443
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001445 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1446 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001447 } else {
1448 return 0;
1449 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001451
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001452 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001453 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001454 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001455 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001456 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001458 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001459 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001461
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001462 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001463 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1464 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001465 } else {
1466 return 0;
1467 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001469
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001470 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001471 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001472 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001474 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001475 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001476 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001477 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001479
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001480 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001481 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1482 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001483 } else {
1484 return 0;
1485 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001486 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001489 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001490 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001491 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001492 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001493 Item.uDataType != QCBOR_TYPE_INT64 ||
1494 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001495 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001496
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001497 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001498 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1499 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001500 } else {
1501 return 0;
1502 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001504
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001505 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001506 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001507 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001509 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001511 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001512 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001514
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301515 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001516 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001517 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001518
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001519 return 0;
1520}
1521
1522
1523
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001524
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001525int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001527 // Parse a moderatly complex map structure very thoroughly
1528 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1529 if(nResult) {
1530 return nResult;
1531 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001532
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001533 // Again, but in strings-only mode. It should succeed since the input
1534 // map has only string labels.
1535 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1536 if(nResult) {
1537 return nResult;
1538 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001539
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001540 // Again, but try to finish the decoding before the end of the
1541 // input at 10 different place and see that the right error code
1542 // is returned.
1543 for(int i = 0; i < 10; i++) {
1544 nResult = ExtraBytesTest(i);
1545 if(nResult) {
1546 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001547 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001549
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001550 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551}
1552
1553
Laurence Lundbladeee851742020-01-08 08:37:05 -08001554static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1555 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1556 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001558int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001559{
1560 QCBORDecodeContext DCtx;
1561 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001562 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001563
1564
Laurence Lundbladeee851742020-01-08 08:37:05 -08001565 QCBORDecode_Init(&DCtx,
1566 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1567 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001568
1569
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001570 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001571 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001572 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1573 Item.val.uCount != 10)
1574 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001577 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001578 if(Item.uDataType != QCBOR_TYPE_FALSE)
1579 return -1;
1580
1581 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001582 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583 if(Item.uDataType != QCBOR_TYPE_TRUE)
1584 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001586 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001587 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588 if(Item.uDataType != QCBOR_TYPE_NULL)
1589 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001591 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001592 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001593 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1594 return -1;
1595
1596 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001597 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001598 return -1;
1599
1600 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001601 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001602 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1603 return -1;
1604
1605 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001606 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001607 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1608 return -1;
1609
Laurence Lundblade077475f2019-04-26 09:06:33 -07001610 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001611 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001612
Laurence Lundblade077475f2019-04-26 09:06:33 -07001613 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001614 return -1;
1615
Laurence Lundblade077475f2019-04-26 09:06:33 -07001616 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001617 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001618
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001619 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001620 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001621 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1622 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001624 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001625 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001626 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1627 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001628
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001629 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001630
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001631}
1632
1633
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001634int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001635{
1636 // Loop over all the not-well-formed instance of CBOR
1637 // that are test vectors in not_well_formed_cbor.h
1638 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1639 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1640 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1641 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1642
Laurence Lundbladeee851742020-01-08 08:37:05 -08001643 // Set up decoder context. String allocator needed for indefinite
1644 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001645 QCBORDecodeContext DCtx;
1646 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1647 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1648 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1649
1650 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001651 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001652 do {
1653 QCBORItem Item;
1654
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001655 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1656 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001657
1658 // Every test vector must fail with
1659 // a not-well-formed error. If not
1660 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001661 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001662 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001663 // Return index of failure in the error code
1664 return 2000 + nIterate;
1665 }
1666 }
1667 return 0;
1668}
1669
1670
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001671struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001672 UsefulBufC Input;
1673 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001674};
1675
Laurence Lundblade59289e52019-12-30 13:44:37 -08001676
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001677static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001678{
1679 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1680 // Set up the decoding context including a memory pool so that
1681 // indefinite length items can be checked
1682 QCBORDecodeContext DCtx;
1683 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1684 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001685
Laurence Lundblade59289e52019-12-30 13:44:37 -08001686 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1687 if(nCBORError) {
1688 return -9;
1689 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001690
Laurence Lundblade59289e52019-12-30 13:44:37 -08001691 // Iterate until there is an error of some sort error
1692 QCBORItem Item;
1693 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001694 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001695 memset(&Item, 0x33, sizeof(Item));
1696
1697 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1698 } while(nCBORError == QCBOR_SUCCESS);
1699
1700 // Must get the expected error or the this test fails
1701 // The data and label type must also be QCBOR_TYPE_NONE
1702 if(nCBORError != pF->nError ||
1703 Item.uDataType != QCBOR_TYPE_NONE ||
1704 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001705 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001706 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001707 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001708 }
1709 }
1710
1711 return 0;
1712}
1713
1714
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001715struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001716 // Most of this is copied from not_well_formed.h. Here the error code
1717 // returned is also checked.
1718
1719 // Indefinite length strings must be closed off
1720 // An indefinite length byte string not closed off
1721 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1722 // An indefinite length text string not closed off
1723 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1724
1725
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001726 // All the chunks in an indefinite length string must be of the type of
1727 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001728 // indefinite length byte string with text string chunk
1729 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1730 // indefinite length text string with a byte string chunk
1731 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1732 // indefinite length byte string with an positive integer chunk
1733 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1734 // indefinite length byte string with an negative integer chunk
1735 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1736 // indefinite length byte string with an array chunk
1737 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1738 // indefinite length byte string with an map chunk
1739 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1740 // indefinite length byte string with tagged integer chunk
1741 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1742 // indefinite length byte string with an simple type chunk
1743 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1744 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1745 // indefinite length text string with indefinite string inside
1746 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1747
1748
1749 // Definte length maps and arrays must be closed by having the right number of items
1750 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001751 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001753 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001754 // A definte length array that is supposed to have 511 items, but has only 1
1755 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1756 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001757 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001758 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001759 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001760
1761
1762 // Indefinte length maps and arrays must be ended by a break
1763 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001764 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001766 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001767 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001768 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001769 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001770 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001771
1772
1773 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001774 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001775 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001776 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001777 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001778 // Unclosed indefinite map containing a closed definite length array
1779 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1780 // Definite length map containing an unclosed indefinite length array
1781 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001782 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001783 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001784 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001785 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001786 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001787 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001788 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001789 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001790 // Unclosed indefinite length map in definite length maps
1791 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1792 QCBOR_ERR_NO_MORE_ITEMS},
1793 // Unclosed definite length map in indefinite length maps
1794 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1795 // Unclosed indefinite length array in definite length maps
1796 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1797 QCBOR_ERR_NO_MORE_ITEMS},
1798 // Unclosed definite length array in indefinite length maps
1799 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1800 // Unclosed indefinite length map in definite length arrays
1801 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1802 // Unclosed definite length map in indefinite length arrays
1803 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001804
1805
1806 // The "argument" for the data item is incomplete
1807 // Positive integer missing 1 byte argument
1808 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1809 // Positive integer missing 2 byte argument
1810 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1811 // Positive integer missing 4 byte argument
1812 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1813 // Positive integer missing 8 byte argument
1814 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1815 // Positive integer missing 1 byte of 2 byte argument
1816 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1817 // Positive integer missing 2 bytes of 4 byte argument
1818 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1819 // Positive integer missing 1 bytes of 7 byte argument
1820 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1821 // Negative integer missing 1 byte argument
1822 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1823 // Binary string missing 1 byte argument
1824 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1825 // Text string missing 1 byte argument
1826 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1827 // Array missing 1 byte argument
1828 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1829 // Map missing 1 byte argument
1830 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1831 // Tag missing 1 byte argument
1832 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1833 // Simple missing 1 byte argument
1834 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001835 // half-precision with 1 byte argument
1836 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1837 // single-precision with 2 byte argument
1838 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1839 // double-precision with 3 byte argument
1840 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1841
1842
1843 // Tag with no content
1844 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001845
1846
1847 // Breaks must not occur in definite length arrays and maps
1848 // Array of length 1 with sole member replaced by a break
1849 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1850 // Array of length 2 with 2nd member replaced by a break
1851 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1852 // Map of length 1 with sole member label replaced by a break
1853 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1854 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001855 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001856 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1857 // Array of length 1 with 2nd member value replaced by a break
1858 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1859 // Map of length 2 with 2nd member replaced by a break
1860 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1861
1862
1863 // Breaks must not occur on their own out of an indefinite length data item
1864 // A bare break is not well formed
1865 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1866 // A bare break after a zero length definite length array
1867 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1868 // A bare break after a zero length indefinite length map
1869 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001870 // A break inside a definite length array inside an indefenite length array
1871 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1872 // Complicated mixed nesting with break outside indefinite length array
1873 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001874
1875
1876 // Forbidden two byte encodings of simple types
1877 // Must use 0xe0 instead
1878 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1879 // Should use 0xe1 instead
1880 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1881 // Should use 0xe2 instead
1882 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1883 // Should use 0xe3 instead
1884 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1885 // Should use 0xe4 instead
1886 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1887 // Should use 0xe5 instead
1888 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1889 // Should use 0xe6 instead
1890 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1891 // Should use 0xe7 instead
1892 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1893 // Should use 0xe8 instead
1894 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1895 // Should use 0xe9 instead
1896 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1897 // Should use 0xea instead
1898 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1899 // Should use 0xeb instead
1900 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1901 // Should use 0xec instead
1902 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1903 // Should use 0xed instead
1904 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1905 // Should use 0xee instead
1906 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1907 // Should use 0xef instead
1908 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1909 // Should use 0xf0 instead
1910 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1911 // Should use 0xf1 instead
1912 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1913 // Should use 0xf2 instead
1914 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1915 // Must use 0xf3 instead
1916 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1917 // Must use 0xf4 instead
1918 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1919 // Must use 0xf5 instead
1920 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1921 // Must use 0xf6 instead
1922 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1923 // Must use 0xf7 instead
1924 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1925 // Must use 0xf8 instead
1926 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001927 // Reserved
1928 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001929
1930 // Integers with additional info indefinite length
1931 // Positive integer with additional info indefinite length
1932 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1933 // Negative integer with additional info indefinite length
1934 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1935 // CBOR tag with "argument" an indefinite length
1936 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1937 // CBOR tag with "argument" an indefinite length alternate vector
1938 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1939
1940
1941 // Missing bytes from a deterministic length string
1942 // A byte string is of length 1 without the 1 byte
1943 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1944 // A text string is of length 1 without the 1 byte
1945 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001946 // Byte string should have 2^32-15 bytes, but has one
1947 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1948 // Byte string should have 2^32-15 bytes, but has one
1949 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001950 // Byte string should have 2^64 bytes, but has 3
1951 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1952 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
1953 // Text string should have 2^64 bytes, but has 3
1954 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1955 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001956
1957 // Use of unassigned additional information values
1958 // Major type positive integer with reserved value 28
1959 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1960 // Major type positive integer with reserved value 29
1961 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1962 // Major type positive integer with reserved value 30
1963 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1964 // Major type negative integer with reserved value 28
1965 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1966 // Major type negative integer with reserved value 29
1967 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1968 // Major type negative integer with reserved value 30
1969 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1970 // Major type byte string with reserved value 28 length
1971 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1972 // Major type byte string with reserved value 29 length
1973 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1974 // Major type byte string with reserved value 30 length
1975 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1976 // Major type text string with reserved value 28 length
1977 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1978 // Major type text string with reserved value 29 length
1979 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1980 // Major type text string with reserved value 30 length
1981 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1982 // Major type array with reserved value 28 length
1983 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1984 // Major type array with reserved value 29 length
1985 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1986 // Major type array with reserved value 30 length
1987 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1988 // Major type map with reserved value 28 length
1989 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1990 // Major type map with reserved value 29 length
1991 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1992 // Major type map with reserved value 30 length
1993 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1994 // Major type tag with reserved value 28 length
1995 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1996 // Major type tag with reserved value 29 length
1997 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1998 // Major type tag with reserved value 30 length
1999 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2000 // Major type simple with reserved value 28 length
2001 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2002 // Major type simple with reserved value 29 length
2003 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2004 // Major type simple with reserved value 30 length
2005 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2006
2007
2008 // Maps must have an even number of data items (key & value)
2009 // Map with 1 item when it should have 2
2010 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2011 // Map with 3 item when it should have 4
2012 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
2013 // Map with 1 item when it should have 2
2014 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2015 // Map with 3 item when it should have 4
2016 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
2017
2018
2019 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002020 // Text-based date, with an integer
2021 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2022 // Epoch date, with an byte string
2023 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2024 // tagged as both epoch and string dates
2025 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2026 // big num tagged an int, not a byte string
2027 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002028};
2029
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002030int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002031{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002032 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002033
Laurence Lundblade59289e52019-12-30 13:44:37 -08002034 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
2035 if(nResult) {
2036 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002037 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002038
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002039 // Corrupt the UsefulInputBuf and see that
2040 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002041 QCBORDecodeContext DCtx;
2042 QCBORItem Item;
2043 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002044
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002045 QCBORDecode_Init(&DCtx,
2046 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2047 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002048
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002049 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2050 return (int32_t)uQCBORError;
2051 }
2052 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2053 // This wasn't supposed to happen
2054 return -1;
2055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002056
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002057 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002058
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002059 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2060 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2061 // Did not get back the error expected
2062 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002063 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002064
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002065
Laurence Lundblade98427e92020-09-28 21:33:23 -07002066 /*
2067 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2068 tests here can be performed to see that the max length
2069 error check works correctly. See DecodeBytes(). If the max
2070 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002071
Laurence Lundblade98427e92020-09-28 21:33:23 -07002072 This test will automatocally adapt the all CPU sizes
2073 through the use of SIZE_MAX.
2074 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002075
Laurence Lundblade98427e92020-09-28 21:33:23 -07002076 MakeUsefulBufOnStack( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
2077 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002078
Laurence Lundblade98427e92020-09-28 21:33:23 -07002079 // This makes a CBOR head with a text string that is very long
2080 // but doesn't fill in the bytes of the text string as that is
2081 // not needed to test this part of QCBOR.
2082 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2083
2084 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2085
2086 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2087 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002088 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002089
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002090 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091}
2092
2093
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002094/* Try all 256 values of the byte at nLen including recursing for
2095 each of the values to try values at nLen+1 ... up to nLenMax
2096 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002097static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002098{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002099 if(nLen >= nLenMax) {
2100 return;
2101 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002102
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002103 for(int inputByte = 0; inputByte < 256; inputByte++) {
2104 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002105 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002106 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002107
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002108 // Get ready to parse
2109 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002110 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002111
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002112 // Parse by getting the next item until an error occurs
2113 // Just about every possible decoder error can occur here
2114 // The goal of this test is not to check for the correct
2115 // error since that is not really possible. It is to
2116 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002117 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002118 QCBORItem Item;
2119 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002120 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002121 break;
2122 }
2123 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002124
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002125 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002126 }
2127}
2128
2129
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002130int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002132 // Size 2 tests 64K inputs and runs quickly
2133 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002134
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002135 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002136
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002137 return 0;
2138}
2139
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002140
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002141int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002142{
2143 // size 3 tests 16 million inputs and runs OK
2144 // in seconds on fast machines. Size 4 takes
2145 // 10+ minutes and 5 half a day on fast
2146 // machines. This test is kept separate from
2147 // the others so as to no slow down the use
2148 // of them as a very frequent regression.
2149 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002150
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002151 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002152
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002153 return 0;
2154}
2155
2156
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002157static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158 0xc0, // tag for string date
2159 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002160
Laurence Lundbladec7114722020-08-13 05:11:40 -07002161 0xc0, // tag for string date
2162 0x00, // Wrong type for a string date
2163
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164 0xc1, // tag for epoch date
2165 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2166
Laurence Lundbladec7114722020-08-13 05:11:40 -07002167 0xc1,
2168 0x62, 'h', 'i', // wrong type tagged
2169
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002170 // CBOR_TAG_B64
Laurence Lundblade9b334962020-08-27 10:55:53 -07002171 0xcf, 0xd8, 0x22, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002172 0x1a, 0x53, 0x72, 0x4E, 0x01,
2173
2174 0xc1, // tag for epoch date
2175 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002177 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002178 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002179
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002180 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002181 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002182
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002183 0xc1, // tag for epoch date
2184 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2185 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2186
2187 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002188 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2189
2190 0xc1, // tag for epoch date
2191 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2192
2193 0xc1,
2194 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2195
2196 0xc1, // tag for epoch date
2197 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002198};
2199
2200
Laurence Lundbladec7114722020-08-13 05:11:40 -07002201
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002202// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002203#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002204static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002206 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002208 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002209
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002210 return diff > 0.0000001;
2211}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002212#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213
2214
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002215int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002216{
2217 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002218 QCBORItem Item;
2219 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002220
Laurence Lundbladeee851742020-01-08 08:37:05 -08002221 QCBORDecode_Init(&DCtx,
2222 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2223 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002224
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002225 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002226 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002228 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002229 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002230 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002231 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002232 }
2233
Laurence Lundbladec7114722020-08-13 05:11:40 -07002234 // Wrong type for a string date
2235 uError = QCBORDecode_GetNext(&DCtx, &Item);
2236 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002237 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002238 }
2239
2240 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2241 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2242 return -4;
2243 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002244 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2245 Item.val.epochDate.nSeconds != 1400000000 ||
2246 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002247 return -5;
2248 }
2249
2250 // Wrong type for an epoch date
2251 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2252 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002254
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002255 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2256 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002257 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2258 return -7;
2259 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002260 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2261 Item.val.epochDate.nSeconds != 1400000001 ||
2262 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002263 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002264 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002267 // Epoch date that is too large for our representation
2268 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002269 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002271
Laurence Lundblade9682a532020-06-06 18:33:04 -07002272#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002273 // Epoch date in float format with fractional seconds
2274 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2275 return -10;
2276 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002277 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2278 Item.val.epochDate.nSeconds != 1 ||
2279 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002280 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002283 // Epoch date float that is too large for our representation
2284 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002285 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002287
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002288 // Epoch date double that is just slightly too large
2289 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002290 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002291 }
2292
2293 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002294 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2295 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2296 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2297 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002298 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002299 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002300
2301 // Nan
2302 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2303 return -15;
2304 }
2305
2306 // +Inifinity double-precision
2307 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2308 return -16;
2309 }
2310
2311#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2312 // -Inifinity half-precision
2313 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2314 return -17;
2315 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002316#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002317 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2318 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002319 }
2320#endif
2321
Laurence Lundbladec7114722020-08-13 05:11:40 -07002322#else
2323 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2324 return -19;
2325 }
2326 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2327 return -20;
2328 }
2329 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2330 return -21;
2331 }
2332 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2333 return -22;
2334 }
2335 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2336 return -23;
2337 }
2338 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2339 return -24;
2340 }
2341#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2342 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2343 return -25;
2344 }
2345#else
2346 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2347 return -26;
2348 }
2349#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002350
Laurence Lundbladec7114722020-08-13 05:11:40 -07002351#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002352
2353 return 0;
2354}
2355
Laurence Lundblade4b270642020-08-14 12:53:07 -07002356/*
2357 Test cases covered here. Some items cover more than one of these.
2358 positive integer (zero counts as a positive integer)
2359 negative integer
2360 half-precision float
2361 single-precision float
2362 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002363
Laurence Lundblade4b270642020-08-14 12:53:07 -07002364 float Overflow error
2365 Wrong type error for epoch
2366 Wrong type error for date string
2367 float disabled error
2368 half-precision disabled error
2369 -Infinity
2370 Slightly too large integer
2371 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002372
Laurence Lundblade4b270642020-08-14 12:53:07 -07002373 Get epoch by int
2374 Get string by int
2375 Get epoch by string
2376 Get string by string
2377 Fail to get epoch by wrong int label
2378 Fail to get string by wrong string label
2379 Fail to get epoch by string because it is invalid
2380 Fail to get epoch by int because it is invalid
2381
2382 Untagged values
2383 */
2384static uint8_t spSpiffyDateTestInput[] = {
2385 0x86,
2386
2387 0xc1,
2388 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2389
Laurence Lundbladec7114722020-08-13 05:11:40 -07002390 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002391 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2392
2393 0xc1, // tag for epoch date
2394 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2395
2396 0xc1, // tag for epoch date
2397 0x9f, 0xff, // Erroneous empty array as content for date
2398
2399 0xc0, // tag for string date
2400 0xbf, 0xff, // Erroneous empty map as content for date
2401
2402 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002403
2404 0x00,
2405 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002406 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002407
2408 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002409 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002410 0xc1, // tag for epoch date
2411 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2412
2413 // Untagged integer 0
2414 0x08,
2415 0x00,
2416
2417 // Utagged date string with string label y
2418 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002419 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002420
2421 // Untagged -1000 with label z
2422 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002423 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002424 0x39, 0x03, 0xe7,
2425
Laurence Lundbladec7114722020-08-13 05:11:40 -07002426 0x07,
2427 0xc1, // tag for epoch date
2428 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2429
Laurence Lundblade4b270642020-08-14 12:53:07 -07002430 0x05,
2431 0xc1,
2432 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2433
Laurence Lundbladec7114722020-08-13 05:11:40 -07002434 // Untagged single-precision float with value 3.14 with string label x
2435 0x61, 0x78,
2436 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2437
Laurence Lundbladec7114722020-08-13 05:11:40 -07002438 // Untagged half-precision float with value -2
2439 0x09,
2440 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002441
2442 0xff,
2443};
2444
2445int32_t SpiffyDateDecodeTest()
2446{
2447 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002448 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002449 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2450 nEpochDate4, nEpochDate6, nEpochDateFail,
2451 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002452 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002453 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002454
2455 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002456 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002457 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002458 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002459
Laurence Lundblade9b334962020-08-27 10:55:53 -07002460 // Too-negative float, -9.2233720368547748E+18
2461 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002462 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002463#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002464 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2465 return 1111;
2466 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002467#else
2468 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2469 return 1112;
2470 }
2471#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002472
2473 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002474 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002475 uError = QCBORDecode_GetAndResetError(&DC);
2476 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002477 return 1;
2478 }
2479
Laurence Lundblade4b270642020-08-14 12:53:07 -07002480 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002481 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002482 uError = QCBORDecode_GetAndResetError(&DC);
2483#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2484#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2485 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2486#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2487 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2488#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2489#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2490 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2491#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2492 if(uError != uExpectedforHalfMinusInfinity) {
2493 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002494 }
2495
Laurence Lundblade4b270642020-08-14 12:53:07 -07002496 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002497 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002498 uError = QCBORDecode_GetAndResetError(&DC);
2499 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2500 return 3;
2501 }
2502
2503 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002504 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002505 uError = QCBORDecode_GetAndResetError(&DC);
2506 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2507 return 4;
2508 }
2509
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002510 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002511
2512 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002513 QCBORDecode_GetEpochDateInMapN(&DC,
2514 5,
2515 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2516 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2517 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002518#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2519 if(nEpochDate2 != -9223372036854773760LL) {
2520 return 101;
2521 }
2522#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2523 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002524 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002525 return 102;
2526 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002527#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002528
Laurence Lundblade4b270642020-08-14 12:53:07 -07002529 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002530 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2531 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002532#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2533 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002534 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002535 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002536#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2537 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002538 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002539 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002540 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002541#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2542
2543 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002544 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2545 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002546#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2547 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002548 return 103;
2549 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002550#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2551 uError = QCBORDecode_GetAndResetError(&DC);
2552 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2553 return 104;
2554 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002555#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2556
Laurence Lundblade9b334962020-08-27 10:55:53 -07002557 // A half-precision date with value -2 FFF
2558 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2559 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002560#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2561 if(nEpochDate4 != -2) {
2562 return 105;
2563 }
2564#else
2565 uError = QCBORDecode_GetAndResetError(&DC);
2566 if(uError == QCBOR_SUCCESS) {
2567 return 106;
2568 }
2569#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002570
Laurence Lundblade4b270642020-08-14 12:53:07 -07002571
2572 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002573 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2574 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2575 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002576 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002577 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002578 return 107;
2579 }
2580
2581 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002582 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2583 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002584 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002585 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002586 return 108;
2587 }
2588
2589 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002590 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2591 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2592 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002593 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002594 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002595 return 109;
2596 }
2597
2598 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002599 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2600 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002601 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002602 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002603 return 110;
2604 }
2605
2606 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002607
Laurence Lundblade4b270642020-08-14 12:53:07 -07002608 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002609 QCBORDecode_GetEpochDateInMapN(&DC,
2610 1,
2611 QCBOR_TAG_REQUIREMENT_TAG |
2612 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2613 &nEpochDate1400000000);
2614 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002615 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002616 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2617 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002618 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002619 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2620 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002621 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002622 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2623 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002624 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002625 QCBORDecode_GetEpochDateInMapSZ(&DC,
2626 "z",
2627 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2628 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2629 &nEpochDate6);
2630 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002631
2632 QCBORDecode_ExitMap(&DC);
2633 QCBORDecode_ExitArray(&DC);
2634 uError = QCBORDecode_Finish(&DC);
2635 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002636 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002637 }
2638
Laurence Lundblade9b334962020-08-27 10:55:53 -07002639 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002640 return 200;
2641 }
2642
Laurence Lundblade9b334962020-08-27 10:55:53 -07002643 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002644 return 201;
2645 }
2646
Laurence Lundblade9b334962020-08-27 10:55:53 -07002647 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002648 return 202;
2649 }
2650
Laurence Lundblade9b334962020-08-27 10:55:53 -07002651 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002652 return 203;
2653 }
2654
Laurence Lundblade9b334962020-08-27 10:55:53 -07002655 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002656 return 204;
2657 }
2658
Laurence Lundblade9b334962020-08-27 10:55:53 -07002659 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2660 return 205;
2661 }
2662
2663 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2664 return 206;
2665 }
2666
Laurence Lundbladec7114722020-08-13 05:11:40 -07002667 return 0;
2668}
2669
2670
2671
Laurence Lundblade9b334962020-08-27 10:55:53 -07002672// Input for one of the tagging tests
2673static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002674 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002675 0x81, // Array of one
2676 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2677 0x82, // Array of two that is the faction 1/3
2678 0x01,
2679 0x03,
2680
2681 /*
2682 More than 4 tags on an item 225(226(227(228(229([])))))
2683 */
2684 0xd8, 0xe1,
2685 0xd8, 0xe2,
2686 0xd8, 0xe3,
2687 0xd8, 0xe4,
2688 0xd8, 0xe5,
2689 0x80,
2690
2691 /* tag 10489608748473423768(
2692 2442302356(
2693 21590(
2694 240(
2695 []))))
2696 */
2697 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2698 0xda, 0x91, 0x92, 0x93, 0x94,
2699 0xd9, 0x54, 0x56,
2700 0xd8, 0xf0,
2701 0x80,
2702
2703 /* tag 21590(
2704 10489608748473423768(
2705 2442302357(
2706 65534(
2707 []))))
2708 */
2709 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2710 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2711 0xda, 0x91, 0x92, 0x93, 0x95,
2712 0xd9, 0xff, 0xfe,
2713 0x80,
2714
2715 /* Make sure to blow past the limit of tags that must be mapped.
2716 works in conjuntion with entries above.
2717 269488144(269488145(269488146(269488147([]))))
2718 */
2719 0xda, 0x10, 0x10, 0x10, 0x10,
2720 0xda, 0x10, 0x10, 0x10, 0x11,
2721 0xda, 0x10, 0x10, 0x10, 0x12,
2722 0xda, 0x10, 0x10, 0x10, 0x13,
2723 0x80,
2724
2725 /* An invalid decimal fraction with an additional tag */
2726 0xd9, 0xff, 0xfa,
2727 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2728 0x00, // the integer 0; should be a byte string
2729};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002730
Laurence Lundblade59289e52019-12-30 13:44:37 -08002731/*
2732 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002733 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002734 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002735static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2736 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002737
Laurence Lundblade59289e52019-12-30 13:44:37 -08002738/*
2739DB 9192939495969798 # tag(10489608748473423768)
2740 D8 88 # tag(136)
2741 C6 # tag(6)
2742 C7 # tag(7)
2743 80 # array(0)
2744*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002745static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2746 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002747
2748/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002749 55799(55799(55799({
2750 6(7(-23)): 5859837686836516696(7({
2751 7(-20): 11({
2752 17(-18): 17(17(17("Organization"))),
2753 9(-17): 773("SSG"),
2754 -15: 16(17(6(7("Confusion")))),
2755 17(-16): 17("San Diego"),
2756 17(-14): 17("US")
2757 }),
2758 23(-19): 19({
2759 -11: 9({
2760 -9: -7
2761 }),
2762 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2763 })
2764 })),
2765 16(-22): 23({
2766 11(8(7(-5))): 8(-3)
2767 })
2768 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002769 */
2770static uint8_t spCSRWithTags[] = {
2771 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2772 0xc6, 0xc7, 0x36,
2773 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2774 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2775 0xcb, 0xa5,
2776 0xd1, 0x31,
2777 0xd1, 0xd1, 0xd1, 0x6c,
2778 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2779 0xc9, 0x30,
2780 0xd9, 0x03, 0x05, 0x63,
2781 0x53, 0x53, 0x47,
2782 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002783 0xd0, 0xd1, 0xc6, 0xc7,
2784 0x69,
2785 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002786 0xd1, 0x2f,
2787 0xd1, 0x69,
2788 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2789 0xd1, 0x2d,
2790 0xd1, 0x62,
2791 0x55, 0x53,
2792 0xd7, 0x32,
2793 0xd3, 0xa2,
2794 0x2a,
2795 0xc9, 0xa1,
2796 0x28,
2797 0x26,
2798 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2799 0xcc, 0x4a,
2800 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2801 0xd0, 0x35,
2802 0xd7, 0xa1,
2803 0xcb, 0xc8, 0xc7, 0x24,
2804 0xc8, 0x22};
2805
Laurence Lundblade9b334962020-08-27 10:55:53 -07002806
2807static uint8_t spSpiffyTagInput[] = {
2808 0x9f, // Open indefinite array
2809
2810 0xc0, // tag for string date
2811 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2812
2813 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2814
2815 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2816
2817 0xd8, 0x23, // tag for regex
2818 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2819
2820 0xc0, // tag for string date
2821 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2822
2823 0xff
2824};
2825
2826
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002827static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002828
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002829
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002830int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002831{
2832 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002833 QCBORItem Item;
2834 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002835
Laurence Lundbladeee851742020-01-08 08:37:05 -08002836 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002837 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002838 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002839
Laurence Lundblade9b334962020-08-27 10:55:53 -07002840 /*
2841 This test matches the magic number tag and the fraction tag
2842 55799([...])
2843 */
2844 uError = QCBORDecode_GetNext(&DCtx, &Item);
2845 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002846 return -2;
2847 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002848 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002849 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2850 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002851 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002852
Laurence Lundblade9b334962020-08-27 10:55:53 -07002853 /*
2854 4([1,3])
2855 */
2856 uError = QCBORDecode_GetNext(&DCtx, &Item);
2857#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2858 if(uError != QCBOR_SUCCESS ||
2859 Item.uDataType != QCBOR_TYPE_ARRAY ||
2860 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2861 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2862 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2863 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2864 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2865 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2866 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002867 return -4;
2868 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002869 // consume the items in the array
2870 uError = QCBORDecode_GetNext(&DCtx, &Item);
2871 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002872
Laurence Lundblade59289e52019-12-30 13:44:37 -08002873#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002874 if(uError != QCBOR_SUCCESS ||
2875 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2876 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2877 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2878 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2879 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2880 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2881 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002882 }
2883#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002884
Laurence Lundblade9b334962020-08-27 10:55:53 -07002885 /*
2886 More than 4 tags on an item 225(226(227(228(229([])))))
2887 */
2888 uError = QCBORDecode_GetNext(&DCtx, &Item);
2889 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002890 return -6;
2891 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002892
2893 /* tag 10489608748473423768(
2894 2442302356(
2895 21590(
2896 240(
2897 []))))
2898 */
2899 uError = QCBORDecode_GetNext(&DCtx, &Item);
2900 if(uError != QCBOR_SUCCESS ||
2901 Item.uDataType != QCBOR_TYPE_ARRAY ||
2902 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2903 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2904 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2905 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002906 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002907 }
2908
2909 /* tag 21590(
2910 10489608748473423768(
2911 2442302357(
2912 21591(
2913 []))))
2914 */
2915 uError = QCBORDecode_GetNext(&DCtx, &Item);
2916 if(uError != QCBOR_SUCCESS ||
2917 Item.uDataType != QCBOR_TYPE_ARRAY ||
2918 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2919 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2920 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2921 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2922 return -8;
2923 }
2924
2925 /* Make sure to blow past the limit of tags that must be mapped.
2926 works in conjuntion with entries above.
2927 269488144(269488145(269488146(269488147([]))))
2928 */
2929 uError = QCBORDecode_GetNext(&DCtx, &Item);
2930 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2931 return -9;
2932 }
2933
2934 uError = QCBORDecode_GetNext(&DCtx, &Item);
2935 if(uError == QCBOR_SUCCESS) {
2936 return -10;
2937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002938
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002939 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002940 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002941 // tage and then matches it. Caller-config lists are no longer
2942 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002943 QCBORDecode_Init(&DCtx,
2944 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2945 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002946 const uint64_t puList[] = {0x9192939495969798, 257};
2947 const QCBORTagListIn TL = {2, puList};
2948 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002949
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002950 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2951 return -8;
2952 }
2953 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2954 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2955 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2956 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2957 Item.val.uCount != 0) {
2958 return -9;
2959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002960
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002961 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002962 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002963 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002964 const uint64_t puLongList[17] = {1,2,1};
2965 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002966 QCBORDecode_Init(&DCtx,
2967 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2968 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002969 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2970 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2971 return -11;
2972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002973
Laurence Lundblade9b334962020-08-27 10:55:53 -07002974 uint64_t puTags[16];
2975 QCBORTagListOut Out = {0, 4, puTags};
2976
2977
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002978 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002979 QCBORDecode_Init(&DCtx,
2980 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2981 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002982 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2983 return -12;
2984 }
2985 if(puTags[0] != 0x9192939495969798 ||
2986 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002987 puTags[2] != 0x06 ||
2988 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002989 return -13;
2990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002991
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002992 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002993 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002994 QCBORDecode_Init(&DCtx,
2995 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2996 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002997 QCBORTagListOut OutSmall = {0, 3, puTags};
2998 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2999 return -14;
3000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003001
Laurence Lundblade9b334962020-08-27 10:55:53 -07003002
3003
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003004 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003005 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3006 // It is a bit of a messy test and maybe could be improved, but
3007 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003008 QCBORDecode_Init(&DCtx,
3009 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3010 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003011 int n = CheckCSRMaps(&DCtx);
3012 if(n) {
3013 return n-2000;
3014 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003015
Laurence Lundblade59289e52019-12-30 13:44:37 -08003016 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003017 QCBORDecode_Init(&DCtx,
3018 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3019 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003020
Laurence Lundblade9b334962020-08-27 10:55:53 -07003021 /* With the spiffy decode revision, this tag list is not used.
3022 It doesn't matter if a tag is in this list or not so some
3023 tests that couldn't process a tag because it isn't in this list
3024 now can process these unlisted tags. The tests have been
3025 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003026 const uint64_t puTagList[] = {773, 1, 90599561};
3027 const QCBORTagListIn TagList = {3, puTagList};
3028 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003029
3030
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003031 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3032 return -100;
3033 }
3034 if(Item.uDataType != QCBOR_TYPE_MAP ||
3035 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3036 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3037 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3038 Item.val.uCount != 2 ||
3039 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3040 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3041 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3042 Out.uNumUsed != 3) {
3043 return -101;
3044 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003045
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003046 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3047 return -102;
3048 }
3049 if(Item.uDataType != QCBOR_TYPE_MAP ||
3050 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3051 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003052 !QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003053 Item.val.uCount != 2 ||
3054 puTags[0] != 5859837686836516696 ||
3055 puTags[1] != 7 ||
3056 Out.uNumUsed != 2) {
3057 return -103;
3058 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003059
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003060 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3061 return -104;
3062 }
3063 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003064 //Item.uTagBits ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003065 Item.val.uCount != 5 ||
3066 puTags[0] != 0x0b ||
3067 Out.uNumUsed != 1) {
3068 return -105;
3069 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003070
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003071 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3072 return -106;
3073 }
3074 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3075 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3076 Item.val.string.len != 12 ||
3077 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3078 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3079 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3080 Out.uNumUsed != 3) {
3081 return -105;
3082 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003083
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003084 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3085 return -107;
3086 }
3087 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3088 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3089 Item.val.string.len != 3 ||
3090 puTags[0] != 773 ||
3091 Out.uNumUsed != 1) {
3092 return -108;
3093 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003094
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003095 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3096 return -109;
3097 }
3098 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003099 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003100 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003101 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003102 puTags[3] != 7 ||
3103 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003104 return -110;
3105 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003106
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003107 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3108 return -111;
3109 }
3110 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3111 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3112 Item.val.string.len != 9 ||
3113 puTags[0] != 17 ||
3114 Out.uNumUsed != 1) {
3115 return -112;
3116 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003117
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003118 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3119 return -111;
3120 }
3121 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3122 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3123 Item.val.string.len != 2 ||
3124 puTags[0] != 17 ||
3125 Out.uNumUsed != 1) {
3126 return -112;
3127 }
3128
3129 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3130 return -113;
3131 }
3132 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003133 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003134 Item.val.uCount != 2 ||
3135 puTags[0] != 19 ||
3136 Out.uNumUsed != 1) {
3137 return -114;
3138 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003139
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003140 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3141 return -115;
3142 }
3143 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003144 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003145 Item.val.uCount != 1 ||
3146 puTags[0] != 9 ||
3147 Out.uNumUsed != 1) {
3148 return -116;
3149 }
3150
3151 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3152 return -116;
3153 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003154 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003155 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003156 Out.uNumUsed != 0) {
3157 return -117;
3158 }
3159
3160 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3161 return -118;
3162 }
3163 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3164 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003165 puTags[0] != 12 ||
3166 Out.uNumUsed != 1) {
3167 return -119;
3168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003169
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003170 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3171 return -120;
3172 }
3173 if(Item.uDataType != QCBOR_TYPE_MAP ||
3174 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3175 Item.val.uCount != 1 ||
3176 puTags[0] != 0x17 ||
3177 Out.uNumUsed != 1) {
3178 return -121;
3179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003180
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003181 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3182 return -122;
3183 }
3184 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003185 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003186 Item.val.int64 != -3 ||
3187 puTags[0] != 8 ||
3188 Out.uNumUsed != 1) {
3189 return -123;
3190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003191
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003192 if(QCBORDecode_Finish(&DCtx)) {
3193 return -124;
3194 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003195
3196 UsefulBufC DateString;
3197 QCBORDecode_Init(&DCtx,
3198 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3199 QCBOR_DECODE_MODE_NORMAL);
3200
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003201 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003202 // tagged date string
3203 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3204 // untagged date string
3205 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3206 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3207 return 100;
3208 }
3209 // untagged byte string
3210 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3211 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3212 return 101;
3213 }
3214 // tagged regex
3215 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3216 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3217 return 102;
3218 }
3219 // tagged date string with a byte string
3220 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3221 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3222 return 103;
3223 }
3224 QCBORDecode_ExitArray(&DCtx);
3225 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3226 return 104;
3227 }
3228
3229
3230 QCBORDecode_Init(&DCtx,
3231 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3232 QCBOR_DECODE_MODE_NORMAL);
3233
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003234 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003235 // tagged date string
3236 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3237 // untagged date string
3238 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3239 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3240 return 200;
3241 }
3242 // untagged byte string
3243 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3244 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3245 return 201;
3246 }
3247 // tagged regex
3248 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3249 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3250 return 202;
3251 }
3252 // tagged date string with a byte string
3253 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3254 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3255 return 203;
3256 }
3257 QCBORDecode_ExitArray(&DCtx);
3258 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3259 return 204;
3260 }
3261
3262 QCBORDecode_Init(&DCtx,
3263 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3264 QCBOR_DECODE_MODE_NORMAL);
3265
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003266 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003267 // tagged date string
3268 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3269 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3270 return 300;
3271 }
3272 // untagged date string
3273 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3274 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3275 return 301;
3276 }
3277 // untagged byte string
3278 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3279 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3280 return 302;
3281 }
3282 // tagged regex
3283 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3284 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3285 return 303;
3286 }
3287 // tagged date string with a byte string
3288 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3289 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3290 return 304;
3291 }
3292 QCBORDecode_ExitArray(&DCtx);
3293 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3294 return 305;
3295 }
3296
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003297 return 0;
3298}
3299
3300
3301
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003303static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003304 0x83,
3305 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3306 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3307 0xA4,
3308 0x63, 0x42, 0x4E, 0x2B,
3309 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3310 0x18, 0x40,
3311 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3312 0x63, 0x42, 0x4E, 0x2D,
3313 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3314 0x38, 0x3F,
3315 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3316
3317
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003318static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003319
3320
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003321int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003322{
3323 QCBORDecodeContext DCtx;
3324 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003325 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003326
Laurence Lundbladeee851742020-01-08 08:37:05 -08003327 QCBORDecode_Init(&DCtx,
3328 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3329 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003330
3331
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003332 //
3333 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3334 return -1;
3335 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003336 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003338
3339 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003340 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003341 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003342 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003343 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003344 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003345 }
3346
3347 //
3348 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003349 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003350 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003351 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003352 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003353 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003355 //
3356 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003357 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003358 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003359 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003360 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003361
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003362 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003363 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003364 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3365 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003366 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003367 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003368 }
3369
3370 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003371 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003372 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3373 Item.uLabelType != QCBOR_TYPE_INT64 ||
3374 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003375 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003376 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003377 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003378
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003379 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003380 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003381 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3382 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003383 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003384 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003387 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003388 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003389 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3390 Item.uLabelType != QCBOR_TYPE_INT64 ||
3391 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003392 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003393 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003395
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003396 return 0;
3397}
3398
3399
3400
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003401static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003402 uint8_t uDataType,
3403 uint8_t uNestingLevel,
3404 uint8_t uNextNest,
3405 int64_t nLabel,
3406 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003407{
3408 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003409 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003411 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3412 if(Item.uDataType != uDataType) return -1;
3413 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003414 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3415 Item.uLabelType != QCBOR_TYPE_UINT64) {
3416 return -1;
3417 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003418 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3419 if(Item.label.int64 != nLabel) return -1;
3420 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003421 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003422 }
3423 }
3424 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303425 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003427 if(pItem) {
3428 *pItem = Item;
3429 }
3430 return 0;
3431}
3432
3433
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003434// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003435static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003436{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303437 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003438
Laurence Lundblade9b334962020-08-27 10:55:53 -07003439 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003440
Laurence Lundblade9b334962020-08-27 10:55:53 -07003441 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003442
Laurence Lundblade9b334962020-08-27 10:55:53 -07003443 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3444 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3445 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3446 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3447 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003448
Laurence Lundblade9b334962020-08-27 10:55:53 -07003449 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3450 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003451
Laurence Lundblade9b334962020-08-27 10:55:53 -07003452 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3453 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003454
Laurence Lundblade9b334962020-08-27 10:55:53 -07003455 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3456 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003457
Laurence Lundblade9b334962020-08-27 10:55:53 -07003458 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003459
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003460 return 0;
3461}
3462
3463
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003464/*
3465// cbor.me decoded output
3466{
3467 -23: {
3468 -20: {
3469 -18: "Organization",
3470 -17: "SSG",
3471 -15: "Confusion",
3472 -16: "San Diego",
3473 -14: "US"
3474 },
3475 -19: {
3476 -11: {
3477 -9: -7
3478 },
3479 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3480 }
3481 },
3482 -22: {
3483 -5: -3
3484 }
3485}
3486 */
3487
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003488
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003489static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003490 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3491 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3492 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3493 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3494 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3495 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3496 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3497 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3498 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3499
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003500int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003501{
3502 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003503
Laurence Lundbladeee851742020-01-08 08:37:05 -08003504 QCBORDecode_Init(&DCtx,
3505 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3506 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003507
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003508 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003509}
3510
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003511
3512
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003513int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003514{
3515 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003516
Laurence Lundbladeee851742020-01-08 08:37:05 -08003517 QCBORDecode_Init(&DCtx,
3518 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3519 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003520
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003521 QCBORItem Item;
3522 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003523
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003524 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3525 return -1;
3526 }
3527 if(Item.uDataType != QCBOR_TYPE_MAP) {
3528 return -2;
3529 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003530
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003531 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3532 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3533 return -3;
3534 }
3535
3536 return 0;
3537}
3538
3539
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003540// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003541static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003542 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3543 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3544 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3545 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3546 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3547 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003548 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3549 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3550 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3551 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003552
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003553int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003554{
3555 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003556
Laurence Lundbladeee851742020-01-08 08:37:05 -08003557 QCBORDecode_Init(&DCtx,
3558 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3559 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003560
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003561 return CheckCSRMaps(&DCtx);
3562}
3563
3564
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003565
Laurence Lundblade17ede402018-10-13 11:43:07 +08003566static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3567{
3568 UsefulOutBuf UOB;
3569 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003570
Laurence Lundblade17ede402018-10-13 11:43:07 +08003571 int i;
3572 for(i = 0; i < n; i++) {
3573 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3574 }
3575
3576 for(i = 0; i < n; i++) {
3577 UsefulOutBuf_AppendByte(&UOB, 0xff);
3578 }
3579 return UsefulOutBuf_OutUBuf(&UOB);
3580}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003581
3582
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003583static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003584{
3585 QCBORDecodeContext DC;
3586 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003587
Laurence Lundblade17ede402018-10-13 11:43:07 +08003588 int j;
3589 for(j = 0; j < nNestLevel; j++) {
3590 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003591 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003592 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3593 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003594 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003595 return -4;
3596 } else {
3597 return 0; // Decoding doesn't recover after an error
3598 }
3599 } else {
3600 // Should be no error
3601 if(nReturn) {
3602 return -9; // Should not have got an error
3603 }
3604 }
3605 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3606 return -7;
3607 }
3608 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003609 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003610 if(nReturn) {
3611 return -3;
3612 }
3613 return 0;
3614}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003615
3616
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003617int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003618{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303619 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003620 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003621 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003622 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003623 int nReturn = parse_indeflen_nested(Nested, i);
3624 if(nReturn) {
3625 return nReturn;
3626 }
3627 }
3628 return 0;
3629}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003630
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003631
Laurence Lundbladeee851742020-01-08 08:37:05 -08003632// [1, [2, 3]]
3633static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3634// No closing break
3635static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3636// Not enough closing breaks
3637static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3638// Too many closing breaks
3639static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3640// Unclosed indeflen inside def len
3641static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3642// confused tag
3643static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003644
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003645int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003646{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003647 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003648 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003649 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003650
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003651 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303652 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003653 QCBORDecodeContext DC;
3654 QCBORItem Item;
3655 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003656
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003657 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003658
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003659 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303660
3661 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3662 Item.uNestingLevel != 0 ||
3663 Item.uNextNestLevel != 1) {
3664 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003665 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003666
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003667 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303668 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3669 Item.uNestingLevel != 1 ||
3670 Item.uNextNestLevel != 1) {
3671 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003672 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003673
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003674 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303675 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3676 Item.uNestingLevel != 1 ||
3677 Item.uNextNestLevel != 2) {
3678 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003679 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003680
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003681 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003682 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303683 Item.uNestingLevel != 2 ||
3684 Item.uNextNestLevel != 2) {
3685 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003686 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003687
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003688 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003689 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303690 Item.uNestingLevel != 2 ||
3691 Item.uNextNestLevel != 0) {
3692 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003693 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003694
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003695 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303696 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003698
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003699 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003700 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003701
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003702 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003703
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003704 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003705
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003706 nResult = QCBORDecode_GetNext(&DC, &Item);
3707 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303708 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003709 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003710
Laurence Lundblade570fab52018-10-13 18:28:27 +08003711 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003712 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303713 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003714 }
3715
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003716
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003717 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003718 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003719
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003720 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003721
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003722 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003723
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003724 nResult = QCBORDecode_GetNext(&DC, &Item);
3725 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303726 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003727 }
3728
3729 nResult = QCBORDecode_GetNext(&DC, &Item);
3730 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303731 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003732 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003733
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003734 nResult = QCBORDecode_GetNext(&DC, &Item);
3735 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303736 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003737 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003738
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003739 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003740 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303741 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003742 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003743
3744
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003745 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003746 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003747
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003748 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003749
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003750 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003751
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003752 nResult = QCBORDecode_GetNext(&DC, &Item);
3753 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303754 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003755 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003756
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003757 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003758 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303759 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003760 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303761
Laurence Lundblade642282a2020-06-23 12:00:33 -07003762 nResult = QCBORDecode_GetNext(&DC, &Item);
3763 if(nResult != QCBOR_ERR_BAD_BREAK) {
3764 return -140;
3765 }
3766
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003767
Laurence Lundblade570fab52018-10-13 18:28:27 +08003768 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003769 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003770
Laurence Lundblade570fab52018-10-13 18:28:27 +08003771 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003772
Laurence Lundblade570fab52018-10-13 18:28:27 +08003773 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003774
Laurence Lundblade570fab52018-10-13 18:28:27 +08003775 nResult = QCBORDecode_GetNext(&DC, &Item);
3776 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303777 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003778 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003779
Laurence Lundblade570fab52018-10-13 18:28:27 +08003780 nResult = QCBORDecode_GetNext(&DC, &Item);
3781 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303782 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003783 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003784
Laurence Lundblade570fab52018-10-13 18:28:27 +08003785 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003786 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303787 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003788 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003789
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303790 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003791 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003792
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303793 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003794
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303795 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003796
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303797 nResult = QCBORDecode_GetNext(&DC, &Item);
3798 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303799 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303800 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003801
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303802 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303803 if(nResult != QCBOR_ERR_BAD_BREAK) {
3804 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303805 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003806
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003807 return 0;
3808}
3809
Laurence Lundblade17ede402018-10-13 11:43:07 +08003810
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003811static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003812 0x81, // Array of length one
3813 0x7f, // text string marked with indefinite length
3814 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3815 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3816 0xff // ending break
3817};
3818
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003819static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303820 0x81, // Array of length one
3821 0x7f, // text string marked with indefinite length
3822 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3823 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3824 0xff // ending break
3825};
3826
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003827static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303828 0x81, // Array of length one
3829 0x7f, // text string marked with indefinite length
3830 0x01, 0x02, // Not a string
3831 0xff // ending break
3832};
3833
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003834static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303835 0x81, // Array of length one
3836 0x7f, // text string marked with indefinite length
3837 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3838 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3839 // missing end of string
3840};
3841
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003842static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303843 0xa1, // Array of length one
3844 0x7f, // text string marked with indefinite length
3845 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3846 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3847 0xff, // ending break
3848 0x01 // integer being labeled.
3849};
3850
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003851/**
3852 Make an indefinite length string
3853
3854 @param Storage Storage for string, must be 144 bytes in size
3855 @return The indefinite length string
3856
3857 This makes an array with one indefinite length string that has 7 chunks
3858 from size of 1 byte up to 64 bytes.
3859 */
3860static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303861{
3862 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003863
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303864 UsefulOutBuf_Init(&UOB, Storage);
3865 UsefulOutBuf_AppendByte(&UOB, 0x81);
3866 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003867
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003868 uint8_t uStringByte = 0;
3869 // Use of type int is intentional
3870 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3871 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303872 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003873 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3874 for(int j = 0; j < uChunkSize; j++) {
3875 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3876 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303877 }
3878 }
3879 UsefulOutBuf_AppendByte(&UOB, 0xff);
3880
3881 return UsefulOutBuf_OutUBuf(&UOB);
3882}
3883
3884static int CheckBigString(UsefulBufC BigString)
3885{
3886 if(BigString.len != 255) {
3887 return 1;
3888 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003889
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303890 for(uint8_t i = 0; i < 255; i++){
3891 if(((const uint8_t *)BigString.ptr)[i] != i) {
3892 return 1;
3893 }
3894 }
3895 return 0;
3896}
3897
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303898
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003899int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303900{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303901 QCBORDecodeContext DC;
3902 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303903 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003904 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003905
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303906 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003907 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303908 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003909
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303910 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303911 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303912 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003913
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303914 if(QCBORDecode_GetNext(&DC, &Item)) {
3915 return -2;
3916 }
3917 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3918 return -3;
3919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003920
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303921 if(QCBORDecode_GetNext(&DC, &Item)) {
3922 return -4;
3923 }
3924 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3925 return -5;
3926 }
3927 if(QCBORDecode_Finish(&DC)) {
3928 return -6;
3929 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303930
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303931 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003932 QCBORDecode_Init(&DC,
3933 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3934 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003935
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303936 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3937 return -7;
3938 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003939
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303940 if(QCBORDecode_GetNext(&DC, &Item)) {
3941 return -8;
3942 }
3943 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3944 return -9;
3945 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003946
Laurence Lundblade30816f22018-11-10 13:40:22 +07003947 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303948 return -10;
3949 }
3950
3951 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003952 QCBORDecode_Init(&DC,
3953 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3954 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003955
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303956 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3957 return -11;
3958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003959
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303960 if(QCBORDecode_GetNext(&DC, &Item)) {
3961 return -12;
3962 }
3963 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3964 return -13;
3965 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003966
Laurence Lundblade30816f22018-11-10 13:40:22 +07003967 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303968 return -14;
3969 }
3970
3971 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003972 QCBORDecode_Init(&DC,
3973 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3974 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003975
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303976 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3977 return -15;
3978 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003979
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303980 if(QCBORDecode_GetNext(&DC, &Item)) {
3981 return -16;
3982 }
3983 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3984 return -17;
3985 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003986
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303987 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3988 return -18;
3989 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303991 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303992 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003993
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303994 QCBORDecode_GetNext(&DC, &Item);
3995 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303996 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003998
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303999 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304000 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004002
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304003 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004004 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304005
4006 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4007 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304008 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304009 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004010
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304011 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304012 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004013 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004014
Laurence Lundbladeee851742020-01-08 08:37:05 -08004015 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4016 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004017
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304018 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304019 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304020 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004022
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304023 QCBORDecode_GetNext(&DC, &Item);
4024 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304025 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304026 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004027 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304028 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004030
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304031 // ---- big bstr -----
4032 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004033
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304034 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4035 return -25;
4036 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004037
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304038 if(QCBORDecode_GetNext(&DC, &Item)) {
4039 return -26;
4040 }
4041 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304042 return -26;
4043 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004044
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304045 if(QCBORDecode_GetNext(&DC, &Item)) {
4046 return -27;
4047 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304048 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304049 return -28;
4050 }
4051 if(CheckBigString(Item.val.string)) {
4052 return -3;
4053 }
4054 if(QCBORDecode_Finish(&DC)) {
4055 return -29;
4056 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004057
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304058 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004059 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004060
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304061 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4062 return -30;
4063 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004064
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304065 QCBORDecode_GetNext(&DC, &Item);
4066 if(Item.uDataType != QCBOR_TYPE_MAP) {
4067 return -31;
4068 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004069
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304070 if(QCBORDecode_GetNext(&DC, &Item)){
4071 return -32;
4072 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004073 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4074 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304075 Item.uDataAlloc || !Item.uLabelAlloc ||
4076 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4077 return -33;
4078 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004079
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304080 if(QCBORDecode_Finish(&DC)) {
4081 return -34;
4082 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004083
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004084 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004085}
4086
4087
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004088int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304089{
4090 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004091 QCBORError nCBORError;
4092
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004093
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304094 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004095 QCBORDecode_Init(&DC,
4096 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4097 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004098
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004099 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004100
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004101 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4102 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304103 return -1;
4104 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004105
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004106 if(CheckCSRMaps(&DC)) {
4107 return -2;
4108 }
4109
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004110 // Next parse, save pointers to a few strings, destroy original and
4111 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004112 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004113 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004114
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304115 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004116 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304117 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004118
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304119 QCBORItem Item1, Item2, Item3, Item4;
4120 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004121 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304122 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4123 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004124 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304125 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004126 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304127 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004128 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304129 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
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, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004132 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004133
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304134 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004135
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304136 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304137 Item1.uDataType != QCBOR_TYPE_INT64 ||
4138 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004139 Item1.uDataAlloc != 0 ||
4140 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004141 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004142 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004143 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004144
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304145
4146 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004147 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304148 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004149 Item2.uDataAlloc != 0 ||
4150 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304151 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004152 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004153
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304154 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004155 Item3.uDataAlloc == 0 ||
4156 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004157 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004158 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004160
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304161 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004162 Item4.uDataAlloc == 0 ||
4163 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004164 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004165 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004167
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304168 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004169 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004170 QCBORDecode_Init(&DC,
4171 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4172 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304173 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4174 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004175 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304176 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004177 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004178 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004179 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304180 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4181 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4182 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4183 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4184 }
4185 }
4186 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004187 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004188 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304189 }
4190
4191 return 0;
4192}
4193
Laurence Lundbladef6531662018-12-04 10:42:22 +09004194
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304195
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004196int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004197{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004198 // Set up the decoder with a tiny bit of CBOR to parse because
4199 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004200 QCBORDecodeContext DC;
4201 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4202 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004203
Laurence Lundbladef6531662018-12-04 10:42:22 +09004204 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004205 // Then fish into the internals of the decode context
4206 // to get the allocator function so it can be called directly.
4207 // Also figure out how much pool is available for use
4208 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004209 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004210 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4211 if(nError) {
4212 return -9;
4213 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004214 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4215 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4216 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004217
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004218 // First test -- ask for one more byte than available and see failure
4219 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004220 if(!UsefulBuf_IsNULL(Allocated)) {
4221 return -1;
4222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004223
Laurence Lundbladef6531662018-12-04 10:42:22 +09004224 // Re do the set up for the next test that will do a successful alloc,
4225 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004226 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004227 pAlloc = DC.StringAllocator.pfAllocator;
4228 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4229 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004230
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004231 // Allocate one byte less than available and see success
4232 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004233 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4234 return -2;
4235 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004236 // Ask for some more and see failure
4237 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004238 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4239 return -3;
4240 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004241 // Free the first allocate, retry the second and see success
4242 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4243 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004244 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4245 return -4;
4246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004247
Laurence Lundbladef6531662018-12-04 10:42:22 +09004248 // Re do set up for next test that involves a successful alloc,
4249 // and a successful realloc and a failed realloc
4250 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004251 pAlloc = DC.StringAllocator.pfAllocator;
4252 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004253
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004254 // Allocate half the pool and see success
4255 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004256 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4257 return -5;
4258 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004259 // Reallocate to take up the whole pool and see success
4260 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004261 if(UsefulBuf_IsNULL(Allocated2)) {
4262 return -6;
4263 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004264 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004265 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4266 return -7;
4267 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004268 // Try to allocate more to be sure there is failure after a realloc
4269 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4270 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004271 return -8;
4272 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004273
Laurence Lundbladef6531662018-12-04 10:42:22 +09004274 return 0;
4275}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004276
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004277
4278/* Just enough of an allocator to test configuration of one */
4279static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4280{
4281 (void)pOldMem; // unused variable
4282
4283 if(uNewSize) {
4284 // Assumes the context pointer is the buffer and
4285 // nothing too big will ever be asked for.
4286 // This is only good for this basic test!
4287 return (UsefulBuf) {pCtx, uNewSize};
4288 } else {
4289 return NULLUsefulBuf;
4290 }
4291}
4292
4293
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004294int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004295{
4296 // Set up the decoder with a tiny bit of CBOR to parse because
4297 // nothing can be done with it unless that is set up.
4298 QCBORDecodeContext DC;
4299 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4300 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4301
4302 uint8_t pAllocatorBuffer[50];
4303
4304 // This is really just to test that this call works.
4305 // The full functionality of string allocators is tested
4306 // elsewhere with the MemPool internal allocator.
4307 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4308
4309 QCBORItem Item;
4310 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4311 return -1;
4312 }
4313
4314 if(Item.uDataAlloc == 0 ||
4315 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4316 Item.val.string.ptr != pAllocatorBuffer) {
4317 return -2;
4318 }
4319
4320 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4321 return -3;
4322 }
4323
4324 return 0;
4325}
4326
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004327#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004328
Laurence Lundbladea826c502020-05-10 21:07:00 -07004329/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004330 [
4331 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004332 4([-20, 4759477275222530853136]),
4333 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004334 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004335 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004336 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004337 5([ 9223372036854775806, -4759477275222530853137])
4338 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004339 ]
4340 */
4341
4342static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004343 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004344 0xC4, 0x82, 0x20,
4345 0x03,
4346 0xC4, 0x82, 0x33,
4347 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4348 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4349 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4350 0xC5, 0x82, 0x19, 0x01, 0x2C,
4351 0x18, 0x64,
4352 0xC5, 0x82, 0x33,
4353 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4354 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4355 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004356 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4357 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004358 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4359 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4360};
4361
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004362
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004363int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004364{
4365 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004366 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004367 QCBORItem item;
4368
Laurence Lundblade17af4902020-01-07 19:11:55 -08004369 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4370 0x06, 0x07, 0x08, 0x09, 0x010};
4371 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004372
4373
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004374 QCBORDecode_Init(&DC,
4375 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4376 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004377
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004378 uErr = QCBORDecode_GetNext(&DC, &item);
4379 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004380 return 1;
4381 }
4382
4383 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4384 return 2;
4385 }
4386
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004387 uErr = QCBORDecode_GetNext(&DC, &item);
4388 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004389 return 3;
4390 }
4391
4392 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4393 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4394 item.val.expAndMantissa.nExponent != -1) {
4395 return 4;
4396 }
4397
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004398 uErr = QCBORDecode_GetNext(&DC, &item);
4399 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004400 return 5;
4401 }
4402
4403 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4404 item.val.expAndMantissa.nExponent != -20 ||
4405 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4406 return 6;
4407 }
4408
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004409 uErr = QCBORDecode_GetNext(&DC, &item);
4410 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004411 return 7;
4412 }
4413
4414 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4415 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4416 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4417 return 8;
4418 }
4419
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004420 uErr = QCBORDecode_GetNext(&DC, &item);
4421 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004422 return 9;
4423 }
4424
4425 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4426 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4427 item.val.expAndMantissa.nExponent != 300) {
4428 return 10;
4429 }
4430
Laurence Lundbladea826c502020-05-10 21:07:00 -07004431 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004432 uErr = QCBORDecode_GetNext(&DC, &item);
4433 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004434 return 11;
4435 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004436 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4437 item.val.expAndMantissa.nExponent != -20 ||
4438 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4439 return 12;
4440 }
4441
Laurence Lundbladea826c502020-05-10 21:07:00 -07004442 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004443 uErr = QCBORDecode_GetNext(&DC, &item);
4444 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004445 return 13;
4446 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004447 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4448 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4449 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4450 return 14;
4451 }
4452
Laurence Lundbladea826c502020-05-10 21:07:00 -07004453 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004454 uErr = QCBORDecode_GetNext(&DC, &item);
4455 if(uErr != QCBOR_SUCCESS) {
4456 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004457 }
4458 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4459 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4460 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004461 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004462 }
4463
Laurence Lundbladea826c502020-05-10 21:07:00 -07004464 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004465 uErr = QCBORDecode_GetNext(&DC, &item);
4466 if(uErr != QCBOR_SUCCESS) {
4467 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004468 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004469 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4470 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4471 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004472 return 18;
4473 }
4474
4475 uErr = QCBORDecode_Finish(&DC);
4476 if(uErr != QCBOR_SUCCESS) {
4477 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004478 }
4479
4480 /* Now encode some stuff and then decode it */
4481 uint8_t pBuf[40];
4482 QCBOREncodeContext EC;
4483 UsefulBufC Encoded;
4484
4485 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4486 QCBOREncode_OpenArray(&EC);
4487 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4488 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4489 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4490 QCBOREncode_CloseArray(&EC);
4491 QCBOREncode_Finish(&EC, &Encoded);
4492
4493
4494 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004495 uErr = QCBORDecode_GetNext(&DC, &item);
4496 if(uErr != QCBOR_SUCCESS) {
4497 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004498 }
4499
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004500 uErr = QCBORDecode_GetNext(&DC, &item);
4501 if(uErr != QCBOR_SUCCESS) {
4502 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004503 }
4504
4505 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4506 item.val.expAndMantissa.nExponent != 1000 ||
4507 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004508 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004509 }
4510
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004511 uErr = QCBORDecode_GetNext(&DC, &item);
4512 if(uErr != QCBOR_SUCCESS) {
4513 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004514 }
4515
4516 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4517 item.val.expAndMantissa.nExponent != INT32_MIN ||
4518 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004519 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004520 }
4521
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004522 uErr = QCBORDecode_GetNext(&DC, &item);
4523 if(uErr != QCBOR_SUCCESS) {
4524 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004525 }
4526
4527 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4528 item.val.expAndMantissa.nExponent != INT32_MAX ||
4529 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004530 return 106;
4531 }
4532
4533
4534 int64_t nExp, nMant;
4535 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4536 UsefulBufC Mant;
4537 bool bIsNeg;
4538
4539 QCBORDecode_Init(&DC,
4540 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4541 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004542 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004543
4544 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004545 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004546
4547 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004548 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4549 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004550
4551 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004552 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4553 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004554
4555 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004556 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004557
4558 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004559 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4560 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004561
4562 // 5([-9223372036854775807, -4759477275222530853137])
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([ 9223372036854775806, -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, 9223372036854775806])]
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 QCBORDecode_ExitArray(&DC);
4575
4576 uErr = QCBORDecode_Finish(&DC);
4577 if(uErr != QCBOR_SUCCESS) {
4578 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004579 }
4580
4581 return 0;
4582}
4583
4584
4585static struct FailInput ExponentAndMantissaFailures[] = {
4586 // Exponent > INT64_MAX
4587 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4588 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4589 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4590 // Mantissa > INT64_MAX
4591 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4592 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4593 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4594 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004595 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004596 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004597 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004598 // bad content for big num
4599 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4600 // bad content for big num
4601 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4602 // Bad integer for exponent
4603 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4604 // Bad integer for mantissa
4605 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4606 // 3 items in array
4607 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4608 // unterminated indefinite length array
4609 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4610 // Empty array
4611 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4612 // Second is not an integer
4613 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4614 // First is not an integer
4615 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4616 // Not an array
4617 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4618};
4619
4620
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004621int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004622{
4623 return ProcessFailures(ExponentAndMantissaFailures,
4624 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4625}
4626
4627#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004628
4629
4630
4631/*
4632 Some basic CBOR with map and array used in a lot of tests.
4633 The map labels are all strings
4634
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004635 {
4636 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004637 "an array of two strings": [
4638 "string1", "string2"
4639 ],
4640 "map in a map": {
4641 "bytes 1": h'78787878',
4642 "bytes 2": h'79797979',
4643 "another int": 98,
4644 "text 2": "lies, damn lies and statistics"
4645 }
4646 }
4647 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004648
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004649int32_t EMap(UsefulBufC input)
4650{
4651 QCBORItem Item1, Item2, Item3;
4652 int64_t nDecodedInt1, nDecodedInt2;
4653 UsefulBufC B1, B2, S1, S2, S3;
4654
4655 QCBORDecodeContext DCtx;
4656 QCBORError nCBORError;
4657
4658 QCBORDecode_Init(&DCtx, input, 0);
4659
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004660 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004661
4662 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4663
4664 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4665 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004666 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4667 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4668 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004669 QCBORDecode_ExitMap(&DCtx);
4670
4671 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4672 QCBORDecode_GetNext(&DCtx, &Item1);
4673 QCBORDecode_GetNext(&DCtx, &Item2);
4674 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4675 return -400;
4676 }
4677 QCBORDecode_ExitArray(&DCtx);
4678
4679 // Parse the same array again using GetText() instead of GetItem()
4680 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004681 QCBORDecode_GetTextString(&DCtx, &S2);
4682 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004683 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4684 return 5000;
4685 }
4686 /* QCBORDecode_GetText(&DCtx, &S3);
4687 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4688 return 5001;
4689 } */
4690
4691 QCBORDecode_ExitArray(&DCtx);
4692
4693 QCBORDecode_ExitMap(&DCtx);
4694
4695 nCBORError = QCBORDecode_Finish(&DCtx);
4696
4697 if(nCBORError) {
4698 return (int32_t)nCBORError;
4699 }
4700
4701 if(nDecodedInt1 != 42) {
4702 return 1001;
4703 }
4704
4705 if(nDecodedInt2 != 98) {
4706 return 1002;
4707 }
4708
4709 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004710 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004711 return 1003;
4712 }
4713
4714 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004715 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004716 return 1004;
4717 }
4718
Laurence Lundblade9b334962020-08-27 10:55:53 -07004719 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004720 return 1005;
4721 }
4722
4723 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4724 return 1006;
4725 }
4726
4727 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4728 return 1007;
4729 }
4730
4731 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4732 return 1008;
4733 }
4734
4735 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4736 return 1009;
4737 }
4738
4739 return 0;
4740}
4741
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004742
4743/*
4744 [23,
4745 6000,
4746 h'67616C6163746963',
4747 h'686176656E20746F6B656E'
4748 ]
4749 */
4750static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004751 0x84,
4752 0x17,
4753 0x19, 0x17, 0x70,
4754 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4755 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004756
4757
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004758static const uint8_t spEmptyMap[] = {0xa0};
4759
4760static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004761
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004762static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4763
Laurence Lundbladef0499502020-08-01 11:55:57 -07004764/*
4765 {
4766 0: [],
4767 9: [
4768 [],
4769 []
4770 ],
4771 8: {
4772 1: [],
4773 2: {},
4774 3: []
4775 },
4776 4: {},
4777 5: [],
4778 6: [
4779 [],
4780 []
4781 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004782 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004783 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004784
Laurence Lundbladef0499502020-08-01 11:55:57 -07004785static const uint8_t spMapOfEmpty[] = {
4786 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4787 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4788 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004789
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004790/*
4791 Too many tags
4792 Invalid tag content
4793 Duplicate label
4794 Integer overflow
4795 Date overflow
4796
4797 {1: 224(225(226(227(4(0))))),
4798 2: 1(h''),
4799 3: -18446744073709551616,
4800 4: 1(1.0e+300),
4801 5: 0, 8: 8}
4802 */
4803static const uint8_t spRecoverableMapErrors[] = {
4804 0xbf,
4805 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
4806 0x02, 0xc1, 0x40,
4807 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4808 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
4809 0x05, 0x00,
4810 0x05, 0x00,
4811 0x08, 0x08,
4812 0xff
4813};
4814
4815// Bad break
4816static const uint8_t spUnRecoverableMapError1[] = {
4817 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
4818};
4819
4820// No more items
4821static const uint8_t spUnRecoverableMapError2[] = {
4822 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
4823};
4824
4825// Hit end because string is too long
4826static const uint8_t spUnRecoverableMapError3[] = {
4827 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
4828};
4829
4830// Hit end because string is too long
4831static const uint8_t spUnRecoverableMapError4[] = {
4832 0xbf,
4833 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4834 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4835 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4836 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4837 0xff
4838};
4839
4840
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004841int32_t EnterMapTest()
4842{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004843 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004844 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004845 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004846 int32_t nReturn;
4847 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004848
4849
4850 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004851 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004852
Laurence Lundbladef0499502020-08-01 11:55:57 -07004853
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004854 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004855 QCBORDecode_ExitArray(&DCtx);
4856
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004857 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
4858 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004859 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004860 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004861 QCBORDecode_ExitArray(&DCtx);
4862 QCBORDecode_ExitArray(&DCtx);
4863
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004864 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
4865 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004866 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004867 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004868 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004869 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004870 QCBORDecode_ExitArray(&DCtx);
4871 QCBORDecode_ExitMap(&DCtx);
4872
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004873 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004874 QCBORDecode_ExitMap(&DCtx);
4875
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004876 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004877 QCBORDecode_ExitArray(&DCtx);
4878
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004879 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
4880 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004881 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004882 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004883 QCBORDecode_ExitArray(&DCtx);
4884 QCBORDecode_ExitArray(&DCtx);
4885
4886 QCBORDecode_ExitMap(&DCtx);
4887
4888 uErr = QCBORDecode_Finish(&DCtx);
4889 if(uErr != QCBOR_SUCCESS){
4890 return 3011;
4891 }
4892
4893
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004894 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004895 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004896 if(nReturn) {
4897 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004898 }
4899
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004900 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004901 if(nReturn) {
4902 return nReturn;
4903 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004904
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004905
Laurence Lundblade937ea812020-05-08 11:38:23 -07004906
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004907 // These tests confirm the cursor is at the right place after entering
4908 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004909 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004910
4911 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004912 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004913 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004914 QCBORDecode_GetNext(&DCtx, &Item1);
4915 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4916 return 2001;
4917 }
4918
4919
Laurence Lundblade9b334962020-08-27 10:55:53 -07004920 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004921 QCBORDecode_VGetNext(&DCtx, &Item1);
4922 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004923 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
4924 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4925 UsefulBuf_Compare(ArrayItem.label.string,
4926 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
4927 return 2051;
4928 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004929 QCBORDecode_GetNext(&DCtx, &Item1);
4930 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4931 return 2002;
4932 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004933 QCBORDecode_ExitArray(&DCtx);
4934 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
4935 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4936 UsefulBuf_Compare(ArrayItem.label.string,
4937 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
4938 return 2052;
4939 }
4940
Laurence Lundblade937ea812020-05-08 11:38:23 -07004941
Laurence Lundblade9b334962020-08-27 10:55:53 -07004942 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004943 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004944 QCBORDecode_GetNext(&DCtx, &Item1);
4945 QCBORDecode_GetNext(&DCtx, &Item1);
4946 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004947 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4948 QCBORDecode_GetNext(&DCtx, &Item1);
4949 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4950 return 2003;
4951 }
4952
Laurence Lundblade9b334962020-08-27 10:55:53 -07004953 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004954 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004955 QCBORDecode_GetNext(&DCtx, &Item1);
4956 QCBORDecode_GetNext(&DCtx, &Item1);
4957 QCBORDecode_GetNext(&DCtx, &Item1);
4958 QCBORDecode_GetNext(&DCtx, &Item1);
4959 QCBORDecode_GetNext(&DCtx, &Item1);
4960 QCBORDecode_GetNext(&DCtx, &Item1);
4961 QCBORDecode_GetNext(&DCtx, &Item1);
4962 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4963 QCBORDecode_GetNext(&DCtx, &Item1);
4964 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004965 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004966 }
4967
Laurence Lundblade9b334962020-08-27 10:55:53 -07004968 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004969 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004970 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4971 QCBORDecode_ExitArray(&DCtx);
4972 QCBORDecode_GetNext(&DCtx, &Item1);
4973 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4974 return 2006;
4975 }
4976 QCBORDecode_ExitMap(&DCtx);
4977 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4978 return 2007;
4979 }
4980
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004981 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004982 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004983 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004984 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4985 uErr = QCBORDecode_GetAndResetError(&DCtx);
4986 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004987 return 2008;
4988 }
4989 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004990 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004991 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004992 return 2009;
4993 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004994
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004995
4996 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004997 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004998 // This will fail because the map is empty.
4999 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5000 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005001 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005002 return 2010;
5003 }
5004 QCBORDecode_ExitMap(&DCtx);
5005 uErr = QCBORDecode_Finish(&DCtx);
5006 if(uErr != QCBOR_SUCCESS){
5007 return 2011;
5008 }
5009
5010
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005011 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005012 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005013 // This will fail because the map is empty.
5014 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5015 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005016 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005017 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005018 }
5019 QCBORDecode_ExitMap(&DCtx);
5020 uErr = QCBORDecode_Finish(&DCtx);
5021 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005022 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005023 }
5024
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005025
5026 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005027 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005028 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005029 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005030 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005031 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005032 QCBORDecode_ExitArray(&DCtx);
5033 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5034 QCBORDecode_ExitArray(&DCtx);
5035 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005036 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005037 return 2014;
5038 }
5039
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005040 int64_t nInt;
5041 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005042 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005043 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5044 uErr = QCBORDecode_GetAndResetError(&DCtx);
5045 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5046 return 2021;
5047 }
5048
5049 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5050 uErr = QCBORDecode_GetAndResetError(&DCtx);
5051 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5052 return 2022;
5053 }
5054
5055 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5056 uErr = QCBORDecode_GetAndResetError(&DCtx);
5057 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5058 return 2023;
5059 }
5060
5061 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5062 uErr = QCBORDecode_GetAndResetError(&DCtx);
5063#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5064 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5065 return 2024;
5066 }
5067#else
5068 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5069 return 2027;
5070 }
5071#endif
5072
5073 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5074 uErr = QCBORDecode_GetAndResetError(&DCtx);
5075 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5076 return 2025;
5077 }
5078
5079 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5080
5081 QCBORDecode_ExitMap(&DCtx);
5082 uErr = QCBORDecode_Finish(&DCtx);
5083 if(uErr != QCBOR_SUCCESS) {
5084 return 2026;
5085 }
5086
5087 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005088 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005089 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5090 uErr = QCBORDecode_GetAndResetError(&DCtx);
5091 if(uErr != QCBOR_ERR_BAD_BREAK) {
5092 return 2030;
5093 }
5094
5095 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005096 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005097 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5098 uErr = QCBORDecode_GetAndResetError(&DCtx);
5099 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5100 return 2031;
5101 }
5102
5103 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005104 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005105 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5106 uErr = QCBORDecode_GetAndResetError(&DCtx);
5107 if(uErr != QCBOR_ERR_HIT_END) {
5108 return 2032;
5109 }
5110
5111 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005112 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005113 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5114 uErr = QCBORDecode_GetAndResetError(&DCtx);
5115 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5116 return 2033;
5117 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005118
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005119 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005120}
5121
5122
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005123struct NumberConversion {
5124 char *szDescription;
5125 UsefulBufC CBOR;
5126 int64_t nConvertedToInt64;
5127 QCBORError uErrorInt64;
5128 uint64_t uConvertToUInt64;
5129 QCBORError uErrorUint64;
5130 double dConvertToDouble;
5131 QCBORError uErrorDouble;
5132};
5133
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005134static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005135 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005136 "too large to fit into int64_t",
5137 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5138 0,
5139 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5140 0,
5141 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5142 ((double)INT64_MIN) + 1 ,
5143 QCBOR_SUCCESS
5144 },
5145 {
5146 "largest negative int that fits in int64_t",
5147 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5148 INT64_MIN,
5149 QCBOR_SUCCESS,
5150 0,
5151 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5152 (double)INT64_MIN,
5153 QCBOR_SUCCESS
5154 },
5155 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005156 "negative bignum -1",
5157 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5158 -1,
5159 QCBOR_SUCCESS,
5160 0,
5161 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5162 -1.0,
5163 QCBOR_SUCCESS
5164 },
5165 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005166 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005167 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5168 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005169#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005170 257000,
5171 QCBOR_SUCCESS,
5172 257000,
5173 QCBOR_SUCCESS,
5174 257000.0,
5175 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005176#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5177 0,
5178 QCBOR_ERR_UNEXPECTED_TYPE,
5179 0,
5180 QCBOR_ERR_UNEXPECTED_TYPE,
5181 0.0,
5182 QCBOR_ERR_UNEXPECTED_TYPE
5183#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005184 },
5185 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005186 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005187 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5188 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005189#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005190 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005191 QCBOR_SUCCESS,
5192 0,
5193 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005194 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005195 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005196#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5197 0,
5198 QCBOR_ERR_UNEXPECTED_TYPE,
5199 0,
5200 QCBOR_ERR_UNEXPECTED_TYPE,
5201 0.0,
5202 QCBOR_ERR_UNEXPECTED_TYPE
5203#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005204 },
5205 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005206 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005207 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5208 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005209#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005210 2056,
5211 QCBOR_SUCCESS,
5212 2056,
5213 QCBOR_SUCCESS,
5214 2056.0,
5215 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005216#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5217 0,
5218 QCBOR_ERR_UNEXPECTED_TYPE,
5219 0,
5220 QCBOR_ERR_UNEXPECTED_TYPE,
5221 0.0,
5222 QCBOR_ERR_UNEXPECTED_TYPE
5223#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005224 },
5225 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005226 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005227 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5228 0,
5229 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5230 0,
5231 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5232 -18446744073709551617.0,
5233 QCBOR_SUCCESS
5234 },
5235 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005236 "Positive bignum 0x01020304 indefinite length string",
5237 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5238 0x01020304,
5239 QCBOR_SUCCESS,
5240 0x01020304,
5241 QCBOR_SUCCESS,
5242 16909060.0,
5243 QCBOR_SUCCESS
5244 },
5245 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005246 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005247 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5248 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005249#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005250 0,
5251 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5252 0,
5253 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5254 -INFINITY,
5255 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005256#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5257 0,
5258 QCBOR_ERR_UNEXPECTED_TYPE,
5259 0,
5260 QCBOR_ERR_UNEXPECTED_TYPE,
5261 0.0,
5262 QCBOR_ERR_UNEXPECTED_TYPE
5263#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005264 },
5265 {
5266 "big float [9223372036854775806, 9223372036854775806]",
5267 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5268 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005269#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005270 0,
5271 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5272 0,
5273 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5274 INFINITY,
5275 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005276#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5277 0,
5278 QCBOR_ERR_UNEXPECTED_TYPE,
5279 0,
5280 QCBOR_ERR_UNEXPECTED_TYPE,
5281 0.0,
5282 QCBOR_ERR_UNEXPECTED_TYPE
5283#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005284 },
5285 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005286 "Big float 3 * 2^^2",
5287 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005288#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005289 12,
5290 QCBOR_SUCCESS,
5291 12,
5292 QCBOR_SUCCESS,
5293 12.0,
5294 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005295#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5296 0,
5297 QCBOR_ERR_UNEXPECTED_TYPE,
5298 0,
5299 QCBOR_ERR_UNEXPECTED_TYPE,
5300 0.0,
5301 QCBOR_ERR_UNEXPECTED_TYPE
5302#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005303 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005304 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005305 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005306 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5307 0,
5308 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5309 18446744073709551615ULL,
5310 QCBOR_SUCCESS,
5311 18446744073709551615.0,
5312 QCBOR_SUCCESS
5313 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005314 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005315 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005316 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5317 65536-1,
5318 QCBOR_SUCCESS,
5319 0xffff,
5320 QCBOR_SUCCESS,
5321 65535.0,
5322 QCBOR_SUCCESS
5323 },
5324 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005325 "Postive integer 0",
5326 {(uint8_t[]){0x0}, 1},
5327 0LL,
5328 QCBOR_SUCCESS,
5329 0ULL,
5330 QCBOR_SUCCESS,
5331 0.0,
5332 QCBOR_SUCCESS
5333 },
5334 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005335 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005336 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5337 -9223372036854775807-1, // INT64_MIN
5338 QCBOR_SUCCESS,
5339 0ULL,
5340 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5341 -9223372036854775808.0,
5342 QCBOR_SUCCESS
5343 },
5344 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005345 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005346 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005347#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005348 100L,
5349 QCBOR_SUCCESS,
5350 100ULL,
5351 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005352#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5353 0,
5354 QCBOR_ERR_HW_FLOAT_DISABLED,
5355 0,
5356 QCBOR_ERR_HW_FLOAT_DISABLED,
5357#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005358 100.3,
5359 QCBOR_SUCCESS
5360 },
5361 {
5362 "Floating point value NaN 0xfa7fc00000",
5363 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005364#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005365 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005366 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005367 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005368 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005369#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5370 0,
5371 QCBOR_ERR_HW_FLOAT_DISABLED,
5372 0,
5373 QCBOR_ERR_HW_FLOAT_DISABLED,
5374#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005375 NAN,
5376 QCBOR_SUCCESS
5377 },
5378 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005379 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005380 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005381#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5382#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5383 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005384 -4,
5385 QCBOR_SUCCESS,
5386 0,
5387 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5388 -4.0,
5389 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005390#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5391 // Float HW disabled
5392 -4,
5393 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5394 0,
5395 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5396 -4.0,
5397 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5398#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5399#else
5400 // Half-precision disabled
5401 -4,
5402 QCBOR_ERR_HALF_PRECISION_DISABLED,
5403 0,
5404 QCBOR_ERR_HALF_PRECISION_DISABLED,
5405 -4.0,
5406 QCBOR_ERR_HALF_PRECISION_DISABLED
5407#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005408 },
5409 {
5410 "Decimal fraction 3/10",
5411 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005412#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005413 0,
5414 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5415 0,
5416 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5417 0.30000000000000004,
5418 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005419#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5420 0,
5421 QCBOR_ERR_UNEXPECTED_TYPE,
5422 0,
5423 QCBOR_ERR_UNEXPECTED_TYPE,
5424 0.0,
5425 QCBOR_ERR_UNEXPECTED_TYPE
5426#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005427 },
5428 {
5429 "+inifinity",
5430 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5431#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5432 0,
5433 QCBOR_ERR_FLOAT_EXCEPTION,
5434 0,
5435 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5436#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5437 0,
5438 QCBOR_ERR_HW_FLOAT_DISABLED,
5439 0,
5440 QCBOR_ERR_HW_FLOAT_DISABLED,
5441#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5442 INFINITY,
5443 QCBOR_SUCCESS
5444 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005445
5446 {
5447 "extreme pos bignum",
5448 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5449 // 50 rows of 8 is 400 digits.
5450 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5451 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5452 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5453 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5454 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5455 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5456 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5457 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5458 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5481 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5482 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5483 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5484 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5485 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5486 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5487 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5488 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5489 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5490 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5491 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5492 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5493 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5494 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5495 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5496 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5497 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5498 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5499 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5500 404},
5501 0,
5502 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5503 0,
5504 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5505#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5506 INFINITY,
5507 QCBOR_SUCCESS
5508#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5509 0,
5510 QCBOR_ERR_HW_FLOAT_DISABLED,
5511#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5512 },
5513
5514 {
5515 "extreme neg bignum",
5516 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5517 // 50 rows of 8 is 400 digits.
5518 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5519 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5520 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5521 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5522 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5523 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5524 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5525 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5526 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5527 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5528 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5529 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5530 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5531 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5532 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5533 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5534 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5535 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5536 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5537 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5538 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5539 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5540 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5541 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5542 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5543 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5544 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5545 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5546 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5547 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5548 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5549 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5550 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5551 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5552 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5553 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5554 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5555 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5556 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5557 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5558 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5559 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5560 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5561 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5562 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5563 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5564 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5565 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5566 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5567 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5568 404},
5569 0,
5570 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5571 0,
5572 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5573#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5574 -INFINITY,
5575 QCBOR_SUCCESS
5576#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5577 0,
5578 QCBOR_ERR_HW_FLOAT_DISABLED,
5579#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5580 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005581
5582 {
5583 "big float underflow [9223372036854775806, -9223372036854775806]",
5584 {(uint8_t[]){
5585 0xC5, 0x82,
5586 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5587 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5588#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5589 0,
5590 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5591 0,
5592 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5593 0,
5594 QCBOR_SUCCESS
5595#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5596 0,
5597 QCBOR_ERR_UNEXPECTED_TYPE,
5598 0,
5599 QCBOR_ERR_UNEXPECTED_TYPE,
5600 0.0,
5601 QCBOR_ERR_UNEXPECTED_TYPE
5602#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5603 },
5604
5605 {
5606 "bigfloat that evaluates to -INFINITY",
5607 {(uint8_t[]){
5608 0xC5, 0x82,
5609 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5610 0xC3, 0x42, 0x01, 0x01}, 15},
5611#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5612 0,
5613 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5614 0,
5615 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5616 -INFINITY,
5617 QCBOR_SUCCESS
5618#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5619 0,
5620 QCBOR_ERR_UNEXPECTED_TYPE,
5621 0,
5622 QCBOR_ERR_UNEXPECTED_TYPE,
5623 0.0,
5624 QCBOR_ERR_UNEXPECTED_TYPE
5625#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5626 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005627};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005628
5629
5630
Laurence Lundblade313b2862020-05-16 01:23:06 -07005631int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005632{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005633 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005634
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005635 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5636 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005637
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005638 // Set up the decoding context including a memory pool so that
5639 // indefinite length items can be checked
5640 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005641 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005642
5643 /* ----- test conversion to int64_t ------ */
5644 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005645 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5646 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005647 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005648 }
5649
5650 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005651 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005652 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005653 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005654 }
5655 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005656 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005657 }
5658
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005659 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005660 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5661 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5662 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005663 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005664 }
5665 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005666 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005667 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005668 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005669 }
5670 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005671 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005672 }
5673
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005674 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005675 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5676 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5677 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005678 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005679 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005680#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005681 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005682 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005683 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005684 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005685 }
5686 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5687 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005688 // NaN's can't be compared for equality. A NaN is
5689 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005690 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005691 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005692 }
5693 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005694 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005695 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005696 }
5697 }
5698 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005699#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005700 }
5701
5702 return 0;
5703}
5704
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005705
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005706
5707
Laurence Lundbladee3553422020-05-02 11:11:17 -07005708int32_t CBORSequenceDecodeTests(void)
5709{
5710 QCBORDecodeContext DCtx;
5711 QCBORItem Item;
5712 QCBORError uCBORError;
5713
5714 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005715
Laurence Lundbladee3553422020-05-02 11:11:17 -07005716 // The input for the date test happens to be a sequence so it
5717 // is reused. It is a sequence because it doesn't start as
5718 // an array or map.
5719 QCBORDecode_Init(&DCtx,
5720 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5721 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005722
Laurence Lundbladee3553422020-05-02 11:11:17 -07005723 // Get the first item
5724 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5725 if(uCBORError != QCBOR_SUCCESS) {
5726 return 1;
5727 }
5728 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5729 return 2;
5730 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005731
Laurence Lundbladee3553422020-05-02 11:11:17 -07005732 // Get a second item
5733 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005734 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5735 return 66;
5736 }
5737
5738 // Get a third item
5739 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005740 if(uCBORError != QCBOR_SUCCESS) {
5741 return 2;
5742 }
5743 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5744 return 3;
5745 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005746
Laurence Lundbladee3553422020-05-02 11:11:17 -07005747 // A sequence can have stuff at the end that may
5748 // or may not be valid CBOR. The protocol decoder knows
5749 // when to stop by definition of the protocol, not
5750 // when the top-level map or array is ended.
5751 // Finish still has to be called to know that
5752 // maps and arrays (if there were any) were closed
5753 // off correctly. When called like this it
5754 // must return the error QCBOR_ERR_EXTRA_BYTES.
5755 uCBORError = QCBORDecode_Finish(&DCtx);
5756 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5757 return 4;
5758 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005759
5760
Laurence Lundbladee3553422020-05-02 11:11:17 -07005761 // --- Test an empty input ----
5762 uint8_t empty[1];
5763 UsefulBufC Empty = {empty, 0};
5764 QCBORDecode_Init(&DCtx,
5765 Empty,
5766 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005767
Laurence Lundbladee3553422020-05-02 11:11:17 -07005768 uCBORError = QCBORDecode_Finish(&DCtx);
5769 if(uCBORError != QCBOR_SUCCESS) {
5770 return 5;
5771 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005772
5773
Laurence Lundbladee3553422020-05-02 11:11:17 -07005774 // --- Sequence with unclosed indefinite length array ---
5775 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005776
Laurence Lundbladee3553422020-05-02 11:11:17 -07005777 QCBORDecode_Init(&DCtx,
5778 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5779 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005780
Laurence Lundbladee3553422020-05-02 11:11:17 -07005781 // Get the first item
5782 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5783 if(uCBORError != QCBOR_SUCCESS) {
5784 return 7;
5785 }
5786 if(Item.uDataType != QCBOR_TYPE_INT64) {
5787 return 8;
5788 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005789
Laurence Lundbladee3553422020-05-02 11:11:17 -07005790 // Get a second item
5791 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5792 if(uCBORError != QCBOR_SUCCESS) {
5793 return 9;
5794 }
5795 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5796 return 10;
5797 }
5798
5799 // Try to finish before consuming all bytes to confirm
5800 // that the still-open error is returned.
5801 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005802 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005803 return 11;
5804 }
5805
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005806
Laurence Lundbladee3553422020-05-02 11:11:17 -07005807 // --- Sequence with a closed indefinite length array ---
5808 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005809
Laurence Lundbladee3553422020-05-02 11:11:17 -07005810 QCBORDecode_Init(&DCtx,
5811 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5812 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005813
Laurence Lundbladee3553422020-05-02 11:11:17 -07005814 // Get the first item
5815 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5816 if(uCBORError != QCBOR_SUCCESS) {
5817 return 12;
5818 }
5819 if(Item.uDataType != QCBOR_TYPE_INT64) {
5820 return 13;
5821 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005822
Laurence Lundbladee3553422020-05-02 11:11:17 -07005823 // Get a second item
5824 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5825 if(uCBORError != QCBOR_SUCCESS) {
5826 return 14;
5827 }
5828 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5829 return 15;
5830 }
5831
5832 // Try to finish before consuming all bytes to confirm
5833 // that the still-open error is returned.
5834 uCBORError = QCBORDecode_Finish(&DCtx);
5835 if(uCBORError != QCBOR_SUCCESS) {
5836 return 16;
5837 }
5838
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005839
Laurence Lundbladee3553422020-05-02 11:11:17 -07005840 return 0;
5841}
5842
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005843
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005844
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005845int32_t IntToTests()
5846{
5847 int nErrCode;
5848 int32_t n32;
5849 int16_t n16;
5850 int8_t n8;
5851 uint32_t u32;
5852 uint16_t u16;
5853 uint8_t u8;
5854 uint64_t u64;
5855
5856 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5857 if(nErrCode == -1 || n32 != 1) {
5858 return 1;
5859 }
5860
5861 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5862 if(nErrCode == -1 || n32 != INT32_MAX) {
5863 return 2;
5864 }
5865
5866 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5867 if(nErrCode == -1 || n32 != INT32_MIN) {
5868 return 3;
5869 }
5870
5871 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5872 if(nErrCode != -1) {
5873 return 4;
5874 }
5875
5876 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5877 if(nErrCode != -1) {
5878 return 5;
5879 }
5880
5881
5882 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5883 if(nErrCode == -1 || n16 != INT16_MAX) {
5884 return 6;
5885 }
5886
5887 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5888 if(nErrCode == -1 || n16 != INT16_MIN) {
5889 return 7;
5890 }
5891
5892 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5893 if(nErrCode == -1 || n16 != 1) {
5894 return 8;
5895 }
5896
5897 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5898 if(nErrCode != -1) {
5899 return 9;
5900 }
5901
5902 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5903 if(nErrCode != -1) {
5904 return 10;
5905 }
5906
5907
5908 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5909 if(nErrCode == -1 || n8 != 1) {
5910 return 11;
5911 }
5912
5913 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5914 if(nErrCode == -1 || n8 != INT8_MAX) {
5915 return 12;
5916 }
5917
5918 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5919 if(nErrCode == -1 || n8 != INT8_MIN) {
5920 return 13;
5921 }
5922
5923 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5924 if(nErrCode != -1) {
5925 return 14;
5926 }
5927
5928 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5929 if(nErrCode != -1) {
5930 return 15;
5931 }
5932
5933
5934 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5935 if(nErrCode == -1 || u32 != 1) {
5936 return 16;
5937 }
5938
5939 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5940 if(nErrCode == -1 || u32 != UINT32_MAX) {
5941 return 17;
5942 }
5943
5944 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5945 if(nErrCode == -1 || u32 != 0) {
5946 return 18;
5947 }
5948
5949 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5950 if(nErrCode != -1) {
5951 return 19;
5952 }
5953
5954 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5955 if(nErrCode != -1) {
5956 return 20;
5957 }
5958
5959
5960 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5961 if(nErrCode == -1 || u16 != UINT16_MAX) {
5962 return 21;
5963 }
5964
5965 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5966 if(nErrCode == -1 || u16 != 0) {
5967 return 22;
5968 }
5969
5970 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5971 if(nErrCode == -1 || u16 != 1) {
5972 return 23;
5973 }
5974
5975 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5976 if(nErrCode != -1) {
5977 return 24;
5978 }
5979
5980 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5981 if(nErrCode != -1) {
5982 return 25;
5983 }
5984
5985
5986 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5987 if(nErrCode == -1 || u8 != UINT8_MAX) {
5988 return 26;
5989 }
5990
5991 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5992 if(nErrCode == -1 || u8 != 0) {
5993 return 27;
5994 }
5995
5996 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5997 if(nErrCode == -1 || u8 != 1) {
5998 return 28;
5999 }
6000
6001 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6002 if(nErrCode != -1) {
6003 return 29;
6004 }
6005
6006 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6007 if(nErrCode != -1) {
6008 return 30;
6009 }
6010
6011
6012 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6013 if(nErrCode == -1 || u64 != 1) {
6014 return 31;
6015 }
6016
6017 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6018 if(nErrCode == -1 || u64 != INT64_MAX) {
6019 return 32;
6020 }
6021
6022 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6023 if(nErrCode == -1 || u64 != 0) {
6024 return 33;
6025 }
6026
6027 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6028 if(nErrCode != -1) {
6029 return 34;
6030 }
6031
6032 return 0;
6033}
6034
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006035
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006036
6037
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006038/*
6039A sequence with
6040 A wrapping bstr
6041 containing a map
6042 1
6043 2
6044 A wrapping bstr
6045 containing an array
6046 3
6047 wrapping bstr
6048 4
6049 5
6050 6
6051 array
6052 7
6053 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006054 */
6055
Laurence Lundblade55013642020-09-23 05:39:22 -07006056static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006057{
Laurence Lundblade55013642020-09-23 05:39:22 -07006058 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006059 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006060 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006061
Laurence Lundblade55013642020-09-23 05:39:22 -07006062 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006063
6064 QCBOREncode_BstrWrap(&EC);
6065 QCBOREncode_OpenMap(&EC);
6066 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6067 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6068 QCBOREncode_CloseMap(&EC);
6069 QCBOREncode_BstrWrap(&EC);
6070 QCBOREncode_OpenArray(&EC);
6071 QCBOREncode_AddInt64(&EC, 3);
6072 QCBOREncode_BstrWrap(&EC);
6073 QCBOREncode_AddInt64(&EC, 4);
6074 QCBOREncode_CloseBstrWrap(&EC, NULL);
6075 QCBOREncode_AddInt64(&EC, 5);
6076 QCBOREncode_CloseArray(&EC);
6077 QCBOREncode_CloseBstrWrap(&EC, NULL);
6078 QCBOREncode_AddInt64(&EC, 6);
6079 QCBOREncode_CloseBstrWrap(&EC, NULL);
6080 QCBOREncode_OpenArray(&EC);
6081 QCBOREncode_AddInt64(&EC, 7);
6082 QCBOREncode_AddInt64(&EC, 8);
6083 QCBOREncode_CloseArray(&EC);
6084
6085 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006086 if(uErr) {
6087 Encoded = NULLUsefulBufC;
6088 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006089
6090 return Encoded;
6091}
6092
6093
6094int32_t EnterBstrTest()
6095{
Laurence Lundblade55013642020-09-23 05:39:22 -07006096 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006097
6098 QCBORDecodeContext DC;
6099
Laurence Lundblade55013642020-09-23 05:39:22 -07006100 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006101
Laurence Lundblade55013642020-09-23 05:39:22 -07006102 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006103
6104
Laurence Lundblade9b334962020-08-27 10:55:53 -07006105 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006106 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006107 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6108 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006109 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006110 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006111 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006112 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006113 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006114 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006115 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006116 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006117 QCBORDecode_ExitArray(&DC);
6118 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006119 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006120 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006121 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006122 QCBORDecode_GetInt64(&DC, &n7);
6123 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006124 QCBORDecode_ExitArray(&DC);
6125
6126 QCBORError uErr = QCBORDecode_Finish(&DC);
6127
6128 return (int32_t)uErr;
6129}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006130
6131
6132
6133
6134static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006135 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006136
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006137 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006138 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006139 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6140 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6141 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006142
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006143 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006144 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6145 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6146 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006147
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006148 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006149 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006150 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6151 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006152
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006153 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006154 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6155 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006156
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006157 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006158 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006159 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6160 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006161
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006162 21,
6163 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6164 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6165
6166 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006167 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006168 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6169 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006170
6171 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006172 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6173 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006174
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006175 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006176 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006177 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6178 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006179
6180 0x18, 0x29,
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 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006185 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006186 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6187 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006188
6189 0x18, 0x33,
6190 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6191
6192 // MIME
6193 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006194 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6195 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6196 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006197
6198 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006199 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6200 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006201
6202 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006203 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6204 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6205 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006206
6207 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006208 0x52, 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 // UUID
6212 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006213 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6214 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006215
6216 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006217 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6218 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006219};
6220
6221int32_t DecodeTaggedTypeTests()
6222{
6223 QCBORDecodeContext DC;
6224 QCBORError uErr;
6225
6226 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6227
6228 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006229 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006230
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006231 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006232 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006233 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006234 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6235 return 1;
6236 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006237 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006238 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6239 return 2;
6240 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006241 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006242 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6243 return 3;
6244 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006245 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006246 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006247 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6248 return 4;
6249 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006250 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006251 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006252 return 5;
6253 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006254
Laurence Lundblade9b334962020-08-27 10:55:53 -07006255 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006256 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6257 bNeg != false) {
6258 return 10;
6259 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006260 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006261 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6262 bNeg != true) {
6263 return 11;
6264 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006265 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006266 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6267 return 12;
6268 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006269 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006270 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006271 return 13;
6272 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006273 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006274 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006275 return 14;
6276 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006277
Laurence Lundblade9b334962020-08-27 10:55:53 -07006278 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006279 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6280 return 20;
6281 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006282 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006283 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6284 return 21;
6285 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006286 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006287 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006288 return 22;
6289 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006290 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006291 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006292 return 23;
6293 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006294
Laurence Lundblade9b334962020-08-27 10:55:53 -07006295 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006296 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6297 return 30;
6298 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006299 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006300 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6301 return 31;
6302 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006303 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006304 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006305 return 32;
6306 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006307 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006308 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006309 return 33;
6310 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006311
Laurence Lundblade9b334962020-08-27 10:55:53 -07006312 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006313 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6314 return 40;
6315 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006316 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006317 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6318 return 41;
6319 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006320 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006321 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006322 return 42;
6323 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006324 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006325 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006326 return 43;
6327 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006328
Laurence Lundblade9b334962020-08-27 10:55:53 -07006329 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006330 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6331 return 50;
6332 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006333 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006334 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6335 return 51;
6336 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006337 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006338 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006339 return 52;
6340 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006341 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006342 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006343 return 53;
6344 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006345
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006346 // MIME
6347 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006348 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006349 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6350 bIsNot7Bit == true) {
6351 return 60;
6352 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006353 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006354 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6355 bIsNot7Bit == true) {
6356 return 61;
6357 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006358 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006359 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6360 bIsNot7Bit == false) {
6361 return 62;
6362 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006363 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006364 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6365 bIsNot7Bit == false) {
6366 return 63;
6367 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006368 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006369 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006370 return 64;
6371 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006372 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006373 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006374 return 65;
6375 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006376
Laurence Lundblade9b334962020-08-27 10:55:53 -07006377 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006378 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6379 return 70;
6380 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006381 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006382 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6383 return 71;
6384 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006385 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006386 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006387 return 72;
6388 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006389 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006390 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006391 return 73;
6392 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006393
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006394 // Improvement: add some more error test cases
6395
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006396 QCBORDecode_ExitMap(&DC);
6397
6398 uErr = QCBORDecode_Finish(&DC);
6399 if(uErr != QCBOR_SUCCESS) {
6400 return 100;
6401 }
6402
6403 return 0;
6404}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006405
6406
6407
6408
6409/*
6410 [
6411 "aaaaaaaaaa",
6412 {}
6413 ]
6414 */
6415static const uint8_t spTooLarge1[] = {
6416 0x9f,
6417 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6418 0xa0,
6419 0xff
6420};
6421
6422/*
6423 [
6424 {
6425 0: "aaaaaaaaaa"
6426 }
6427 ]
6428 */
6429static const uint8_t spTooLarge2[] = {
6430 0x9f,
6431 0xa1,
6432 0x00,
6433 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6434 0xff
6435};
6436
6437/*
6438 h'A1006A61616161616161616161'
6439
6440 {
6441 0: "aaaaaaaaaa"
6442 }
6443 */
6444static const uint8_t spTooLarge3[] = {
6445 0x4d,
6446 0xa1,
6447 0x00,
6448 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6449};
6450
6451int32_t TooLargeInputTest(void)
6452{
6453 QCBORDecodeContext DC;
6454 QCBORError uErr;
6455 UsefulBufC String;
6456
6457 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6458 // to 10 There's not really any way to test this error
6459 // condition. The error condition is not complex, so setting
6460 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6461
6462 // The input CBOR is only too large because the
6463 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6464 //
6465 // This test is disabled for the normal test runs because of the
6466 // special build requirement.
6467
6468
6469 // Tests the start of a map being too large
6470 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006471 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006472 QCBORDecode_GetTextString(&DC, &String);
6473 uErr = QCBORDecode_GetError(&DC);
6474 if(uErr != QCBOR_SUCCESS) {
6475 return 1;
6476 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006477 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006478 uErr = QCBORDecode_GetError(&DC);
6479 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6480 return 2;
6481 }
6482
6483 // Tests the end of a map being too large
6484 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006485 QCBORDecode_EnterArray(&DC, NULL);
6486 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006487 uErr = QCBORDecode_GetError(&DC);
6488 if(uErr != QCBOR_SUCCESS) {
6489 return 3;
6490 }
6491 QCBORDecode_ExitMap(&DC);
6492 uErr = QCBORDecode_GetError(&DC);
6493 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6494 return 4;
6495 }
6496
6497 // Tests the entire input CBOR being too large when processing bstr wrapping
6498 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6499 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6500 uErr = QCBORDecode_GetError(&DC);
6501 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6502 return 5;
6503 }
6504
6505 return 0;
6506}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006507
6508
6509static const uint8_t spMapWithIndefLenStrings[] = {
6510 0xbf,
6511 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6512 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6513 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6514 0x03,
6515 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6516 0xc3,
6517 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6518 0xff
6519};
6520
6521int32_t SpiffyIndefiniteLengthStringsTests()
6522{
6523 QCBORDecodeContext DCtx;
6524
6525 QCBORDecode_Init(&DCtx,
6526 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6527 QCBOR_DECODE_MODE_NORMAL);
6528
6529 MakeUsefulBufOnStack(StringBuf, 200);
6530 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6531
6532 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006533 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006534 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6535 if(QCBORDecode_GetAndResetError(&DCtx)) {
6536 return 1;
6537 }
6538
6539 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6540 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6541 return 2;
6542 }
6543
6544 uint64_t uInt;
6545 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6546 if(QCBORDecode_GetAndResetError(&DCtx)) {
6547 return 3;
6548 }
6549 if(uInt != 3) {
6550 return 4;
6551 }
6552
6553 double uDouble;
6554 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6555 "label2",
6556 0xff,
6557 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006558#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006559 if(QCBORDecode_GetAndResetError(&DCtx)) {
6560 return 5;
6561 }
6562 if(uDouble != -16777474) {
6563 return 6;
6564 }
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006565#else
6566 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6567 return 7;
6568 }
6569#endif
6570
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006571
6572 QCBORDecode_ExitMap(&DCtx);
6573
6574 if(QCBORDecode_Finish(&DCtx)) {
6575 return 99;
6576 }
6577
6578 return 0;
6579}