blob: e21971fbd519ae430cb7923471230790b72fe35d [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 Lundblade1f8b5b02019-01-01 22:27:38 -080034#include "qcbor.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080035#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080036#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070037#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080038
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080039
Laurence Lundbladea2e29072018-12-30 09:20:06 -080040#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080041#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080042
43static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044{
45 if(szLabel) {
46 printf("%s ", szLabel);
47 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080048
Laurence Lundblade570fab52018-10-13 18:28:27 +080049 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080050 for(i = 0; i < Buf.len; i++) {
51 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052 printf("%02x ", Z);
53 }
54 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080055
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056 fflush(stdout);
57}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080058#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059
60
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070061static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080062 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
63 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
64 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
65 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
66 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
67 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
68 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
69 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
70 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
71 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
72 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
73 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
74 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
75 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
76 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
77 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
78 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
79 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
80 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
81 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
82 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
83 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0xff};
86
87
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080088// return CBOR error or -1 if type of value doesn't match
89
Laurence Lundbladec5fef682020-01-25 11:38:45 -080090static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080091{
92 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -070093 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080094
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080095 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -070096 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080097 if(Item.uDataType != QCBOR_TYPE_ARRAY)
98 return -1;
99
100 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700101 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800102 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800103 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800104 return -1;
105
106 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700107 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800108 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800109 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800110 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800111
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800112 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700113 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800115 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800117
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800118 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700119 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800121 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800123
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800124 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700125 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800127 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800128 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800129
130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800131 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700132 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 if(Item.uDataType != QCBOR_TYPE_INT64 ||
134 Item.val.int64 != -2147483648)
135 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800136
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800137 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700138 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800139 if(Item.uDataType != QCBOR_TYPE_INT64 ||
140 Item.val.int64 != -2147483647)
141 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800142
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800143 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700144 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800145 if(Item.uDataType != QCBOR_TYPE_INT64 ||
146 Item.val.int64 != -65538)
147 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800148
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800149 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700150 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800151 if(Item.uDataType != QCBOR_TYPE_INT64 ||
152 Item.val.int64 != -65537)
153 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800154
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800155 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700156 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800157 if(Item.uDataType != QCBOR_TYPE_INT64 ||
158 Item.val.int64 != -65536)
159 return -1;
160
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800162 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700163 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164 if(Item.uDataType != QCBOR_TYPE_INT64 ||
165 Item.val.int64 != -65535)
166 return -1;
167
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700170 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171 if(Item.uDataType != QCBOR_TYPE_INT64 ||
172 Item.val.int64 != -65534)
173 return -1;
174
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800175
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700177 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800178 if(Item.uDataType != QCBOR_TYPE_INT64 ||
179 Item.val.int64 != -257)
180 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800181
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800182 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700183 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800184 if(Item.uDataType != QCBOR_TYPE_INT64 ||
185 Item.val.int64 != -256)
186 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800187
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800188 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700189 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800190 if(Item.uDataType != QCBOR_TYPE_INT64 ||
191 Item.val.int64 != -255)
192 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800193
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800194 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700195 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800196 if(Item.uDataType != QCBOR_TYPE_INT64 ||
197 Item.val.int64 != -254)
198 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800199
200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700202 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800203 if(Item.uDataType != QCBOR_TYPE_INT64 ||
204 Item.val.int64 != -25)
205 return -1;
206
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700209 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800210 if(Item.uDataType != QCBOR_TYPE_INT64 ||
211 Item.val.int64 != -24)
212 return -1;
213
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800214
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700216 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800217 if(Item.uDataType != QCBOR_TYPE_INT64 ||
218 Item.val.int64 != -23)
219 return -1;
220
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700223 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 if(Item.uDataType != QCBOR_TYPE_INT64 ||
225 Item.val.int64 != -1)
226 return -1;
227
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800228
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800229 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700230 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800231 if(Item.uDataType != QCBOR_TYPE_INT64 ||
232 Item.val.int64 != 0)
233 return -1;
234
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700237 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238 if(Item.uDataType != QCBOR_TYPE_INT64 ||
239 Item.val.int64 != 0)
240 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800242 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700243 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800244 if(Item.uDataType != QCBOR_TYPE_INT64 ||
245 Item.val.int64 != 1)
246 return -1;
247
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800248
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700250 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(Item.uDataType != QCBOR_TYPE_INT64 ||
252 Item.val.int64 != 22)
253 return -1;
254
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700257 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 if(Item.uDataType != QCBOR_TYPE_INT64 ||
259 Item.val.int64 != 23)
260 return -1;
261
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700264 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 if(Item.uDataType != QCBOR_TYPE_INT64 ||
266 Item.val.int64 != 24)
267 return -1;
268
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800269
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800270 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700271 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800272 if(Item.uDataType != QCBOR_TYPE_INT64 ||
273 Item.val.int64 != 25)
274 return -1;
275
276 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700277 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800278 if(Item.uDataType != QCBOR_TYPE_INT64 ||
279 Item.val.int64 != 26)
280 return -1;
281
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700284 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800285 if(Item.uDataType != QCBOR_TYPE_INT64 ||
286 Item.val.int64 != 254)
287 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800288
289
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700291 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800292 if(Item.uDataType != QCBOR_TYPE_INT64 ||
293 Item.val.int64 != 255)
294 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800295
296
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700298 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800299 if(Item.uDataType != QCBOR_TYPE_INT64 ||
300 Item.val.int64 != 256)
301 return -1;
302
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800303
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700305 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306 if(Item.uDataType != QCBOR_TYPE_INT64 ||
307 Item.val.int64 != 257)
308 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800309
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800310 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700311 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800312 if(Item.uDataType != QCBOR_TYPE_INT64 ||
313 Item.val.int64 != 65534)
314 return -1;
315
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700318 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800319 if(Item.uDataType != QCBOR_TYPE_INT64 ||
320 Item.val.int64 != 65535)
321 return -1;
322
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800323
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700325 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800326 if(Item.uDataType != QCBOR_TYPE_INT64 ||
327 Item.val.int64 != 65536)
328 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800329
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800330 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700331 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800332 if(Item.uDataType != QCBOR_TYPE_INT64 ||
333 Item.val.int64 != 65537)
334 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800335
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800336 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700337 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800338 if(Item.uDataType != QCBOR_TYPE_INT64 ||
339 Item.val.int64 != 65538)
340 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800341
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800342 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700343 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800344 if(Item.uDataType != QCBOR_TYPE_INT64 ||
345 Item.val.int64 != 2147483647)
346 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800347
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800348 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700349 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800350 if(Item.uDataType != QCBOR_TYPE_INT64 ||
351 Item.val.int64 != 2147483647)
352 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800353
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800354 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700355 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800356 if(Item.uDataType != QCBOR_TYPE_INT64 ||
357 Item.val.int64 != 2147483648)
358 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800359
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800360 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700361 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800362 if(Item.uDataType != QCBOR_TYPE_INT64 ||
363 Item.val.int64 != 2147483649)
364 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800365
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800366 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700367 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800368 if(Item.uDataType != QCBOR_TYPE_INT64 ||
369 Item.val.int64 != 4294967294)
370 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800371
372
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700374 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800375 if(Item.uDataType != QCBOR_TYPE_INT64 ||
376 Item.val.int64 != 4294967295)
377 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800378
379
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700381 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800382 if(Item.uDataType != QCBOR_TYPE_INT64 ||
383 Item.val.int64 != 4294967296)
384 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800385
386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700388 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800389 if(Item.uDataType != QCBOR_TYPE_INT64 ||
390 Item.val.int64 != 4294967297)
391 return -1;
392
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800393
394
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800395 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700396 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800397 if(Item.uDataType != QCBOR_TYPE_INT64 ||
398 Item.val.int64 != 9223372036854775807LL)
399 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800400
401
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800402 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -0700403 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800404 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
405 Item.val.uint64 != 18446744073709551615ULL)
406 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800407
408
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
410 return -1;
411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800412
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 return 0;
414}
415
416
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800417// One less than the smallest negative integer allowed in C. Decoding
418// this should fail.
419static const uint8_t spTooSmallNegative[] = {
420 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000421};
422
423
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800424/*
425 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800426 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800427 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800428int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000430 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800431 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000433 QCBORDecode_Init(&DCtx,
434 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
435 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800436
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000437 // The really big test of all successes
438 nReturn = IntegerValuesParseTestInternal(&DCtx);
439 if(nReturn) {
440 return nReturn;
441 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800442
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000443 // The one large negative integer that can be parsed
444 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800445 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000446 QCBOR_DECODE_MODE_NORMAL);
447
448 QCBORItem item;
449 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
450 nReturn = -4000;
451 }
452
453 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454}
455
456
457/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800458 Creates a simple CBOR array and returns it in *pEncoded. The array is
459 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800460
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800461 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800462
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800463 */
464
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800465static uint8_t spSimpleArrayBuffer[50];
466
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800467static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468{
469 QCBOREncodeContext ECtx;
470 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800471
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800472 *pEncoded = NULL;
473 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800474
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800476 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 // and last with the buffer to do the actual encoding
478 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700479 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480 QCBOREncode_OpenArray(&ECtx);
481 QCBOREncode_AddInt64(&ECtx, nInt1);
482 QCBOREncode_AddInt64(&ECtx, nInt2);
483 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
484 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
485 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800486
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800487 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800488 goto Done;
489
490 if(*pEncoded != NULL) {
491 nReturn = 0;
492 goto Done;
493 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800494
495 // Use static buffer to avoid dependency on malloc()
496 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800497 goto Done;
498 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800499 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800500
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800501 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800502
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800503Done:
504 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800505}
506
507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800508/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800509 Some basic CBOR with map and array used in a lot of tests.
510 The map labels are all strings
511
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800512 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900513 "an array of two strings": [
514 "string1", "string2"
515 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800516 "map in a map": {
517 "bytes 1": h'78787878',
518 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900519 "another int": 98,
520 "text 2": "lies, damn lies and statistics"
521 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800522 }
523 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800524static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700525 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
526 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
527 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
528 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
529 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
530 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
531 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
532 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
533 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
534 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
535 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
536 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
537 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
538 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
539 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
540 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800541
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800542static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800543 size_t nLen,
544 int64_t *pInt1,
545 int64_t *pInt2,
546 const uint8_t **pBuf3,
547 size_t *pBuf3Len,
548 const uint8_t **pBuf4,
549 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800550{
551 QCBORDecodeContext DCtx;
552 QCBORItem Item;
553 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800554
Laurence Lundbladeee851742020-01-08 08:37:05 -0800555 QCBORDecode_Init(&DCtx,
556 (UsefulBufC){pEncoded, nLen},
557 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800558
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559 // Make sure the first thing is a map
560 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
561 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800562
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800563 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800564 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800565 goto Done;
566 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800567
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800568 // Second integer
569 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
570 goto Done;
571 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800573 // First string
574 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
575 goto Done;
576 *pBuf3 = Item.val.string.ptr;
577 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800578
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 // Second string
580 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
581 goto Done;
582 *pBuf4 = Item.val.string.ptr;
583 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800585 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587Done:
588 return(nReturn);
589}
590
591
592
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800593
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800594int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595{
596 uint8_t *pEncoded;
597 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800598
Laurence Lundblade5e390822019-01-06 12:35:01 -0800599 int64_t i1=0, i2=0;
600 size_t i3=0, i4=0;
601 const uint8_t *s3= (uint8_t *)"";
602 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
604
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800605 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
606 return(-1);
607 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800610
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800611 if(i1 != 23 ||
612 i2 != 6000 ||
613 i3 != 8 ||
614 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530615 memcmp("galactic", s3, 8) !=0 ||
616 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 return(-1);
618 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620 return(0);
621}
622
623
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700624/*
625 [
626 0,
627 [],
628 [
629 [],
630 [
631 0
632 ],
633 {},
634 {
635 1: {},
636 2: {},
637 3: []
638 }
639 ]
640 ]
641 */
642static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
643 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
644
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800645int32_t EmptyMapsAndArraysTest()
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700646{
647 QCBORDecodeContext DCtx;
648 QCBORItem Item;
649
Laurence Lundbladeee851742020-01-08 08:37:05 -0800650 QCBORDecode_Init(&DCtx,
651 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
652 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700653
654 // Array with 3 items
655 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
656 Item.uDataType != QCBOR_TYPE_ARRAY ||
657 Item.uNestingLevel != 0 ||
658 Item.uNextNestLevel != 1 ||
659 Item.val.uCount != 3) {
660 return -1;
661 }
662
663 // An integer 0
664 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
665 Item.uDataType != QCBOR_TYPE_INT64 ||
666 Item.uNestingLevel != 1 ||
667 Item.uNextNestLevel != 1 ||
668 Item.val.uint64 != 0) {
669 return -2;
670 }
671
672 // An empty array
673 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
674 Item.uDataType != QCBOR_TYPE_ARRAY ||
675 Item.uNestingLevel != 1 ||
676 Item.uNextNestLevel != 1 ||
677 Item.val.uCount != 0) {
678 return -3;
679 }
680
681 // An array with 4 items
682 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
683 Item.uDataType != QCBOR_TYPE_ARRAY ||
684 Item.uNestingLevel != 1 ||
685 Item.uNextNestLevel != 2 ||
686 Item.val.uCount != 4) {
687 return -4;
688 }
689
690 // An empty array
691 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
692 Item.uDataType != QCBOR_TYPE_ARRAY ||
693 Item.uNestingLevel != 2 ||
694 Item.uNextNestLevel != 2 ||
695 Item.val.uCount != 0) {
696 return -5;
697 }
698
699 // An array with 1 item
700 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
701 Item.uDataType != QCBOR_TYPE_ARRAY ||
702 Item.uNestingLevel != 2 ||
703 Item.uNextNestLevel != 3 ||
704 Item.val.uCount != 1) {
705 return -6;
706 }
707
708 // An integer 0
709 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
710 Item.uDataType != QCBOR_TYPE_INT64 ||
711 Item.uNestingLevel != 3 ||
712 Item.uNextNestLevel != 2 ||
713 Item.val.uint64 != 0) {
714 return -7;
715 }
716
717 // An empty map
718 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
719 Item.uDataType != QCBOR_TYPE_MAP ||
720 Item.uNestingLevel != 2 ||
721 Item.uNextNestLevel != 2 ||
722 Item.val.uCount != 0) {
723 return -8;
724 }
725
726 // An map with 3 items
727 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
728 Item.uDataType != QCBOR_TYPE_MAP ||
729 Item.uNestingLevel != 2 ||
730 Item.uNextNestLevel != 3 ||
731 Item.val.uCount != 3) {
732 return -9;
733 }
734
735 // An empty map
736 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
737 Item.uDataType != QCBOR_TYPE_MAP ||
738 Item.uNestingLevel != 3 ||
739 Item.uNextNestLevel != 3 ||
740 Item.val.uCount != 0) {
741 return -10;
742 }
743
744 // An empty map
745 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
746 Item.uDataType != QCBOR_TYPE_MAP ||
747 Item.uNestingLevel != 3 ||
748 Item.uNextNestLevel != 3 ||
749 Item.val.uCount != 0) {
750 return -11;
751 }
752
753 // An empty array
754 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
755 Item.uDataType != QCBOR_TYPE_ARRAY ||
756 Item.uNestingLevel != 3 ||
757 Item.uNextNestLevel != 0 ||
758 Item.val.uCount != 0) {
759 return -12;
760 }
761
762 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
763 return -13;
764 }
765
766 return 0;
767}
768
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800769
Laurence Lundbladeee851742020-01-08 08:37:05 -0800770static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
771 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800772
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800773int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800774{
775 QCBORDecodeContext DCtx;
776 int nReturn = 0;
777 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800778
Laurence Lundbladeee851742020-01-08 08:37:05 -0800779 QCBORDecode_Init(&DCtx,
780 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
781 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800782
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800783 for(i = 0; i < 10; i++) {
784 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800785
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
787 Item.uDataType != QCBOR_TYPE_ARRAY ||
788 Item.uNestingLevel != i) {
789 nReturn = -1;
790 break;
791 }
792 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800793
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 return(nReturn);
795}
796
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700797// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800798static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
799 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
800 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
801 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800802
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800803int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800804{
805 QCBORDecodeContext DCtx;
806 int nReturn = 0;
807 int i;
808 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800809
810
Laurence Lundbladeee851742020-01-08 08:37:05 -0800811 QCBORDecode_Init(&DCtx,
812 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
813 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800814
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700815 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800816
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
818 Item.uDataType != QCBOR_TYPE_ARRAY ||
819 Item.uNestingLevel != i) {
820 nReturn = -1;
821 break;
822 }
823 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800824
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800825 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
826 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800827
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800828 return(nReturn);
829}
830
831
832
833
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800834int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800835{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700836 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800837
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800838 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700839 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800840
Laurence Lundbladeee851742020-01-08 08:37:05 -0800841 QCBORDecode_Init(&DCtx,
842 (UsefulBufC){spExpectedEncodedInts, nNum},
843 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800844
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800845 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800846
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700847 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800848 nResult = -1;
849 goto Done;
850 }
851 }
852Done:
853 return nResult;
854}
855
856
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800857
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800858int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800859{
860 uint8_t *pEncoded;
861 int nReturn;
862 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800863
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800864 int64_t i1, i2;
865 size_t i3, i4;
866 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800867
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800868 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
871 return(-1);
872 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800873
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800874 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
875 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
876 if(nResult == 0) {
877 nReturn = -1;
878 }
879 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800881 return(nReturn);
882}
883
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530884/*
885 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800886 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
887 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530888 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800889static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800890{
891 QCBORDecodeContext DCtx;
892 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -0700893 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800894
Laurence Lundbladeee851742020-01-08 08:37:05 -0800895 QCBORDecode_Init(&DCtx,
896 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
897 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900899 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700900 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900901 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800902 if(Item.uDataType != QCBOR_TYPE_MAP ||
903 Item.val.uCount != 3)
904 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800905
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900906 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700907 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900908 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800909 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 Item.uDataType != QCBOR_TYPE_INT64 ||
911 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530912 Item.uDataAlloc ||
913 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900914 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800915 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900916 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800917
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900918 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700919 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900920 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800921 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530922 Item.uDataAlloc ||
923 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900924 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800925 Item.uDataType != QCBOR_TYPE_ARRAY ||
926 Item.val.uCount != 2)
927 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800928
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900929 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700930 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900931 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800932 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530933 Item.uDataAlloc ||
934 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900935 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800936 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800938
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900939 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700940 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900941 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800942 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530943 Item.uDataAlloc ||
944 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900945 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800946 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900947 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800948
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900949 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700950 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900951 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530953 Item.uDataAlloc ||
954 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900955 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800956 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900957 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800960
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900961 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700962 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900963 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800964 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900965 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800966 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530967 Item.uDataAlloc ||
968 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900969 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800970 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900971 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800972
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900973 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700974 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900975 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800976 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900977 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800978 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530979 Item.uDataAlloc ||
980 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900981 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800982 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800984
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900985 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700986 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900987 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800988 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530989 Item.uDataAlloc ||
990 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800992 Item.uDataType != QCBOR_TYPE_INT64 ||
993 Item.val.int64 != 98)
994 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800995
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900996 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -0700997 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900998 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800999 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001000 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301002 Item.uDataAlloc ||
1003 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001005 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001007
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001008 return 0;
1009}
1010
1011
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001012/*
1013 Decode and thoroughly check a moderately complex
1014 set of maps
1015 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001016int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001017{
1018 QCBORDecodeContext DCtx;
1019 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -07001020 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001021
Laurence Lundbladeee851742020-01-08 08:37:05 -08001022 QCBORDecode_Init(&DCtx,
1023 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1024 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001025
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001026 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001027 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001028 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001029 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1030 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001031 return -1;
1032 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001033
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001034 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001035 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001036 }
1037 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1038 Item.uDataAlloc ||
1039 Item.uLabelAlloc ||
1040 Item.uLabelType != QCBOR_TYPE_NONE ||
1041 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1042 return -2;
1043 }
1044
1045 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001046 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001047 }
1048 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1049 Item.uDataType != QCBOR_TYPE_INT64 ||
1050 Item.val.int64 != 42 ||
1051 Item.uDataAlloc ||
1052 Item.uLabelAlloc) {
1053 return -3;
1054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001055
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001056 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001057 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001058 }
1059 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1060 Item.uDataAlloc ||
1061 Item.uLabelAlloc ||
1062 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1063 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1064 return -4;
1065 }
1066
1067 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001068 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001069 }
1070 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1071 Item.uDataAlloc ||
1072 Item.uLabelAlloc ||
1073 Item.uDataType != QCBOR_TYPE_ARRAY ||
1074 Item.val.uCount != 2) {
1075 return -5;
1076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001077
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001078 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001079 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001080 }
1081 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1082 Item.val.string.len != 7 ||
1083 Item.uDataAlloc ||
1084 Item.uLabelAlloc ||
1085 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1086 return -6;
1087 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001088
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001089 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001090 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001091 }
1092 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1093 Item.uDataAlloc ||
1094 Item.uLabelAlloc ||
1095 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1096 return -7;
1097 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001098
1099
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001100 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001101 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001102 }
1103 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1104 Item.uDataAlloc ||
1105 Item.uLabelAlloc ||
1106 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1107 return -8;
1108 }
1109
1110 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001111 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 }
1113 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1114 Item.uDataAlloc ||
1115 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001116 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1117 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 return -9;
1119 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001120
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001121 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001122 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001123 }
1124 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1125 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1126 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1127 Item.uDataAlloc ||
1128 Item.uLabelAlloc) {
1129 return -10;
1130 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001131
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001132 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001133 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 }
1135 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1136 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1137 Item.uDataAlloc ||
1138 Item.uLabelAlloc ||
1139 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1140 return -11;
1141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001142
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001143 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001144 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001145 }
1146 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1147 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1148 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1149 Item.uDataAlloc ||
1150 Item.uLabelAlloc) {
1151 return -12;
1152 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001153
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001154 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001155 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001156 }
1157 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1158 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1159 Item.uDataAlloc ||
1160 Item.uLabelAlloc ||
1161 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1162 return -13;
1163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001164
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001166 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 }
1168 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1169 Item.uDataAlloc ||
1170 Item.uLabelAlloc ||
1171 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1172 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1173 return -14;
1174 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001175
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001176 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001177 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001178 }
1179 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1180 Item.uDataAlloc ||
1181 Item.uLabelAlloc ||
1182 Item.uDataType != QCBOR_TYPE_INT64 ||
1183 Item.val.int64 != 98) {
1184 return -15;
1185 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001186
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001187 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001188 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 }
1190 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1191 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1192 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1193 Item.uDataAlloc ||
1194 Item.uLabelAlloc) {
1195 return -16;
1196 }
1197
1198 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001199 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 }
1201 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1202 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1203 Item.uDataAlloc ||
1204 Item.uLabelAlloc ||
1205 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1206 return -17;
1207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001208
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 return 0;
1210}
1211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001212
1213/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301214 Fully or partially decode pValidMapEncoded. When
1215 partially decoding check for the right error code.
1216 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001217
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301218 The partial decodes test error conditions of
1219 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001220
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301221 This could be combined with the above test
1222 and made prettier and maybe a little more
1223 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001224 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001225static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001226{
1227 QCBORDecodeContext DCtx;
1228 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -07001229 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001230
Laurence Lundbladeee851742020-01-08 08:37:05 -08001231 QCBORDecode_Init(&DCtx,
1232 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1233 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001234
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001235 if(nLevel < 1) {
1236 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1237 return -1;
1238 } else {
1239 return 0;
1240 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301242
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001243
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001244 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001245 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001246 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001247 if(Item.uDataType != QCBOR_TYPE_MAP ||
1248 Item.val.uCount != 3)
1249 return -1;
1250
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001251 if(nLevel < 2) {
1252 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1253 return -1;
1254 } else {
1255 return 0;
1256 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001258
1259
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001261 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001262 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001263 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001264 Item.uDataType != QCBOR_TYPE_INT64 ||
1265 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001266 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001267 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001269
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001270 if(nLevel < 3) {
1271 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1272 return -1;
1273 } else {
1274 return 0;
1275 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001277
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001278 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001279 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001280 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001282 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001283 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001284 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001285 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001287
1288
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001289 if(nLevel < 4) {
1290 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1291 return -1;
1292 } else {
1293 return 0;
1294 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001295 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001296
1297
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001298 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001299 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001300 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001301 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001302 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001303 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001305
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001306 if(nLevel < 5) {
1307 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1308 return -1;
1309 } else {
1310 return 0;
1311 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001313
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001314 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001315 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001316 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001317 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001318 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001319 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001322 if(nLevel < 6) {
1323 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1324 return -1;
1325 } else {
1326 return 0;
1327 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001329
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001331 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001332 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001333 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001334 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001335 Item.uDataType != QCBOR_TYPE_MAP ||
1336 Item.val.uCount != 4)
1337 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001338
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001339 if(nLevel < 7) {
1340 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1341 return -1;
1342 } else {
1343 return 0;
1344 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001346
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001348 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001353 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001354 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001356
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001357 if(nLevel < 8) {
1358 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1359 return -1;
1360 } else {
1361 return 0;
1362 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001364
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -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 Lundbladeccfb8cd2018-12-07 21:11:30 +09001369 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001370 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001371 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001372 return -1;
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 < 9) {
1376 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1377 return -1;
1378 } 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 Lundblade29497c02020-07-11 15:44:03 -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 Lundbladeccfb8cd2018-12-07 21:11:30 +09001387 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 Item.uDataType != QCBOR_TYPE_INT64 ||
1389 Item.val.int64 != 98)
1390 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001392 if(nLevel < 10) {
1393 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1394 return -1;
1395 } else {
1396 return 0;
1397 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001399
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade29497c02020-07-11 15:44:03 -07001401 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001402 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001406 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001407 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001409
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301410 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001411 return -1;
1412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001414 return 0;
1415}
1416
1417
1418
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001419/*
1420 Public function for initialization. See header qcbor.h
1421 */
1422int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001423{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001424 // Parse a moderatly complex map structure very thoroughly
1425 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1426 if(nResult) {
1427 return nResult;
1428 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001429
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001430 // Again, but in strings-only mode. It should succeed since the input
1431 // map has only string labels.
1432 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1433 if(nResult) {
1434 return nResult;
1435 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001436
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001437 // Again, but try to finish the decoding before the end of the
1438 // input at 10 different place and see that the right error code
1439 // is returned.
1440 for(int i = 0; i < 10; i++) {
1441 nResult = ExtraBytesTest(i);
1442 if(nResult) {
1443 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001446
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001447 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001448}
1449
1450
Laurence Lundbladeee851742020-01-08 08:37:05 -08001451static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1452 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1453 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001455int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456{
1457 QCBORDecodeContext DCtx;
1458 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -07001459 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001460
1461
Laurence Lundbladeee851742020-01-08 08:37:05 -08001462 QCBORDecode_Init(&DCtx,
1463 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1464 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001465
1466
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001468 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001469 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1470 Item.val.uCount != 10)
1471 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001474 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001475 if(Item.uDataType != QCBOR_TYPE_FALSE)
1476 return -1;
1477
1478 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001479 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001480 if(Item.uDataType != QCBOR_TYPE_TRUE)
1481 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001482
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001483 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001484 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001485 if(Item.uDataType != QCBOR_TYPE_NULL)
1486 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001489 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1491 return -1;
1492
1493 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001494 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001495 return -1;
1496
1497 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001498 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001499 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1500 return -1;
1501
1502 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001503 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001504 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1505 return -1;
1506
Laurence Lundblade077475f2019-04-26 09:06:33 -07001507 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001509
Laurence Lundblade077475f2019-04-26 09:06:33 -07001510 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001511 return -1;
1512
Laurence Lundblade077475f2019-04-26 09:06:33 -07001513 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001515
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001516 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001517 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001518 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1519 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001520
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001521 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001522 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1524 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001525
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001527
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528}
1529
1530
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001531static int IsNotWellFormedError(QCBORError nErr)
1532{
1533 switch(nErr){
1534 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1535 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1536 case QCBOR_ERR_UNSUPPORTED:
1537 case QCBOR_ERR_HIT_END:
1538 case QCBOR_ERR_BAD_TYPE_7:
1539 case QCBOR_ERR_BAD_BREAK:
1540 case QCBOR_ERR_EXTRA_BYTES:
1541 case QCBOR_ERR_BAD_INT:
1542 return 1;
1543 default:
1544 return 0;
1545 }
1546}
1547
1548
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001549int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001550{
1551 // Loop over all the not-well-formed instance of CBOR
1552 // that are test vectors in not_well_formed_cbor.h
1553 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1554 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1555 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1556 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1557
Laurence Lundbladeee851742020-01-08 08:37:05 -08001558 // Set up decoder context. String allocator needed for indefinite
1559 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001560 QCBORDecodeContext DCtx;
1561 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1562 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1563 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1564
1565 // Loop getting items until no more to get
1566 QCBORError nCBORError;
1567 do {
1568 QCBORItem Item;
1569
1570 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1571 } while(nCBORError == QCBOR_SUCCESS);
1572
1573 // Every test vector must fail with
1574 // a not-well-formed error. If not
1575 // this test fails.
1576 if(!IsNotWellFormedError(nCBORError)) {
1577 // Return index of failure in the error code
1578 return 2000 + nIterate;
1579 }
1580 }
1581 return 0;
1582}
1583
1584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001586 UsefulBufC Input;
1587 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588};
1589
Laurence Lundblade59289e52019-12-30 13:44:37 -08001590
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001591static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001592{
1593 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1594 // Set up the decoding context including a memory pool so that
1595 // indefinite length items can be checked
1596 QCBORDecodeContext DCtx;
1597 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1598 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1599 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1600 if(nCBORError) {
1601 return -9;
1602 }
1603
1604 // Iterate until there is an error of some sort error
1605 QCBORItem Item;
1606 do {
1607 // Set to something none-zero other than QCBOR_TYPE_NONE
1608 memset(&Item, 0x33, sizeof(Item));
1609
1610 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1611 } while(nCBORError == QCBOR_SUCCESS);
1612
1613 // Must get the expected error or the this test fails
1614 // The data and label type must also be QCBOR_TYPE_NONE
1615 if(nCBORError != pF->nError ||
1616 Item.uDataType != QCBOR_TYPE_NONE ||
1617 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001618 // return index of CBOR + 100
1619 const size_t nIndex = (size_t)(pF - pFailInputs)/sizeof(struct FailInput);
1620 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001621 }
1622 }
1623
1624 return 0;
1625}
1626
1627
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001628struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001629 // Most of this is copied from not_well_formed.h. Here the error code
1630 // returned is also checked.
1631
1632 // Indefinite length strings must be closed off
1633 // An indefinite length byte string not closed off
1634 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1635 // An indefinite length text string not closed off
1636 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1637
1638
1639 // All the chunks in an indefinite length string must be of the type of indefinite length string
1640 // indefinite length byte string with text string chunk
1641 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1642 // indefinite length text string with a byte string chunk
1643 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1644 // indefinite length byte string with an positive integer chunk
1645 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1646 // indefinite length byte string with an negative integer chunk
1647 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1648 // indefinite length byte string with an array chunk
1649 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1650 // indefinite length byte string with an map chunk
1651 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1652 // indefinite length byte string with tagged integer chunk
1653 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1654 // indefinite length byte string with an simple type chunk
1655 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1656 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1657 // indefinite length text string with indefinite string inside
1658 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1659
1660
1661 // Definte length maps and arrays must be closed by having the right number of items
1662 // A definte length array that is supposed to have 1 item, but has none
1663 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1664 // A definte length array that is supposed to have 2 items, but has only 1
1665 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1666 // A definte length array that is supposed to have 511 items, but has only 1
1667 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1668 // A definte length map that is supposed to have 1 item, but has none
1669 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1670 // A definte length map that is supposed to have s item, but has only 1
1671 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1672
1673
1674 // Indefinte length maps and arrays must be ended by a break
1675 // Indefinite length array with zero items and no break
1676 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1677 // Indefinite length array with two items and no break
1678 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1679 // Indefinite length map with zero items and no break
1680 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1681 // Indefinite length map with two items and no break
1682 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1683
1684
1685 // Nested maps and arrays must be closed off (some extra nested test vectors)
1686 // Unclosed indefinite array containing a close definite array
1687 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
1688 // Definite length array containing an unclosed indefinite array
1689 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1690 // Deeply nested definite length arrays with deepest one unclosed
1691 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1692 // Deeply nested indefinite length arrays with deepest one unclosed
1693 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1694 // Mixed nesting with indefinite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001695 // TODO: think through this one
1696 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001697 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001698 // TODO: think through this one
1699 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001700
1701
1702 // The "argument" for the data item is incomplete
1703 // Positive integer missing 1 byte argument
1704 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1705 // Positive integer missing 2 byte argument
1706 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1707 // Positive integer missing 4 byte argument
1708 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1709 // Positive integer missing 8 byte argument
1710 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1711 // Positive integer missing 1 byte of 2 byte argument
1712 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1713 // Positive integer missing 2 bytes of 4 byte argument
1714 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1715 // Positive integer missing 1 bytes of 7 byte argument
1716 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1717 // Negative integer missing 1 byte argument
1718 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1719 // Binary string missing 1 byte argument
1720 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1721 // Text string missing 1 byte argument
1722 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1723 // Array missing 1 byte argument
1724 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1725 // Map missing 1 byte argument
1726 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1727 // Tag missing 1 byte argument
1728 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1729 // Simple missing 1 byte argument
1730 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1731
1732
1733 // Breaks must not occur in definite length arrays and maps
1734 // Array of length 1 with sole member replaced by a break
1735 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1736 // Array of length 2 with 2nd member replaced by a break
1737 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1738 // Map of length 1 with sole member label replaced by a break
1739 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1740 // Map of length 1 with sole member label replaced by break
1741 // Alternate representation that some decoders handle difference
1742 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1743 // Array of length 1 with 2nd member value replaced by a break
1744 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1745 // Map of length 2 with 2nd member replaced by a break
1746 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1747
1748
1749 // Breaks must not occur on their own out of an indefinite length data item
1750 // A bare break is not well formed
1751 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1752 // A bare break after a zero length definite length array
1753 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1754 // A bare break after a zero length indefinite length map
1755 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1756
1757
1758 // Forbidden two byte encodings of simple types
1759 // Must use 0xe0 instead
1760 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1761 // Should use 0xe1 instead
1762 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1763 // Should use 0xe2 instead
1764 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1765 // Should use 0xe3 instead
1766 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1767 // Should use 0xe4 instead
1768 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1769 // Should use 0xe5 instead
1770 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1771 // Should use 0xe6 instead
1772 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1773 // Should use 0xe7 instead
1774 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1775 // Should use 0xe8 instead
1776 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1777 // Should use 0xe9 instead
1778 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1779 // Should use 0xea instead
1780 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1781 // Should use 0xeb instead
1782 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1783 // Should use 0xec instead
1784 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1785 // Should use 0xed instead
1786 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1787 // Should use 0xee instead
1788 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1789 // Should use 0xef instead
1790 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1791 // Should use 0xf0 instead
1792 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1793 // Should use 0xf1 instead
1794 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1795 // Should use 0xf2 instead
1796 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1797 // Must use 0xf3 instead
1798 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1799 // Must use 0xf4 instead
1800 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1801 // Must use 0xf5 instead
1802 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1803 // Must use 0xf6 instead
1804 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1805 // Must use 0xf7 instead
1806 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1807 // Must use 0xf8 instead
1808 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1809
1810
1811 // Integers with additional info indefinite length
1812 // Positive integer with additional info indefinite length
1813 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1814 // Negative integer with additional info indefinite length
1815 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1816 // CBOR tag with "argument" an indefinite length
1817 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1818 // CBOR tag with "argument" an indefinite length alternate vector
1819 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1820
1821
1822 // Missing bytes from a deterministic length string
1823 // A byte string is of length 1 without the 1 byte
1824 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1825 // A text string is of length 1 without the 1 byte
1826 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001827 // Byte string should have 2^32-15 bytes, but has one
1828 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1829 // Byte string should have 2^32-15 bytes, but has one
1830 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001831
1832
1833 // Use of unassigned additional information values
1834 // Major type positive integer with reserved value 28
1835 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1836 // Major type positive integer with reserved value 29
1837 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1838 // Major type positive integer with reserved value 30
1839 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1840 // Major type negative integer with reserved value 28
1841 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1842 // Major type negative integer with reserved value 29
1843 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1844 // Major type negative integer with reserved value 30
1845 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1846 // Major type byte string with reserved value 28 length
1847 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1848 // Major type byte string with reserved value 29 length
1849 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1850 // Major type byte string with reserved value 30 length
1851 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1852 // Major type text string with reserved value 28 length
1853 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1854 // Major type text string with reserved value 29 length
1855 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1856 // Major type text string with reserved value 30 length
1857 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1858 // Major type array with reserved value 28 length
1859 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1860 // Major type array with reserved value 29 length
1861 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1862 // Major type array with reserved value 30 length
1863 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1864 // Major type map with reserved value 28 length
1865 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1866 // Major type map with reserved value 29 length
1867 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1868 // Major type map with reserved value 30 length
1869 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1870 // Major type tag with reserved value 28 length
1871 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1872 // Major type tag with reserved value 29 length
1873 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1874 // Major type tag with reserved value 30 length
1875 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1876 // Major type simple with reserved value 28 length
1877 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1878 // Major type simple with reserved value 29 length
1879 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1880 // Major type simple with reserved value 30 length
1881 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1882
1883
1884 // Maps must have an even number of data items (key & value)
1885 // Map with 1 item when it should have 2
1886 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1887 // Map with 3 item when it should have 4
1888 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1889 // Map with 1 item when it should have 2
1890 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1891 // Map with 3 item when it should have 4
1892 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1893
1894
1895 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001896 // Text-based date, with an integer
1897 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1898 // Epoch date, with an byte string
1899 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1900 // tagged as both epoch and string dates
1901 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1902 // big num tagged an int, not a byte string
1903 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001904};
1905
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001906int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001907{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001908 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001909
Laurence Lundblade59289e52019-12-30 13:44:37 -08001910 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1911 if(nResult) {
1912 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001913 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001914
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001915 // Corrupt the UsefulInputBuf and see that
1916 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001917 {
1918 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001919 QCBORItem Item;
1920 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001921
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001922 QCBORDecode_Init(&DCtx,
1923 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1924 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001925
1926 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07001927 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001928 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001929 Item.val.uCount != 10) {
1930 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001931 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001932 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001933
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001934 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001935
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001936 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001937 if(nCBORError != QCBOR_ERR_HIT_END) {
1938 // Did not get back the error expected
1939 return -2;
1940 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001941 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001942
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001943/*
1944 This test is disabled until QCBOREncode_EncodeHead() is brought in so
1945 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
1946
1947 This relies on the largest string allowed being SIZE_MAX -4 rather than
1948 SIZE_MAX. That way the test can be performed.
1949 {
1950 QCBORDecodeContext DCtx;
1951 QCBORItem Item;
1952
1953 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
1954 0xff, 0xff, 0xff, 0xff};
1955
1956 QCBORDecode_Init(&DCtx,
1957 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
1958 QCBOR_DECODE_MODE_NORMAL);
1959
1960 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
1961 return -4;
1962 }
1963 }
1964*/
1965
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001966 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001967}
1968
1969
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001970/* Try all 256 values of the byte at nLen including recursing for
1971 each of the values to try values at nLen+1 ... up to nLenMax
1972 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001973static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001974{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001975 if(nLen >= nLenMax) {
1976 return;
1977 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001978
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001979 for(int inputByte = 0; inputByte < 256; inputByte++) {
1980 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001981 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001982 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001983
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001984 // Get ready to parse
1985 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001986 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001987
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001988 // Parse by getting the next item until an error occurs
1989 // Just about every possible decoder error can occur here
1990 // The goal of this test is not to check for the correct
1991 // error since that is not really possible. It is to
1992 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001993 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001994 QCBORItem Item;
1995 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001996 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001997 break;
1998 }
1999 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002000
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002001 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002002 }
2003}
2004
2005
2006/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002007 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002008 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002009int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002010{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002011 // Size 2 tests 64K inputs and runs quickly
2012 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002013
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002014 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002015
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002016 return 0;
2017}
2018
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002019
2020/*
2021 Public function for initialization. See header qcbor.h
2022 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002023int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002024{
2025 // size 3 tests 16 million inputs and runs OK
2026 // in seconds on fast machines. Size 4 takes
2027 // 10+ minutes and 5 half a day on fast
2028 // machines. This test is kept separate from
2029 // the others so as to no slow down the use
2030 // of them as a very frequent regression.
2031 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002032
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002033 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002034
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002035 return 0;
2036}
2037
2038
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002039static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002040 0xc0, // tag for string date
2041 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002042
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002043 0xc1, // tag for epoch date
2044 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2045
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002046 // CBOR_TAG_B64
2047 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002048 0x1a, 0x53, 0x72, 0x4E, 0x01,
2049
2050 0xc1, // tag for epoch date
2051 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002052
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002053 0xc1, // tag for epoch date
2054 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002055
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002056 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002057 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002058
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002059 0xc1, // tag for epoch date
2060 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2061 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2062
2063 0xc1, // tag for epoch date
2064 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002065};
2066
2067
2068// have to check float expected only to within an epsilon
2069int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002071 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002072
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002073 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002074
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002075 return diff > 0.0000001;
2076}
2077
2078
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002079int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080{
2081 QCBORDecodeContext DCtx;
2082 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -07002083 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002084
Laurence Lundbladeee851742020-01-08 08:37:05 -08002085 QCBORDecode_Init(&DCtx,
2086 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2087 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002088
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002089 const uint64_t uTags[] = {15};
2090 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002091
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002092 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002093
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002094 // String date
2095 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2096 return -1;
2097 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002098 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002099 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002100 }
2101
2102 // Epoch date
2103 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002104 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002105 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2106 Item.val.epochDate.nSeconds != 1400000000 ||
2107 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002108 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002109 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002110
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002111 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2112 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002113 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002114 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002115 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2116 Item.val.epochDate.nSeconds != 1400000001 ||
2117 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002118 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002119 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002121
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002122 // Epoch date that is too large for our representation
2123 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002124 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002126
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002127 // Epoch date in float format with fractional seconds
Laurence Lundblade9682a532020-06-06 18:33:04 -07002128#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002129 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002130 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2132 Item.val.epochDate.nSeconds != 1 ||
2133 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002134 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002135 }
Laurence Lundblade9682a532020-06-06 18:33:04 -07002136#else
2137 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_UNSUPPORTED)
2138 return -80;
2139#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002140
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002141 // Epoch date float that is too large for our representation
2142 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002143 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002145
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002146 // Epoch date double that is just slightly too large
2147 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2148 return -11;
2149 }
2150
2151 // Largest double epoch date supported
2152 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2153 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2154 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2155 Item.val.epochDate.nSeconds == 0) {
2156 return -12;
2157 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002158 // TODO: could use a few more tests with float, double, and half precsion
2159 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002160
2161 return 0;
2162}
2163
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002164// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002165static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002166 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002167 0x81, // Array of one
2168 0xd8, 0x04, // non-preferred serialization of tag 4
2169 0x82, 0x01, 0x03}; // fraction 1/3
2170
Laurence Lundblade59289e52019-12-30 13:44:37 -08002171/*
2172 DB 9192939495969798 # tag(10489608748473423768)
2173 80 # array(0)
2174 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002175static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2176 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002177
Laurence Lundblade59289e52019-12-30 13:44:37 -08002178/*
2179DB 9192939495969798 # tag(10489608748473423768)
2180 D8 88 # tag(136)
2181 C6 # tag(6)
2182 C7 # tag(7)
2183 80 # array(0)
2184*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002185static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2186 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002187
2188/*
2189 The cbor.me parse of this.
2190 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002191 9(-17): 773("SSG"), -15: 16(17(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002192 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2193 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2194 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2195 */
2196static uint8_t spCSRWithTags[] = {
2197 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2198 0xc6, 0xc7, 0x36,
2199 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2200 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2201 0xcb, 0xa5,
2202 0xd1, 0x31,
2203 0xd1, 0xd1, 0xd1, 0x6c,
2204 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2205 0xc9, 0x30,
2206 0xd9, 0x03, 0x05, 0x63,
2207 0x53, 0x53, 0x47,
2208 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002209 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002210 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2211 0xd1, 0x2f,
2212 0xd1, 0x69,
2213 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2214 0xd1, 0x2d,
2215 0xd1, 0x62,
2216 0x55, 0x53,
2217 0xd7, 0x32,
2218 0xd3, 0xa2,
2219 0x2a,
2220 0xc9, 0xa1,
2221 0x28,
2222 0x26,
2223 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2224 0xcc, 0x4a,
2225 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2226 0xd0, 0x35,
2227 0xd7, 0xa1,
2228 0xcb, 0xc8, 0xc7, 0x24,
2229 0xc8, 0x22};
2230
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002231static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002232
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002233
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002234int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002235{
2236 QCBORDecodeContext DCtx;
2237 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002238
Laurence Lundbladeee851742020-01-08 08:37:05 -08002239 QCBORDecode_Init(&DCtx,
2240 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2241 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002242
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002243 //-------------------------
2244 // This text matches the magic number tag and the fraction tag
2245 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2246 return -2;
2247 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002248 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002249 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2250 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002252
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002253 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2254 return -4;
2255 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002256
2257#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002258 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002259 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002260 Item.val.uCount != 2) {
2261 return -5;
2262 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002263#else
2264 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2265 return -6;
2266 }
2267#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002268
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002269 // --------------------------------
2270 // This test decodes the very large tag, but it is not in
2271 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002272 QCBORDecode_Init(&DCtx,
2273 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2274 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002275 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2276 return -6;
2277 }
2278 if(Item.uTagBits) {
2279 return -7;
2280 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002281
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002282 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002283 // This test sets up a caller-config list that includes the very large
2284 // tage and then matches it.
2285 QCBORDecode_Init(&DCtx,
2286 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2287 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002288 const uint64_t puList[] = {0x9192939495969798, 257};
2289 const QCBORTagListIn TL = {2, puList};
2290 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002291
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002292 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2293 return -8;
2294 }
2295 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2296 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2297 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2298 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2299 Item.val.uCount != 0) {
2300 return -9;
2301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002302
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002303 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002304 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002305 const uint64_t puLongList[17] = {1,2,1};
2306 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002307 QCBORDecode_Init(&DCtx,
2308 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2309 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002310 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2311 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2312 return -11;
2313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002314
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002315 // -----------------------
2316 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002317 QCBORDecode_Init(&DCtx,
2318 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2319 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002320 uint64_t puTags[16];
2321 QCBORTagListOut Out = {0, 4, puTags};
2322 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2323 return -12;
2324 }
2325 if(puTags[0] != 0x9192939495969798 ||
2326 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002327 puTags[2] != 0x06 ||
2328 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002329 return -13;
2330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002331
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002332 // ----------------------
2333 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002334 QCBORDecode_Init(&DCtx,
2335 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2336 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002337 QCBORTagListOut OutSmall = {0, 3, puTags};
2338 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2339 return -14;
2340 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002341
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002342 // ---------------
2343 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002344 QCBORDecode_Init(&DCtx,
2345 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2346 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002347 int n = CheckCSRMaps(&DCtx);
2348 if(n) {
2349 return n-2000;
2350 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002351
Laurence Lundblade59289e52019-12-30 13:44:37 -08002352 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002353 QCBORDecode_Init(&DCtx,
2354 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2355 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002356
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002357 const uint64_t puTagList[] = {773, 1, 90599561};
2358 const QCBORTagListIn TagList = {3, puTagList};
2359 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002360
2361
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002362 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2363 return -100;
2364 }
2365 if(Item.uDataType != QCBOR_TYPE_MAP ||
2366 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2367 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2368 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2369 Item.val.uCount != 2 ||
2370 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2371 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2372 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2373 Out.uNumUsed != 3) {
2374 return -101;
2375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002376
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002377 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2378 return -102;
2379 }
2380 if(Item.uDataType != QCBOR_TYPE_MAP ||
2381 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2382 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2383 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2384 Item.val.uCount != 2 ||
2385 puTags[0] != 5859837686836516696 ||
2386 puTags[1] != 7 ||
2387 Out.uNumUsed != 2) {
2388 return -103;
2389 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002390
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002391 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2392 return -104;
2393 }
2394 if(Item.uDataType != QCBOR_TYPE_MAP ||
2395 Item.uTagBits ||
2396 Item.val.uCount != 5 ||
2397 puTags[0] != 0x0b ||
2398 Out.uNumUsed != 1) {
2399 return -105;
2400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002401
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002402 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2403 return -106;
2404 }
2405 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2406 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2407 Item.val.string.len != 12 ||
2408 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2409 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2410 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2411 Out.uNumUsed != 3) {
2412 return -105;
2413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002414
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002415 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2416 return -107;
2417 }
2418 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2419 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2420 Item.val.string.len != 3 ||
2421 puTags[0] != 773 ||
2422 Out.uNumUsed != 1) {
2423 return -108;
2424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002425
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002426 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2427 return -109;
2428 }
2429 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002430 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002431 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002432 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002433 puTags[11] != 0x0f ||
2434 Out.uNumUsed != 12) {
2435 return -110;
2436 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002437
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002438 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2439 return -111;
2440 }
2441 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2442 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2443 Item.val.string.len != 9 ||
2444 puTags[0] != 17 ||
2445 Out.uNumUsed != 1) {
2446 return -112;
2447 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002448
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002449 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2450 return -111;
2451 }
2452 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2453 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2454 Item.val.string.len != 2 ||
2455 puTags[0] != 17 ||
2456 Out.uNumUsed != 1) {
2457 return -112;
2458 }
2459
2460 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2461 return -113;
2462 }
2463 if(Item.uDataType != QCBOR_TYPE_MAP ||
2464 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2465 Item.val.uCount != 2 ||
2466 puTags[0] != 19 ||
2467 Out.uNumUsed != 1) {
2468 return -114;
2469 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002470
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002471 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2472 return -115;
2473 }
2474 if(Item.uDataType != QCBOR_TYPE_MAP ||
2475 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2476 Item.uTagBits ||
2477 Item.val.uCount != 1 ||
2478 puTags[0] != 9 ||
2479 Out.uNumUsed != 1) {
2480 return -116;
2481 }
2482
2483 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2484 return -116;
2485 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002486 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002487 Item.val.int64 != -7 ||
2488 Item.uTagBits ||
2489 Out.uNumUsed != 0) {
2490 return -117;
2491 }
2492
2493 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2494 return -118;
2495 }
2496 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2497 Item.val.string.len != 10 ||
2498 Item.uTagBits ||
2499 puTags[0] != 12 ||
2500 Out.uNumUsed != 1) {
2501 return -119;
2502 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002503
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002504 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2505 return -120;
2506 }
2507 if(Item.uDataType != QCBOR_TYPE_MAP ||
2508 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2509 Item.val.uCount != 1 ||
2510 puTags[0] != 0x17 ||
2511 Out.uNumUsed != 1) {
2512 return -121;
2513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002514
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002515 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2516 return -122;
2517 }
2518 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2519 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2520 Item.val.int64 != -3 ||
2521 puTags[0] != 8 ||
2522 Out.uNumUsed != 1) {
2523 return -123;
2524 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002525
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002526 if(QCBORDecode_Finish(&DCtx)) {
2527 return -124;
2528 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002530 return 0;
2531}
2532
2533
2534
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002535
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002536static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002537 0x83,
2538 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2539 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2540 0xA4,
2541 0x63, 0x42, 0x4E, 0x2B,
2542 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2543 0x18, 0x40,
2544 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2545 0x63, 0x42, 0x4E, 0x2D,
2546 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2547 0x38, 0x3F,
2548 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2549
2550
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002551static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002552
2553
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002554int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002555{
2556 QCBORDecodeContext DCtx;
2557 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -07002558 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002559
Laurence Lundbladeee851742020-01-08 08:37:05 -08002560 QCBORDecode_Init(&DCtx,
2561 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2562 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002563
2564
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002565 //
2566 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2567 return -1;
2568 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2569 return -1;
2570 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002571
2572 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002573 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2574 return -1;
2575 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002576 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002577 return -1;
2578 }
2579
2580 //
2581 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2582 return -1;
2583 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002584 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002585 return -1;
2586 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002588 //
2589 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2590 return -1;
2591 if(Item.uDataType != QCBOR_TYPE_MAP) {
2592 return -1;
2593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002594
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002595 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2596 return -1;
2597 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2598 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002599 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002600 return -1;
2601 }
2602
2603 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2604 return -1;
2605 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2606 Item.uLabelType != QCBOR_TYPE_INT64 ||
2607 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002608 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002609 return -1;
2610 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002612 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2613 return -1;
2614 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2615 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002616 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002617 return -1;
2618 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002620 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2621 return -1;
2622 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2623 Item.uLabelType != QCBOR_TYPE_INT64 ||
2624 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002625 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002626 return -1;
2627 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002628
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002629 return 0;
2630}
2631
2632
2633
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002634static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002635 uint8_t uDataType,
2636 uint8_t uNestingLevel,
2637 uint8_t uNextNest,
2638 int64_t nLabel,
2639 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002640{
2641 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -07002642 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002644 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2645 if(Item.uDataType != uDataType) return -1;
2646 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002647 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2648 Item.uLabelType != QCBOR_TYPE_UINT64) {
2649 return -1;
2650 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002651 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2652 if(Item.label.int64 != nLabel) return -1;
2653 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002654 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002655 }
2656 }
2657 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302658 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002659
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002660 if(pItem) {
2661 *pItem = Item;
2662 }
2663 return 0;
2664}
2665
2666
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002667// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002668static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002669{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302670 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002671
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302672 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002673
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302674 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002675
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302676 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2677 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2678 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2679 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2680 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002681
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302682 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2683 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002684
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302685 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2686 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002687
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302688 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2689 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002690
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002691 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002692
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002693 return 0;
2694}
2695
2696
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002697/*
2698// cbor.me decoded output
2699{
2700 -23: {
2701 -20: {
2702 -18: "Organization",
2703 -17: "SSG",
2704 -15: "Confusion",
2705 -16: "San Diego",
2706 -14: "US"
2707 },
2708 -19: {
2709 -11: {
2710 -9: -7
2711 },
2712 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2713 }
2714 },
2715 -22: {
2716 -5: -3
2717 }
2718}
2719 */
2720
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002721
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002722static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002723 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2724 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2725 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2726 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2727 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2728 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2729 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2730 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2731 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2732
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002733int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002734{
2735 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002736
Laurence Lundbladeee851742020-01-08 08:37:05 -08002737 QCBORDecode_Init(&DCtx,
2738 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2739 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002740
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002741 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002742}
2743
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002744
2745
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002746int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002747{
2748 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002749
Laurence Lundbladeee851742020-01-08 08:37:05 -08002750 QCBORDecode_Init(&DCtx,
2751 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2752 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002753
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002754 QCBORItem Item;
2755 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002756
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002757 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2758 return -1;
2759 }
2760 if(Item.uDataType != QCBOR_TYPE_MAP) {
2761 return -2;
2762 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002763
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002764 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2765 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2766 return -3;
2767 }
2768
2769 return 0;
2770}
2771
2772
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002773// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002774static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002775 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2776 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2777 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2778 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2779 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2780 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002781 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2782 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2783 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2784 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002785
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002786int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002787{
2788 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002789
Laurence Lundbladeee851742020-01-08 08:37:05 -08002790 QCBORDecode_Init(&DCtx,
2791 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2792 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002793
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002794 return CheckCSRMaps(&DCtx);
2795}
2796
2797
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002798
Laurence Lundblade17ede402018-10-13 11:43:07 +08002799static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2800{
2801 UsefulOutBuf UOB;
2802 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002803
Laurence Lundblade17ede402018-10-13 11:43:07 +08002804 int i;
2805 for(i = 0; i < n; i++) {
2806 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2807 }
2808
2809 for(i = 0; i < n; i++) {
2810 UsefulOutBuf_AppendByte(&UOB, 0xff);
2811 }
2812 return UsefulOutBuf_OutUBuf(&UOB);
2813}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002814
2815
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002816static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002817{
2818 QCBORDecodeContext DC;
2819 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002820
Laurence Lundblade17ede402018-10-13 11:43:07 +08002821 int j;
2822 for(j = 0; j < nNestLevel; j++) {
2823 QCBORItem Item;
Laurence Lundblade29497c02020-07-11 15:44:03 -07002824 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002825 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2826 // Should be in error
2827 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2828 return -4;
2829 } else {
2830 return 0; // Decoding doesn't recover after an error
2831 }
2832 } else {
2833 // Should be no error
2834 if(nReturn) {
2835 return -9; // Should not have got an error
2836 }
2837 }
2838 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2839 return -7;
2840 }
2841 }
Laurence Lundblade29497c02020-07-11 15:44:03 -07002842 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002843 if(nReturn) {
2844 return -3;
2845 }
2846 return 0;
2847}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002848
2849
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002850int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002851{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302852 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002853 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002854 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002855 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002856 int nReturn = parse_indeflen_nested(Nested, i);
2857 if(nReturn) {
2858 return nReturn;
2859 }
2860 }
2861 return 0;
2862}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002863
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002864
Laurence Lundbladeee851742020-01-08 08:37:05 -08002865// [1, [2, 3]]
2866static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2867// No closing break
2868static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2869// Not enough closing breaks
2870static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2871// Too many closing breaks
2872static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2873// Unclosed indeflen inside def len
2874static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2875// confused tag
2876static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002877
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002878int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002879{
Laurence Lundblade29497c02020-07-11 15:44:03 -07002880 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002881 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002882 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002883
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002884 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302885 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002886 QCBORDecodeContext DC;
2887 QCBORItem Item;
2888 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002889
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002890 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002891
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002892 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302893
2894 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2895 Item.uNestingLevel != 0 ||
2896 Item.uNextNestLevel != 1) {
2897 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002898 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002899
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002900 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302901 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2902 Item.uNestingLevel != 1 ||
2903 Item.uNextNestLevel != 1) {
2904 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002906
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002907 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302908 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2909 Item.uNestingLevel != 1 ||
2910 Item.uNextNestLevel != 2) {
2911 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002912 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002913
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002914 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002915 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302916 Item.uNestingLevel != 2 ||
2917 Item.uNextNestLevel != 2) {
2918 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002920
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002921 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002922 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302923 Item.uNestingLevel != 2 ||
2924 Item.uNextNestLevel != 0) {
2925 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002926 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002927
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002928 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302929 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002930 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002931
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002932 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002933 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002934
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002935 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002936
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002937 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002938
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002939 nResult = QCBORDecode_GetNext(&DC, &Item);
2940 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302941 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002943
Laurence Lundblade570fab52018-10-13 18:28:27 +08002944 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302945 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2946 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002947 }
2948
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002949
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002950 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002951 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002952
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002953 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002954
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002955 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002956
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002957 nResult = QCBORDecode_GetNext(&DC, &Item);
2958 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302959 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002960 }
2961
2962 nResult = QCBORDecode_GetNext(&DC, &Item);
2963 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302964 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002965 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002966
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002967 nResult = QCBORDecode_GetNext(&DC, &Item);
2968 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302969 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002970 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002971
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002972 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302973 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2974 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002975 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002976
2977
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002978 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002979 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002980
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002981 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002982
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002983 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002984
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002985 nResult = QCBORDecode_GetNext(&DC, &Item);
2986 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302987 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002988 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002989
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002990 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302991 if(nResult != QCBOR_ERR_BAD_BREAK) {
2992 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002993 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302994
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002995
Laurence Lundblade570fab52018-10-13 18:28:27 +08002996 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002997 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002998
Laurence Lundblade570fab52018-10-13 18:28:27 +08002999 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003000
Laurence Lundblade570fab52018-10-13 18:28:27 +08003001 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003002
Laurence Lundblade570fab52018-10-13 18:28:27 +08003003 nResult = QCBORDecode_GetNext(&DC, &Item);
3004 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303005 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003007
Laurence Lundblade570fab52018-10-13 18:28:27 +08003008 nResult = QCBORDecode_GetNext(&DC, &Item);
3009 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303010 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003011 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003012
Laurence Lundblade570fab52018-10-13 18:28:27 +08003013 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303014 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3015 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003017
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303018 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003019 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003020
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303021 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003022
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303023 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003024
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303025 nResult = QCBORDecode_GetNext(&DC, &Item);
3026 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303027 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303028 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003029
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303030 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303031 if(nResult != QCBOR_ERR_BAD_BREAK) {
3032 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003034
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003035 return 0;
3036}
3037
Laurence Lundblade17ede402018-10-13 11:43:07 +08003038
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003039static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003040 0x81, // Array of length one
3041 0x7f, // text string marked with indefinite length
3042 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3043 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3044 0xff // ending break
3045};
3046
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003047static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303048 0x81, // Array of length one
3049 0x7f, // text string marked with indefinite length
3050 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3051 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3052 0xff // ending break
3053};
3054
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003055static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303056 0x81, // Array of length one
3057 0x7f, // text string marked with indefinite length
3058 0x01, 0x02, // Not a string
3059 0xff // ending break
3060};
3061
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003062static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303063 0x81, // Array of length one
3064 0x7f, // text string marked with indefinite length
3065 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3066 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3067 // missing end of string
3068};
3069
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003070static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303071 0xa1, // Array of length one
3072 0x7f, // text string marked with indefinite length
3073 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3074 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3075 0xff, // ending break
3076 0x01 // integer being labeled.
3077};
3078
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003079/**
3080 Make an indefinite length string
3081
3082 @param Storage Storage for string, must be 144 bytes in size
3083 @return The indefinite length string
3084
3085 This makes an array with one indefinite length string that has 7 chunks
3086 from size of 1 byte up to 64 bytes.
3087 */
3088static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303089{
3090 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003091
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303092 UsefulOutBuf_Init(&UOB, Storage);
3093 UsefulOutBuf_AppendByte(&UOB, 0x81);
3094 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003095
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003096 uint8_t uStringByte = 0;
3097 // Use of type int is intentional
3098 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3099 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303100 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003101 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3102 for(int j = 0; j < uChunkSize; j++) {
3103 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3104 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303105 }
3106 }
3107 UsefulOutBuf_AppendByte(&UOB, 0xff);
3108
3109 return UsefulOutBuf_OutUBuf(&UOB);
3110}
3111
3112static int CheckBigString(UsefulBufC BigString)
3113{
3114 if(BigString.len != 255) {
3115 return 1;
3116 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003117
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303118 for(uint8_t i = 0; i < 255; i++){
3119 if(((const uint8_t *)BigString.ptr)[i] != i) {
3120 return 1;
3121 }
3122 }
3123 return 0;
3124}
3125
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303126
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003127int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303128{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303129 QCBORDecodeContext DC;
3130 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303131 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003132 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003133
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303134 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003135 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303136 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003137
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303138 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303139 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303140 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003141
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303142 if(QCBORDecode_GetNext(&DC, &Item)) {
3143 return -2;
3144 }
3145 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3146 return -3;
3147 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003148
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303149 if(QCBORDecode_GetNext(&DC, &Item)) {
3150 return -4;
3151 }
3152 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3153 return -5;
3154 }
3155 if(QCBORDecode_Finish(&DC)) {
3156 return -6;
3157 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303158
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303159 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003160 QCBORDecode_Init(&DC,
3161 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3162 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003163
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303164 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3165 return -7;
3166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003167
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303168 if(QCBORDecode_GetNext(&DC, &Item)) {
3169 return -8;
3170 }
3171 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3172 return -9;
3173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003174
Laurence Lundblade30816f22018-11-10 13:40:22 +07003175 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303176 return -10;
3177 }
3178
3179 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003180 QCBORDecode_Init(&DC,
3181 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3182 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003183
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303184 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3185 return -11;
3186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003187
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303188 if(QCBORDecode_GetNext(&DC, &Item)) {
3189 return -12;
3190 }
3191 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3192 return -13;
3193 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003194
Laurence Lundblade30816f22018-11-10 13:40:22 +07003195 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303196 return -14;
3197 }
3198
3199 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003200 QCBORDecode_Init(&DC,
3201 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3202 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003203
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303204 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3205 return -15;
3206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003207
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303208 if(QCBORDecode_GetNext(&DC, &Item)) {
3209 return -16;
3210 }
3211 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3212 return -17;
3213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003214
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303215 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3216 return -18;
3217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003218
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303219 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303220 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003221
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303222 QCBORDecode_GetNext(&DC, &Item);
3223 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303224 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003226
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303227 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303228 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003230
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303231 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003232 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303233
3234 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3235 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303236 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303237 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003238
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303239 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303240 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003241 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003242
Laurence Lundbladeee851742020-01-08 08:37:05 -08003243 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3244 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003245
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303246 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303247 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303248 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003250
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303251 QCBORDecode_GetNext(&DC, &Item);
3252 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303253 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303254 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003255 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303256 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003258
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303259 // ---- big bstr -----
3260 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003261
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303262 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3263 return -25;
3264 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003265
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303266 if(QCBORDecode_GetNext(&DC, &Item)) {
3267 return -26;
3268 }
3269 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303270 return -26;
3271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003272
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303273 if(QCBORDecode_GetNext(&DC, &Item)) {
3274 return -27;
3275 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303276 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303277 return -28;
3278 }
3279 if(CheckBigString(Item.val.string)) {
3280 return -3;
3281 }
3282 if(QCBORDecode_Finish(&DC)) {
3283 return -29;
3284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003285
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303286 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003287 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003288
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303289 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3290 return -30;
3291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003292
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303293 QCBORDecode_GetNext(&DC, &Item);
3294 if(Item.uDataType != QCBOR_TYPE_MAP) {
3295 return -31;
3296 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003297
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303298 if(QCBORDecode_GetNext(&DC, &Item)){
3299 return -32;
3300 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003301 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3302 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303303 Item.uDataAlloc || !Item.uLabelAlloc ||
3304 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3305 return -33;
3306 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003307
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303308 if(QCBORDecode_Finish(&DC)) {
3309 return -34;
3310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003311
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003312 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003313}
3314
3315
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003316int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303317{
3318 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003319 QCBORError nCBORError;
3320
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003321
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303322 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003323 QCBORDecode_Init(&DC,
3324 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3325 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003326
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003327 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003328
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003329 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3330 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303331 return -1;
3332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003333
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003334 if(CheckCSRMaps(&DC)) {
3335 return -2;
3336 }
3337
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303338 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003339 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003340 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003341
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303342 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003343 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303344 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003345
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303346 QCBORItem Item1, Item2, Item3, Item4;
3347 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07003348 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303349 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3350 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003351 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303352 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07003353 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303354 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07003355 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303356 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07003357 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303358 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundblade29497c02020-07-11 15:44:03 -07003359 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003360
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303361 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003362
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303363 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303364 Item1.uDataType != QCBOR_TYPE_INT64 ||
3365 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003366 Item1.uDataAlloc != 0 ||
3367 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003368 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003369 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003370 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003371
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303372
3373 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003374 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303375 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003376 Item2.uDataAlloc != 0 ||
3377 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303378 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003379 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003380
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303381 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003382 Item3.uDataAlloc == 0 ||
3383 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003384 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003385 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003387
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303388 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003389 Item4.uDataAlloc == 0 ||
3390 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003391 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003392 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003393 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003394
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303395 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003396 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003397 QCBORDecode_Init(&DC,
3398 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3399 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303400 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3401 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003402 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303403 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003404 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003405 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003406 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303407 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3408 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3409 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3410 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3411 }
3412 }
3413 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003414 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003415 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303416 }
3417
3418 return 0;
3419}
3420
Laurence Lundbladef6531662018-12-04 10:42:22 +09003421
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303422
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003423int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003424{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003425 // Set up the decoder with a tiny bit of CBOR to parse because
3426 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003427 QCBORDecodeContext DC;
3428 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3429 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003430
Laurence Lundbladef6531662018-12-04 10:42:22 +09003431 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003432 // Then fish into the internals of the decode context
3433 // to get the allocator function so it can be called directly.
3434 // Also figure out how much pool is available for use
3435 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003436 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003437 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3438 if(nError) {
3439 return -9;
3440 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003441 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3442 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3443 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003444
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003445 // First test -- ask for one more byte than available and see failure
3446 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003447 if(!UsefulBuf_IsNULL(Allocated)) {
3448 return -1;
3449 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003450
Laurence Lundbladef6531662018-12-04 10:42:22 +09003451 // Re do the set up for the next test that will do a successful alloc,
3452 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003453 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003454 pAlloc = DC.StringAllocator.pfAllocator;
3455 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3456 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003457
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003458 // Allocate one byte less than available and see success
3459 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003460 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3461 return -2;
3462 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003463 // Ask for some more and see failure
3464 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003465 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3466 return -3;
3467 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003468 // Free the first allocate, retry the second and see success
3469 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3470 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003471 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3472 return -4;
3473 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003474
Laurence Lundbladef6531662018-12-04 10:42:22 +09003475 // Re do set up for next test that involves a successful alloc,
3476 // and a successful realloc and a failed realloc
3477 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003478 pAlloc = DC.StringAllocator.pfAllocator;
3479 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003480
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003481 // Allocate half the pool and see success
3482 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003483 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3484 return -5;
3485 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003486 // Reallocate to take up the whole pool and see success
3487 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003488 if(UsefulBuf_IsNULL(Allocated2)) {
3489 return -6;
3490 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003491 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003492 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3493 return -7;
3494 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003495 // Try to allocate more to be sure there is failure after a realloc
3496 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3497 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003498 return -8;
3499 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003500
Laurence Lundbladef6531662018-12-04 10:42:22 +09003501 return 0;
3502}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003503
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003504
3505/* Just enough of an allocator to test configuration of one */
3506static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3507{
3508 (void)pOldMem; // unused variable
3509
3510 if(uNewSize) {
3511 // Assumes the context pointer is the buffer and
3512 // nothing too big will ever be asked for.
3513 // This is only good for this basic test!
3514 return (UsefulBuf) {pCtx, uNewSize};
3515 } else {
3516 return NULLUsefulBuf;
3517 }
3518}
3519
3520
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003521int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003522{
3523 // Set up the decoder with a tiny bit of CBOR to parse because
3524 // nothing can be done with it unless that is set up.
3525 QCBORDecodeContext DC;
3526 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3527 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3528
3529 uint8_t pAllocatorBuffer[50];
3530
3531 // This is really just to test that this call works.
3532 // The full functionality of string allocators is tested
3533 // elsewhere with the MemPool internal allocator.
3534 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3535
3536 QCBORItem Item;
3537 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3538 return -1;
3539 }
3540
3541 if(Item.uDataAlloc == 0 ||
3542 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3543 Item.val.string.ptr != pAllocatorBuffer) {
3544 return -2;
3545 }
3546
3547 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3548 return -3;
3549 }
3550
3551 return 0;
3552}
3553
Laurence Lundblade59289e52019-12-30 13:44:37 -08003554
3555#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3556/*
3557 [
3558 4([-1, 3]),
3559 4([-20, 4759477275222530853136]),
3560 4([9223372036854775807, -4759477275222530853137]),
3561 5([300, 100]),
3562 5([-20, 4759477275222530853136]),
3563 5([-9223372036854775807, -4759477275222530853137])
3564 5([9223372036854775806, -4759477275222530853137])
3565 5([9223372036854775806, 9223372036854775806])]
3566 ]
3567 */
3568
3569static const uint8_t spExpectedExponentsAndMantissas[] = {
3570 0x87,
3571 0xC4, 0x82, 0x20,
3572 0x03,
3573 0xC4, 0x82, 0x33,
3574 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3575 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3576 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3577 0xC5, 0x82, 0x19, 0x01, 0x2C,
3578 0x18, 0x64,
3579 0xC5, 0x82, 0x33,
3580 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3581 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3582 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3583 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3584 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3585};
3586
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003587int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003588{
3589 QCBORDecodeContext DC;
3590 QCBORError nCBORError;
3591 QCBORItem item;
3592
Laurence Lundblade17af4902020-01-07 19:11:55 -08003593 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3594 0x06, 0x07, 0x08, 0x09, 0x010};
3595 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003596
3597
3598 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3599
3600 nCBORError = QCBORDecode_GetNext(&DC, &item);
3601 if(nCBORError != QCBOR_SUCCESS) {
3602 return 1;
3603 }
3604
3605 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3606 return 2;
3607 }
3608
3609 nCBORError = QCBORDecode_GetNext(&DC, &item);
3610 if(nCBORError != QCBOR_SUCCESS) {
3611 return 3;
3612 }
3613
3614 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3615 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3616 item.val.expAndMantissa.nExponent != -1) {
3617 return 4;
3618 }
3619
3620 nCBORError = QCBORDecode_GetNext(&DC, &item);
3621 if(nCBORError != QCBOR_SUCCESS) {
3622 return 5;
3623 }
3624
3625 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3626 item.val.expAndMantissa.nExponent != -20 ||
3627 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3628 return 6;
3629 }
3630
3631 nCBORError = QCBORDecode_GetNext(&DC, &item);
3632 if(nCBORError != QCBOR_SUCCESS) {
3633 return 7;
3634 }
3635
3636 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3637 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3638 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3639 return 8;
3640 }
3641
3642 nCBORError = QCBORDecode_GetNext(&DC, &item);
3643 if(nCBORError != QCBOR_SUCCESS) {
3644 return 9;
3645 }
3646
3647 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3648 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3649 item.val.expAndMantissa.nExponent != 300) {
3650 return 10;
3651 }
3652
3653 nCBORError = QCBORDecode_GetNext(&DC, &item);
3654 if(nCBORError != QCBOR_SUCCESS) {
3655 return 11;
3656 }
3657
3658 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3659 item.val.expAndMantissa.nExponent != -20 ||
3660 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3661 return 12;
3662 }
3663
3664 nCBORError = QCBORDecode_GetNext(&DC, &item);
3665 if(nCBORError != QCBOR_SUCCESS) {
3666 return 13;
3667 }
3668
3669 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3670 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3671 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3672 return 14;
3673 }
3674
3675 nCBORError = QCBORDecode_GetNext(&DC, &item);
3676 if(nCBORError != QCBOR_SUCCESS) {
3677 return 15;
3678 }
3679
3680 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3681 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3682 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3683 return 16;
3684 }
3685
3686 /* Now encode some stuff and then decode it */
3687 uint8_t pBuf[40];
3688 QCBOREncodeContext EC;
3689 UsefulBufC Encoded;
3690
3691 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3692 QCBOREncode_OpenArray(&EC);
3693 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3694 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3695 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3696 QCBOREncode_CloseArray(&EC);
3697 QCBOREncode_Finish(&EC, &Encoded);
3698
3699
3700 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3701 nCBORError = QCBORDecode_GetNext(&DC, &item);
3702 if(nCBORError != QCBOR_SUCCESS) {
3703 return 13;
3704 }
3705
3706 nCBORError = QCBORDecode_GetNext(&DC, &item);
3707 if(nCBORError != QCBOR_SUCCESS) {
3708 return 13;
3709 }
3710
3711 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3712 item.val.expAndMantissa.nExponent != 1000 ||
3713 item.val.expAndMantissa.Mantissa.nInt != 999) {
3714 return 15;
3715 }
3716
3717 nCBORError = QCBORDecode_GetNext(&DC, &item);
3718 if(nCBORError != QCBOR_SUCCESS) {
3719 return 13;
3720 }
3721
3722 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3723 item.val.expAndMantissa.nExponent != INT32_MIN ||
3724 item.val.expAndMantissa.Mantissa.nInt != 100) {
3725 return 15;
3726 }
3727
3728 nCBORError = QCBORDecode_GetNext(&DC, &item);
3729 if(nCBORError != QCBOR_SUCCESS) {
3730 return 13;
3731 }
3732
3733 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3734 item.val.expAndMantissa.nExponent != INT32_MAX ||
3735 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3736 return 12;
3737 }
3738
3739 return 0;
3740}
3741
3742
3743static struct FailInput ExponentAndMantissaFailures[] = {
3744 // Exponent > INT64_MAX
3745 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3746 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3747 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3748 // Mantissa > INT64_MAX
3749 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3750 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3751 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3752 // End of input
3753 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END},
3754 // End of input
3755 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END},
3756 // bad content for big num
3757 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3758 // bad content for big num
3759 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3760 // Bad integer for exponent
3761 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3762 // Bad integer for mantissa
3763 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3764 // 3 items in array
3765 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3766 // unterminated indefinite length array
3767 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3768 // Empty array
3769 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3770 // Second is not an integer
3771 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3772 // First is not an integer
3773 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3774 // Not an array
3775 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3776};
3777
3778
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003779int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003780{
3781 return ProcessFailures(ExponentAndMantissaFailures,
3782 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3783}
3784
3785#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */