blob: f748104fb329d3b989fe3b0355955af84dd6a5b2 [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
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001638 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
1639 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001640 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1641 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1642 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1643
Laurence Lundbladeee851742020-01-08 08:37:05 -08001644 // Set up decoder context. String allocator needed for indefinite
1645 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001646 QCBORDecodeContext DCtx;
1647 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001648#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001649 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1650 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001651#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001652
1653 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001654 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001655 do {
1656 QCBORItem Item;
1657
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001658 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1659 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001660
1661 // Every test vector must fail with
1662 // a not-well-formed error. If not
1663 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001664 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001665 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001666 // Return index of failure in the error code
1667 return 2000 + nIterate;
1668 }
1669 }
1670 return 0;
1671}
1672
1673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001674struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001675 UsefulBufC Input;
1676 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001677};
1678
Laurence Lundblade59289e52019-12-30 13:44:37 -08001679
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001680static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001681{
1682 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1683 // Set up the decoding context including a memory pool so that
1684 // indefinite length items can be checked
1685 QCBORDecodeContext DCtx;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001686 QCBORError nCBORError;
1687
Laurence Lundblade59289e52019-12-30 13:44:37 -08001688 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001689
1690#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08001691 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001692
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001693 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001694 if(nCBORError) {
1695 return -9;
1696 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001697#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1698
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001699
Laurence Lundblade59289e52019-12-30 13:44:37 -08001700 // Iterate until there is an error of some sort error
1701 QCBORItem Item;
1702 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001703 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001704 memset(&Item, 0x33, sizeof(Item));
1705
1706 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1707 } while(nCBORError == QCBOR_SUCCESS);
1708
1709 // Must get the expected error or the this test fails
1710 // The data and label type must also be QCBOR_TYPE_NONE
1711 if(nCBORError != pF->nError ||
1712 Item.uDataType != QCBOR_TYPE_NONE ||
1713 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001714 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001715 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001716 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001717 }
1718 }
1719
1720 return 0;
1721}
1722
1723
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001724struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001725 // Most of this is copied from not_well_formed.h. Here the error code
1726 // returned is also checked.
1727
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001728#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001729 // Indefinite length strings must be closed off
1730 // An indefinite length byte string not closed off
1731 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1732 // An indefinite length text string not closed off
1733 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1734
1735
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001736 // All the chunks in an indefinite length string must be of the type of
1737 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001738 // indefinite length byte string with text string chunk
1739 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1740 // indefinite length text string with a byte string chunk
1741 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1742 // indefinite length byte string with an positive integer chunk
1743 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1744 // indefinite length byte string with an negative integer chunk
1745 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1746 // indefinite length byte string with an array chunk
1747 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1748 // indefinite length byte string with an map chunk
1749 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1750 // indefinite length byte string with tagged integer chunk
1751 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1752 // indefinite length byte string with an simple type chunk
1753 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1754 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1755 // indefinite length text string with indefinite string inside
1756 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1757
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001758#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1759
1760 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1761 // An indefinite length text string not closed off
1762 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1763
1764
1765 // All the chunks in an indefinite length string must be of the type of
1766 // indefinite length string
1767 // indefinite length byte string with text string chunk
1768 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1769 // indefinite length text string with a byte string chunk
1770 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1771 // indefinite length byte string with an positive integer chunk
1772 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1773 // indefinite length byte string with an negative integer chunk
1774 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1775 // indefinite length byte string with an array chunk
1776 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1777 // indefinite length byte string with an map chunk
1778 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1779 // indefinite length byte string with tagged integer chunk
1780 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1781 // indefinite length byte string with an simple type chunk
1782 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1783 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1784 // indefinite length text string with indefinite string inside
1785 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1786#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1787
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001788
1789 // Definte length maps and arrays must be closed by having the right number of items
1790 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001791 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001792 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001793 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001794 // A definte length array that is supposed to have 511 items, but has only 1
1795 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1796 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001797 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001798 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001799 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001800
1801
1802 // Indefinte length maps and arrays must be ended by a break
1803 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001804 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001805 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001806 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001807 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001808 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001809 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001810 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001811
1812
1813 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001814 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001815 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001816 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001817 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001818 // Unclosed indefinite map containing a closed definite length array
1819 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1820 // Definite length map containing an unclosed indefinite length array
1821 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001822 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001823 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001824 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001825 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001826 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001827 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001828 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001829 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001830 // Unclosed indefinite length map in definite length maps
1831 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1832 QCBOR_ERR_NO_MORE_ITEMS},
1833 // Unclosed definite length map in indefinite length maps
1834 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1835 // Unclosed indefinite length array in definite length maps
1836 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1837 QCBOR_ERR_NO_MORE_ITEMS},
1838 // Unclosed definite length array in indefinite length maps
1839 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1840 // Unclosed indefinite length map in definite length arrays
1841 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1842 // Unclosed definite length map in indefinite length arrays
1843 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001844
1845
1846 // The "argument" for the data item is incomplete
1847 // Positive integer missing 1 byte argument
1848 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1849 // Positive integer missing 2 byte argument
1850 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1851 // Positive integer missing 4 byte argument
1852 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1853 // Positive integer missing 8 byte argument
1854 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1855 // Positive integer missing 1 byte of 2 byte argument
1856 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1857 // Positive integer missing 2 bytes of 4 byte argument
1858 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1859 // Positive integer missing 1 bytes of 7 byte argument
1860 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1861 // Negative integer missing 1 byte argument
1862 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1863 // Binary string missing 1 byte argument
1864 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1865 // Text string missing 1 byte argument
1866 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1867 // Array missing 1 byte argument
1868 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1869 // Map missing 1 byte argument
1870 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1871 // Tag missing 1 byte argument
1872 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1873 // Simple missing 1 byte argument
1874 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001875 // half-precision with 1 byte argument
1876 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1877 // single-precision with 2 byte argument
1878 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1879 // double-precision with 3 byte argument
1880 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
1881
1882
1883 // Tag with no content
1884 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001885
1886
1887 // Breaks must not occur in definite length arrays and maps
1888 // Array of length 1 with sole member replaced by a break
1889 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1890 // Array of length 2 with 2nd member replaced by a break
1891 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1892 // Map of length 1 with sole member label replaced by a break
1893 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1894 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001895 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001896 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1897 // Array of length 1 with 2nd member value replaced by a break
1898 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1899 // Map of length 2 with 2nd member replaced by a break
1900 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1901
1902
1903 // Breaks must not occur on their own out of an indefinite length data item
1904 // A bare break is not well formed
1905 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1906 // A bare break after a zero length definite length array
1907 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1908 // A bare break after a zero length indefinite length map
1909 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001910 // A break inside a definite length array inside an indefenite length array
1911 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1912 // Complicated mixed nesting with break outside indefinite length array
1913 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001914
1915
1916 // Forbidden two byte encodings of simple types
1917 // Must use 0xe0 instead
1918 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1919 // Should use 0xe1 instead
1920 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1921 // Should use 0xe2 instead
1922 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1923 // Should use 0xe3 instead
1924 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1925 // Should use 0xe4 instead
1926 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1927 // Should use 0xe5 instead
1928 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1929 // Should use 0xe6 instead
1930 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1931 // Should use 0xe7 instead
1932 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1933 // Should use 0xe8 instead
1934 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1935 // Should use 0xe9 instead
1936 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1937 // Should use 0xea instead
1938 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1939 // Should use 0xeb instead
1940 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1941 // Should use 0xec instead
1942 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1943 // Should use 0xed instead
1944 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1945 // Should use 0xee instead
1946 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1947 // Should use 0xef instead
1948 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1949 // Should use 0xf0 instead
1950 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1951 // Should use 0xf1 instead
1952 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1953 // Should use 0xf2 instead
1954 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1955 // Must use 0xf3 instead
1956 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1957 // Must use 0xf4 instead
1958 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1959 // Must use 0xf5 instead
1960 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1961 // Must use 0xf6 instead
1962 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1963 // Must use 0xf7 instead
1964 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1965 // Must use 0xf8 instead
1966 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001967 // Reserved
1968 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001969
1970 // Integers with additional info indefinite length
1971 // Positive integer with additional info indefinite length
1972 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1973 // Negative integer with additional info indefinite length
1974 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1975 // CBOR tag with "argument" an indefinite length
1976 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1977 // CBOR tag with "argument" an indefinite length alternate vector
1978 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1979
1980
1981 // Missing bytes from a deterministic length string
1982 // A byte string is of length 1 without the 1 byte
1983 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1984 // A text string is of length 1 without the 1 byte
1985 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001986 // Byte string should have 2^32-15 bytes, but has one
1987 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1988 // Byte string should have 2^32-15 bytes, but has one
1989 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001990 // Byte string should have 2^64 bytes, but has 3
1991 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1992 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
1993 // Text string should have 2^64 bytes, but has 3
1994 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1995 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001996
1997 // Use of unassigned additional information values
1998 // Major type positive integer with reserved value 28
1999 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2000 // Major type positive integer with reserved value 29
2001 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2002 // Major type positive integer with reserved value 30
2003 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2004 // Major type negative integer with reserved value 28
2005 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2006 // Major type negative integer with reserved value 29
2007 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2008 // Major type negative integer with reserved value 30
2009 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2010 // Major type byte string with reserved value 28 length
2011 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2012 // Major type byte string with reserved value 29 length
2013 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2014 // Major type byte string with reserved value 30 length
2015 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2016 // Major type text string with reserved value 28 length
2017 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2018 // Major type text string with reserved value 29 length
2019 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2020 // Major type text string with reserved value 30 length
2021 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2022 // Major type array with reserved value 28 length
2023 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2024 // Major type array with reserved value 29 length
2025 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2026 // Major type array with reserved value 30 length
2027 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2028 // Major type map with reserved value 28 length
2029 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2030 // Major type map with reserved value 29 length
2031 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2032 // Major type map with reserved value 30 length
2033 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2034 // Major type tag with reserved value 28 length
2035 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2036 // Major type tag with reserved value 29 length
2037 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2038 // Major type tag with reserved value 30 length
2039 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2040 // Major type simple with reserved value 28 length
2041 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2042 // Major type simple with reserved value 29 length
2043 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2044 // Major type simple with reserved value 30 length
2045 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2046
2047
2048 // Maps must have an even number of data items (key & value)
2049 // Map with 1 item when it should have 2
2050 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2051 // Map with 3 item when it should have 4
2052 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
2053 // Map with 1 item when it should have 2
2054 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2055 // Map with 3 item when it should have 4
2056 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
2057
2058
2059 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002060 // Text-based date, with an integer
2061 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2062 // Epoch date, with an byte string
2063 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2064 // tagged as both epoch and string dates
2065 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2066 // big num tagged an int, not a byte string
2067 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002068};
2069
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002070int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002071{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002072 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002073
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002074 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002075 if(nResult) {
2076 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002078
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002079 // Corrupt the UsefulInputBuf and see that
2080 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002081 QCBORDecodeContext DCtx;
2082 QCBORItem Item;
2083 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002084
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002085 QCBORDecode_Init(&DCtx,
2086 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2087 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002088
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002089 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2090 return (int32_t)uQCBORError;
2091 }
2092 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2093 // This wasn't supposed to happen
2094 return -1;
2095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002096
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002097 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002098
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002099 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2100 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2101 // Did not get back the error expected
2102 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002104
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002105
Laurence Lundblade98427e92020-09-28 21:33:23 -07002106 /*
2107 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2108 tests here can be performed to see that the max length
2109 error check works correctly. See DecodeBytes(). If the max
2110 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002111
Laurence Lundblade98427e92020-09-28 21:33:23 -07002112 This test will automatocally adapt the all CPU sizes
2113 through the use of SIZE_MAX.
2114 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002115
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002116 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002117 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002118
Laurence Lundblade98427e92020-09-28 21:33:23 -07002119 // This makes a CBOR head with a text string that is very long
2120 // but doesn't fill in the bytes of the text string as that is
2121 // not needed to test this part of QCBOR.
2122 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2123
2124 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2125
2126 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2127 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002128 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002129
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002130 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131}
2132
2133
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002134/* Try all 256 values of the byte at nLen including recursing for
2135 each of the values to try values at nLen+1 ... up to nLenMax
2136 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002137static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002138{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002139 if(nLen >= nLenMax) {
2140 return;
2141 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002142
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002143 for(int inputByte = 0; inputByte < 256; inputByte++) {
2144 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002145 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002146 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002147
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002148 // Get ready to parse
2149 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002150 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002151
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002152 // Parse by getting the next item until an error occurs
2153 // Just about every possible decoder error can occur here
2154 // The goal of this test is not to check for the correct
2155 // error since that is not really possible. It is to
2156 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002157 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002158 QCBORItem Item;
2159 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002160 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161 break;
2162 }
2163 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002165 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002166 }
2167}
2168
2169
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002170int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002171{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002172 // Size 2 tests 64K inputs and runs quickly
2173 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002174
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002175 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002177 return 0;
2178}
2179
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002180
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002181int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002182{
2183 // size 3 tests 16 million inputs and runs OK
2184 // in seconds on fast machines. Size 4 takes
2185 // 10+ minutes and 5 half a day on fast
2186 // machines. This test is kept separate from
2187 // the others so as to no slow down the use
2188 // of them as a very frequent regression.
2189 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002190
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002191 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002192
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002193 return 0;
2194}
2195
2196
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002197static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002198 0xc0, // tag for string date
2199 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002200
Laurence Lundbladec7114722020-08-13 05:11:40 -07002201 0xc0, // tag for string date
2202 0x00, // Wrong type for a string date
2203
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002204 0xc1, // tag for epoch date
2205 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2206
Laurence Lundbladec7114722020-08-13 05:11:40 -07002207 0xc1,
2208 0x62, 'h', 'i', // wrong type tagged
2209
Laurence Lundblade99615302020-11-29 11:19:47 -08002210 // CBOR_TAG_ENC_AS_B64
2211 0xcf, 0xd8, 0x16, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002212 0x1a, 0x53, 0x72, 0x4E, 0x01,
2213
2214 0xc1, // tag for epoch date
2215 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002216
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002217 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002218 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002220 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002221 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002222
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002223 0xc1, // tag for epoch date
2224 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2225 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2226
2227 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002228 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2229
2230 0xc1, // tag for epoch date
2231 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2232
2233 0xc1,
2234 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2235
2236 0xc1, // tag for epoch date
2237 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002238};
2239
2240
Laurence Lundbladec7114722020-08-13 05:11:40 -07002241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002242// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002243#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002244static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002245
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002246 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002247
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002248 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002250 return diff > 0.0000001;
2251}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002252#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002253
2254
Laurence Lundblade99615302020-11-29 11:19:47 -08002255
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002256int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002257{
2258 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002259 QCBORItem Item;
2260 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002261
Laurence Lundbladeee851742020-01-08 08:37:05 -08002262 QCBORDecode_Init(&DCtx,
2263 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2264 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002265
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002266 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002267 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002268 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002269 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002270 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002271 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002272 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002273 }
2274
Laurence Lundbladec7114722020-08-13 05:11:40 -07002275 // Wrong type for a string date
2276 uError = QCBORDecode_GetNext(&DCtx, &Item);
2277 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002278 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002279 }
2280
2281 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2282 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2283 return -4;
2284 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002285 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2286 Item.val.epochDate.nSeconds != 1400000000 ||
2287 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002288 return -5;
2289 }
2290
2291 // Wrong type for an epoch date
2292 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2293 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundblade99615302020-11-29 11:19:47 -08002296 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2297 // The date is decoded and the two tags are returned. This is to
2298 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002299 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2300 return -7;
2301 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002302 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2303 Item.val.epochDate.nSeconds != 1400000001 ||
2304 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundblade99615302020-11-29 11:19:47 -08002305 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002306 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002307 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002308
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002309 // Epoch date that is too large for our representation
2310 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002311 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002313
Laurence Lundblade9682a532020-06-06 18:33:04 -07002314#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002315 // Epoch date in float format with fractional seconds
2316 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2317 return -10;
2318 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002319 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2320 Item.val.epochDate.nSeconds != 1 ||
2321 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002322 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002323 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002324
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002325 // Epoch date float that is too large for our representation
2326 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002327 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002328 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002329
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002330 // Epoch date double that is just slightly too large
2331 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002332 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002333 }
2334
2335 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002336 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2337 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2338 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2339 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002340 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002341 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002342
2343 // Nan
2344 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2345 return -15;
2346 }
2347
2348 // +Inifinity double-precision
2349 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2350 return -16;
2351 }
2352
2353#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2354 // -Inifinity half-precision
2355 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2356 return -17;
2357 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002358#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002359 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2360 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002361 }
2362#endif
2363
Laurence Lundbladec7114722020-08-13 05:11:40 -07002364#else
2365 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2366 return -19;
2367 }
2368 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2369 return -20;
2370 }
2371 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2372 return -21;
2373 }
2374 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2375 return -22;
2376 }
2377 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2378 return -23;
2379 }
2380 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2381 return -24;
2382 }
2383#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2384 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2385 return -25;
2386 }
2387#else
2388 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2389 return -26;
2390 }
2391#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002392
Laurence Lundbladec7114722020-08-13 05:11:40 -07002393#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002394
2395 return 0;
2396}
2397
Laurence Lundblade4b270642020-08-14 12:53:07 -07002398/*
2399 Test cases covered here. Some items cover more than one of these.
2400 positive integer (zero counts as a positive integer)
2401 negative integer
2402 half-precision float
2403 single-precision float
2404 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002405
Laurence Lundblade4b270642020-08-14 12:53:07 -07002406 float Overflow error
2407 Wrong type error for epoch
2408 Wrong type error for date string
2409 float disabled error
2410 half-precision disabled error
2411 -Infinity
2412 Slightly too large integer
2413 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002414
Laurence Lundblade4b270642020-08-14 12:53:07 -07002415 Get epoch by int
2416 Get string by int
2417 Get epoch by string
2418 Get string by string
2419 Fail to get epoch by wrong int label
2420 Fail to get string by wrong string label
2421 Fail to get epoch by string because it is invalid
2422 Fail to get epoch by int because it is invalid
2423
2424 Untagged values
2425 */
2426static uint8_t spSpiffyDateTestInput[] = {
2427 0x86,
2428
2429 0xc1,
2430 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2431
Laurence Lundbladec7114722020-08-13 05:11:40 -07002432 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002433 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2434
2435 0xc1, // tag for epoch date
2436 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2437
2438 0xc1, // tag for epoch date
2439 0x9f, 0xff, // Erroneous empty array as content for date
2440
2441 0xc0, // tag for string date
2442 0xbf, 0xff, // Erroneous empty map as content for date
2443
2444 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002445
2446 0x00,
2447 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002448 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002449
2450 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002451 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002452 0xc1, // tag for epoch date
2453 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2454
2455 // Untagged integer 0
2456 0x08,
2457 0x00,
2458
2459 // Utagged date string with string label y
2460 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002461 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002462
2463 // Untagged -1000 with label z
2464 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002465 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002466 0x39, 0x03, 0xe7,
2467
Laurence Lundbladec7114722020-08-13 05:11:40 -07002468 0x07,
2469 0xc1, // tag for epoch date
2470 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2471
Laurence Lundblade4b270642020-08-14 12:53:07 -07002472 0x05,
2473 0xc1,
2474 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2475
Laurence Lundbladec7114722020-08-13 05:11:40 -07002476 // Untagged single-precision float with value 3.14 with string label x
2477 0x61, 0x78,
2478 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2479
Laurence Lundbladec7114722020-08-13 05:11:40 -07002480 // Untagged half-precision float with value -2
2481 0x09,
2482 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002483
2484 0xff,
2485};
2486
2487int32_t SpiffyDateDecodeTest()
2488{
2489 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002490 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002491 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2492 nEpochDate4, nEpochDate6, nEpochDateFail,
2493 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002494 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002495 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002496
2497 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002498 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002499 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002500 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002501
Laurence Lundblade9b334962020-08-27 10:55:53 -07002502 // Too-negative float, -9.2233720368547748E+18
2503 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002504 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002505#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002506 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2507 return 1111;
2508 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002509#else
2510 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2511 return 1112;
2512 }
2513#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002514
2515 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002516 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002517 uError = QCBORDecode_GetAndResetError(&DC);
2518 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002519 return 1;
2520 }
2521
Laurence Lundblade4b270642020-08-14 12:53:07 -07002522 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002523 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002524 uError = QCBORDecode_GetAndResetError(&DC);
2525#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2526#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2527 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2528#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2529 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2530#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2531#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2532 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2533#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2534 if(uError != uExpectedforHalfMinusInfinity) {
2535 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002536 }
2537
Laurence Lundblade4b270642020-08-14 12:53:07 -07002538 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002539 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002540 uError = QCBORDecode_GetAndResetError(&DC);
2541 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2542 return 3;
2543 }
2544
2545 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002546 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002547 uError = QCBORDecode_GetAndResetError(&DC);
2548 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2549 return 4;
2550 }
2551
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002552 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002553
2554 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002555 QCBORDecode_GetEpochDateInMapN(&DC,
2556 5,
2557 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2558 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2559 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002560#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2561 if(nEpochDate2 != -9223372036854773760LL) {
2562 return 101;
2563 }
2564#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2565 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002566 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002567 return 102;
2568 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002569#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002570
Laurence Lundblade4b270642020-08-14 12:53:07 -07002571 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002572 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2573 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002574#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2575 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002576 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002577 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002578#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2579 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002580 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002581 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002582 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002583#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2584
2585 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002586 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2587 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002588#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2589 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002590 return 103;
2591 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002592#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2593 uError = QCBORDecode_GetAndResetError(&DC);
2594 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2595 return 104;
2596 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002597#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2598
Laurence Lundblade9b334962020-08-27 10:55:53 -07002599 // A half-precision date with value -2 FFF
2600 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2601 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002602#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2603 if(nEpochDate4 != -2) {
2604 return 105;
2605 }
2606#else
2607 uError = QCBORDecode_GetAndResetError(&DC);
2608 if(uError == QCBOR_SUCCESS) {
2609 return 106;
2610 }
2611#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002612
Laurence Lundblade4b270642020-08-14 12:53:07 -07002613
2614 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002615 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2616 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2617 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002618 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002619 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002620 return 107;
2621 }
2622
2623 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002624 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2625 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002626 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002627 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002628 return 108;
2629 }
2630
2631 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002632 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2633 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2634 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002635 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002636 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002637 return 109;
2638 }
2639
2640 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002641 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2642 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002643 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002644 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002645 return 110;
2646 }
2647
2648 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002649
Laurence Lundblade4b270642020-08-14 12:53:07 -07002650 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002651 QCBORDecode_GetEpochDateInMapN(&DC,
2652 1,
2653 QCBOR_TAG_REQUIREMENT_TAG |
2654 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2655 &nEpochDate1400000000);
2656 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002657 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002658 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2659 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002660 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002661 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2662 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002663 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002664 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2665 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002666 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002667 QCBORDecode_GetEpochDateInMapSZ(&DC,
2668 "z",
2669 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2670 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2671 &nEpochDate6);
2672 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002673
2674 QCBORDecode_ExitMap(&DC);
2675 QCBORDecode_ExitArray(&DC);
2676 uError = QCBORDecode_Finish(&DC);
2677 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002678 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002679 }
2680
Laurence Lundblade9b334962020-08-27 10:55:53 -07002681 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002682 return 200;
2683 }
2684
Laurence Lundblade9b334962020-08-27 10:55:53 -07002685 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002686 return 201;
2687 }
2688
Laurence Lundblade9b334962020-08-27 10:55:53 -07002689 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002690 return 202;
2691 }
2692
Laurence Lundblade9b334962020-08-27 10:55:53 -07002693 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002694 return 203;
2695 }
2696
Laurence Lundblade9b334962020-08-27 10:55:53 -07002697 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002698 return 204;
2699 }
2700
Laurence Lundblade9b334962020-08-27 10:55:53 -07002701 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2702 return 205;
2703 }
2704
2705 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2706 return 206;
2707 }
2708
Laurence Lundbladec7114722020-08-13 05:11:40 -07002709 return 0;
2710}
2711
2712
2713
Laurence Lundblade9b334962020-08-27 10:55:53 -07002714// Input for one of the tagging tests
2715static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002716 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002717 0x81, // Array of one
2718 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2719 0x82, // Array of two that is the faction 1/3
2720 0x01,
2721 0x03,
2722
2723 /*
2724 More than 4 tags on an item 225(226(227(228(229([])))))
2725 */
2726 0xd8, 0xe1,
2727 0xd8, 0xe2,
2728 0xd8, 0xe3,
2729 0xd8, 0xe4,
2730 0xd8, 0xe5,
2731 0x80,
2732
2733 /* tag 10489608748473423768(
2734 2442302356(
2735 21590(
2736 240(
2737 []))))
2738 */
2739 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2740 0xda, 0x91, 0x92, 0x93, 0x94,
2741 0xd9, 0x54, 0x56,
2742 0xd8, 0xf0,
2743 0x80,
2744
2745 /* tag 21590(
2746 10489608748473423768(
2747 2442302357(
2748 65534(
2749 []))))
2750 */
2751 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2752 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2753 0xda, 0x91, 0x92, 0x93, 0x95,
2754 0xd9, 0xff, 0xfe,
2755 0x80,
2756
2757 /* Make sure to blow past the limit of tags that must be mapped.
2758 works in conjuntion with entries above.
2759 269488144(269488145(269488146(269488147([]))))
2760 */
2761 0xda, 0x10, 0x10, 0x10, 0x10,
2762 0xda, 0x10, 0x10, 0x10, 0x11,
2763 0xda, 0x10, 0x10, 0x10, 0x12,
2764 0xda, 0x10, 0x10, 0x10, 0x13,
2765 0x80,
2766
2767 /* An invalid decimal fraction with an additional tag */
2768 0xd9, 0xff, 0xfa,
2769 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2770 0x00, // the integer 0; should be a byte string
2771};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002772
Laurence Lundblade59289e52019-12-30 13:44:37 -08002773/*
2774 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002775 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002776 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002777static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2778 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002779
Laurence Lundblade59289e52019-12-30 13:44:37 -08002780/*
2781DB 9192939495969798 # tag(10489608748473423768)
2782 D8 88 # tag(136)
2783 C6 # tag(6)
2784 C7 # tag(7)
2785 80 # array(0)
2786*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002787static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2788 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002789
2790/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002791 55799(55799(55799({
2792 6(7(-23)): 5859837686836516696(7({
2793 7(-20): 11({
2794 17(-18): 17(17(17("Organization"))),
2795 9(-17): 773("SSG"),
2796 -15: 16(17(6(7("Confusion")))),
2797 17(-16): 17("San Diego"),
2798 17(-14): 17("US")
2799 }),
2800 23(-19): 19({
2801 -11: 9({
2802 -9: -7
2803 }),
2804 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2805 })
2806 })),
2807 16(-22): 23({
2808 11(8(7(-5))): 8(-3)
2809 })
2810 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002811 */
2812static uint8_t spCSRWithTags[] = {
2813 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2814 0xc6, 0xc7, 0x36,
2815 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2816 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2817 0xcb, 0xa5,
2818 0xd1, 0x31,
2819 0xd1, 0xd1, 0xd1, 0x6c,
2820 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2821 0xc9, 0x30,
2822 0xd9, 0x03, 0x05, 0x63,
2823 0x53, 0x53, 0x47,
2824 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002825 0xd0, 0xd1, 0xc6, 0xc7,
2826 0x69,
2827 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002828 0xd1, 0x2f,
2829 0xd1, 0x69,
2830 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2831 0xd1, 0x2d,
2832 0xd1, 0x62,
2833 0x55, 0x53,
2834 0xd7, 0x32,
2835 0xd3, 0xa2,
2836 0x2a,
2837 0xc9, 0xa1,
2838 0x28,
2839 0x26,
2840 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2841 0xcc, 0x4a,
2842 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2843 0xd0, 0x35,
2844 0xd7, 0xa1,
2845 0xcb, 0xc8, 0xc7, 0x24,
2846 0xc8, 0x22};
2847
Laurence Lundblade9b334962020-08-27 10:55:53 -07002848
2849static uint8_t spSpiffyTagInput[] = {
2850 0x9f, // Open indefinite array
2851
2852 0xc0, // tag for string date
2853 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2854
2855 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2856
2857 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2858
2859 0xd8, 0x23, // tag for regex
2860 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2861
2862 0xc0, // tag for string date
2863 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2864
2865 0xff
2866};
2867
2868
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002869static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002870
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002871
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002872int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002873{
2874 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002875 QCBORItem Item;
2876 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002877
Laurence Lundbladeee851742020-01-08 08:37:05 -08002878 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002879 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002880 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002881
Laurence Lundblade9b334962020-08-27 10:55:53 -07002882 /*
2883 This test matches the magic number tag and the fraction tag
2884 55799([...])
2885 */
2886 uError = QCBORDecode_GetNext(&DCtx, &Item);
2887 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002888 return -2;
2889 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002890 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002891 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2892 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002893 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002894
Laurence Lundblade9b334962020-08-27 10:55:53 -07002895 /*
2896 4([1,3])
2897 */
2898 uError = QCBORDecode_GetNext(&DCtx, &Item);
2899#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2900 if(uError != QCBOR_SUCCESS ||
2901 Item.uDataType != QCBOR_TYPE_ARRAY ||
2902 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2903 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2904 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2905 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2906 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2907 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2908 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002909 return -4;
2910 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002911 // consume the items in the array
2912 uError = QCBORDecode_GetNext(&DCtx, &Item);
2913 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002914
Laurence Lundblade59289e52019-12-30 13:44:37 -08002915#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002916 if(uError != QCBOR_SUCCESS ||
2917 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2918 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2919 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2920 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2921 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2922 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2923 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002924 }
2925#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002926
Laurence Lundblade9b334962020-08-27 10:55:53 -07002927 /*
2928 More than 4 tags on an item 225(226(227(228(229([])))))
2929 */
2930 uError = QCBORDecode_GetNext(&DCtx, &Item);
2931 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002932 return -6;
2933 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002934
Laurence Lundblade88e9db22020-11-02 03:56:33 -08002935 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
2936 return -106;
2937 }
2938
2939
Laurence Lundblade9b334962020-08-27 10:55:53 -07002940 /* tag 10489608748473423768(
2941 2442302356(
2942 21590(
2943 240(
2944 []))))
2945 */
2946 uError = QCBORDecode_GetNext(&DCtx, &Item);
2947 if(uError != QCBOR_SUCCESS ||
2948 Item.uDataType != QCBOR_TYPE_ARRAY ||
2949 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2950 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2951 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2952 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002953 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002954 }
2955
2956 /* tag 21590(
2957 10489608748473423768(
2958 2442302357(
2959 21591(
2960 []))))
2961 */
2962 uError = QCBORDecode_GetNext(&DCtx, &Item);
2963 if(uError != QCBOR_SUCCESS ||
2964 Item.uDataType != QCBOR_TYPE_ARRAY ||
2965 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2966 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2967 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2968 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2969 return -8;
2970 }
2971
2972 /* Make sure to blow past the limit of tags that must be mapped.
2973 works in conjuntion with entries above.
2974 269488144(269488145(269488146(269488147([]))))
2975 */
2976 uError = QCBORDecode_GetNext(&DCtx, &Item);
2977 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2978 return -9;
2979 }
2980
2981 uError = QCBORDecode_GetNext(&DCtx, &Item);
2982 if(uError == QCBOR_SUCCESS) {
2983 return -10;
2984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002985
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002986 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002987 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002988 // tage and then matches it. Caller-config lists are no longer
2989 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002990 QCBORDecode_Init(&DCtx,
2991 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2992 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002993 const uint64_t puList[] = {0x9192939495969798, 257};
2994 const QCBORTagListIn TL = {2, puList};
2995 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002996
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002997 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2998 return -8;
2999 }
3000 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3001 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3002 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3003 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3004 Item.val.uCount != 0) {
3005 return -9;
3006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003007
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003008 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003009 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003010 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003011 const uint64_t puLongList[17] = {1,2,1};
3012 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003013 QCBORDecode_Init(&DCtx,
3014 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3015 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003016 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3017 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3018 return -11;
3019 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003020
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003021 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003022 QCBORTagListOut Out = {0, 4, puTags};
3023
3024
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003025 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003026 QCBORDecode_Init(&DCtx,
3027 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3028 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003029 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3030 return -12;
3031 }
3032 if(puTags[0] != 0x9192939495969798 ||
3033 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003034 puTags[2] != 0x06 ||
3035 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003036 return -13;
3037 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003038
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003039 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003040 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003041 QCBORDecode_Init(&DCtx,
3042 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3043 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003044 QCBORTagListOut OutSmall = {0, 3, puTags};
3045 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3046 return -14;
3047 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003048
Laurence Lundblade9b334962020-08-27 10:55:53 -07003049
3050
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003051 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003052 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3053 // It is a bit of a messy test and maybe could be improved, but
3054 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003055 QCBORDecode_Init(&DCtx,
3056 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3057 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003058 int n = CheckCSRMaps(&DCtx);
3059 if(n) {
3060 return n-2000;
3061 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003062
Laurence Lundblade59289e52019-12-30 13:44:37 -08003063 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003064 QCBORDecode_Init(&DCtx,
3065 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3066 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003067
Laurence Lundblade9b334962020-08-27 10:55:53 -07003068 /* With the spiffy decode revision, this tag list is not used.
3069 It doesn't matter if a tag is in this list or not so some
3070 tests that couldn't process a tag because it isn't in this list
3071 now can process these unlisted tags. The tests have been
3072 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003073 const uint64_t puTagList[] = {773, 1, 90599561};
3074 const QCBORTagListIn TagList = {3, puTagList};
3075 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003076
3077
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003078 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3079 return -100;
3080 }
3081 if(Item.uDataType != QCBOR_TYPE_MAP ||
3082 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3083 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3084 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3085 Item.val.uCount != 2 ||
3086 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3087 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3088 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3089 Out.uNumUsed != 3) {
3090 return -101;
3091 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003092
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003093 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3094 return -102;
3095 }
3096 if(Item.uDataType != QCBOR_TYPE_MAP ||
3097 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3098 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003099 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003100 Item.val.uCount != 2 ||
3101 puTags[0] != 5859837686836516696 ||
3102 puTags[1] != 7 ||
3103 Out.uNumUsed != 2) {
3104 return -103;
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 -104;
3109 }
3110 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003111 Item.val.uCount != 5 ||
3112 puTags[0] != 0x0b ||
3113 Out.uNumUsed != 1) {
3114 return -105;
3115 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003116
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003117 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3118 return -106;
3119 }
3120 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3121 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3122 Item.val.string.len != 12 ||
3123 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3124 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3125 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3126 Out.uNumUsed != 3) {
3127 return -105;
3128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003129
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003130 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3131 return -107;
3132 }
3133 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3134 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3135 Item.val.string.len != 3 ||
3136 puTags[0] != 773 ||
3137 Out.uNumUsed != 1) {
3138 return -108;
3139 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003140
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003141 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3142 return -109;
3143 }
3144 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003145 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003146 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003147 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003148 puTags[3] != 7 ||
3149 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003150 return -110;
3151 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003152
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003153 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3154 return -111;
3155 }
3156 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3157 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3158 Item.val.string.len != 9 ||
3159 puTags[0] != 17 ||
3160 Out.uNumUsed != 1) {
3161 return -112;
3162 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003163
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003164 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3165 return -111;
3166 }
3167 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3168 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3169 Item.val.string.len != 2 ||
3170 puTags[0] != 17 ||
3171 Out.uNumUsed != 1) {
3172 return -112;
3173 }
3174
3175 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3176 return -113;
3177 }
3178 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003179 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003180 Item.val.uCount != 2 ||
3181 puTags[0] != 19 ||
3182 Out.uNumUsed != 1) {
3183 return -114;
3184 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003185
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003186 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3187 return -115;
3188 }
3189 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003190 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003191 Item.val.uCount != 1 ||
3192 puTags[0] != 9 ||
3193 Out.uNumUsed != 1) {
3194 return -116;
3195 }
3196
3197 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3198 return -116;
3199 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003200 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003201 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003202 Out.uNumUsed != 0) {
3203 return -117;
3204 }
3205
3206 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3207 return -118;
3208 }
3209 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3210 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003211 puTags[0] != 12 ||
3212 Out.uNumUsed != 1) {
3213 return -119;
3214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003215
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003216 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3217 return -120;
3218 }
3219 if(Item.uDataType != QCBOR_TYPE_MAP ||
3220 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3221 Item.val.uCount != 1 ||
3222 puTags[0] != 0x17 ||
3223 Out.uNumUsed != 1) {
3224 return -121;
3225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003226
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003227 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3228 return -122;
3229 }
3230 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003231 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003232 Item.val.int64 != -3 ||
3233 puTags[0] != 8 ||
3234 Out.uNumUsed != 1) {
3235 return -123;
3236 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003237
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003238 if(QCBORDecode_Finish(&DCtx)) {
3239 return -124;
3240 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003241
3242 UsefulBufC DateString;
3243 QCBORDecode_Init(&DCtx,
3244 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3245 QCBOR_DECODE_MODE_NORMAL);
3246
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003247 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003248 // tagged date string
3249 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3250 // untagged date string
3251 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3252 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3253 return 100;
3254 }
3255 // untagged byte string
3256 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3257 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3258 return 101;
3259 }
3260 // tagged regex
3261 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3262 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3263 return 102;
3264 }
3265 // tagged date string with a byte string
3266 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3267 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3268 return 103;
3269 }
3270 QCBORDecode_ExitArray(&DCtx);
3271 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3272 return 104;
3273 }
3274
3275
3276 QCBORDecode_Init(&DCtx,
3277 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3278 QCBOR_DECODE_MODE_NORMAL);
3279
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003280 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003281 // tagged date string
3282 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3283 // untagged date string
3284 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3285 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3286 return 200;
3287 }
3288 // untagged byte string
3289 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3290 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3291 return 201;
3292 }
3293 // tagged regex
3294 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3295 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3296 return 202;
3297 }
3298 // tagged date string with a byte string
3299 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3300 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3301 return 203;
3302 }
3303 QCBORDecode_ExitArray(&DCtx);
3304 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3305 return 204;
3306 }
3307
3308 QCBORDecode_Init(&DCtx,
3309 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3310 QCBOR_DECODE_MODE_NORMAL);
3311
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003312 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003313 // tagged date string
3314 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3315 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3316 return 300;
3317 }
3318 // untagged date string
3319 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3320 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3321 return 301;
3322 }
3323 // untagged byte string
3324 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3325 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3326 return 302;
3327 }
3328 // tagged regex
3329 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3330 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3331 return 303;
3332 }
3333 // tagged date string with a byte string
3334 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3335 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3336 return 304;
3337 }
3338 QCBORDecode_ExitArray(&DCtx);
3339 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3340 return 305;
3341 }
3342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003343 return 0;
3344}
3345
3346
3347
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003348
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003349static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003350 0x83,
3351 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3352 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3353 0xA4,
3354 0x63, 0x42, 0x4E, 0x2B,
3355 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3356 0x18, 0x40,
3357 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3358 0x63, 0x42, 0x4E, 0x2D,
3359 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3360 0x38, 0x3F,
3361 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3362
3363
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003364static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003365
3366
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003367int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003368{
3369 QCBORDecodeContext DCtx;
3370 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003371 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003372
Laurence Lundbladeee851742020-01-08 08:37:05 -08003373 QCBORDecode_Init(&DCtx,
3374 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3375 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003376
3377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003378 //
3379 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3380 return -1;
3381 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003382 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003383 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003384
3385 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003386 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003387 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003388 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003389 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003390 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003391 }
3392
3393 //
3394 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003395 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003396 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003397 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003398 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003400
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003401 //
3402 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003403 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003404 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003405 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003406 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003407
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003408 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003409 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003410 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3411 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003412 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003413 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003414 }
3415
3416 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003417 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003418 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3419 Item.uLabelType != QCBOR_TYPE_INT64 ||
3420 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003421 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003422 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003423 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003424
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003425 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003426 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003427 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3428 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003429 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003430 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003432
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003433 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003434 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003435 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3436 Item.uLabelType != QCBOR_TYPE_INT64 ||
3437 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003438 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003439 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003440 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003441
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003442 return 0;
3443}
3444
3445
3446
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003447static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003448 uint8_t uDataType,
3449 uint8_t uNestingLevel,
3450 uint8_t uNextNest,
3451 int64_t nLabel,
3452 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003453{
3454 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003455 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003456
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003457 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3458 if(Item.uDataType != uDataType) return -1;
3459 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003460 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3461 Item.uLabelType != QCBOR_TYPE_UINT64) {
3462 return -1;
3463 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003464 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3465 if(Item.label.int64 != nLabel) return -1;
3466 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003467 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003468 }
3469 }
3470 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303471 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003473 if(pItem) {
3474 *pItem = Item;
3475 }
3476 return 0;
3477}
3478
3479
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003480// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003481static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003482{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303483 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003484
Laurence Lundblade9b334962020-08-27 10:55:53 -07003485 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003486
Laurence Lundblade9b334962020-08-27 10:55:53 -07003487 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003488
Laurence Lundblade9b334962020-08-27 10:55:53 -07003489 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3490 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3491 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3492 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3493 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003494
Laurence Lundblade9b334962020-08-27 10:55:53 -07003495 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3496 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003497
Laurence Lundblade9b334962020-08-27 10:55:53 -07003498 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3499 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003500
Laurence Lundblade9b334962020-08-27 10:55:53 -07003501 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3502 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003503
Laurence Lundblade9b334962020-08-27 10:55:53 -07003504 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003505
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003506 return 0;
3507}
3508
3509
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003510/*
3511// cbor.me decoded output
3512{
3513 -23: {
3514 -20: {
3515 -18: "Organization",
3516 -17: "SSG",
3517 -15: "Confusion",
3518 -16: "San Diego",
3519 -14: "US"
3520 },
3521 -19: {
3522 -11: {
3523 -9: -7
3524 },
3525 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3526 }
3527 },
3528 -22: {
3529 -5: -3
3530 }
3531}
3532 */
3533
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003534
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003535static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003536 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3537 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3538 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3539 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3540 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3541 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3542 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3543 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3544 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3545
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003546int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003547{
3548 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003549
Laurence Lundbladeee851742020-01-08 08:37:05 -08003550 QCBORDecode_Init(&DCtx,
3551 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3552 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003553
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003554 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003555}
3556
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003557
3558
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003559int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003560{
3561 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003562
Laurence Lundbladeee851742020-01-08 08:37:05 -08003563 QCBORDecode_Init(&DCtx,
3564 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3565 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003566
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003567 QCBORItem Item;
3568 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003569
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003570 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3571 return -1;
3572 }
3573 if(Item.uDataType != QCBOR_TYPE_MAP) {
3574 return -2;
3575 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003576
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003577 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3578 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3579 return -3;
3580 }
3581
3582 return 0;
3583}
3584
3585
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003586// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003587static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003588 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3589 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3590 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3591 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3592 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3593 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003594 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3595 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3596 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3597 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003598
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003599int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003600{
3601 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003602
Laurence Lundbladeee851742020-01-08 08:37:05 -08003603 QCBORDecode_Init(&DCtx,
3604 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3605 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003606
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003607 return CheckCSRMaps(&DCtx);
3608}
3609
3610
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003611
Laurence Lundblade17ede402018-10-13 11:43:07 +08003612static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3613{
3614 UsefulOutBuf UOB;
3615 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003616
Laurence Lundblade17ede402018-10-13 11:43:07 +08003617 int i;
3618 for(i = 0; i < n; i++) {
3619 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3620 }
3621
3622 for(i = 0; i < n; i++) {
3623 UsefulOutBuf_AppendByte(&UOB, 0xff);
3624 }
3625 return UsefulOutBuf_OutUBuf(&UOB);
3626}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003627
3628
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003629static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003630{
3631 QCBORDecodeContext DC;
3632 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003633
Laurence Lundblade17ede402018-10-13 11:43:07 +08003634 int j;
3635 for(j = 0; j < nNestLevel; j++) {
3636 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003637 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003638 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3639 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003640 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003641 return -4;
3642 } else {
3643 return 0; // Decoding doesn't recover after an error
3644 }
3645 } else {
3646 // Should be no error
3647 if(nReturn) {
3648 return -9; // Should not have got an error
3649 }
3650 }
3651 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3652 return -7;
3653 }
3654 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003655 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003656 if(nReturn) {
3657 return -3;
3658 }
3659 return 0;
3660}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003661
3662
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003663int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003664{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303665 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003666 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003667 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003668 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003669 int nReturn = parse_indeflen_nested(Nested, i);
3670 if(nReturn) {
3671 return nReturn;
3672 }
3673 }
3674 return 0;
3675}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003676
Laurence Lundbladeee851742020-01-08 08:37:05 -08003677// [1, [2, 3]]
3678static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3679// No closing break
3680static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3681// Not enough closing breaks
3682static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3683// Too many closing breaks
3684static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3685// Unclosed indeflen inside def len
3686static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3687// confused tag
3688static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003689
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003690int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003691{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003692 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003693 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003694 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003695
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003696 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003697 QCBORDecodeContext DC;
3698 QCBORItem Item;
3699 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003700
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003701 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303702
3703 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3704 Item.uNestingLevel != 0 ||
3705 Item.uNextNestLevel != 1) {
3706 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003708
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003709 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303710 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3711 Item.uNestingLevel != 1 ||
3712 Item.uNextNestLevel != 1) {
3713 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003714 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003715
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003716 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303717 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3718 Item.uNestingLevel != 1 ||
3719 Item.uNextNestLevel != 2) {
3720 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003721 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003722
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003723 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003724 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303725 Item.uNestingLevel != 2 ||
3726 Item.uNextNestLevel != 2) {
3727 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003728 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003729
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003730 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003731 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303732 Item.uNestingLevel != 2 ||
3733 Item.uNextNestLevel != 0) {
3734 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003735 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003736
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003737 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303738 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003739 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003740
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003741 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003742 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003743
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003744 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003745
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003746 nResult = QCBORDecode_GetNext(&DC, &Item);
3747 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303748 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003749 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003750
Laurence Lundblade570fab52018-10-13 18:28:27 +08003751 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003752 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303753 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003754 }
3755
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003756
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003757 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003758 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003759
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003760 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003761
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003762 nResult = QCBORDecode_GetNext(&DC, &Item);
3763 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303764 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003765 }
3766
3767 nResult = QCBORDecode_GetNext(&DC, &Item);
3768 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303769 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003770 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003771
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003772 nResult = QCBORDecode_GetNext(&DC, &Item);
3773 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303774 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003775 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003776
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003777 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003778 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303779 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003780 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003781
3782
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003783 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003784 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003785
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003786 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003787
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003788 nResult = QCBORDecode_GetNext(&DC, &Item);
3789 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303790 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003791 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003792
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003793 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003794 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303795 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003796 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303797
Laurence Lundblade642282a2020-06-23 12:00:33 -07003798 nResult = QCBORDecode_GetNext(&DC, &Item);
3799 if(nResult != QCBOR_ERR_BAD_BREAK) {
3800 return -140;
3801 }
3802
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003803
Laurence Lundblade570fab52018-10-13 18:28:27 +08003804 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003805 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003806
Laurence Lundblade570fab52018-10-13 18:28:27 +08003807 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003808
Laurence Lundblade570fab52018-10-13 18:28:27 +08003809 nResult = QCBORDecode_GetNext(&DC, &Item);
3810 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303811 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003812 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003813
Laurence Lundblade570fab52018-10-13 18:28:27 +08003814 nResult = QCBORDecode_GetNext(&DC, &Item);
3815 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303816 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003817 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003818
Laurence Lundblade570fab52018-10-13 18:28:27 +08003819 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003820 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303821 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003822 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003823
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303824 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003825 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003826
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303827 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003828
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303829 nResult = QCBORDecode_GetNext(&DC, &Item);
3830 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303831 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303832 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003833
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303834 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303835 if(nResult != QCBOR_ERR_BAD_BREAK) {
3836 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303837 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003838
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003839 return 0;
3840}
3841
Laurence Lundblade17ede402018-10-13 11:43:07 +08003842
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08003843#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
3844
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003845static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003846 0x81, // Array of length one
3847 0x7f, // text string marked with indefinite length
3848 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3849 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3850 0xff // ending break
3851};
3852
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003853static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303854 0x81, // Array of length one
3855 0x7f, // text string marked with indefinite length
3856 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3857 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3858 0xff // ending break
3859};
3860
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003861static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303862 0x81, // Array of length one
3863 0x7f, // text string marked with indefinite length
3864 0x01, 0x02, // Not a string
3865 0xff // ending break
3866};
3867
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003868static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303869 0x81, // Array of length one
3870 0x7f, // text string marked with indefinite length
3871 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3872 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3873 // missing end of string
3874};
3875
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003876static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303877 0xa1, // Array of length one
3878 0x7f, // text string marked with indefinite length
3879 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3880 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3881 0xff, // ending break
3882 0x01 // integer being labeled.
3883};
3884
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003885/**
3886 Make an indefinite length string
3887
3888 @param Storage Storage for string, must be 144 bytes in size
3889 @return The indefinite length string
3890
3891 This makes an array with one indefinite length string that has 7 chunks
3892 from size of 1 byte up to 64 bytes.
3893 */
3894static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303895{
3896 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003897
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303898 UsefulOutBuf_Init(&UOB, Storage);
3899 UsefulOutBuf_AppendByte(&UOB, 0x81);
3900 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003901
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003902 uint8_t uStringByte = 0;
3903 // Use of type int is intentional
3904 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3905 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303906 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003907 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3908 for(int j = 0; j < uChunkSize; j++) {
3909 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3910 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303911 }
3912 }
3913 UsefulOutBuf_AppendByte(&UOB, 0xff);
3914
3915 return UsefulOutBuf_OutUBuf(&UOB);
3916}
3917
3918static int CheckBigString(UsefulBufC BigString)
3919{
3920 if(BigString.len != 255) {
3921 return 1;
3922 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003923
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303924 for(uint8_t i = 0; i < 255; i++){
3925 if(((const uint8_t *)BigString.ptr)[i] != i) {
3926 return 1;
3927 }
3928 }
3929 return 0;
3930}
3931
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303932
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003933int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303934{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303935 QCBORDecodeContext DC;
3936 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303937 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003938 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003939
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303940 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003941 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303942 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003943
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303944 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303945 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303946 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003947
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303948 if(QCBORDecode_GetNext(&DC, &Item)) {
3949 return -2;
3950 }
3951 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3952 return -3;
3953 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003954
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303955 if(QCBORDecode_GetNext(&DC, &Item)) {
3956 return -4;
3957 }
3958 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3959 return -5;
3960 }
3961 if(QCBORDecode_Finish(&DC)) {
3962 return -6;
3963 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303964
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303965 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003966 QCBORDecode_Init(&DC,
3967 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3968 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003969
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303970 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3971 return -7;
3972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003973
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303974 if(QCBORDecode_GetNext(&DC, &Item)) {
3975 return -8;
3976 }
3977 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3978 return -9;
3979 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003980
Laurence Lundblade30816f22018-11-10 13:40:22 +07003981 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303982 return -10;
3983 }
3984
3985 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003986 QCBORDecode_Init(&DC,
3987 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3988 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003989
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303990 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3991 return -11;
3992 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003993
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303994 if(QCBORDecode_GetNext(&DC, &Item)) {
3995 return -12;
3996 }
3997 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3998 return -13;
3999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004000
Laurence Lundblade30816f22018-11-10 13:40:22 +07004001 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304002 return -14;
4003 }
4004
4005 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004006 QCBORDecode_Init(&DC,
4007 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4008 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004009
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304010 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4011 return -15;
4012 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004013
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304014 if(QCBORDecode_GetNext(&DC, &Item)) {
4015 return -16;
4016 }
4017 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4018 return -17;
4019 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004020
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304021 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4022 return -18;
4023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004024
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304025 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304026 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004027
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304028 QCBORDecode_GetNext(&DC, &Item);
4029 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304030 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304031 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004032
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304033 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304034 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304035 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004036
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304037 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004038 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304039
4040 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4041 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304042 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304043 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004044
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304045 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304046 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004047 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004048
Laurence Lundbladeee851742020-01-08 08:37:05 -08004049 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4050 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004051
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304052 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304053 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304054 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004056
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304057 QCBORDecode_GetNext(&DC, &Item);
4058 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304059 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304060 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004061 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304062 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304063 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004064
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304065 // ---- big bstr -----
4066 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004067
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304068 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4069 return -25;
4070 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004071
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304072 if(QCBORDecode_GetNext(&DC, &Item)) {
4073 return -26;
4074 }
4075 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304076 return -26;
4077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004078
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304079 if(QCBORDecode_GetNext(&DC, &Item)) {
4080 return -27;
4081 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304082 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304083 return -28;
4084 }
4085 if(CheckBigString(Item.val.string)) {
4086 return -3;
4087 }
4088 if(QCBORDecode_Finish(&DC)) {
4089 return -29;
4090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004091
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304092 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004093 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004094
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304095 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4096 return -30;
4097 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004098
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304099 QCBORDecode_GetNext(&DC, &Item);
4100 if(Item.uDataType != QCBOR_TYPE_MAP) {
4101 return -31;
4102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004103
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304104 if(QCBORDecode_GetNext(&DC, &Item)){
4105 return -32;
4106 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004107 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4108 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304109 Item.uDataAlloc || !Item.uLabelAlloc ||
4110 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4111 return -33;
4112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004113
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304114 if(QCBORDecode_Finish(&DC)) {
4115 return -34;
4116 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004117
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004118 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004119}
4120
4121
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004122int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304123{
4124 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004125 QCBORError nCBORError;
4126
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004127
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304128 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004129 QCBORDecode_Init(&DC,
4130 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4131 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004133 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004134
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004135 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4136 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304137 return -1;
4138 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004139
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004140 if(CheckCSRMaps(&DC)) {
4141 return -2;
4142 }
4143
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004144 // Next parse, save pointers to a few strings, destroy original and
4145 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004146 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004147 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004148
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304149 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004150 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304151 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004152
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304153 QCBORItem Item1, Item2, Item3, Item4;
4154 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004155 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304156 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4157 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004158 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304159 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004160 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304161 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004162 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304163 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004164 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304165 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004166 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004167
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304168 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004169
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304170 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304171 Item1.uDataType != QCBOR_TYPE_INT64 ||
4172 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004173 Item1.uDataAlloc != 0 ||
4174 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004175 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004176 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004178
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304179
4180 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004181 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304182 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004183 Item2.uDataAlloc != 0 ||
4184 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304185 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004186 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004187
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304188 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004189 Item3.uDataAlloc == 0 ||
4190 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004191 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004192 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004193 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004194
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304195 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004196 Item4.uDataAlloc == 0 ||
4197 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004198 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004199 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004201
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304202 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004203 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004204 QCBORDecode_Init(&DC,
4205 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4206 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304207 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4208 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004209 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304210 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004211 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004212 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004213 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304214 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4215 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4216 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4217 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4218 }
4219 }
4220 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004221 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004222 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304223 }
4224
4225 return 0;
4226}
4227
Laurence Lundbladef6531662018-12-04 10:42:22 +09004228
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004229int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004230{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004231 // Set up the decoder with a tiny bit of CBOR to parse because
4232 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004233 QCBORDecodeContext DC;
4234 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4235 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004236
Laurence Lundbladef6531662018-12-04 10:42:22 +09004237 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004238 // Then fish into the internals of the decode context
4239 // to get the allocator function so it can be called directly.
4240 // Also figure out how much pool is available for use
4241 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004242 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004243 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4244 if(nError) {
4245 return -9;
4246 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004247 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4248 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4249 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004250
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004251 // First test -- ask for one more byte than available and see failure
4252 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004253 if(!UsefulBuf_IsNULL(Allocated)) {
4254 return -1;
4255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004256
Laurence Lundbladef6531662018-12-04 10:42:22 +09004257 // Re do the set up for the next test that will do a successful alloc,
4258 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004259 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004260 pAlloc = DC.StringAllocator.pfAllocator;
4261 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4262 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004263
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004264 // Allocate one byte less than available and see success
4265 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004266 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4267 return -2;
4268 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004269 // Ask for some more and see failure
4270 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004271 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4272 return -3;
4273 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004274 // Free the first allocate, retry the second and see success
4275 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4276 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004277 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4278 return -4;
4279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004280
Laurence Lundbladef6531662018-12-04 10:42:22 +09004281 // Re do set up for next test that involves a successful alloc,
4282 // and a successful realloc and a failed realloc
4283 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004284 pAlloc = DC.StringAllocator.pfAllocator;
4285 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004286
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004287 // Allocate half the pool and see success
4288 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004289 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4290 return -5;
4291 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004292 // Reallocate to take up the whole pool and see success
4293 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004294 if(UsefulBuf_IsNULL(Allocated2)) {
4295 return -6;
4296 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004297 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004298 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4299 return -7;
4300 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004301 // Try to allocate more to be sure there is failure after a realloc
4302 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4303 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004304 return -8;
4305 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004306
Laurence Lundbladef6531662018-12-04 10:42:22 +09004307 return 0;
4308}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004309
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004310
4311/* Just enough of an allocator to test configuration of one */
4312static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4313{
4314 (void)pOldMem; // unused variable
4315
4316 if(uNewSize) {
4317 // Assumes the context pointer is the buffer and
4318 // nothing too big will ever be asked for.
4319 // This is only good for this basic test!
4320 return (UsefulBuf) {pCtx, uNewSize};
4321 } else {
4322 return NULLUsefulBuf;
4323 }
4324}
4325
4326
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004327int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004328{
4329 // Set up the decoder with a tiny bit of CBOR to parse because
4330 // nothing can be done with it unless that is set up.
4331 QCBORDecodeContext DC;
4332 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4333 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4334
4335 uint8_t pAllocatorBuffer[50];
4336
4337 // This is really just to test that this call works.
4338 // The full functionality of string allocators is tested
4339 // elsewhere with the MemPool internal allocator.
4340 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4341
4342 QCBORItem Item;
4343 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4344 return -1;
4345 }
4346
4347 if(Item.uDataAlloc == 0 ||
4348 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4349 Item.val.string.ptr != pAllocatorBuffer) {
4350 return -2;
4351 }
4352
4353 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4354 return -3;
4355 }
4356
4357 return 0;
4358}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004359#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4360
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004361
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004362#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004363
Laurence Lundbladea826c502020-05-10 21:07:00 -07004364/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004365 [
4366 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004367 4([-20, 4759477275222530853136]),
4368 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004369 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004370 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004371 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004372 5([ 9223372036854775806, -4759477275222530853137])
4373 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004374 ]
4375 */
4376
4377static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004378 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004379 0xC4, 0x82, 0x20,
4380 0x03,
4381 0xC4, 0x82, 0x33,
4382 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4383 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4384 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4385 0xC5, 0x82, 0x19, 0x01, 0x2C,
4386 0x18, 0x64,
4387 0xC5, 0x82, 0x33,
4388 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4389 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4390 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004391 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4392 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004393 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4394 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4395};
4396
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004397
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004398int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004399{
4400 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004401 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004402 QCBORItem item;
4403
Laurence Lundblade17af4902020-01-07 19:11:55 -08004404 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4405 0x06, 0x07, 0x08, 0x09, 0x010};
4406 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004407
4408
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004409 QCBORDecode_Init(&DC,
4410 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4411 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004412
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004413 uErr = QCBORDecode_GetNext(&DC, &item);
4414 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004415 return 1;
4416 }
4417
4418 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4419 return 2;
4420 }
4421
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004422 uErr = QCBORDecode_GetNext(&DC, &item);
4423 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004424 return 3;
4425 }
4426
4427 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4428 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4429 item.val.expAndMantissa.nExponent != -1) {
4430 return 4;
4431 }
4432
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004433 uErr = QCBORDecode_GetNext(&DC, &item);
4434 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004435 return 5;
4436 }
4437
4438 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4439 item.val.expAndMantissa.nExponent != -20 ||
4440 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4441 return 6;
4442 }
4443
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004444 uErr = QCBORDecode_GetNext(&DC, &item);
4445 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004446 return 7;
4447 }
4448
4449 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4450 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4451 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4452 return 8;
4453 }
4454
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004455 uErr = QCBORDecode_GetNext(&DC, &item);
4456 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004457 return 9;
4458 }
4459
4460 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4461 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4462 item.val.expAndMantissa.nExponent != 300) {
4463 return 10;
4464 }
4465
Laurence Lundbladea826c502020-05-10 21:07:00 -07004466 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004467 uErr = QCBORDecode_GetNext(&DC, &item);
4468 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004469 return 11;
4470 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004471 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4472 item.val.expAndMantissa.nExponent != -20 ||
4473 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4474 return 12;
4475 }
4476
Laurence Lundbladea826c502020-05-10 21:07:00 -07004477 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004478 uErr = QCBORDecode_GetNext(&DC, &item);
4479 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004480 return 13;
4481 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004482 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4483 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4484 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4485 return 14;
4486 }
4487
Laurence Lundbladea826c502020-05-10 21:07:00 -07004488 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004489 uErr = QCBORDecode_GetNext(&DC, &item);
4490 if(uErr != QCBOR_SUCCESS) {
4491 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004492 }
4493 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4494 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4495 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004496 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004497 }
4498
Laurence Lundbladea826c502020-05-10 21:07:00 -07004499 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004500 uErr = QCBORDecode_GetNext(&DC, &item);
4501 if(uErr != QCBOR_SUCCESS) {
4502 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004503 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004504 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4505 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4506 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004507 return 18;
4508 }
4509
4510 uErr = QCBORDecode_Finish(&DC);
4511 if(uErr != QCBOR_SUCCESS) {
4512 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004513 }
4514
4515 /* Now encode some stuff and then decode it */
4516 uint8_t pBuf[40];
4517 QCBOREncodeContext EC;
4518 UsefulBufC Encoded;
4519
4520 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4521 QCBOREncode_OpenArray(&EC);
4522 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4523 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4524 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4525 QCBOREncode_CloseArray(&EC);
4526 QCBOREncode_Finish(&EC, &Encoded);
4527
4528
4529 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004530 uErr = QCBORDecode_GetNext(&DC, &item);
4531 if(uErr != QCBOR_SUCCESS) {
4532 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004533 }
4534
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004535 uErr = QCBORDecode_GetNext(&DC, &item);
4536 if(uErr != QCBOR_SUCCESS) {
4537 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004538 }
4539
4540 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4541 item.val.expAndMantissa.nExponent != 1000 ||
4542 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004543 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004544 }
4545
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004546 uErr = QCBORDecode_GetNext(&DC, &item);
4547 if(uErr != QCBOR_SUCCESS) {
4548 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004549 }
4550
4551 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4552 item.val.expAndMantissa.nExponent != INT32_MIN ||
4553 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004554 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004555 }
4556
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004557 uErr = QCBORDecode_GetNext(&DC, &item);
4558 if(uErr != QCBOR_SUCCESS) {
4559 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004560 }
4561
4562 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4563 item.val.expAndMantissa.nExponent != INT32_MAX ||
4564 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004565 return 106;
4566 }
4567
4568
4569 int64_t nExp, nMant;
4570 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4571 UsefulBufC Mant;
4572 bool bIsNeg;
4573
4574 QCBORDecode_Init(&DC,
4575 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4576 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004577 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004578
4579 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004580 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004581
4582 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004583 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4584 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004585
4586 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004587 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4588 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004589
4590 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004591 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004592
4593 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004594 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4595 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004596
4597 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004598 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4599 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004600
4601 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004602 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4603 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004604
4605 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004606 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4607 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004608
4609 QCBORDecode_ExitArray(&DC);
4610
4611 uErr = QCBORDecode_Finish(&DC);
4612 if(uErr != QCBOR_SUCCESS) {
4613 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004614 }
4615
4616 return 0;
4617}
4618
4619
4620static struct FailInput ExponentAndMantissaFailures[] = {
4621 // Exponent > INT64_MAX
4622 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4623 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4624 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4625 // Mantissa > INT64_MAX
4626 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4627 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4628 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4629 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004630 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004631 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004632 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004633 // bad content for big num
4634 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4635 // bad content for big num
4636 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4637 // Bad integer for exponent
4638 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4639 // Bad integer for mantissa
4640 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4641 // 3 items in array
4642 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4643 // unterminated indefinite length array
4644 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4645 // Empty array
4646 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4647 // Second is not an integer
4648 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4649 // First is not an integer
4650 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4651 // Not an array
4652 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4653};
4654
4655
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004656int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004657{
4658 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004659 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4660 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004661}
4662
4663#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004664
4665
4666
4667/*
4668 Some basic CBOR with map and array used in a lot of tests.
4669 The map labels are all strings
4670
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004671 {
4672 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004673 "an array of two strings": [
4674 "string1", "string2"
4675 ],
4676 "map in a map": {
4677 "bytes 1": h'78787878',
4678 "bytes 2": h'79797979',
4679 "another int": 98,
4680 "text 2": "lies, damn lies and statistics"
4681 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004682 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004683 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004684
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004685int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004686{
4687 QCBORItem Item1, Item2, Item3;
4688 int64_t nDecodedInt1, nDecodedInt2;
4689 UsefulBufC B1, B2, S1, S2, S3;
4690
4691 QCBORDecodeContext DCtx;
4692 QCBORError nCBORError;
4693
4694 QCBORDecode_Init(&DCtx, input, 0);
4695
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004696 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004697
4698 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4699
4700 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4701 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004702 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4703 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4704 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004705 QCBORDecode_ExitMap(&DCtx);
4706
4707 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4708 QCBORDecode_GetNext(&DCtx, &Item1);
4709 QCBORDecode_GetNext(&DCtx, &Item2);
4710 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4711 return -400;
4712 }
4713 QCBORDecode_ExitArray(&DCtx);
4714
4715 // Parse the same array again using GetText() instead of GetItem()
4716 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004717 QCBORDecode_GetTextString(&DCtx, &S2);
4718 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004719 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4720 return 5000;
4721 }
4722 /* QCBORDecode_GetText(&DCtx, &S3);
4723 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4724 return 5001;
4725 } */
4726
4727 QCBORDecode_ExitArray(&DCtx);
4728
4729 QCBORDecode_ExitMap(&DCtx);
4730
4731 nCBORError = QCBORDecode_Finish(&DCtx);
4732
4733 if(nCBORError) {
4734 return (int32_t)nCBORError;
4735 }
4736
4737 if(nDecodedInt1 != 42) {
4738 return 1001;
4739 }
4740
4741 if(nDecodedInt2 != 98) {
4742 return 1002;
4743 }
4744
4745 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004746 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004747 return 1003;
4748 }
4749
4750 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004751 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004752 return 1004;
4753 }
4754
Laurence Lundblade9b334962020-08-27 10:55:53 -07004755 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004756 return 1005;
4757 }
4758
4759 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4760 return 1006;
4761 }
4762
4763 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4764 return 1007;
4765 }
4766
4767 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4768 return 1008;
4769 }
4770
4771 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4772 return 1009;
4773 }
4774
4775 return 0;
4776}
4777
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004778/*
4779 {
4780 -75008: h'05083399',
4781 88: [
4782 ],
4783 100100: {
4784 "sub1": {
4785 10: [
4786 0
4787 ],
4788 -75009: h'A46823990001',
4789 100100: {
4790 "json": "{ \"ueid\", \"xyz\"}",
4791 "subsub": {
4792 100002: h'141813191001'
4793 }
4794 }
4795 }
4796 }
4797 }
4798 */
4799
4800static const uint8_t spNestedCBOR[] = {
48010xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05, 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
48020x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62, 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
48030x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
48040x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22, 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
48050x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75, 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
48060x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10, 0x01
4807};
4808
4809/* Get item in multi-level nesting in spNestedCBOR */
4810static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4811{
4812 UsefulBufC String;
4813
4814 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4815 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4816
4817 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4818 QCBORDecode_EnterMap(pDCtx, NULL);
4819 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4820 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4821 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
4822 if(QCBORDecode_GetError(pDCtx)) {
4823 return 4001;
4824 }
4825 if(UsefulBuf_Compare(String, test_oemid)) {
4826 return 4002;
4827 }
4828 QCBORDecode_ExitMap(pDCtx);
4829 QCBORDecode_ExitMap(pDCtx);
4830 QCBORDecode_ExitMap(pDCtx);
4831 QCBORDecode_ExitMap(pDCtx);
4832
4833 return 0;
4834}
4835
4836/* Iterations on the zero-length array in spNestedCBOR */
4837static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
4838{
4839 QCBORItem Item;
4840 QCBORError uErr;
4841
4842 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
4843 for(int x = 0; x < 20; x++) {
4844 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4845 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4846 return 4100;
4847
4848 }
4849 }
4850 QCBORDecode_ExitArray(pDCtx);
4851 if(QCBORDecode_GetError(pDCtx)) {
4852 return 4101;
4853 }
4854
4855 return 0;
4856}
4857
4858/* Various iterations on the array that contains a zero in spNestedCBOR */
4859static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
4860{
4861 QCBORError uErr;
4862
4863 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4864 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
4865 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4866 int64_t nInt = 99;
4867 QCBORDecode_GetInt64(pDCtx, &nInt);
4868 if(nInt != 0) {
4869 return 4200;
4870 }
4871 for(int x = 0; x < 20; x++) {
4872 QCBORItem Item;
4873 uErr = QCBORDecode_GetNext(pDCtx, &Item);
4874 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4875 return 4201;
4876
4877 }
4878 }
4879 QCBORDecode_ExitArray(pDCtx);
4880 if(QCBORDecode_GetAndResetError(pDCtx)) {
4881 return 4202;
4882 }
4883 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
4884 UsefulBufC dD;
4885 QCBORDecode_GetByteString(pDCtx, &dD);
4886 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4887 return 4203;
4888 }
4889 for(int x = 0; x < 20; x++) {
4890 QCBORDecode_GetByteString(pDCtx, &dD);
4891 uErr = QCBORDecode_GetAndResetError(pDCtx);
4892 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
4893 return 4204;
4894 }
4895 }
4896 QCBORDecode_ExitArray(pDCtx);
4897 QCBORDecode_ExitMap(pDCtx);
4898 QCBORDecode_ExitMap(pDCtx);
4899
4900 return 0;
4901}
4902
4903/* Repeatedly enter and exit maps and arrays, go off the end of maps
4904 and arrays and such. */
4905static int32_t DecodeNestedIterate()
4906{
4907 QCBORDecodeContext DCtx;
4908 int32_t nReturn;
4909 QCBORError uErr;
4910
4911 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
4912 QCBORDecode_EnterMap(&DCtx, NULL);
4913
4914 for(int j = 0; j < 5; j++) {
4915 for(int i = 0; i < 20; i++) {
4916 nReturn = DecodeNestedGetSubSub(&DCtx);
4917 if(nReturn) {
4918 return nReturn;
4919 }
4920 }
4921
4922 for(int i = 0; i < 20; i++) {
4923 nReturn = DecodeNestedGetEmpty(&DCtx);
4924 if(nReturn ) {
4925 return nReturn;
4926 }
4927 }
4928
4929 for(int i = 0; i < 20; i++) {
4930 nReturn = DecodeNestedGetZero(&DCtx);
4931 if(nReturn ) {
4932 return nReturn;
4933 }
4934 }
4935 }
4936
4937 QCBORDecode_ExitMap(&DCtx);
4938 uErr = QCBORDecode_Finish(&DCtx);
4939 if(uErr) {
4940 return (int32_t)uErr + 4100;
4941 }
4942
4943 return 0;
4944}
4945
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004946
4947/*
4948 [23,
4949 6000,
4950 h'67616C6163746963',
4951 h'686176656E20746F6B656E'
4952 ]
4953 */
4954static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004955 0x84,
4956 0x17,
4957 0x19, 0x17, 0x70,
4958 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4959 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004960
4961
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004962static const uint8_t spEmptyMap[] = {0xa0};
4963
4964static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004965
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004966static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4967
Laurence Lundbladef0499502020-08-01 11:55:57 -07004968/*
4969 {
4970 0: [],
4971 9: [
4972 [],
4973 []
4974 ],
4975 8: {
4976 1: [],
4977 2: {},
4978 3: []
4979 },
4980 4: {},
4981 5: [],
4982 6: [
4983 [],
4984 []
4985 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004986 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004987 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004988
Laurence Lundbladef0499502020-08-01 11:55:57 -07004989static const uint8_t spMapOfEmpty[] = {
4990 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4991 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4992 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004993
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004994/*
4995 Too many tags
4996 Invalid tag content
4997 Duplicate label
4998 Integer overflow
4999 Date overflow
5000
5001 {1: 224(225(226(227(4(0))))),
5002 2: 1(h''),
5003 3: -18446744073709551616,
5004 4: 1(1.0e+300),
5005 5: 0, 8: 8}
5006 */
5007static const uint8_t spRecoverableMapErrors[] = {
5008 0xbf,
5009 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5010 0x02, 0xc1, 0x40,
5011 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5012 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5013 0x05, 0x00,
5014 0x05, 0x00,
5015 0x08, 0x08,
5016 0xff
5017};
5018
5019// Bad break
5020static const uint8_t spUnRecoverableMapError1[] = {
5021 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5022};
5023
5024// No more items
5025static const uint8_t spUnRecoverableMapError2[] = {
5026 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5027};
5028
5029// Hit end because string is too long
5030static const uint8_t spUnRecoverableMapError3[] = {
5031 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5032};
5033
5034// Hit end because string is too long
5035static const uint8_t spUnRecoverableMapError4[] = {
5036 0xbf,
5037 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5038 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5039 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5040 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5041 0xff
5042};
5043
5044
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005045int32_t EnterMapTest()
5046{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005047 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005048 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005049 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005050 int32_t nReturn;
5051 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005052
5053
5054 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005055 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005056
Laurence Lundbladef0499502020-08-01 11:55:57 -07005057
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005058 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005059 QCBORDecode_ExitArray(&DCtx);
5060
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005061 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5062 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005063 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005064 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005065 QCBORDecode_ExitArray(&DCtx);
5066 QCBORDecode_ExitArray(&DCtx);
5067
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005068 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5069 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005070 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005071 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005072 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005073 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005074 QCBORDecode_ExitArray(&DCtx);
5075 QCBORDecode_ExitMap(&DCtx);
5076
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005077 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005078 QCBORDecode_ExitMap(&DCtx);
5079
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005080 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005081 QCBORDecode_ExitArray(&DCtx);
5082
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005083 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5084 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005085 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005086 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005087 QCBORDecode_ExitArray(&DCtx);
5088 QCBORDecode_ExitArray(&DCtx);
5089
5090 QCBORDecode_ExitMap(&DCtx);
5091
5092 uErr = QCBORDecode_Finish(&DCtx);
5093 if(uErr != QCBOR_SUCCESS){
5094 return 3011;
5095 }
5096
5097
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005098 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005099 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005100 if(nReturn) {
5101 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005102 }
5103
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005104 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005105 if(nReturn) {
5106 return nReturn;
5107 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005108
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005109
Laurence Lundblade937ea812020-05-08 11:38:23 -07005110
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005111 // These tests confirm the cursor is at the right place after entering
5112 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005113 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005114
5115 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005116 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005117 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005118 QCBORDecode_GetNext(&DCtx, &Item1);
5119 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5120 return 2001;
5121 }
5122
5123
Laurence Lundblade9b334962020-08-27 10:55:53 -07005124 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005125 QCBORDecode_VGetNext(&DCtx, &Item1);
5126 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005127 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5128 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5129 UsefulBuf_Compare(ArrayItem.label.string,
5130 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5131 return 2051;
5132 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005133 QCBORDecode_GetNext(&DCtx, &Item1);
5134 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5135 return 2002;
5136 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005137 QCBORDecode_ExitArray(&DCtx);
5138 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5139 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5140 UsefulBuf_Compare(ArrayItem.label.string,
5141 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5142 return 2052;
5143 }
5144
Laurence Lundblade937ea812020-05-08 11:38:23 -07005145
Laurence Lundblade9b334962020-08-27 10:55:53 -07005146 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005147 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005148 QCBORDecode_GetNext(&DCtx, &Item1);
5149 QCBORDecode_GetNext(&DCtx, &Item1);
5150 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005151 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5152 QCBORDecode_GetNext(&DCtx, &Item1);
5153 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5154 return 2003;
5155 }
5156
Laurence Lundblade9b334962020-08-27 10:55:53 -07005157 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005158 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005159 QCBORDecode_GetNext(&DCtx, &Item1);
5160 QCBORDecode_GetNext(&DCtx, &Item1);
5161 QCBORDecode_GetNext(&DCtx, &Item1);
5162 QCBORDecode_GetNext(&DCtx, &Item1);
5163 QCBORDecode_GetNext(&DCtx, &Item1);
5164 QCBORDecode_GetNext(&DCtx, &Item1);
5165 QCBORDecode_GetNext(&DCtx, &Item1);
5166 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5167 QCBORDecode_GetNext(&DCtx, &Item1);
5168 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005169 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005170 }
5171
Laurence Lundblade9b334962020-08-27 10:55:53 -07005172 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005173 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005174 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5175 QCBORDecode_ExitArray(&DCtx);
5176 QCBORDecode_GetNext(&DCtx, &Item1);
5177 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5178 return 2006;
5179 }
5180 QCBORDecode_ExitMap(&DCtx);
5181 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5182 return 2007;
5183 }
5184
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005185 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005186 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005187 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005188 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5189 uErr = QCBORDecode_GetAndResetError(&DCtx);
5190 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005191 return 2008;
5192 }
5193 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005194 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005195 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005196 return 2009;
5197 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005198
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005199
5200 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005201 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005202 // This will fail because the map is empty.
5203 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5204 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005205 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005206 return 2010;
5207 }
5208 QCBORDecode_ExitMap(&DCtx);
5209 uErr = QCBORDecode_Finish(&DCtx);
5210 if(uErr != QCBOR_SUCCESS){
5211 return 2011;
5212 }
5213
5214
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005215 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005216 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005217 // This will fail because the map is empty.
5218 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5219 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005220 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005221 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005222 }
5223 QCBORDecode_ExitMap(&DCtx);
5224 uErr = QCBORDecode_Finish(&DCtx);
5225 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005226 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005227 }
5228
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005229
5230 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005231 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005232 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005233 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005234 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005235 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005236 QCBORDecode_ExitArray(&DCtx);
5237 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5238 QCBORDecode_ExitArray(&DCtx);
5239 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005240 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005241 return 2014;
5242 }
5243
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005244 int64_t nInt;
5245 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005246 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005247 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005248 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005249 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5250 return 2021;
5251 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005252 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5253 return 2121;
5254 }
5255 (void)QCBORDecode_GetAndResetError(&DCtx);
5256
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005257
5258 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5259 uErr = QCBORDecode_GetAndResetError(&DCtx);
5260 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5261 return 2022;
5262 }
5263
5264 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5265 uErr = QCBORDecode_GetAndResetError(&DCtx);
5266 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5267 return 2023;
5268 }
5269
5270 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5271 uErr = QCBORDecode_GetAndResetError(&DCtx);
5272#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5273 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5274 return 2024;
5275 }
5276#else
5277 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5278 return 2027;
5279 }
5280#endif
5281
5282 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5283 uErr = QCBORDecode_GetAndResetError(&DCtx);
5284 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5285 return 2025;
5286 }
5287
5288 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5289
5290 QCBORDecode_ExitMap(&DCtx);
5291 uErr = QCBORDecode_Finish(&DCtx);
5292 if(uErr != QCBOR_SUCCESS) {
5293 return 2026;
5294 }
5295
5296 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005297 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005298 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5299 uErr = QCBORDecode_GetAndResetError(&DCtx);
5300 if(uErr != QCBOR_ERR_BAD_BREAK) {
5301 return 2030;
5302 }
5303
5304 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005305 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005306 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5307 uErr = QCBORDecode_GetAndResetError(&DCtx);
5308 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5309 return 2031;
5310 }
5311
5312 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005313 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005314 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5315 uErr = QCBORDecode_GetAndResetError(&DCtx);
5316 if(uErr != QCBOR_ERR_HIT_END) {
5317 return 2032;
5318 }
5319
5320 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005321 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005322 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5323 uErr = QCBORDecode_GetAndResetError(&DCtx);
5324 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5325 return 2033;
5326 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005327
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005328 nReturn = DecodeNestedIterate();
5329
5330 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005331}
5332
5333
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005334struct NumberConversion {
5335 char *szDescription;
5336 UsefulBufC CBOR;
5337 int64_t nConvertedToInt64;
5338 QCBORError uErrorInt64;
5339 uint64_t uConvertToUInt64;
5340 QCBORError uErrorUint64;
5341 double dConvertToDouble;
5342 QCBORError uErrorDouble;
5343};
5344
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005345static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005346 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005347 "too large to fit into int64_t",
5348 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5349 0,
5350 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5351 0,
5352 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5353 ((double)INT64_MIN) + 1 ,
5354 QCBOR_SUCCESS
5355 },
5356 {
5357 "largest negative int that fits in int64_t",
5358 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5359 INT64_MIN,
5360 QCBOR_SUCCESS,
5361 0,
5362 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5363 (double)INT64_MIN,
5364 QCBOR_SUCCESS
5365 },
5366 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005367 "negative bignum -1",
5368 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5369 -1,
5370 QCBOR_SUCCESS,
5371 0,
5372 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5373 -1.0,
5374 QCBOR_SUCCESS
5375 },
5376 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005377 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005378 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5379 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005380#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005381 257000,
5382 QCBOR_SUCCESS,
5383 257000,
5384 QCBOR_SUCCESS,
5385 257000.0,
5386 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005387#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5388 0,
5389 QCBOR_ERR_UNEXPECTED_TYPE,
5390 0,
5391 QCBOR_ERR_UNEXPECTED_TYPE,
5392 0.0,
5393 QCBOR_ERR_UNEXPECTED_TYPE
5394#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005395 },
5396 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005397 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005398 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5399 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005400#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005401 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005402 QCBOR_SUCCESS,
5403 0,
5404 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005405 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005406 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005407#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5408 0,
5409 QCBOR_ERR_UNEXPECTED_TYPE,
5410 0,
5411 QCBOR_ERR_UNEXPECTED_TYPE,
5412 0.0,
5413 QCBOR_ERR_UNEXPECTED_TYPE
5414#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005415 },
5416 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005417 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005418 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5419 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005420#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005421 2056,
5422 QCBOR_SUCCESS,
5423 2056,
5424 QCBOR_SUCCESS,
5425 2056.0,
5426 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005427#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5428 0,
5429 QCBOR_ERR_UNEXPECTED_TYPE,
5430 0,
5431 QCBOR_ERR_UNEXPECTED_TYPE,
5432 0.0,
5433 QCBOR_ERR_UNEXPECTED_TYPE
5434#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005435 },
5436 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005437 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005438 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5439 0,
5440 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5441 0,
5442 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5443 -18446744073709551617.0,
5444 QCBOR_SUCCESS
5445 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005446#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005447 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005448 "Positive bignum 0x01020304 indefinite length string",
5449 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5450 0x01020304,
5451 QCBOR_SUCCESS,
5452 0x01020304,
5453 QCBOR_SUCCESS,
5454 16909060.0,
5455 QCBOR_SUCCESS
5456 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005457#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005458 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005459 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005460 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5461 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005462#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005463 0,
5464 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5465 0,
5466 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5467 -INFINITY,
5468 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005469#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5470 0,
5471 QCBOR_ERR_UNEXPECTED_TYPE,
5472 0,
5473 QCBOR_ERR_UNEXPECTED_TYPE,
5474 0.0,
5475 QCBOR_ERR_UNEXPECTED_TYPE
5476#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005477 },
5478 {
5479 "big float [9223372036854775806, 9223372036854775806]",
5480 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5481 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005482#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005483 0,
5484 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5485 0,
5486 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5487 INFINITY,
5488 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005489#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5490 0,
5491 QCBOR_ERR_UNEXPECTED_TYPE,
5492 0,
5493 QCBOR_ERR_UNEXPECTED_TYPE,
5494 0.0,
5495 QCBOR_ERR_UNEXPECTED_TYPE
5496#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005497 },
5498 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005499 "Big float 3 * 2^^2",
5500 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005501#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005502 12,
5503 QCBOR_SUCCESS,
5504 12,
5505 QCBOR_SUCCESS,
5506 12.0,
5507 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005508#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5509 0,
5510 QCBOR_ERR_UNEXPECTED_TYPE,
5511 0,
5512 QCBOR_ERR_UNEXPECTED_TYPE,
5513 0.0,
5514 QCBOR_ERR_UNEXPECTED_TYPE
5515#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005516 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005517 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005518 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005519 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5520 0,
5521 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5522 18446744073709551615ULL,
5523 QCBOR_SUCCESS,
5524 18446744073709551615.0,
5525 QCBOR_SUCCESS
5526 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005527 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005528 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005529 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5530 65536-1,
5531 QCBOR_SUCCESS,
5532 0xffff,
5533 QCBOR_SUCCESS,
5534 65535.0,
5535 QCBOR_SUCCESS
5536 },
5537 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005538 "Postive integer 0",
5539 {(uint8_t[]){0x0}, 1},
5540 0LL,
5541 QCBOR_SUCCESS,
5542 0ULL,
5543 QCBOR_SUCCESS,
5544 0.0,
5545 QCBOR_SUCCESS
5546 },
5547 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005548 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005549 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5550 -9223372036854775807-1, // INT64_MIN
5551 QCBOR_SUCCESS,
5552 0ULL,
5553 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5554 -9223372036854775808.0,
5555 QCBOR_SUCCESS
5556 },
5557 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005558 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005559 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005560#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005561 100L,
5562 QCBOR_SUCCESS,
5563 100ULL,
5564 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005565#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5566 0,
5567 QCBOR_ERR_HW_FLOAT_DISABLED,
5568 0,
5569 QCBOR_ERR_HW_FLOAT_DISABLED,
5570#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005571 100.3,
5572 QCBOR_SUCCESS
5573 },
5574 {
5575 "Floating point value NaN 0xfa7fc00000",
5576 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005577#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005578 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005579 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005580 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005581 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005582#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5583 0,
5584 QCBOR_ERR_HW_FLOAT_DISABLED,
5585 0,
5586 QCBOR_ERR_HW_FLOAT_DISABLED,
5587#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005588 NAN,
5589 QCBOR_SUCCESS
5590 },
5591 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005592 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005593 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005594#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5595#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5596 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005597 -4,
5598 QCBOR_SUCCESS,
5599 0,
5600 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5601 -4.0,
5602 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005603#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5604 // Float HW disabled
5605 -4,
5606 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5607 0,
5608 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5609 -4.0,
5610 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5611#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5612#else
5613 // Half-precision disabled
5614 -4,
5615 QCBOR_ERR_HALF_PRECISION_DISABLED,
5616 0,
5617 QCBOR_ERR_HALF_PRECISION_DISABLED,
5618 -4.0,
5619 QCBOR_ERR_HALF_PRECISION_DISABLED
5620#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005621 },
5622 {
5623 "Decimal fraction 3/10",
5624 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005625#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005626 0,
5627 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5628 0,
5629 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5630 0.30000000000000004,
5631 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005632#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5633 0,
5634 QCBOR_ERR_UNEXPECTED_TYPE,
5635 0,
5636 QCBOR_ERR_UNEXPECTED_TYPE,
5637 0.0,
5638 QCBOR_ERR_UNEXPECTED_TYPE
5639#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005640 },
5641 {
5642 "+inifinity",
5643 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5644#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5645 0,
5646 QCBOR_ERR_FLOAT_EXCEPTION,
5647 0,
5648 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5649#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5650 0,
5651 QCBOR_ERR_HW_FLOAT_DISABLED,
5652 0,
5653 QCBOR_ERR_HW_FLOAT_DISABLED,
5654#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5655 INFINITY,
5656 QCBOR_SUCCESS
5657 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005658
5659 {
5660 "extreme pos bignum",
5661 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5662 // 50 rows of 8 is 400 digits.
5663 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5664 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5665 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5666 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5667 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5668 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5669 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5670 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5671 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5672 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5673 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5674 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5675 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5676 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5677 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5678 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5679 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5680 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5681 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5682 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5683 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5684 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5685 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5686 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5687 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5688 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5689 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5690 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5691 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5692 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5693 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5694 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5695 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5696 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5697 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5698 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5699 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5700 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5701 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5702 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5703 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5704 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5705 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5706 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5707 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5708 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5709 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5710 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5711 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5712 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5713 404},
5714 0,
5715 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5716 0,
5717 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5718#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5719 INFINITY,
5720 QCBOR_SUCCESS
5721#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5722 0,
5723 QCBOR_ERR_HW_FLOAT_DISABLED,
5724#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5725 },
5726
5727 {
5728 "extreme neg bignum",
5729 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5730 // 50 rows of 8 is 400 digits.
5731 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5732 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5733 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5734 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5735 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5736 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5737 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5738 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5739 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5740 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5741 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5742 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5743 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5744 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5745 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5746 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5747 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5748 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5749 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5750 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5751 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5752 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5753 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5754 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5755 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5756 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5757 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5758 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5759 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5760 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5761 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5762 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5763 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5764 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5765 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5766 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5767 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5768 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5769 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5770 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5771 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5772 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5773 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5774 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5775 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5776 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5777 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5778 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5779 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5780 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5781 404},
5782 0,
5783 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5784 0,
5785 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5786#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5787 -INFINITY,
5788 QCBOR_SUCCESS
5789#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5790 0,
5791 QCBOR_ERR_HW_FLOAT_DISABLED,
5792#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5793 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005794
5795 {
5796 "big float underflow [9223372036854775806, -9223372036854775806]",
5797 {(uint8_t[]){
5798 0xC5, 0x82,
5799 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5800 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5801#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5802 0,
5803 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5804 0,
5805 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5806 0,
5807 QCBOR_SUCCESS
5808#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5809 0,
5810 QCBOR_ERR_UNEXPECTED_TYPE,
5811 0,
5812 QCBOR_ERR_UNEXPECTED_TYPE,
5813 0.0,
5814 QCBOR_ERR_UNEXPECTED_TYPE
5815#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5816 },
5817
5818 {
5819 "bigfloat that evaluates to -INFINITY",
5820 {(uint8_t[]){
5821 0xC5, 0x82,
5822 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5823 0xC3, 0x42, 0x01, 0x01}, 15},
5824#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5825 0,
5826 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5827 0,
5828 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5829 -INFINITY,
5830 QCBOR_SUCCESS
5831#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5832 0,
5833 QCBOR_ERR_UNEXPECTED_TYPE,
5834 0,
5835 QCBOR_ERR_UNEXPECTED_TYPE,
5836 0.0,
5837 QCBOR_ERR_UNEXPECTED_TYPE
5838#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5839 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005840};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005841
5842
5843
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005844
5845static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
5846{
5847 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
5848#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5849 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
5850 return 1;
5851 }
5852#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5853 (void)Pool;
5854#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5855 return 0;
5856}
5857
5858
Laurence Lundblade313b2862020-05-16 01:23:06 -07005859int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005860{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005861 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
5862 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005863
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005864 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5865 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005866
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005867 // Set up the decoding context including a memory pool so that
5868 // indefinite length items can be checked
5869 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005870 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005871
5872 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005873 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5874 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005875 }
5876
5877 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005878 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005879 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005880 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005881 }
5882 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005883 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005884 }
5885
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005886 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005887 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5888 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005889 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005890
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005891 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005892 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005893 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005894 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005895 }
5896 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005897 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005898 }
5899
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005900 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005901 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5902 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005903 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005904#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005905 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005906 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005907 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005908 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005909 }
5910 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5911 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005912 // NaN's can't be compared for equality. A NaN is
5913 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005914 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005915 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005916 }
5917 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005918 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005919 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005920 }
5921 }
5922 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005923#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005924 }
5925
5926 return 0;
5927}
5928
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005929
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005930
5931
Laurence Lundbladee3553422020-05-02 11:11:17 -07005932int32_t CBORSequenceDecodeTests(void)
5933{
5934 QCBORDecodeContext DCtx;
5935 QCBORItem Item;
5936 QCBORError uCBORError;
5937
5938 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005939
Laurence Lundbladee3553422020-05-02 11:11:17 -07005940 // The input for the date test happens to be a sequence so it
5941 // is reused. It is a sequence because it doesn't start as
5942 // an array or map.
5943 QCBORDecode_Init(&DCtx,
5944 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5945 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005946
Laurence Lundbladee3553422020-05-02 11:11:17 -07005947 // Get the first item
5948 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5949 if(uCBORError != QCBOR_SUCCESS) {
5950 return 1;
5951 }
5952 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5953 return 2;
5954 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005955
Laurence Lundbladee3553422020-05-02 11:11:17 -07005956 // Get a second item
5957 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005958 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5959 return 66;
5960 }
5961
5962 // Get a third item
5963 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005964 if(uCBORError != QCBOR_SUCCESS) {
5965 return 2;
5966 }
5967 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5968 return 3;
5969 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005970
Laurence Lundbladee3553422020-05-02 11:11:17 -07005971 // A sequence can have stuff at the end that may
5972 // or may not be valid CBOR. The protocol decoder knows
5973 // when to stop by definition of the protocol, not
5974 // when the top-level map or array is ended.
5975 // Finish still has to be called to know that
5976 // maps and arrays (if there were any) were closed
5977 // off correctly. When called like this it
5978 // must return the error QCBOR_ERR_EXTRA_BYTES.
5979 uCBORError = QCBORDecode_Finish(&DCtx);
5980 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5981 return 4;
5982 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005983
5984
Laurence Lundbladee3553422020-05-02 11:11:17 -07005985 // --- Test an empty input ----
5986 uint8_t empty[1];
5987 UsefulBufC Empty = {empty, 0};
5988 QCBORDecode_Init(&DCtx,
5989 Empty,
5990 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005991
Laurence Lundbladee3553422020-05-02 11:11:17 -07005992 uCBORError = QCBORDecode_Finish(&DCtx);
5993 if(uCBORError != QCBOR_SUCCESS) {
5994 return 5;
5995 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005996
5997
Laurence Lundbladee3553422020-05-02 11:11:17 -07005998 // --- Sequence with unclosed indefinite length array ---
5999 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006000
Laurence Lundbladee3553422020-05-02 11:11:17 -07006001 QCBORDecode_Init(&DCtx,
6002 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6003 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006004
Laurence Lundbladee3553422020-05-02 11:11:17 -07006005 // Get the first item
6006 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6007 if(uCBORError != QCBOR_SUCCESS) {
6008 return 7;
6009 }
6010 if(Item.uDataType != QCBOR_TYPE_INT64) {
6011 return 8;
6012 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006013
Laurence Lundbladee3553422020-05-02 11:11:17 -07006014 // Get a second item
6015 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6016 if(uCBORError != QCBOR_SUCCESS) {
6017 return 9;
6018 }
6019 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6020 return 10;
6021 }
6022
6023 // Try to finish before consuming all bytes to confirm
6024 // that the still-open error is returned.
6025 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006026 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006027 return 11;
6028 }
6029
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006030
Laurence Lundbladee3553422020-05-02 11:11:17 -07006031 // --- Sequence with a closed indefinite length array ---
6032 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006033
Laurence Lundbladee3553422020-05-02 11:11:17 -07006034 QCBORDecode_Init(&DCtx,
6035 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6036 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006037
Laurence Lundbladee3553422020-05-02 11:11:17 -07006038 // Get the first item
6039 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6040 if(uCBORError != QCBOR_SUCCESS) {
6041 return 12;
6042 }
6043 if(Item.uDataType != QCBOR_TYPE_INT64) {
6044 return 13;
6045 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006046
Laurence Lundbladee3553422020-05-02 11:11:17 -07006047 // Get a second item
6048 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6049 if(uCBORError != QCBOR_SUCCESS) {
6050 return 14;
6051 }
6052 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6053 return 15;
6054 }
6055
6056 // Try to finish before consuming all bytes to confirm
6057 // that the still-open error is returned.
6058 uCBORError = QCBORDecode_Finish(&DCtx);
6059 if(uCBORError != QCBOR_SUCCESS) {
6060 return 16;
6061 }
6062
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006063
Laurence Lundbladee3553422020-05-02 11:11:17 -07006064 return 0;
6065}
6066
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006067
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006068
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006069int32_t IntToTests()
6070{
6071 int nErrCode;
6072 int32_t n32;
6073 int16_t n16;
6074 int8_t n8;
6075 uint32_t u32;
6076 uint16_t u16;
6077 uint8_t u8;
6078 uint64_t u64;
6079
6080 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6081 if(nErrCode == -1 || n32 != 1) {
6082 return 1;
6083 }
6084
6085 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6086 if(nErrCode == -1 || n32 != INT32_MAX) {
6087 return 2;
6088 }
6089
6090 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6091 if(nErrCode == -1 || n32 != INT32_MIN) {
6092 return 3;
6093 }
6094
6095 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6096 if(nErrCode != -1) {
6097 return 4;
6098 }
6099
6100 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6101 if(nErrCode != -1) {
6102 return 5;
6103 }
6104
6105
6106 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6107 if(nErrCode == -1 || n16 != INT16_MAX) {
6108 return 6;
6109 }
6110
6111 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6112 if(nErrCode == -1 || n16 != INT16_MIN) {
6113 return 7;
6114 }
6115
6116 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6117 if(nErrCode == -1 || n16 != 1) {
6118 return 8;
6119 }
6120
6121 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6122 if(nErrCode != -1) {
6123 return 9;
6124 }
6125
6126 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6127 if(nErrCode != -1) {
6128 return 10;
6129 }
6130
6131
6132 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6133 if(nErrCode == -1 || n8 != 1) {
6134 return 11;
6135 }
6136
6137 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6138 if(nErrCode == -1 || n8 != INT8_MAX) {
6139 return 12;
6140 }
6141
6142 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6143 if(nErrCode == -1 || n8 != INT8_MIN) {
6144 return 13;
6145 }
6146
6147 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6148 if(nErrCode != -1) {
6149 return 14;
6150 }
6151
6152 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6153 if(nErrCode != -1) {
6154 return 15;
6155 }
6156
6157
6158 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6159 if(nErrCode == -1 || u32 != 1) {
6160 return 16;
6161 }
6162
6163 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6164 if(nErrCode == -1 || u32 != UINT32_MAX) {
6165 return 17;
6166 }
6167
6168 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6169 if(nErrCode == -1 || u32 != 0) {
6170 return 18;
6171 }
6172
6173 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6174 if(nErrCode != -1) {
6175 return 19;
6176 }
6177
6178 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6179 if(nErrCode != -1) {
6180 return 20;
6181 }
6182
6183
6184 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6185 if(nErrCode == -1 || u16 != UINT16_MAX) {
6186 return 21;
6187 }
6188
6189 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6190 if(nErrCode == -1 || u16 != 0) {
6191 return 22;
6192 }
6193
6194 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6195 if(nErrCode == -1 || u16 != 1) {
6196 return 23;
6197 }
6198
6199 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6200 if(nErrCode != -1) {
6201 return 24;
6202 }
6203
6204 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6205 if(nErrCode != -1) {
6206 return 25;
6207 }
6208
6209
6210 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6211 if(nErrCode == -1 || u8 != UINT8_MAX) {
6212 return 26;
6213 }
6214
6215 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6216 if(nErrCode == -1 || u8 != 0) {
6217 return 27;
6218 }
6219
6220 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6221 if(nErrCode == -1 || u8 != 1) {
6222 return 28;
6223 }
6224
6225 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6226 if(nErrCode != -1) {
6227 return 29;
6228 }
6229
6230 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6231 if(nErrCode != -1) {
6232 return 30;
6233 }
6234
6235
6236 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6237 if(nErrCode == -1 || u64 != 1) {
6238 return 31;
6239 }
6240
6241 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6242 if(nErrCode == -1 || u64 != INT64_MAX) {
6243 return 32;
6244 }
6245
6246 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6247 if(nErrCode == -1 || u64 != 0) {
6248 return 33;
6249 }
6250
6251 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6252 if(nErrCode != -1) {
6253 return 34;
6254 }
6255
6256 return 0;
6257}
6258
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006259
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006260
6261
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006262/*
6263A sequence with
6264 A wrapping bstr
6265 containing a map
6266 1
6267 2
6268 A wrapping bstr
6269 containing an array
6270 3
6271 wrapping bstr
6272 4
6273 5
6274 6
6275 array
6276 7
6277 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006278 */
6279
Laurence Lundblade55013642020-09-23 05:39:22 -07006280static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006281{
Laurence Lundblade55013642020-09-23 05:39:22 -07006282 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006283 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006284 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006285
Laurence Lundblade55013642020-09-23 05:39:22 -07006286 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006287
6288 QCBOREncode_BstrWrap(&EC);
6289 QCBOREncode_OpenMap(&EC);
6290 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6291 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6292 QCBOREncode_CloseMap(&EC);
6293 QCBOREncode_BstrWrap(&EC);
6294 QCBOREncode_OpenArray(&EC);
6295 QCBOREncode_AddInt64(&EC, 3);
6296 QCBOREncode_BstrWrap(&EC);
6297 QCBOREncode_AddInt64(&EC, 4);
6298 QCBOREncode_CloseBstrWrap(&EC, NULL);
6299 QCBOREncode_AddInt64(&EC, 5);
6300 QCBOREncode_CloseArray(&EC);
6301 QCBOREncode_CloseBstrWrap(&EC, NULL);
6302 QCBOREncode_AddInt64(&EC, 6);
6303 QCBOREncode_CloseBstrWrap(&EC, NULL);
6304 QCBOREncode_OpenArray(&EC);
6305 QCBOREncode_AddInt64(&EC, 7);
6306 QCBOREncode_AddInt64(&EC, 8);
6307 QCBOREncode_CloseArray(&EC);
6308
6309 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006310 if(uErr) {
6311 Encoded = NULLUsefulBufC;
6312 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006313
6314 return Encoded;
6315}
6316
6317
6318int32_t EnterBstrTest()
6319{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006320 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006321
6322 QCBORDecodeContext DC;
6323
Laurence Lundblade55013642020-09-23 05:39:22 -07006324 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006325
Laurence Lundblade55013642020-09-23 05:39:22 -07006326 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006327
6328
Laurence Lundblade9b334962020-08-27 10:55:53 -07006329 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006330 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006331 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6332 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006333 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006334 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006335 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006336 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006337 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006338 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006339 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006340 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006341 QCBORDecode_ExitArray(&DC);
6342 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006343 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006344 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006345 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006346 QCBORDecode_GetInt64(&DC, &n7);
6347 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006348 QCBORDecode_ExitArray(&DC);
6349
6350 QCBORError uErr = QCBORDecode_Finish(&DC);
6351
6352 return (int32_t)uErr;
6353}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006354
6355
6356
6357
6358static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006359 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006360
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006361 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006362 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006363 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6364 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6365 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006366
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006367 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006368 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6369 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6370 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006371
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006372 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006373 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006374 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6375 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006376
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006377 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006378 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6379 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006380
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006381 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006382 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006383 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6384 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006385
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006386 21,
6387 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6388 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6389
6390 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006391 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006392 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6393 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006394
6395 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006396 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6397 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006398
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006399 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006400 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006401 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6402 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006403
6404 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006405 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6406 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006407
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006408 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006409 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006410 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6411 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006412
6413 0x18, 0x33,
6414 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6415
6416 // MIME
6417 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006418 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6419 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6420 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006421
6422 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006423 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6424 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006425
6426 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006427 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6428 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6429 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006430
6431 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006432 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6433 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006434
6435 // UUID
6436 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006437 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6438 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006439
6440 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006441 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6442 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006443};
6444
6445int32_t DecodeTaggedTypeTests()
6446{
6447 QCBORDecodeContext DC;
6448 QCBORError uErr;
6449
6450 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6451
6452 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006453 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006454
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006455 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006456 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006457 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006458 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6459 return 1;
6460 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006461 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006462 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6463 return 2;
6464 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006465 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006466 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6467 return 3;
6468 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006469 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006470 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006471 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6472 return 4;
6473 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006474 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006475 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006476 return 5;
6477 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006478
Laurence Lundblade9b334962020-08-27 10:55:53 -07006479 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006480 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6481 bNeg != false) {
6482 return 10;
6483 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006484 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006485 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6486 bNeg != true) {
6487 return 11;
6488 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006489 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006490 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6491 return 12;
6492 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006493 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006494 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006495 return 13;
6496 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006497 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006498 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006499 return 14;
6500 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006501
Laurence Lundblade9b334962020-08-27 10:55:53 -07006502 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006503 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6504 return 20;
6505 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006506 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006507 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6508 return 21;
6509 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006510 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006511 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006512 return 22;
6513 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006514 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006515 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006516 return 23;
6517 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006518
Laurence Lundblade9b334962020-08-27 10:55:53 -07006519 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006520 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6521 return 30;
6522 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006523 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006524 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6525 return 31;
6526 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006527 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006528 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006529 return 32;
6530 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006531 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006532 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006533 return 33;
6534 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006535
Laurence Lundblade9b334962020-08-27 10:55:53 -07006536 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006537 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6538 return 40;
6539 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006540 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006541 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6542 return 41;
6543 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006544 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006545 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006546 return 42;
6547 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006548 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006549 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006550 return 43;
6551 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006552
Laurence Lundblade9b334962020-08-27 10:55:53 -07006553 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006554 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6555 return 50;
6556 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006557 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006558 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6559 return 51;
6560 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006561 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006562 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006563 return 52;
6564 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006565 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006566 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006567 return 53;
6568 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006569
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006570 // MIME
6571 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006572 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006573 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6574 bIsNot7Bit == true) {
6575 return 60;
6576 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006577 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006578 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6579 bIsNot7Bit == true) {
6580 return 61;
6581 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006582 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006583 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6584 bIsNot7Bit == false) {
6585 return 62;
6586 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006587 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006588 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6589 bIsNot7Bit == false) {
6590 return 63;
6591 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006592 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006593 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006594 return 64;
6595 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006596 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006597 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006598 return 65;
6599 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006600
Laurence Lundblade9b334962020-08-27 10:55:53 -07006601 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006602 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6603 return 70;
6604 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006605 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006606 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6607 return 71;
6608 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006609 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006610 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006611 return 72;
6612 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006613 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006614 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006615 return 73;
6616 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006617
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006618 // Improvement: add some more error test cases
6619
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006620 QCBORDecode_ExitMap(&DC);
6621
6622 uErr = QCBORDecode_Finish(&DC);
6623 if(uErr != QCBOR_SUCCESS) {
6624 return 100;
6625 }
6626
6627 return 0;
6628}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006629
6630
6631
6632
6633/*
6634 [
6635 "aaaaaaaaaa",
6636 {}
6637 ]
6638 */
6639static const uint8_t spTooLarge1[] = {
6640 0x9f,
6641 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6642 0xa0,
6643 0xff
6644};
6645
6646/*
6647 [
6648 {
6649 0: "aaaaaaaaaa"
6650 }
6651 ]
6652 */
6653static const uint8_t spTooLarge2[] = {
6654 0x9f,
6655 0xa1,
6656 0x00,
6657 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6658 0xff
6659};
6660
6661/*
6662 h'A1006A61616161616161616161'
6663
6664 {
6665 0: "aaaaaaaaaa"
6666 }
6667 */
6668static const uint8_t spTooLarge3[] = {
6669 0x4d,
6670 0xa1,
6671 0x00,
6672 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6673};
6674
6675int32_t TooLargeInputTest(void)
6676{
6677 QCBORDecodeContext DC;
6678 QCBORError uErr;
6679 UsefulBufC String;
6680
6681 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6682 // to 10 There's not really any way to test this error
6683 // condition. The error condition is not complex, so setting
6684 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6685
6686 // The input CBOR is only too large because the
6687 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6688 //
6689 // This test is disabled for the normal test runs because of the
6690 // special build requirement.
6691
6692
6693 // Tests the start of a map being too large
6694 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006695 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006696 QCBORDecode_GetTextString(&DC, &String);
6697 uErr = QCBORDecode_GetError(&DC);
6698 if(uErr != QCBOR_SUCCESS) {
6699 return 1;
6700 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006701 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006702 uErr = QCBORDecode_GetError(&DC);
6703 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6704 return 2;
6705 }
6706
6707 // Tests the end of a map being too large
6708 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006709 QCBORDecode_EnterArray(&DC, NULL);
6710 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006711 uErr = QCBORDecode_GetError(&DC);
6712 if(uErr != QCBOR_SUCCESS) {
6713 return 3;
6714 }
6715 QCBORDecode_ExitMap(&DC);
6716 uErr = QCBORDecode_GetError(&DC);
6717 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6718 return 4;
6719 }
6720
6721 // Tests the entire input CBOR being too large when processing bstr wrapping
6722 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6723 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6724 uErr = QCBORDecode_GetError(&DC);
6725 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6726 return 5;
6727 }
6728
6729 return 0;
6730}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006731
6732
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006733#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6734
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006735static const uint8_t spMapWithIndefLenStrings[] = {
6736 0xbf,
6737 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6738 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6739 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6740 0x03,
6741 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6742 0xc3,
6743 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
6744 0xff
6745};
6746
6747int32_t SpiffyIndefiniteLengthStringsTests()
6748{
6749 QCBORDecodeContext DCtx;
6750
6751 QCBORDecode_Init(&DCtx,
6752 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6753 QCBOR_DECODE_MODE_NORMAL);
6754
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006755 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006756 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6757
6758 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006759 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006760 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6761 if(QCBORDecode_GetAndResetError(&DCtx)) {
6762 return 1;
6763 }
6764
6765 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6766 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6767 return 2;
6768 }
6769
6770 uint64_t uInt;
6771 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6772 if(QCBORDecode_GetAndResetError(&DCtx)) {
6773 return 3;
6774 }
6775 if(uInt != 3) {
6776 return 4;
6777 }
6778
6779 double uDouble;
6780 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
6781 "label2",
6782 0xff,
6783 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006784#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006785 if(QCBORDecode_GetAndResetError(&DCtx)) {
6786 return 5;
6787 }
6788 if(uDouble != -16777474) {
6789 return 6;
6790 }
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07006791#else
6792 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
6793 return 7;
6794 }
6795#endif
6796
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006797
6798 QCBORDecode_ExitMap(&DCtx);
6799
6800 if(QCBORDecode_Finish(&DCtx)) {
6801 return 99;
6802 }
6803
6804 return 0;
6805}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006806#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08006807
6808
6809
6810int32_t PeekTest()
6811{
6812 QCBORItem Item;
6813 QCBORError nCBORError;
6814 QCBORDecodeContext DCtx;
6815
6816 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6817
6818 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6819 return 100+(int32_t)nCBORError;
6820 }
6821 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6822 return 200;
6823 }
6824
6825 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6826 return (int32_t)nCBORError;
6827 }
6828 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6829 return 300;
6830 }
6831
6832 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6833 return 400 + (int32_t)nCBORError;
6834 }
6835 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6836 return 500;
6837 }
6838
6839 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6840 return (int32_t)nCBORError;
6841 }
6842 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
6843 return 600;
6844 }
6845
6846 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6847 return 900 + (int32_t)nCBORError;
6848 }
6849 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6850 Item.uDataType != QCBOR_TYPE_INT64 ||
6851 Item.val.int64 != 42 ||
6852 Item.uDataAlloc ||
6853 Item.uLabelAlloc ||
6854 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6855 return 1000;
6856 }
6857
6858 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6859 return 1100 + (int32_t)nCBORError;
6860 }
6861
6862 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6863 Item.uDataType != QCBOR_TYPE_INT64 ||
6864 Item.val.int64 != 42 ||
6865 Item.uDataAlloc ||
6866 Item.uLabelAlloc ||
6867 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
6868 return 1200;
6869 }
6870
6871
6872 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6873 return 1300 + (int32_t)nCBORError;
6874 }
6875 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6876 Item.uDataAlloc ||
6877 Item.uLabelAlloc ||
6878 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
6879 Item.uDataType != QCBOR_TYPE_ARRAY ||
6880 Item.val.uCount != 2)
6881 return 1400;
6882
6883 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6884 return 1500 + (int32_t)nCBORError;
6885 }
6886 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6887 Item.uDataAlloc ||
6888 Item.uLabelAlloc ||
6889 UsefulBufCompareToSZ(Item.val.string, "string1")) {
6890 return 1600;
6891 }
6892
6893 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6894 return 1700 + (int32_t)nCBORError;
6895 }
6896 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6897 Item.uDataAlloc ||
6898 Item.uLabelAlloc ||
6899 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6900 return 1800;
6901 }
6902
6903 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6904 return (int32_t)nCBORError;
6905 }
6906 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6907 Item.uDataAlloc ||
6908 Item.uLabelAlloc ||
6909 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6910 return 1900;
6911 }
6912
6913 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6914 return (int32_t)nCBORError;
6915 }
6916 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6917 Item.uDataAlloc ||
6918 Item.uLabelAlloc ||
6919 UsefulBufCompareToSZ(Item.val.string, "string2")) {
6920 return 2000;
6921 }
6922
6923
6924 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6925 return 2100 + (int32_t)nCBORError;
6926 }
6927 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6928 Item.uDataAlloc ||
6929 Item.uLabelAlloc ||
6930 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
6931 Item.uDataType != QCBOR_TYPE_MAP ||
6932 Item.val.uCount != 4) {
6933 return 2100;
6934 }
6935
6936 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6937 return 2200 + (int32_t)nCBORError;
6938 }
6939 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6940 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
6941 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6942 Item.uDataAlloc ||
6943 Item.uLabelAlloc ||
6944 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
6945 return 2300;
6946 }
6947
6948 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6949 return 2400 + (int32_t)nCBORError;
6950 }
6951 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6952 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6953 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6954 Item.uDataAlloc ||
6955 Item.uLabelAlloc ||
6956 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6957 return 2500;
6958 }
6959
6960 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6961 return 2600 + (int32_t)nCBORError;
6962 }
6963 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6964 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
6965 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
6966 Item.uDataAlloc ||
6967 Item.uLabelAlloc ||
6968 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
6969 return 2700;
6970 }
6971
6972 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6973 return 2800 + (int32_t)nCBORError;
6974 }
6975 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6976 Item.uDataAlloc ||
6977 Item.uLabelAlloc ||
6978 UsefulBufCompareToSZ(Item.label.string, "another int") ||
6979 Item.uDataType != QCBOR_TYPE_INT64 ||
6980 Item.val.int64 != 98)
6981 return 2900;
6982
6983 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
6984 return 3000 + (int32_t)nCBORError;
6985 }
6986 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6987 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
6988 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
6989 Item.uDataAlloc ||
6990 Item.uLabelAlloc ||
6991 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
6992 return 3100;
6993 }
6994
6995 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
6996 return 3200 + (int32_t)nCBORError;
6997 }
6998 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6999 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7000 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7001 Item.uDataAlloc ||
7002 Item.uLabelAlloc ||
7003 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7004 return 3300;
7005 }
7006
7007 return 0;
7008}