blob: 569b0bf095c7c54df321a89a1eb34f63fa8593db [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070036#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080037#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080038#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070039#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080040
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080041
Laurence Lundbladea2e29072018-12-30 09:20:06 -080042#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080043#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080044
45static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080046{
47 if(szLabel) {
48 printf("%s ", szLabel);
49 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080050
Laurence Lundblade570fab52018-10-13 18:28:27 +080051 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080052 for(i = 0; i < Buf.len; i++) {
53 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080054 printf("%02x ", Z);
55 }
56 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080057
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058 fflush(stdout);
59}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080060#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061
62
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070063static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
66 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
67 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
68 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
69 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
70 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
71 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
72 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
73 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
74 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
75 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
76 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
77 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
78 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
79 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
80 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
81 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
82 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
83 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
84 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
85 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff};
88
89
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080090// return CBOR error or -1 if type of value doesn't match
91
Laurence Lundbladec5fef682020-01-25 11:38:45 -080092static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080093{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070094 QCBORItem Item;
95 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080096
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080097 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070098 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080099 if(Item.uDataType != QCBOR_TYPE_ARRAY)
100 return -1;
101
102 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700103 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800104 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800105 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800106 return -1;
107
108 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700109 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800110 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800111 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800112 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800113
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700115 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800117 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800118 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700121 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800123 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800124 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800125
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700127 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800128 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800129 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800130 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800131
132
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700134 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 if(Item.uDataType != QCBOR_TYPE_INT64 ||
136 Item.val.int64 != -2147483648)
137 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800138
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800139 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700140 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800141 if(Item.uDataType != QCBOR_TYPE_INT64 ||
142 Item.val.int64 != -2147483647)
143 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800144
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800145 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700146 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147 if(Item.uDataType != QCBOR_TYPE_INT64 ||
148 Item.val.int64 != -65538)
149 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800150
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800151 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700152 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 if(Item.uDataType != QCBOR_TYPE_INT64 ||
154 Item.val.int64 != -65537)
155 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800157 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700158 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800159 if(Item.uDataType != QCBOR_TYPE_INT64 ||
160 Item.val.int64 != -65536)
161 return -1;
162
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800163
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700165 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166 if(Item.uDataType != QCBOR_TYPE_INT64 ||
167 Item.val.int64 != -65535)
168 return -1;
169
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800170
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700172 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800173 if(Item.uDataType != QCBOR_TYPE_INT64 ||
174 Item.val.int64 != -65534)
175 return -1;
176
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800177
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800178 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700179 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800180 if(Item.uDataType != QCBOR_TYPE_INT64 ||
181 Item.val.int64 != -257)
182 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800183
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800184 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700185 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800186 if(Item.uDataType != QCBOR_TYPE_INT64 ||
187 Item.val.int64 != -256)
188 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800189
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800190 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700191 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800192 if(Item.uDataType != QCBOR_TYPE_INT64 ||
193 Item.val.int64 != -255)
194 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800195
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800196 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700197 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800198 if(Item.uDataType != QCBOR_TYPE_INT64 ||
199 Item.val.int64 != -254)
200 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800201
202
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800203 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700204 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800205 if(Item.uDataType != QCBOR_TYPE_INT64 ||
206 Item.val.int64 != -25)
207 return -1;
208
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800209
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800210 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700211 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if(Item.uDataType != QCBOR_TYPE_INT64 ||
213 Item.val.int64 != -24)
214 return -1;
215
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800216
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800217 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700218 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800219 if(Item.uDataType != QCBOR_TYPE_INT64 ||
220 Item.val.int64 != -23)
221 return -1;
222
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800223
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700225 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 if(Item.uDataType != QCBOR_TYPE_INT64 ||
227 Item.val.int64 != -1)
228 return -1;
229
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800230
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800231 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700232 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800233 if(Item.uDataType != QCBOR_TYPE_INT64 ||
234 Item.val.int64 != 0)
235 return -1;
236
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800237
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700239 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800240 if(Item.uDataType != QCBOR_TYPE_INT64 ||
241 Item.val.int64 != 0)
242 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800243
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800244 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700245 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800246 if(Item.uDataType != QCBOR_TYPE_INT64 ||
247 Item.val.int64 != 1)
248 return -1;
249
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800250
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700252 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 if(Item.uDataType != QCBOR_TYPE_INT64 ||
254 Item.val.int64 != 22)
255 return -1;
256
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800257
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700259 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800260 if(Item.uDataType != QCBOR_TYPE_INT64 ||
261 Item.val.int64 != 23)
262 return -1;
263
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800264
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700266 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(Item.uDataType != QCBOR_TYPE_INT64 ||
268 Item.val.int64 != 24)
269 return -1;
270
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800271
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800272 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700273 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(Item.uDataType != QCBOR_TYPE_INT64 ||
275 Item.val.int64 != 25)
276 return -1;
277
278 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700279 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800280 if(Item.uDataType != QCBOR_TYPE_INT64 ||
281 Item.val.int64 != 26)
282 return -1;
283
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800284
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800285 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700286 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800287 if(Item.uDataType != QCBOR_TYPE_INT64 ||
288 Item.val.int64 != 254)
289 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800290
291
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800292 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700293 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800294 if(Item.uDataType != QCBOR_TYPE_INT64 ||
295 Item.val.int64 != 255)
296 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800297
298
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800299 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700300 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800301 if(Item.uDataType != QCBOR_TYPE_INT64 ||
302 Item.val.int64 != 256)
303 return -1;
304
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800305
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700307 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308 if(Item.uDataType != QCBOR_TYPE_INT64 ||
309 Item.val.int64 != 257)
310 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800311
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800312 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700313 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800314 if(Item.uDataType != QCBOR_TYPE_INT64 ||
315 Item.val.int64 != 65534)
316 return -1;
317
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800318
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800319 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700320 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800321 if(Item.uDataType != QCBOR_TYPE_INT64 ||
322 Item.val.int64 != 65535)
323 return -1;
324
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800325
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800326 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700327 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800328 if(Item.uDataType != QCBOR_TYPE_INT64 ||
329 Item.val.int64 != 65536)
330 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800331
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800332 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700333 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800334 if(Item.uDataType != QCBOR_TYPE_INT64 ||
335 Item.val.int64 != 65537)
336 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800337
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800338 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700339 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800340 if(Item.uDataType != QCBOR_TYPE_INT64 ||
341 Item.val.int64 != 65538)
342 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800343
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800344 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700345 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800346 if(Item.uDataType != QCBOR_TYPE_INT64 ||
347 Item.val.int64 != 2147483647)
348 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800349
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800350 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700351 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800352 if(Item.uDataType != QCBOR_TYPE_INT64 ||
353 Item.val.int64 != 2147483647)
354 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800356 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700357 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 if(Item.uDataType != QCBOR_TYPE_INT64 ||
359 Item.val.int64 != 2147483648)
360 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800361
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800362 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700363 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800364 if(Item.uDataType != QCBOR_TYPE_INT64 ||
365 Item.val.int64 != 2147483649)
366 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800367
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800368 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700369 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 if(Item.uDataType != QCBOR_TYPE_INT64 ||
371 Item.val.int64 != 4294967294)
372 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800373
374
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700375 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
376 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 if(Item.uDataType != QCBOR_TYPE_INT64 ||
378 Item.val.int64 != 4294967295)
379 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800380
381
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800382 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700383 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800384 if(Item.uDataType != QCBOR_TYPE_INT64 ||
385 Item.val.int64 != 4294967296)
386 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800387
388
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800389 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700390 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800391 if(Item.uDataType != QCBOR_TYPE_INT64 ||
392 Item.val.int64 != 4294967297)
393 return -1;
394
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800395
396
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800397 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700398 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(Item.uDataType != QCBOR_TYPE_INT64 ||
400 Item.val.int64 != 9223372036854775807LL)
401 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800402
403
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800404 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700405 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800406 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
407 Item.val.uint64 != 18446744073709551615ULL)
408 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800409
410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
412 return -1;
413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800414
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800415 return 0;
416}
417
418
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800419// One less than the smallest negative integer allowed in C. Decoding
420// this should fail.
421static const uint8_t spTooSmallNegative[] = {
422 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000423};
424
425
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800426/*
427 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800428 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800430int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800431{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000432 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000435 QCBORDecode_Init(&DCtx,
436 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
437 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800438
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000439 // The really big test of all successes
440 nReturn = IntegerValuesParseTestInternal(&DCtx);
441 if(nReturn) {
442 return nReturn;
443 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800444
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000445 // The one large negative integer that can be parsed
446 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800447 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000448 QCBOR_DECODE_MODE_NORMAL);
449
450 QCBORItem item;
451 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
452 nReturn = -4000;
453 }
454
455 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800456}
457
458
459/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800460 Creates a simple CBOR array and returns it in *pEncoded. The array is
461 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800462
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800463 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800464
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800465 */
466
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800467static uint8_t spSimpleArrayBuffer[50];
468
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800469static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470{
471 QCBOREncodeContext ECtx;
472 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800473
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800474 *pEncoded = NULL;
475 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800476
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800478 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800479 // and last with the buffer to do the actual encoding
480 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700481 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 QCBOREncode_OpenArray(&ECtx);
483 QCBOREncode_AddInt64(&ECtx, nInt1);
484 QCBOREncode_AddInt64(&ECtx, nInt2);
485 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
486 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
487 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800488
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800489 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800490 goto Done;
491
492 if(*pEncoded != NULL) {
493 nReturn = 0;
494 goto Done;
495 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800496
497 // Use static buffer to avoid dependency on malloc()
498 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800499 goto Done;
500 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800501 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800502
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800503 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800504
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800505Done:
506 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800507}
508
509
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800510/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800511 Some basic CBOR with map and array used in a lot of tests.
512 The map labels are all strings
513
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800514 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900515 "an array of two strings": [
516 "string1", "string2"
517 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800518 "map in a map": {
519 "bytes 1": h'78787878',
520 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900521 "another int": 98,
522 "text 2": "lies, damn lies and statistics"
523 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800524 }
525 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800526static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700527 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
528 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
529 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
530 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
531 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
532 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
533 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
534 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
535 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
536 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
537 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
538 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
539 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
540 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
541 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700542 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800543
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700544// Same as above, but with indefinite lengths.
545static const uint8_t pValidMapIndefEncoded[] = {
5460xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5470x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5480x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5490x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5500x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5510x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5520x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5530x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5540x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5550x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5560x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5570x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5580x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5590x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5600x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5610x73, 0xff, 0xff};
562
563
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800564static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700565 size_t nLen,
566 int64_t *pInt1,
567 int64_t *pInt2,
568 const uint8_t **pBuf3,
569 size_t *pBuf3Len,
570 const uint8_t **pBuf4,
571 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800572{
573 QCBORDecodeContext DCtx;
574 QCBORItem Item;
575 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800576
Laurence Lundbladeee851742020-01-08 08:37:05 -0800577 QCBORDecode_Init(&DCtx,
578 (UsefulBufC){pEncoded, nLen},
579 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581 // Make sure the first thing is a map
582 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
583 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800585 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800586 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 goto Done;
588 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800590 // Second integer
591 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
592 goto Done;
593 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800594
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 // First string
596 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
597 goto Done;
598 *pBuf3 = Item.val.string.ptr;
599 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800601 // Second string
602 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
603 goto Done;
604 *pBuf4 = Item.val.string.ptr;
605 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609Done:
610 return(nReturn);
611}
612
613
614
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800615
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800616int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617{
618 uint8_t *pEncoded;
619 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800620
Laurence Lundblade5e390822019-01-06 12:35:01 -0800621 int64_t i1=0, i2=0;
622 size_t i3=0, i4=0;
623 const uint8_t *s3= (uint8_t *)"";
624 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800625
626
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800627 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
628 return(-1);
629 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800630
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800631 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800632
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800633 if(i1 != 23 ||
634 i2 != 6000 ||
635 i3 != 8 ||
636 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530637 memcmp("galactic", s3, 8) !=0 ||
638 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800639 return(-1);
640 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800642 return(0);
643}
644
645
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700646/*
647 [
648 0,
649 [],
650 [
651 [],
652 [
653 0
654 ],
655 {},
656 {
657 1: {},
658 2: {},
659 3: []
660 }
661 ]
662 ]
663 */
664static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
665 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
666
Laurence Lundblade02625d42020-06-25 14:41:41 -0700667/* Same as above, but with indefinte lengths */
668static uint8_t sEmptiesIndef[] = {
6690x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700670 0x00,
671 0x9F,
672 0xFF,
673 0x9F,
674 0x9F,
675 0xFF,
676 0x9F,
677 0x00,
678 0xFF,
679 0xBF,
680 0xFF,
681 0xBF,
682 0x01,
683 0xBF,
684 0xFF,
685 0x02,
686 0xBF,
687 0xFF,
688 0x03,
689 0x9F,
690 0xFF,
691 0xFF,
692 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700693 0xFF};
694
695
696
697static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700698{
699 QCBORDecodeContext DCtx;
700 QCBORItem Item;
701
Laurence Lundbladeee851742020-01-08 08:37:05 -0800702 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700703 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800704 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700705
706 // Array with 3 items
707 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
708 Item.uDataType != QCBOR_TYPE_ARRAY ||
709 Item.uNestingLevel != 0 ||
710 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700711 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700712 return -1;
713 }
714
715 // An integer 0
716 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
717 Item.uDataType != QCBOR_TYPE_INT64 ||
718 Item.uNestingLevel != 1 ||
719 Item.uNextNestLevel != 1 ||
720 Item.val.uint64 != 0) {
721 return -2;
722 }
723
724 // An empty array
725 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
726 Item.uDataType != QCBOR_TYPE_ARRAY ||
727 Item.uNestingLevel != 1 ||
728 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700729 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700730 return -3;
731 }
732
733 // An array with 4 items
734 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
735 Item.uDataType != QCBOR_TYPE_ARRAY ||
736 Item.uNestingLevel != 1 ||
737 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700738 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700739 return -4;
740 }
741
742 // An empty array
743 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
744 Item.uDataType != QCBOR_TYPE_ARRAY ||
745 Item.uNestingLevel != 2 ||
746 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700747 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700748 return -5;
749 }
750
751 // An array with 1 item
752 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
753 Item.uDataType != QCBOR_TYPE_ARRAY ||
754 Item.uNestingLevel != 2 ||
755 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700756 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700757 return -6;
758 }
759
760 // An integer 0
761 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
762 Item.uDataType != QCBOR_TYPE_INT64 ||
763 Item.uNestingLevel != 3 ||
764 Item.uNextNestLevel != 2 ||
765 Item.val.uint64 != 0) {
766 return -7;
767 }
768
769 // An empty map
770 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
771 Item.uDataType != QCBOR_TYPE_MAP ||
772 Item.uNestingLevel != 2 ||
773 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700774 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700775 return -8;
776 }
777
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700778 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700779 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
780 Item.uDataType != QCBOR_TYPE_MAP ||
781 Item.uNestingLevel != 2 ||
782 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700783 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700784 return -9;
785 }
786
787 // An empty map
788 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
789 Item.uDataType != QCBOR_TYPE_MAP ||
790 Item.uNestingLevel != 3 ||
791 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700792 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700793 return -10;
794 }
795
796 // An empty map
797 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
798 Item.uDataType != QCBOR_TYPE_MAP ||
799 Item.uNestingLevel != 3 ||
800 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700801 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700802 return -11;
803 }
804
805 // An empty array
806 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
807 Item.uDataType != QCBOR_TYPE_ARRAY ||
808 Item.uNestingLevel != 3 ||
809 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700810 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700811 return -12;
812 }
813
814 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
815 return -13;
816 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700817 return 0;
818}
819
820
821int32_t EmptyMapsAndArraysTest()
822{
823 int nResult;
824 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
825 true);
826 if(nResult) {
827 return nResult;
828 }
829
830 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
831 false);
832
833 if(nResult) {
834 return nResult -100;
835 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700836
837 return 0;
838}
839
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800840
Laurence Lundbladeee851742020-01-08 08:37:05 -0800841static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
842 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800843
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800844int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800845{
846 QCBORDecodeContext DCtx;
847 int nReturn = 0;
848 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800849
Laurence Lundbladeee851742020-01-08 08:37:05 -0800850 QCBORDecode_Init(&DCtx,
851 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
852 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800853
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800854 for(i = 0; i < 10; i++) {
855 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800856
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800857 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
858 Item.uDataType != QCBOR_TYPE_ARRAY ||
859 Item.uNestingLevel != i) {
860 nReturn = -1;
861 break;
862 }
863 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800864
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800865 return(nReturn);
866}
867
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700868// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800869static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
870 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
871 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
872 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800873
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800874int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875{
876 QCBORDecodeContext DCtx;
877 int nReturn = 0;
878 int i;
879 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
881
Laurence Lundbladeee851742020-01-08 08:37:05 -0800882 QCBORDecode_Init(&DCtx,
883 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
884 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800885
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700886 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800887
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
889 Item.uDataType != QCBOR_TYPE_ARRAY ||
890 Item.uNestingLevel != i) {
891 nReturn = -1;
892 break;
893 }
894 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800896 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
897 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800899 return(nReturn);
900}
901
902
903
904
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800905int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800906{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700907 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800908
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800909 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700910 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800911
Laurence Lundbladeee851742020-01-08 08:37:05 -0800912 QCBORDecode_Init(&DCtx,
913 (UsefulBufC){spExpectedEncodedInts, nNum},
914 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800915
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800916 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800917
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700918 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919 nResult = -1;
920 goto Done;
921 }
922 }
923Done:
924 return nResult;
925}
926
927
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800928
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800929int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800930{
931 uint8_t *pEncoded;
932 int nReturn;
933 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800934
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800935 int64_t i1, i2;
936 size_t i3, i4;
937 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800938
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800939 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800940
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800941 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
942 return(-1);
943 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800944
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800945 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
946 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
947 if(nResult == 0) {
948 nReturn = -1;
949 }
950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800951
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 return(nReturn);
953}
954
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530955/*
956 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800957 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
958 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530959 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800960static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800961{
962 QCBORDecodeContext DCtx;
963 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700964 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800965
Laurence Lundbladeee851742020-01-08 08:37:05 -0800966 QCBORDecode_Init(&DCtx,
967 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
968 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800969
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900970 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700971 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900972 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800973 if(Item.uDataType != QCBOR_TYPE_MAP ||
974 Item.val.uCount != 3)
975 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800976
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900977 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700978 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900979 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800980 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800981 Item.uDataType != QCBOR_TYPE_INT64 ||
982 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530983 Item.uDataAlloc ||
984 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900985 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800986 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800988
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900989 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700990 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800992 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530993 Item.uDataAlloc ||
994 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900995 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800996 Item.uDataType != QCBOR_TYPE_ARRAY ||
997 Item.val.uCount != 2)
998 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800999
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001000 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001001 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001002 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001003 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301004 Item.uDataAlloc ||
1005 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001007 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001009
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001010 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001011 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001012 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001013 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301014 Item.uDataAlloc ||
1015 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001016 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001017 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001019
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001020 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001021 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001022 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001023 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301024 Item.uDataAlloc ||
1025 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001026 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001027 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001028 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001029 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001030 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001031
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001032 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001033 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001034 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001035 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001036 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001037 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301038 Item.uDataAlloc ||
1039 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001040 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001041 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001043
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001044 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001045 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001046 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001047 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001048 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001049 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301050 Item.uDataAlloc ||
1051 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001052 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001053 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001055
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001056 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001057 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001058 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001059 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301060 Item.uDataAlloc ||
1061 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001062 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001063 Item.uDataType != QCBOR_TYPE_INT64 ||
1064 Item.val.int64 != 98)
1065 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001066
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001067 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001068 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001069 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001071 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001072 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301073 Item.uDataAlloc ||
1074 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001075 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001076 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 return 0;
1080}
1081
1082
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001083/*
1084 Decode and thoroughly check a moderately complex
1085 set of maps
1086 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001087int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001088{
1089 QCBORDecodeContext DCtx;
1090 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001091 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001092
Laurence Lundbladeee851742020-01-08 08:37:05 -08001093 QCBORDecode_Init(&DCtx,
1094 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1095 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001096
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001097 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001098 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001099 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001100 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1101 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001102 return -1;
1103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001104
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001105 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001106 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001107 }
1108 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1109 Item.uDataAlloc ||
1110 Item.uLabelAlloc ||
1111 Item.uLabelType != QCBOR_TYPE_NONE ||
1112 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1113 return -2;
1114 }
1115
1116 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001117 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 }
1119 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1120 Item.uDataType != QCBOR_TYPE_INT64 ||
1121 Item.val.int64 != 42 ||
1122 Item.uDataAlloc ||
1123 Item.uLabelAlloc) {
1124 return -3;
1125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001126
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001127 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001128 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001129 }
1130 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1131 Item.uDataAlloc ||
1132 Item.uLabelAlloc ||
1133 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1134 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1135 return -4;
1136 }
1137
1138 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001139 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001140 }
1141 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1142 Item.uDataAlloc ||
1143 Item.uLabelAlloc ||
1144 Item.uDataType != QCBOR_TYPE_ARRAY ||
1145 Item.val.uCount != 2) {
1146 return -5;
1147 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001148
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001149 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001150 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001151 }
1152 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1153 Item.val.string.len != 7 ||
1154 Item.uDataAlloc ||
1155 Item.uLabelAlloc ||
1156 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1157 return -6;
1158 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001159
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001160 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001161 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001162 }
1163 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1164 Item.uDataAlloc ||
1165 Item.uLabelAlloc ||
1166 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1167 return -7;
1168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001169
1170
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001171 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001172 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001173 }
1174 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1175 Item.uDataAlloc ||
1176 Item.uLabelAlloc ||
1177 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1178 return -8;
1179 }
1180
1181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001182 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 }
1184 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1185 Item.uDataAlloc ||
1186 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001187 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1188 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 return -9;
1190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001191
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001192 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001193 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001194 }
1195 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1196 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1197 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1198 Item.uDataAlloc ||
1199 Item.uLabelAlloc) {
1200 return -10;
1201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001202
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001203 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001204 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001205 }
1206 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1207 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1208 Item.uDataAlloc ||
1209 Item.uLabelAlloc ||
1210 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1211 return -11;
1212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001213
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001215 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 }
1217 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1218 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1219 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1220 Item.uDataAlloc ||
1221 Item.uLabelAlloc) {
1222 return -12;
1223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001224
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001225 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001226 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001227 }
1228 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1229 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1230 Item.uDataAlloc ||
1231 Item.uLabelAlloc ||
1232 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1233 return -13;
1234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001235
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001236 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001237 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001238 }
1239 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1240 Item.uDataAlloc ||
1241 Item.uLabelAlloc ||
1242 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1243 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1244 return -14;
1245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001246
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001247 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001248 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001249 }
1250 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1251 Item.uDataAlloc ||
1252 Item.uLabelAlloc ||
1253 Item.uDataType != QCBOR_TYPE_INT64 ||
1254 Item.val.int64 != 98) {
1255 return -15;
1256 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001257
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001259 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 }
1261 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1262 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1263 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1264 Item.uDataAlloc ||
1265 Item.uLabelAlloc) {
1266 return -16;
1267 }
1268
1269 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001270 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001271 }
1272 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1273 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1274 Item.uDataAlloc ||
1275 Item.uLabelAlloc ||
1276 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1277 return -17;
1278 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001279
1280
1281 /*
1282 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1283 map that when interpreted as an array will be too many. Test
1284 data just has the start of the map, not all the items in the map.
1285 */
1286 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
1287
1288 QCBORDecode_Init(&DCtx,
1289 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1290 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1291
1292 if((QCBOR_ERR_ARRAY_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
1293 return -50;
1294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001295
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001296 return 0;
1297}
1298
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001299
1300/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301301 Fully or partially decode pValidMapEncoded. When
1302 partially decoding check for the right error code.
1303 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301305 The partial decodes test error conditions of
1306 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001307
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301308 This could be combined with the above test
1309 and made prettier and maybe a little more
1310 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001311 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001312static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001313{
1314 QCBORDecodeContext DCtx;
1315 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001316 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001317
Laurence Lundbladeee851742020-01-08 08:37:05 -08001318 QCBORDecode_Init(&DCtx,
1319 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1320 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001322 if(nLevel < 1) {
1323 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1324 return -1;
1325 } else {
1326 return 0;
1327 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301329
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001330
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001331 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001332 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001333 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 if(Item.uDataType != QCBOR_TYPE_MAP ||
1335 Item.val.uCount != 3)
1336 return -1;
1337
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001338 if(nLevel < 2) {
1339 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1340 return -1;
1341 } else {
1342 return 0;
1343 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001344 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001345
1346
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -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 Lundblade2ded3d92018-10-09 21:36:11 +08001351 Item.uDataType != QCBOR_TYPE_INT64 ||
1352 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001353 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
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 < 3) {
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 Lundbladee6bcef12020-04-01 10:56:27 -07001366 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001367 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001369 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001370 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001371 Item.val.uCount != 2) {
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
1375
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001376 if(nLevel < 4) {
1377 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1378 return -1;
1379 } else {
1380 return 0;
1381 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001382 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001383
1384
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001385 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001386 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001387 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001389 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001390 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001392
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001393 if(nLevel < 5) {
1394 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1395 return -1;
1396 } else {
1397 return 0;
1398 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001400
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001401 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001402 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001403 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001404 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001405 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001408
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001409 if(nLevel < 6) {
1410 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1411 return -1;
1412 } else {
1413 return 0;
1414 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001416
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001417 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001418 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001420 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001421 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422 Item.uDataType != QCBOR_TYPE_MAP ||
1423 Item.val.uCount != 4)
1424 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001425
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001426 if(nLevel < 7) {
1427 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1428 return -1;
1429 } else {
1430 return 0;
1431 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001432 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001433
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001434 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001435 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001436 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001438 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001439 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001440 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001441 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001443
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 if(nLevel < 8) {
1445 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1446 return -1;
1447 } else {
1448 return 0;
1449 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001451
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001452 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001453 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001454 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001455 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001456 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001458 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001459 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001461
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001462 if(nLevel < 9) {
1463 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1464 return -1;
1465 } else {
1466 return 0;
1467 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001469
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001470 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001471 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001472 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001474 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001475 Item.uDataType != QCBOR_TYPE_INT64 ||
1476 Item.val.int64 != 98)
1477 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001479 if(nLevel < 10) {
1480 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1481 return -1;
1482 } else {
1483 return 0;
1484 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001485 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001486
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001487 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001488 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001489 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001491 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001493 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001494 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001495 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001496
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301497 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 return -1;
1499 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001500
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001501 return 0;
1502}
1503
1504
1505
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001506
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001507int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001509 // Parse a moderatly complex map structure very thoroughly
1510 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1511 if(nResult) {
1512 return nResult;
1513 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001514
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001515 // Again, but in strings-only mode. It should succeed since the input
1516 // map has only string labels.
1517 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1518 if(nResult) {
1519 return nResult;
1520 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001521
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001522 // Again, but try to finish the decoding before the end of the
1523 // input at 10 different place and see that the right error code
1524 // is returned.
1525 for(int i = 0; i < 10; i++) {
1526 nResult = ExtraBytesTest(i);
1527 if(nResult) {
1528 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001529 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001531
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001532 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001533}
1534
1535
Laurence Lundbladeee851742020-01-08 08:37:05 -08001536static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1537 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1538 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001539
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001540int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001541{
1542 QCBORDecodeContext DCtx;
1543 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001544 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001545
1546
Laurence Lundbladeee851742020-01-08 08:37:05 -08001547 QCBORDecode_Init(&DCtx,
1548 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1549 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001550
1551
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001552 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001553 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1555 Item.val.uCount != 10)
1556 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001558 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001559 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001560 if(Item.uDataType != QCBOR_TYPE_FALSE)
1561 return -1;
1562
1563 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001564 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001565 if(Item.uDataType != QCBOR_TYPE_TRUE)
1566 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001567
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001568 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001569 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001570 if(Item.uDataType != QCBOR_TYPE_NULL)
1571 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001573 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001574 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1576 return -1;
1577
1578 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001579 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001580 return -1;
1581
1582 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001583 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001584 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1585 return -1;
1586
1587 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001588 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1590 return -1;
1591
Laurence Lundblade077475f2019-04-26 09:06:33 -07001592 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001593 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001594
Laurence Lundblade077475f2019-04-26 09:06:33 -07001595 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001596 return -1;
1597
Laurence Lundblade077475f2019-04-26 09:06:33 -07001598 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001599 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001601 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001602 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001603 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1604 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001606 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001607 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001608 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1609 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001610
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001611 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001612
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001613}
1614
1615
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001616static bool IsNotWellFormedError(QCBORError nErr)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001617{
1618 switch(nErr){
1619 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1620 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1621 case QCBOR_ERR_UNSUPPORTED:
1622 case QCBOR_ERR_HIT_END:
1623 case QCBOR_ERR_BAD_TYPE_7:
1624 case QCBOR_ERR_BAD_BREAK:
1625 case QCBOR_ERR_EXTRA_BYTES:
1626 case QCBOR_ERR_BAD_INT:
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001627 case QCBOR_ERR_NO_MORE_ITEMS: // TODO: really keep this?
1628 return true;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001629 default:
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001630 return false;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001631 }
1632}
1633
1634
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001635int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001636{
1637 // Loop over all the not-well-formed instance of CBOR
1638 // that are test vectors in not_well_formed_cbor.h
1639 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1640 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1641 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1642 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1643
Laurence Lundbladeee851742020-01-08 08:37:05 -08001644 // Set up decoder context. String allocator needed for indefinite
1645 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001646 QCBORDecodeContext DCtx;
1647 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1648 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1649 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1650
1651 // Loop getting items until no more to get
1652 QCBORError nCBORError;
1653 do {
1654 QCBORItem Item;
1655
1656 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1657 } while(nCBORError == QCBOR_SUCCESS);
1658
1659 // Every test vector must fail with
1660 // a not-well-formed error. If not
1661 // this test fails.
1662 if(!IsNotWellFormedError(nCBORError)) {
1663 // Return index of failure in the error code
1664 return 2000 + nIterate;
1665 }
1666 }
1667 return 0;
1668}
1669
1670
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001671struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001672 UsefulBufC Input;
1673 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001674};
1675
Laurence Lundblade59289e52019-12-30 13:44:37 -08001676
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001677static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001678{
1679 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1680 // Set up the decoding context including a memory pool so that
1681 // indefinite length items can be checked
1682 QCBORDecodeContext DCtx;
1683 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1684 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001685
Laurence Lundblade59289e52019-12-30 13:44:37 -08001686 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1687 if(nCBORError) {
1688 return -9;
1689 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001690
Laurence Lundblade59289e52019-12-30 13:44:37 -08001691 // Iterate until there is an error of some sort error
1692 QCBORItem Item;
1693 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001694 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001695 memset(&Item, 0x33, sizeof(Item));
1696
1697 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1698 } while(nCBORError == QCBOR_SUCCESS);
1699
1700 // Must get the expected error or the this test fails
1701 // The data and label type must also be QCBOR_TYPE_NONE
1702 if(nCBORError != pF->nError ||
1703 Item.uDataType != QCBOR_TYPE_NONE ||
1704 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001705 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001706 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001707 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001708 }
1709 }
1710
1711 return 0;
1712}
1713
1714
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001715struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001716 // Most of this is copied from not_well_formed.h. Here the error code
1717 // returned is also checked.
1718
1719 // Indefinite length strings must be closed off
1720 // An indefinite length byte string not closed off
1721 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1722 // An indefinite length text string not closed off
1723 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1724
1725
1726 // All the chunks in an indefinite length string must be of the type of indefinite length string
1727 // indefinite length byte string with text string chunk
1728 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1729 // indefinite length text string with a byte string chunk
1730 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1731 // indefinite length byte string with an positive integer chunk
1732 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1733 // indefinite length byte string with an negative integer chunk
1734 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1735 // indefinite length byte string with an array chunk
1736 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1737 // indefinite length byte string with an map chunk
1738 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1739 // indefinite length byte string with tagged integer chunk
1740 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1741 // indefinite length byte string with an simple type chunk
1742 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1743 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1744 // indefinite length text string with indefinite string inside
1745 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1746
1747
1748 // Definte length maps and arrays must be closed by having the right number of items
1749 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001750 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001751 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001752 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001753 // A definte length array that is supposed to have 511 items, but has only 1
1754 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1755 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001756 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001757 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001758 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001759
1760
1761 // Indefinte length maps and arrays must be ended by a break
1762 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001763 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001764 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001765 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001766 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001767 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001768 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001769 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001770
1771
1772 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001773 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001774 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001775 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001776 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001777 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001778 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, // TODO: 23
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001779 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001780 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001781 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001782 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001783 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001784 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001785 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001786
1787
1788 // The "argument" for the data item is incomplete
1789 // Positive integer missing 1 byte argument
1790 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1791 // Positive integer missing 2 byte argument
1792 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1793 // Positive integer missing 4 byte argument
1794 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1795 // Positive integer missing 8 byte argument
1796 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1797 // Positive integer missing 1 byte of 2 byte argument
1798 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1799 // Positive integer missing 2 bytes of 4 byte argument
1800 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1801 // Positive integer missing 1 bytes of 7 byte argument
1802 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1803 // Negative integer missing 1 byte argument
1804 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1805 // Binary string missing 1 byte argument
1806 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1807 // Text string missing 1 byte argument
1808 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1809 // Array missing 1 byte argument
1810 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1811 // Map missing 1 byte argument
1812 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1813 // Tag missing 1 byte argument
1814 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1815 // Simple missing 1 byte argument
1816 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1817
1818
1819 // Breaks must not occur in definite length arrays and maps
1820 // Array of length 1 with sole member replaced by a break
1821 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1822 // Array of length 2 with 2nd member replaced by a break
1823 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1824 // Map of length 1 with sole member label replaced by a break
1825 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1826 // Map of length 1 with sole member label replaced by break
1827 // Alternate representation that some decoders handle difference
1828 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1829 // Array of length 1 with 2nd member value replaced by a break
1830 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1831 // Map of length 2 with 2nd member replaced by a break
1832 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1833
1834
1835 // Breaks must not occur on their own out of an indefinite length data item
1836 // A bare break is not well formed
1837 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1838 // A bare break after a zero length definite length array
1839 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1840 // A bare break after a zero length indefinite length map
1841 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1842
1843
1844 // Forbidden two byte encodings of simple types
1845 // Must use 0xe0 instead
1846 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1847 // Should use 0xe1 instead
1848 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1849 // Should use 0xe2 instead
1850 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1851 // Should use 0xe3 instead
1852 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1853 // Should use 0xe4 instead
1854 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1855 // Should use 0xe5 instead
1856 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1857 // Should use 0xe6 instead
1858 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1859 // Should use 0xe7 instead
1860 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1861 // Should use 0xe8 instead
1862 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1863 // Should use 0xe9 instead
1864 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1865 // Should use 0xea instead
1866 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1867 // Should use 0xeb instead
1868 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1869 // Should use 0xec instead
1870 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1871 // Should use 0xed instead
1872 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1873 // Should use 0xee instead
1874 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1875 // Should use 0xef instead
1876 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1877 // Should use 0xf0 instead
1878 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1879 // Should use 0xf1 instead
1880 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1881 // Should use 0xf2 instead
1882 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1883 // Must use 0xf3 instead
1884 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1885 // Must use 0xf4 instead
1886 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1887 // Must use 0xf5 instead
1888 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1889 // Must use 0xf6 instead
1890 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1891 // Must use 0xf7 instead
1892 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1893 // Must use 0xf8 instead
1894 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1895
1896
1897 // Integers with additional info indefinite length
1898 // Positive integer with additional info indefinite length
1899 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1900 // Negative integer with additional info indefinite length
1901 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1902 // CBOR tag with "argument" an indefinite length
1903 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1904 // CBOR tag with "argument" an indefinite length alternate vector
1905 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1906
1907
1908 // Missing bytes from a deterministic length string
1909 // A byte string is of length 1 without the 1 byte
1910 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1911 // A text string is of length 1 without the 1 byte
1912 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001913 // Byte string should have 2^32-15 bytes, but has one
1914 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1915 // Byte string should have 2^32-15 bytes, but has one
1916 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001917
1918
1919 // Use of unassigned additional information values
1920 // Major type positive integer with reserved value 28
1921 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1922 // Major type positive integer with reserved value 29
1923 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1924 // Major type positive integer with reserved value 30
1925 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1926 // Major type negative integer with reserved value 28
1927 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1928 // Major type negative integer with reserved value 29
1929 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1930 // Major type negative integer with reserved value 30
1931 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1932 // Major type byte string with reserved value 28 length
1933 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1934 // Major type byte string with reserved value 29 length
1935 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1936 // Major type byte string with reserved value 30 length
1937 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1938 // Major type text string with reserved value 28 length
1939 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1940 // Major type text string with reserved value 29 length
1941 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1942 // Major type text string with reserved value 30 length
1943 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1944 // Major type array with reserved value 28 length
1945 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1946 // Major type array with reserved value 29 length
1947 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1948 // Major type array with reserved value 30 length
1949 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1950 // Major type map with reserved value 28 length
1951 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1952 // Major type map with reserved value 29 length
1953 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1954 // Major type map with reserved value 30 length
1955 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1956 // Major type tag with reserved value 28 length
1957 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1958 // Major type tag with reserved value 29 length
1959 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1960 // Major type tag with reserved value 30 length
1961 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1962 // Major type simple with reserved value 28 length
1963 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1964 // Major type simple with reserved value 29 length
1965 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1966 // Major type simple with reserved value 30 length
1967 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1968
1969
1970 // Maps must have an even number of data items (key & value)
1971 // Map with 1 item when it should have 2
1972 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1973 // Map with 3 item when it should have 4
1974 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1975 // Map with 1 item when it should have 2
1976 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1977 // Map with 3 item when it should have 4
1978 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1979
1980
1981 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001982 // Text-based date, with an integer
1983 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1984 // Epoch date, with an byte string
1985 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1986 // tagged as both epoch and string dates
1987 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1988 // big num tagged an int, not a byte string
1989 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001990};
1991
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001992int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001993{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001994 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001995
Laurence Lundblade59289e52019-12-30 13:44:37 -08001996 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1997 if(nResult) {
1998 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002000
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002001 // Corrupt the UsefulInputBuf and see that
2002 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002003 QCBORDecodeContext DCtx;
2004 QCBORItem Item;
2005 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002006
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002007 QCBORDecode_Init(&DCtx,
2008 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2009 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002010
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002011 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2012 return (int32_t)uQCBORError;
2013 }
2014 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2015 // This wasn't supposed to happen
2016 return -1;
2017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002018
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002019 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002020
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002021 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2022 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2023 // Did not get back the error expected
2024 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002026
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002027/*
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002028 TODO: fix this
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002029 This test is disabled until QCBOREncode_EncodeHead() is brought in so
2030 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
2031
2032 This relies on the largest string allowed being SIZE_MAX -4 rather than
2033 SIZE_MAX. That way the test can be performed.
2034 {
2035 QCBORDecodeContext DCtx;
2036 QCBORItem Item;
2037
2038 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
2039 0xff, 0xff, 0xff, 0xff};
2040
2041 QCBORDecode_Init(&DCtx,
2042 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
2043 QCBOR_DECODE_MODE_NORMAL);
2044
2045 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2046 return -4;
2047 }
2048 }
2049*/
2050
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002051 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002052}
2053
2054
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002055/* Try all 256 values of the byte at nLen including recursing for
2056 each of the values to try values at nLen+1 ... up to nLenMax
2057 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002058static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002059{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002060 if(nLen >= nLenMax) {
2061 return;
2062 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002063
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002064 for(int inputByte = 0; inputByte < 256; inputByte++) {
2065 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002066 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002067 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002068
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002069 // Get ready to parse
2070 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002071 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002072
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002073 // Parse by getting the next item until an error occurs
2074 // Just about every possible decoder error can occur here
2075 // The goal of this test is not to check for the correct
2076 // error since that is not really possible. It is to
2077 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002078 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002079 QCBORItem Item;
2080 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002081 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002082 break;
2083 }
2084 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002085
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002086 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002087 }
2088}
2089
2090
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002091int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002092{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002093 // Size 2 tests 64K inputs and runs quickly
2094 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002095
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002096 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002097
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002098 return 0;
2099}
2100
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002101
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002102int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002103{
2104 // size 3 tests 16 million inputs and runs OK
2105 // in seconds on fast machines. Size 4 takes
2106 // 10+ minutes and 5 half a day on fast
2107 // machines. This test is kept separate from
2108 // the others so as to no slow down the use
2109 // of them as a very frequent regression.
2110 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002111
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002112 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002113
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002114 return 0;
2115}
2116
2117
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002118static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002119 0xc0, // tag for string date
2120 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002121
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002122 0xc1, // tag for epoch date
2123 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2124
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002125 // CBOR_TAG_B64
2126 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002127 0x1a, 0x53, 0x72, 0x4E, 0x01,
2128
2129 0xc1, // tag for epoch date
2130 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002131
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002132 0xc1, // tag for epoch date
2133 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002134
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002135 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002136 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002137
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002138 0xc1, // tag for epoch date
2139 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2140 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2141
2142 0xc1, // tag for epoch date
2143 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002144};
2145
2146
2147// have to check float expected only to within an epsilon
2148int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002149
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002150 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002151
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002152 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002153
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002154 return diff > 0.0000001;
2155}
2156
2157
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002158int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002159{
2160 QCBORDecodeContext DCtx;
2161 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002162 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002163
Laurence Lundbladeee851742020-01-08 08:37:05 -08002164 QCBORDecode_Init(&DCtx,
2165 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2166 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002167
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002168 const uint64_t uTags[] = {15};
2169 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002170
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002171 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002172
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002173 // String date
2174 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2175 return -1;
2176 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002177 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002178 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002179 }
2180
2181 // Epoch date
2182 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002183 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002184 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2185 Item.val.epochDate.nSeconds != 1400000000 ||
2186 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002187 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002188 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002189
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002190 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2191 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002192 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002193 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002194 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2195 Item.val.epochDate.nSeconds != 1400000001 ||
2196 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002197 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002198 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002199 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002201 // Epoch date that is too large for our representation
2202 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002203 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002206 // Epoch date in float format with fractional seconds
2207 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002208 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002209 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2210 Item.val.epochDate.nSeconds != 1 ||
2211 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002212 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002214
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002215 // Epoch date float that is too large for our representation
2216 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002217 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002219
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002220 // Epoch date double that is just slightly too large
2221 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2222 return -11;
2223 }
2224
2225 // Largest double epoch date supported
2226 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2227 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2228 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2229 Item.val.epochDate.nSeconds == 0) {
2230 return -12;
2231 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002232 // TODO: could use a few more tests with float, double, and half precsion
2233 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002234
2235 return 0;
2236}
2237
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002238// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002239static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002240 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002241 0x81, // Array of one
2242 0xd8, 0x04, // non-preferred serialization of tag 4
2243 0x82, 0x01, 0x03}; // fraction 1/3
2244
Laurence Lundblade59289e52019-12-30 13:44:37 -08002245/*
2246 DB 9192939495969798 # tag(10489608748473423768)
2247 80 # array(0)
2248 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002249static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2250 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002251
Laurence Lundblade59289e52019-12-30 13:44:37 -08002252/*
2253DB 9192939495969798 # tag(10489608748473423768)
2254 D8 88 # tag(136)
2255 C6 # tag(6)
2256 C7 # tag(7)
2257 80 # array(0)
2258*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002259static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2260 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002261
2262/*
2263 The cbor.me parse of this.
2264 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002265 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 +07002266 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2267 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2268 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2269 */
2270static uint8_t spCSRWithTags[] = {
2271 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2272 0xc6, 0xc7, 0x36,
2273 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2274 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2275 0xcb, 0xa5,
2276 0xd1, 0x31,
2277 0xd1, 0xd1, 0xd1, 0x6c,
2278 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2279 0xc9, 0x30,
2280 0xd9, 0x03, 0x05, 0x63,
2281 0x53, 0x53, 0x47,
2282 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002283 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002284 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2285 0xd1, 0x2f,
2286 0xd1, 0x69,
2287 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2288 0xd1, 0x2d,
2289 0xd1, 0x62,
2290 0x55, 0x53,
2291 0xd7, 0x32,
2292 0xd3, 0xa2,
2293 0x2a,
2294 0xc9, 0xa1,
2295 0x28,
2296 0x26,
2297 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2298 0xcc, 0x4a,
2299 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2300 0xd0, 0x35,
2301 0xd7, 0xa1,
2302 0xcb, 0xc8, 0xc7, 0x24,
2303 0xc8, 0x22};
2304
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002305static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002306
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002307
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002308int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002309{
2310 QCBORDecodeContext DCtx;
2311 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002312
Laurence Lundbladeee851742020-01-08 08:37:05 -08002313 QCBORDecode_Init(&DCtx,
2314 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2315 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002316
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002317 //-------------------------
2318 // This text matches the magic number tag and the fraction tag
2319 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2320 return -2;
2321 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002322 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002323 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2324 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002326
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002327 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2328 return -4;
2329 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002330
2331#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002332 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002333 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002334 Item.val.uCount != 2) {
2335 return -5;
2336 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002337#else
2338 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2339 return -6;
2340 }
2341#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002342
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002343 // --------------------------------
2344 // This test decodes the very large tag, but it is not in
2345 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002346 QCBORDecode_Init(&DCtx,
2347 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2348 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002349 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2350 return -6;
2351 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002352 /*
2353 if(Item.uTagBits) { // TODO: make sure it is OK to remove this
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002354 return -7;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002355 }*/
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002356
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002357 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002358 // This test sets up a caller-config list that includes the very large
2359 // tage and then matches it.
2360 QCBORDecode_Init(&DCtx,
2361 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2362 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002363 const uint64_t puList[] = {0x9192939495969798, 257};
2364 const QCBORTagListIn TL = {2, puList};
2365 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002366
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002367 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2368 return -8;
2369 }
2370 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2371 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2372 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2373 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2374 Item.val.uCount != 0) {
2375 return -9;
2376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002377
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002378 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002379 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002380 const uint64_t puLongList[17] = {1,2,1};
2381 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002382 QCBORDecode_Init(&DCtx,
2383 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2384 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002385 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2386 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2387 return -11;
2388 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002389
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002390 // -----------------------
2391 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002392 QCBORDecode_Init(&DCtx,
2393 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2394 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002395 uint64_t puTags[16];
2396 QCBORTagListOut Out = {0, 4, puTags};
2397 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2398 return -12;
2399 }
2400 if(puTags[0] != 0x9192939495969798 ||
2401 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002402 puTags[2] != 0x06 ||
2403 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002404 return -13;
2405 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002406
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002407 // ----------------------
2408 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002409 QCBORDecode_Init(&DCtx,
2410 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2411 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002412 QCBORTagListOut OutSmall = {0, 3, puTags};
2413 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2414 return -14;
2415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002416
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002417#if 0
2418 // TODO: this test needs to be re evaluated
2419
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002420 // ---------------
2421 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002422 QCBORDecode_Init(&DCtx,
2423 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2424 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002425 int n = CheckCSRMaps(&DCtx);
2426 if(n) {
2427 return n-2000;
2428 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002429
Laurence Lundblade59289e52019-12-30 13:44:37 -08002430 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002431 QCBORDecode_Init(&DCtx,
2432 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2433 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002434
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002435 const uint64_t puTagList[] = {773, 1, 90599561};
2436 const QCBORTagListIn TagList = {3, puTagList};
2437 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002438
2439
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002440 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2441 return -100;
2442 }
2443 if(Item.uDataType != QCBOR_TYPE_MAP ||
2444 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2445 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2446 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2447 Item.val.uCount != 2 ||
2448 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2449 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2450 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2451 Out.uNumUsed != 3) {
2452 return -101;
2453 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002454
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002455 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2456 return -102;
2457 }
2458 if(Item.uDataType != QCBOR_TYPE_MAP ||
2459 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2460 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2461 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2462 Item.val.uCount != 2 ||
2463 puTags[0] != 5859837686836516696 ||
2464 puTags[1] != 7 ||
2465 Out.uNumUsed != 2) {
2466 return -103;
2467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002468
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002469 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2470 return -104;
2471 }
2472 if(Item.uDataType != QCBOR_TYPE_MAP ||
2473 Item.uTagBits ||
2474 Item.val.uCount != 5 ||
2475 puTags[0] != 0x0b ||
2476 Out.uNumUsed != 1) {
2477 return -105;
2478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002479
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002480 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2481 return -106;
2482 }
2483 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2484 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2485 Item.val.string.len != 12 ||
2486 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2487 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2488 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2489 Out.uNumUsed != 3) {
2490 return -105;
2491 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002492
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002493 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2494 return -107;
2495 }
2496 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2497 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2498 Item.val.string.len != 3 ||
2499 puTags[0] != 773 ||
2500 Out.uNumUsed != 1) {
2501 return -108;
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 -109;
2506 }
2507 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002508 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002509 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002510 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002511 puTags[11] != 0x0f ||
2512 Out.uNumUsed != 12) {
2513 return -110;
2514 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002515
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002516 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2517 return -111;
2518 }
2519 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2520 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2521 Item.val.string.len != 9 ||
2522 puTags[0] != 17 ||
2523 Out.uNumUsed != 1) {
2524 return -112;
2525 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002526
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002527 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2528 return -111;
2529 }
2530 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2531 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2532 Item.val.string.len != 2 ||
2533 puTags[0] != 17 ||
2534 Out.uNumUsed != 1) {
2535 return -112;
2536 }
2537
2538 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2539 return -113;
2540 }
2541 if(Item.uDataType != QCBOR_TYPE_MAP ||
2542 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2543 Item.val.uCount != 2 ||
2544 puTags[0] != 19 ||
2545 Out.uNumUsed != 1) {
2546 return -114;
2547 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002548
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002549 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2550 return -115;
2551 }
2552 if(Item.uDataType != QCBOR_TYPE_MAP ||
2553 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2554 Item.uTagBits ||
2555 Item.val.uCount != 1 ||
2556 puTags[0] != 9 ||
2557 Out.uNumUsed != 1) {
2558 return -116;
2559 }
2560
2561 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2562 return -116;
2563 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002564 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002565 Item.val.int64 != -7 ||
2566 Item.uTagBits ||
2567 Out.uNumUsed != 0) {
2568 return -117;
2569 }
2570
2571 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2572 return -118;
2573 }
2574 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2575 Item.val.string.len != 10 ||
2576 Item.uTagBits ||
2577 puTags[0] != 12 ||
2578 Out.uNumUsed != 1) {
2579 return -119;
2580 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002581
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002582 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2583 return -120;
2584 }
2585 if(Item.uDataType != QCBOR_TYPE_MAP ||
2586 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2587 Item.val.uCount != 1 ||
2588 puTags[0] != 0x17 ||
2589 Out.uNumUsed != 1) {
2590 return -121;
2591 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002592
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002593 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2594 return -122;
2595 }
2596 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2597 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2598 Item.val.int64 != -3 ||
2599 puTags[0] != 8 ||
2600 Out.uNumUsed != 1) {
2601 return -123;
2602 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002603
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002604 if(QCBORDecode_Finish(&DCtx)) {
2605 return -124;
2606 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002607#else
2608 (void)spCSRWithTags;
2609#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002610 return 0;
2611}
2612
2613
2614
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002615
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002616static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002617 0x83,
2618 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2619 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2620 0xA4,
2621 0x63, 0x42, 0x4E, 0x2B,
2622 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2623 0x18, 0x40,
2624 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2625 0x63, 0x42, 0x4E, 0x2D,
2626 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2627 0x38, 0x3F,
2628 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2629
2630
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002631static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002632
2633
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002634int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002635{
2636 QCBORDecodeContext DCtx;
2637 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002638 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002639
Laurence Lundbladeee851742020-01-08 08:37:05 -08002640 QCBORDecode_Init(&DCtx,
2641 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2642 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002643
2644
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002645 //
2646 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2647 return -1;
2648 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002649 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002650 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002651
2652 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002653 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002654 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002655 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002656 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002657 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002658 }
2659
2660 //
2661 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002662 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002663 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002664 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002665 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002666 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002667
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002668 //
2669 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002670 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002671 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002672 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002673 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002674
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002675 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002676 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002677 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2678 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002679 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002680 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002681 }
2682
2683 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002684 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002685 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2686 Item.uLabelType != QCBOR_TYPE_INT64 ||
2687 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002688 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002689 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002690 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002691
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002692 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002693 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002694 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2695 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002696 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002697 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002698 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002699
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002700 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002701 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002702 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2703 Item.uLabelType != QCBOR_TYPE_INT64 ||
2704 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002705 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002706 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002708
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002709 return 0;
2710}
2711
2712
2713
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002714static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002715 uint8_t uDataType,
2716 uint8_t uNestingLevel,
2717 uint8_t uNextNest,
2718 int64_t nLabel,
2719 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002720{
2721 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002722 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002723
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002724 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2725 if(Item.uDataType != uDataType) return -1;
2726 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002727 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2728 Item.uLabelType != QCBOR_TYPE_UINT64) {
2729 return -1;
2730 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002731 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2732 if(Item.label.int64 != nLabel) return -1;
2733 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002734 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002735 }
2736 }
2737 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302738 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002739
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002740 if(pItem) {
2741 *pItem = Item;
2742 }
2743 return 0;
2744}
2745
2746
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002747// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002748static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002749{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302750 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002751
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302752 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002753
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302754 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002755
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302756 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2757 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2758 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2759 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2760 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002761
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302762 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2763 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002764
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302765 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2766 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002767
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302768 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2769 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002770
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002771 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002772
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002773 return 0;
2774}
2775
2776
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002777/*
2778// cbor.me decoded output
2779{
2780 -23: {
2781 -20: {
2782 -18: "Organization",
2783 -17: "SSG",
2784 -15: "Confusion",
2785 -16: "San Diego",
2786 -14: "US"
2787 },
2788 -19: {
2789 -11: {
2790 -9: -7
2791 },
2792 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2793 }
2794 },
2795 -22: {
2796 -5: -3
2797 }
2798}
2799 */
2800
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002801
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002802static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002803 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2804 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2805 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2806 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2807 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2808 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2809 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2810 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2811 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2812
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002813int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002814{
2815 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002816
Laurence Lundbladeee851742020-01-08 08:37:05 -08002817 QCBORDecode_Init(&DCtx,
2818 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2819 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002820
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002821 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002822}
2823
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002824
2825
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002826int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002827{
2828 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002829
Laurence Lundbladeee851742020-01-08 08:37:05 -08002830 QCBORDecode_Init(&DCtx,
2831 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2832 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002833
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002834 QCBORItem Item;
2835 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002836
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002837 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2838 return -1;
2839 }
2840 if(Item.uDataType != QCBOR_TYPE_MAP) {
2841 return -2;
2842 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002843
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002844 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2845 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2846 return -3;
2847 }
2848
2849 return 0;
2850}
2851
2852
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002853// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002854static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002855 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2856 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2857 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2858 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2859 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2860 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002861 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2862 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2863 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2864 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002865
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002866int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002867{
2868 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002869
Laurence Lundbladeee851742020-01-08 08:37:05 -08002870 QCBORDecode_Init(&DCtx,
2871 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2872 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002873
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002874 return CheckCSRMaps(&DCtx);
2875}
2876
2877
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002878
Laurence Lundblade17ede402018-10-13 11:43:07 +08002879static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2880{
2881 UsefulOutBuf UOB;
2882 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002883
Laurence Lundblade17ede402018-10-13 11:43:07 +08002884 int i;
2885 for(i = 0; i < n; i++) {
2886 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2887 }
2888
2889 for(i = 0; i < n; i++) {
2890 UsefulOutBuf_AppendByte(&UOB, 0xff);
2891 }
2892 return UsefulOutBuf_OutUBuf(&UOB);
2893}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002894
2895
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002896static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002897{
2898 QCBORDecodeContext DC;
2899 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002900
Laurence Lundblade17ede402018-10-13 11:43:07 +08002901 int j;
2902 for(j = 0; j < nNestLevel; j++) {
2903 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002904 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002905 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2906 // Should be in error
2907 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2908 return -4;
2909 } else {
2910 return 0; // Decoding doesn't recover after an error
2911 }
2912 } else {
2913 // Should be no error
2914 if(nReturn) {
2915 return -9; // Should not have got an error
2916 }
2917 }
2918 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2919 return -7;
2920 }
2921 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002922 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002923 if(nReturn) {
2924 return -3;
2925 }
2926 return 0;
2927}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002928
2929
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002930int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002931{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302932 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002933 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002934 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002935 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002936 int nReturn = parse_indeflen_nested(Nested, i);
2937 if(nReturn) {
2938 return nReturn;
2939 }
2940 }
2941 return 0;
2942}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002943
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002944
Laurence Lundbladeee851742020-01-08 08:37:05 -08002945// [1, [2, 3]]
2946static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2947// No closing break
2948static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2949// Not enough closing breaks
2950static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2951// Too many closing breaks
2952static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2953// Unclosed indeflen inside def len
2954static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2955// confused tag
2956static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002957
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002958int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002959{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002960 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002961 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002962 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002963
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002964 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302965 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002966 QCBORDecodeContext DC;
2967 QCBORItem Item;
2968 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002969
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002970 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002971
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002972 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302973
2974 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2975 Item.uNestingLevel != 0 ||
2976 Item.uNextNestLevel != 1) {
2977 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002978 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002979
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002980 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302981 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2982 Item.uNestingLevel != 1 ||
2983 Item.uNextNestLevel != 1) {
2984 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002985 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002986
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002987 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302988 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2989 Item.uNestingLevel != 1 ||
2990 Item.uNextNestLevel != 2) {
2991 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002992 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002993
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002994 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002995 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302996 Item.uNestingLevel != 2 ||
2997 Item.uNextNestLevel != 2) {
2998 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003000
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003001 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003002 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303003 Item.uNestingLevel != 2 ||
3004 Item.uNextNestLevel != 0) {
3005 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003007
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003008 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303009 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003010 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003011
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003012 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003013 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003014
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003015 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003016
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003017 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003018
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003019 nResult = QCBORDecode_GetNext(&DC, &Item);
3020 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303021 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003022 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003023
Laurence Lundblade570fab52018-10-13 18:28:27 +08003024 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303025 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3026 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003027 }
3028
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003029
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003030 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003031 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003032
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003033 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003034
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003035 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003036
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003037 nResult = QCBORDecode_GetNext(&DC, &Item);
3038 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303039 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003040 }
3041
3042 nResult = QCBORDecode_GetNext(&DC, &Item);
3043 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303044 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003045 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003046
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003047 nResult = QCBORDecode_GetNext(&DC, &Item);
3048 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303049 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003050 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003051
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003052 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303053 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3054 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003056
3057
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003058 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003059 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003060
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003061 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003062
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003063 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003064
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003065 nResult = QCBORDecode_GetNext(&DC, &Item);
3066 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303067 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003068 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003069
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003070 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003071 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303072 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003073 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303074
Laurence Lundblade642282a2020-06-23 12:00:33 -07003075 nResult = QCBORDecode_GetNext(&DC, &Item);
3076 if(nResult != QCBOR_ERR_BAD_BREAK) {
3077 return -140;
3078 }
3079
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003080
Laurence Lundblade570fab52018-10-13 18:28:27 +08003081 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003082 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003083
Laurence Lundblade570fab52018-10-13 18:28:27 +08003084 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003085
Laurence Lundblade570fab52018-10-13 18:28:27 +08003086 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003087
Laurence Lundblade570fab52018-10-13 18:28:27 +08003088 nResult = QCBORDecode_GetNext(&DC, &Item);
3089 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303090 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003091 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003092
Laurence Lundblade570fab52018-10-13 18:28:27 +08003093 nResult = QCBORDecode_GetNext(&DC, &Item);
3094 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303095 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003096 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003097
Laurence Lundblade570fab52018-10-13 18:28:27 +08003098 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303099 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3100 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003101 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003102
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303103 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003104 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003105
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303106 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003107
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303108 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003109
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303110 nResult = QCBORDecode_GetNext(&DC, &Item);
3111 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303112 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003114
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303115 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303116 if(nResult != QCBOR_ERR_BAD_BREAK) {
3117 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003119
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003120 return 0;
3121}
3122
Laurence Lundblade17ede402018-10-13 11:43:07 +08003123
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003124static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003125 0x81, // Array of length one
3126 0x7f, // text string marked with indefinite length
3127 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3128 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3129 0xff // ending break
3130};
3131
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003132static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303133 0x81, // Array of length one
3134 0x7f, // text string marked with indefinite length
3135 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3136 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3137 0xff // ending break
3138};
3139
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003140static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303141 0x81, // Array of length one
3142 0x7f, // text string marked with indefinite length
3143 0x01, 0x02, // Not a string
3144 0xff // ending break
3145};
3146
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003147static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303148 0x81, // Array of length one
3149 0x7f, // text string marked with indefinite length
3150 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3151 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3152 // missing end of string
3153};
3154
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003155static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303156 0xa1, // Array of length one
3157 0x7f, // text string marked with indefinite length
3158 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3159 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3160 0xff, // ending break
3161 0x01 // integer being labeled.
3162};
3163
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003164/**
3165 Make an indefinite length string
3166
3167 @param Storage Storage for string, must be 144 bytes in size
3168 @return The indefinite length string
3169
3170 This makes an array with one indefinite length string that has 7 chunks
3171 from size of 1 byte up to 64 bytes.
3172 */
3173static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303174{
3175 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003176
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303177 UsefulOutBuf_Init(&UOB, Storage);
3178 UsefulOutBuf_AppendByte(&UOB, 0x81);
3179 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003180
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003181 uint8_t uStringByte = 0;
3182 // Use of type int is intentional
3183 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3184 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303185 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003186 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3187 for(int j = 0; j < uChunkSize; j++) {
3188 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3189 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303190 }
3191 }
3192 UsefulOutBuf_AppendByte(&UOB, 0xff);
3193
3194 return UsefulOutBuf_OutUBuf(&UOB);
3195}
3196
3197static int CheckBigString(UsefulBufC BigString)
3198{
3199 if(BigString.len != 255) {
3200 return 1;
3201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003202
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303203 for(uint8_t i = 0; i < 255; i++){
3204 if(((const uint8_t *)BigString.ptr)[i] != i) {
3205 return 1;
3206 }
3207 }
3208 return 0;
3209}
3210
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303211
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003212int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303213{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303214 QCBORDecodeContext DC;
3215 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303216 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003217 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003218
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303219 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003220 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303221 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003222
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303223 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303224 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003226
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303227 if(QCBORDecode_GetNext(&DC, &Item)) {
3228 return -2;
3229 }
3230 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3231 return -3;
3232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003233
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303234 if(QCBORDecode_GetNext(&DC, &Item)) {
3235 return -4;
3236 }
3237 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3238 return -5;
3239 }
3240 if(QCBORDecode_Finish(&DC)) {
3241 return -6;
3242 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303243
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303244 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003245 QCBORDecode_Init(&DC,
3246 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3247 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003248
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303249 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3250 return -7;
3251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003252
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303253 if(QCBORDecode_GetNext(&DC, &Item)) {
3254 return -8;
3255 }
3256 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3257 return -9;
3258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003259
Laurence Lundblade30816f22018-11-10 13:40:22 +07003260 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303261 return -10;
3262 }
3263
3264 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003265 QCBORDecode_Init(&DC,
3266 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3267 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003268
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303269 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3270 return -11;
3271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003272
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303273 if(QCBORDecode_GetNext(&DC, &Item)) {
3274 return -12;
3275 }
3276 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3277 return -13;
3278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003279
Laurence Lundblade30816f22018-11-10 13:40:22 +07003280 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303281 return -14;
3282 }
3283
3284 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003285 QCBORDecode_Init(&DC,
3286 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3287 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 -15;
3291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003292
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303293 if(QCBORDecode_GetNext(&DC, &Item)) {
3294 return -16;
3295 }
3296 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3297 return -17;
3298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003299
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303300 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3301 return -18;
3302 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003303
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303304 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303305 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003306
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303307 QCBORDecode_GetNext(&DC, &Item);
3308 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303309 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003311
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303312 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303313 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003315
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303316 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003317 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303318
3319 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3320 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303321 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303322 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003323
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303324 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303325 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003326 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003327
Laurence Lundbladeee851742020-01-08 08:37:05 -08003328 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3329 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003330
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303331 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303332 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303333 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003335
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303336 QCBORDecode_GetNext(&DC, &Item);
3337 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303338 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303339 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003340 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303341 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303342 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003343
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303344 // ---- big bstr -----
3345 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003346
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303347 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3348 return -25;
3349 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003350
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303351 if(QCBORDecode_GetNext(&DC, &Item)) {
3352 return -26;
3353 }
3354 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303355 return -26;
3356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003357
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303358 if(QCBORDecode_GetNext(&DC, &Item)) {
3359 return -27;
3360 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303361 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303362 return -28;
3363 }
3364 if(CheckBigString(Item.val.string)) {
3365 return -3;
3366 }
3367 if(QCBORDecode_Finish(&DC)) {
3368 return -29;
3369 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003370
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303371 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003372 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003373
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303374 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3375 return -30;
3376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003377
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303378 QCBORDecode_GetNext(&DC, &Item);
3379 if(Item.uDataType != QCBOR_TYPE_MAP) {
3380 return -31;
3381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003382
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303383 if(QCBORDecode_GetNext(&DC, &Item)){
3384 return -32;
3385 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003386 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3387 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303388 Item.uDataAlloc || !Item.uLabelAlloc ||
3389 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3390 return -33;
3391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003392
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303393 if(QCBORDecode_Finish(&DC)) {
3394 return -34;
3395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003396
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003397 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003398}
3399
3400
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003401int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303402{
3403 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003404 QCBORError nCBORError;
3405
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003406
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303407 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003408 QCBORDecode_Init(&DC,
3409 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3410 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003411
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003412 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003413
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003414 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3415 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303416 return -1;
3417 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003418
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003419 if(CheckCSRMaps(&DC)) {
3420 return -2;
3421 }
3422
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303423 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003424 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003425 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003426
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303427 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003428 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303429 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003430
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303431 QCBORItem Item1, Item2, Item3, Item4;
3432 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003433 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303434 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3435 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003436 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303437 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003438 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303439 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003440 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303441 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003442 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303443 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003444 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003445
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303446 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003447
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303448 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303449 Item1.uDataType != QCBOR_TYPE_INT64 ||
3450 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003451 Item1.uDataAlloc != 0 ||
3452 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003453 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003454 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003455 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003456
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303457
3458 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003459 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303460 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003461 Item2.uDataAlloc != 0 ||
3462 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303463 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003464 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003465
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303466 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003467 Item3.uDataAlloc == 0 ||
3468 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003469 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003470 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003471 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003472
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303473 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003474 Item4.uDataAlloc == 0 ||
3475 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003476 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003477 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003479
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303480 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003481 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003482 QCBORDecode_Init(&DC,
3483 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3484 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303485 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3486 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003487 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303488 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003489 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003490 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003491 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303492 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3493 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3494 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3495 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3496 }
3497 }
3498 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003499 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003500 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303501 }
3502
3503 return 0;
3504}
3505
Laurence Lundbladef6531662018-12-04 10:42:22 +09003506
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303507
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003508int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003509{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003510 // Set up the decoder with a tiny bit of CBOR to parse because
3511 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003512 QCBORDecodeContext DC;
3513 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3514 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003515
Laurence Lundbladef6531662018-12-04 10:42:22 +09003516 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003517 // Then fish into the internals of the decode context
3518 // to get the allocator function so it can be called directly.
3519 // Also figure out how much pool is available for use
3520 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003521 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003522 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3523 if(nError) {
3524 return -9;
3525 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003526 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3527 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3528 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003529
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003530 // First test -- ask for one more byte than available and see failure
3531 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003532 if(!UsefulBuf_IsNULL(Allocated)) {
3533 return -1;
3534 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003535
Laurence Lundbladef6531662018-12-04 10:42:22 +09003536 // Re do the set up for the next test that will do a successful alloc,
3537 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003538 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003539 pAlloc = DC.StringAllocator.pfAllocator;
3540 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3541 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003542
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003543 // Allocate one byte less than available and see success
3544 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003545 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3546 return -2;
3547 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003548 // Ask for some more and see failure
3549 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003550 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3551 return -3;
3552 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003553 // Free the first allocate, retry the second and see success
3554 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3555 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003556 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3557 return -4;
3558 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003559
Laurence Lundbladef6531662018-12-04 10:42:22 +09003560 // Re do set up for next test that involves a successful alloc,
3561 // and a successful realloc and a failed realloc
3562 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003563 pAlloc = DC.StringAllocator.pfAllocator;
3564 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003565
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003566 // Allocate half the pool and see success
3567 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003568 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3569 return -5;
3570 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003571 // Reallocate to take up the whole pool and see success
3572 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003573 if(UsefulBuf_IsNULL(Allocated2)) {
3574 return -6;
3575 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003576 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003577 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3578 return -7;
3579 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003580 // Try to allocate more to be sure there is failure after a realloc
3581 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3582 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003583 return -8;
3584 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003585
Laurence Lundbladef6531662018-12-04 10:42:22 +09003586 return 0;
3587}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003588
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003589
3590/* Just enough of an allocator to test configuration of one */
3591static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3592{
3593 (void)pOldMem; // unused variable
3594
3595 if(uNewSize) {
3596 // Assumes the context pointer is the buffer and
3597 // nothing too big will ever be asked for.
3598 // This is only good for this basic test!
3599 return (UsefulBuf) {pCtx, uNewSize};
3600 } else {
3601 return NULLUsefulBuf;
3602 }
3603}
3604
3605
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003606int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003607{
3608 // Set up the decoder with a tiny bit of CBOR to parse because
3609 // nothing can be done with it unless that is set up.
3610 QCBORDecodeContext DC;
3611 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3612 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3613
3614 uint8_t pAllocatorBuffer[50];
3615
3616 // This is really just to test that this call works.
3617 // The full functionality of string allocators is tested
3618 // elsewhere with the MemPool internal allocator.
3619 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3620
3621 QCBORItem Item;
3622 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3623 return -1;
3624 }
3625
3626 if(Item.uDataAlloc == 0 ||
3627 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3628 Item.val.string.ptr != pAllocatorBuffer) {
3629 return -2;
3630 }
3631
3632 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3633 return -3;
3634 }
3635
3636 return 0;
3637}
3638
Laurence Lundblade59289e52019-12-30 13:44:37 -08003639
3640#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladea826c502020-05-10 21:07:00 -07003641/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08003642 [
3643 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003644 4([-20, 4759477275222530853136]),
3645 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003646 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003647 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003648 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07003649 5([ 9223372036854775806, -4759477275222530853137])
3650 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003651 ]
3652 */
3653
3654static const uint8_t spExpectedExponentsAndMantissas[] = {
3655 0x87,
3656 0xC4, 0x82, 0x20,
3657 0x03,
3658 0xC4, 0x82, 0x33,
3659 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3660 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3661 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3662 0xC5, 0x82, 0x19, 0x01, 0x2C,
3663 0x18, 0x64,
3664 0xC5, 0x82, 0x33,
3665 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3666 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3667 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07003668 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3669 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003670 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3671 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3672};
3673
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003674int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003675{
3676 QCBORDecodeContext DC;
3677 QCBORError nCBORError;
3678 QCBORItem item;
3679
Laurence Lundblade17af4902020-01-07 19:11:55 -08003680 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3681 0x06, 0x07, 0x08, 0x09, 0x010};
3682 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003683
3684
3685 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3686
3687 nCBORError = QCBORDecode_GetNext(&DC, &item);
3688 if(nCBORError != QCBOR_SUCCESS) {
3689 return 1;
3690 }
3691
3692 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3693 return 2;
3694 }
3695
3696 nCBORError = QCBORDecode_GetNext(&DC, &item);
3697 if(nCBORError != QCBOR_SUCCESS) {
3698 return 3;
3699 }
3700
3701 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3702 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3703 item.val.expAndMantissa.nExponent != -1) {
3704 return 4;
3705 }
3706
3707 nCBORError = QCBORDecode_GetNext(&DC, &item);
3708 if(nCBORError != QCBOR_SUCCESS) {
3709 return 5;
3710 }
3711
3712 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3713 item.val.expAndMantissa.nExponent != -20 ||
3714 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3715 return 6;
3716 }
3717
3718 nCBORError = QCBORDecode_GetNext(&DC, &item);
3719 if(nCBORError != QCBOR_SUCCESS) {
3720 return 7;
3721 }
3722
3723 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3724 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3725 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3726 return 8;
3727 }
3728
3729 nCBORError = QCBORDecode_GetNext(&DC, &item);
3730 if(nCBORError != QCBOR_SUCCESS) {
3731 return 9;
3732 }
3733
3734 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3735 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3736 item.val.expAndMantissa.nExponent != 300) {
3737 return 10;
3738 }
3739
Laurence Lundbladea826c502020-05-10 21:07:00 -07003740 // 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003741 nCBORError = QCBORDecode_GetNext(&DC, &item);
3742 if(nCBORError != QCBOR_SUCCESS) {
3743 return 11;
3744 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003745 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3746 item.val.expAndMantissa.nExponent != -20 ||
3747 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3748 return 12;
3749 }
3750
Laurence Lundbladea826c502020-05-10 21:07:00 -07003751 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade59289e52019-12-30 13:44:37 -08003752 nCBORError = QCBORDecode_GetNext(&DC, &item);
3753 if(nCBORError != QCBOR_SUCCESS) {
3754 return 13;
3755 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003756 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3757 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3758 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3759 return 14;
3760 }
3761
Laurence Lundbladea826c502020-05-10 21:07:00 -07003762 // 5([ 9223372036854775806, -4759477275222530853137])
3763 nCBORError = QCBORDecode_GetNext(&DC, &item);
3764 if(nCBORError != QCBOR_SUCCESS) {
3765 return 13;
3766 }
3767 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3768 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3769 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3770 return 14;
3771 }
3772
3773
3774 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003775 nCBORError = QCBORDecode_GetNext(&DC, &item);
3776 if(nCBORError != QCBOR_SUCCESS) {
3777 return 15;
3778 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003779 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3780 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3781 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3782 return 16;
3783 }
3784
3785 /* Now encode some stuff and then decode it */
3786 uint8_t pBuf[40];
3787 QCBOREncodeContext EC;
3788 UsefulBufC Encoded;
3789
3790 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3791 QCBOREncode_OpenArray(&EC);
3792 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3793 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3794 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3795 QCBOREncode_CloseArray(&EC);
3796 QCBOREncode_Finish(&EC, &Encoded);
3797
3798
3799 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3800 nCBORError = QCBORDecode_GetNext(&DC, &item);
3801 if(nCBORError != QCBOR_SUCCESS) {
3802 return 13;
3803 }
3804
3805 nCBORError = QCBORDecode_GetNext(&DC, &item);
3806 if(nCBORError != QCBOR_SUCCESS) {
3807 return 13;
3808 }
3809
3810 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3811 item.val.expAndMantissa.nExponent != 1000 ||
3812 item.val.expAndMantissa.Mantissa.nInt != 999) {
3813 return 15;
3814 }
3815
3816 nCBORError = QCBORDecode_GetNext(&DC, &item);
3817 if(nCBORError != QCBOR_SUCCESS) {
3818 return 13;
3819 }
3820
3821 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3822 item.val.expAndMantissa.nExponent != INT32_MIN ||
3823 item.val.expAndMantissa.Mantissa.nInt != 100) {
3824 return 15;
3825 }
3826
3827 nCBORError = QCBORDecode_GetNext(&DC, &item);
3828 if(nCBORError != QCBOR_SUCCESS) {
3829 return 13;
3830 }
3831
3832 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3833 item.val.expAndMantissa.nExponent != INT32_MAX ||
3834 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3835 return 12;
3836 }
3837
3838 return 0;
3839}
3840
3841
3842static struct FailInput ExponentAndMantissaFailures[] = {
3843 // Exponent > INT64_MAX
3844 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3845 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3846 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3847 // Mantissa > INT64_MAX
3848 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3849 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3850 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3851 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003852 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08003853 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003854 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08003855 // bad content for big num
3856 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3857 // bad content for big num
3858 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3859 // Bad integer for exponent
3860 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3861 // Bad integer for mantissa
3862 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3863 // 3 items in array
3864 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3865 // unterminated indefinite length array
3866 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3867 // Empty array
3868 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3869 // Second is not an integer
3870 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3871 // First is not an integer
3872 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3873 // Not an array
3874 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3875};
3876
3877
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003878int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003879{
3880 return ProcessFailures(ExponentAndMantissaFailures,
3881 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3882}
3883
3884#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003885
3886
3887
3888/*
3889 Some basic CBOR with map and array used in a lot of tests.
3890 The map labels are all strings
3891
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003892 {
3893 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003894 "an array of two strings": [
3895 "string1", "string2"
3896 ],
3897 "map in a map": {
3898 "bytes 1": h'78787878',
3899 "bytes 2": h'79797979',
3900 "another int": 98,
3901 "text 2": "lies, damn lies and statistics"
3902 }
3903 }
3904 */
3905
Laurence Lundbladeeb0b53d2020-04-14 18:34:25 -07003906#include "qcbor/qcbor_decode_map.h"
Laurence Lundblade1341c592020-04-11 14:19:05 -07003907#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003908
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003909static char strbuf[10];
3910const char *PrintType(uint8_t type) {
3911 switch(type) {
3912 case QCBOR_TYPE_INT64: return "INT64";
3913 case QCBOR_TYPE_UINT64: return "UINT64";
3914 case QCBOR_TYPE_ARRAY: return "ARRAY";
3915 case QCBOR_TYPE_MAP: return "MAP";
3916 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
3917 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
3918 default:
3919 sprintf(strbuf, "%d", type);
3920 return strbuf;
3921 }
3922}
3923
3924
3925void PrintItem(QCBORItem Item)
3926{
3927 printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":"");
3928 if(Item.uLabelType) {
3929 printf("Label: %s ", PrintType(Item.uLabelType));
3930 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3931 printf("%lld\n", Item.label.int64);
3932 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07003933 // TODO: proper conversion to null-terminated string
3934 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003935 }
3936 }
3937}
3938
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003939
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07003940int32_t EMap(UsefulBufC input)
3941{
3942 QCBORItem Item1, Item2, Item3;
3943 int64_t nDecodedInt1, nDecodedInt2;
3944 UsefulBufC B1, B2, S1, S2, S3;
3945
3946 QCBORDecodeContext DCtx;
3947 QCBORError nCBORError;
3948
3949 QCBORDecode_Init(&DCtx, input, 0);
3950
3951 QCBORDecode_EnterMap(&DCtx);
3952
3953 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
3954
3955 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
3956 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
3957 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1);
3958 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2);
3959 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
3960 QCBORDecode_ExitMap(&DCtx);
3961
3962 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3963 QCBORDecode_GetNext(&DCtx, &Item1);
3964 QCBORDecode_GetNext(&DCtx, &Item2);
3965 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
3966 return -400;
3967 }
3968 QCBORDecode_ExitArray(&DCtx);
3969
3970 // Parse the same array again using GetText() instead of GetItem()
3971 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3972 QCBORDecode_GetText(&DCtx, &S2);
3973 QCBORDecode_GetText(&DCtx, &S3);
3974 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
3975 return 5000;
3976 }
3977 /* QCBORDecode_GetText(&DCtx, &S3);
3978 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
3979 return 5001;
3980 } */
3981
3982 QCBORDecode_ExitArray(&DCtx);
3983
3984 QCBORDecode_ExitMap(&DCtx);
3985
3986 nCBORError = QCBORDecode_Finish(&DCtx);
3987
3988 if(nCBORError) {
3989 return (int32_t)nCBORError;
3990 }
3991
3992 if(nDecodedInt1 != 42) {
3993 return 1001;
3994 }
3995
3996 if(nDecodedInt2 != 98) {
3997 return 1002;
3998 }
3999
4000 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4001 UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){
4002 return 1003;
4003 }
4004
4005 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4006 UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){
4007 return 1004;
4008 }
4009
4010 if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){
4011 return 1005;
4012 }
4013
4014 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4015 return 1006;
4016 }
4017
4018 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4019 return 1007;
4020 }
4021
4022 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4023 return 1008;
4024 }
4025
4026 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4027 return 1009;
4028 }
4029
4030 return 0;
4031}
4032
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004033
4034/*
4035 [23,
4036 6000,
4037 h'67616C6163746963',
4038 h'686176656E20746F6B656E'
4039 ]
4040 */
4041static const uint8_t spSimpleArray[] = {
40420x84, 0x17, 0x19, 0x17, 0x70, 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
4043
4044
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004045static const uint8_t spEmptyMap[] = {0xa0};
4046
4047static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004048
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004049static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4050
Laurence Lundbladef0499502020-08-01 11:55:57 -07004051/*
4052 {
4053 0: [],
4054 9: [
4055 [],
4056 []
4057 ],
4058 8: {
4059 1: [],
4060 2: {},
4061 3: []
4062 },
4063 4: {},
4064 5: [],
4065 6: [
4066 [],
4067 []
4068 ]
4069 }
4070 */
4071static const uint8_t spMapOfEmpty[] = {
4072 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4073 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4074 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004075
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004076int32_t EnterMapTest()
4077{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004078 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004079 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004080 int32_t nReturn;
4081 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004082
4083
4084 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4085 QCBORDecode_EnterMap(&DCtx);
4086
Laurence Lundbladef0499502020-08-01 11:55:57 -07004087
4088 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004089 QCBORDecode_ExitArray(&DCtx);
4090
Laurence Lundbladef0499502020-08-01 11:55:57 -07004091 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004092 QCBORDecode_EnterArray(&DCtx);
4093 QCBORDecode_ExitArray(&DCtx);
4094 QCBORDecode_EnterArray(&DCtx);
4095 QCBORDecode_ExitArray(&DCtx);
4096 QCBORDecode_ExitArray(&DCtx);
4097
Laurence Lundbladef0499502020-08-01 11:55:57 -07004098 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004099 QCBORDecode_EnterArray(&DCtx);
4100 QCBORDecode_ExitArray(&DCtx);
4101 QCBORDecode_EnterMap(&DCtx);
4102 QCBORDecode_ExitMap(&DCtx);
4103 QCBORDecode_EnterArray(&DCtx);
4104 QCBORDecode_ExitArray(&DCtx);
4105 QCBORDecode_ExitMap(&DCtx);
4106
Laurence Lundbladef0499502020-08-01 11:55:57 -07004107 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004108 QCBORDecode_ExitMap(&DCtx);
4109
Laurence Lundbladef0499502020-08-01 11:55:57 -07004110 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004111 QCBORDecode_ExitArray(&DCtx);
4112
Laurence Lundbladef0499502020-08-01 11:55:57 -07004113 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004114 QCBORDecode_EnterArray(&DCtx);
4115 QCBORDecode_ExitArray(&DCtx);
4116 QCBORDecode_EnterArray(&DCtx);
4117 QCBORDecode_ExitArray(&DCtx);
4118 QCBORDecode_ExitArray(&DCtx);
4119
4120 QCBORDecode_ExitMap(&DCtx);
4121
4122 uErr = QCBORDecode_Finish(&DCtx);
4123 if(uErr != QCBOR_SUCCESS){
4124 return 3011;
4125 }
4126
4127
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004128 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004129 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004130 if(nReturn) {
4131 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004132 }
4133
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004134 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004135 if(nReturn) {
4136 return nReturn;
4137 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004138
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004139
Laurence Lundblade937ea812020-05-08 11:38:23 -07004140
4141 // These tests confirm the cursor is at the right place after entering a map or array
4142
4143 // Confirm cursor is at right place
4144 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4145 QCBORDecode_EnterMap(&DCtx);
4146 QCBORDecode_GetNext(&DCtx, &Item1);
4147 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4148 return 2001;
4149 }
4150
4151
4152 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4153 QCBORDecode_GetNext(&DCtx, &Item1);
4154 QCBORDecode_GetNext(&DCtx, &Item1);
4155 QCBORDecode_EnterArray(&DCtx);
4156 QCBORDecode_GetNext(&DCtx, &Item1);
4157 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4158 return 2002;
4159 }
4160
4161 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4162 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004163 QCBORDecode_GetNext(&DCtx, &Item1);
4164 QCBORDecode_GetNext(&DCtx, &Item1);
4165 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004166 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4167 QCBORDecode_GetNext(&DCtx, &Item1);
4168 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4169 return 2003;
4170 }
4171
4172 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4173 QCBORDecode_EnterMap(&DCtx);
4174 QCBORDecode_GetNext(&DCtx, &Item1);
4175 QCBORDecode_GetNext(&DCtx, &Item1);
4176 QCBORDecode_GetNext(&DCtx, &Item1);
4177 QCBORDecode_GetNext(&DCtx, &Item1);
4178 QCBORDecode_GetNext(&DCtx, &Item1);
4179 QCBORDecode_GetNext(&DCtx, &Item1);
4180 QCBORDecode_GetNext(&DCtx, &Item1);
4181 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4182 QCBORDecode_GetNext(&DCtx, &Item1);
4183 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004184 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004185 }
4186
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004187 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4188 QCBORDecode_EnterMap(&DCtx);
4189 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4190 QCBORDecode_ExitArray(&DCtx);
4191 QCBORDecode_GetNext(&DCtx, &Item1);
4192 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4193 return 2006;
4194 }
4195 QCBORDecode_ExitMap(&DCtx);
4196 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4197 return 2007;
4198 }
4199
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004200 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4201 QCBORDecode_EnterArray(&DCtx);
4202 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004203 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4204 uErr = QCBORDecode_GetAndResetError(&DCtx);
4205 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004206 return 2008;
4207 }
4208 UsefulBufC String;
4209 QCBORDecode_GetTextInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004210 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004211 return 2009;
4212 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004213
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004214
4215 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4216 QCBORDecode_EnterMap(&DCtx);
4217 // This will fail because the map is empty.
4218 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4219 uErr = QCBORDecode_GetAndResetError(&DCtx);
4220 if(uErr != QCBOR_ERR_NOT_FOUND){
4221 return 2010;
4222 }
4223 QCBORDecode_ExitMap(&DCtx);
4224 uErr = QCBORDecode_Finish(&DCtx);
4225 if(uErr != QCBOR_SUCCESS){
4226 return 2011;
4227 }
4228
4229
4230 // TODO: more testing of nested zero length maps; also test zero-length arrays
4231 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4232 QCBORDecode_EnterMap(&DCtx);
4233 // This will fail because the map is empty.
4234 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4235 uErr = QCBORDecode_GetAndResetError(&DCtx);
4236 if(uErr != QCBOR_ERR_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004237 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004238 }
4239 QCBORDecode_ExitMap(&DCtx);
4240 uErr = QCBORDecode_Finish(&DCtx);
4241 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004242 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004243 }
4244
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004245
4246 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
4247 QCBORDecode_EnterArray(&DCtx);
4248 QCBORDecode_GetBytes(&DCtx, &String);
4249 QCBORDecode_EnterMap(&DCtx);
4250 QCBORDecode_ExitMap(&DCtx);
4251 QCBORDecode_EnterArray(&DCtx);
4252 QCBORDecode_ExitArray(&DCtx);
4253 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
4254 QCBORDecode_ExitArray(&DCtx);
4255 uErr = QCBORDecode_Finish(&DCtx);
4256 if(uErr != QCBOR_SUCCESS){
4257 return 2014;
4258 }
4259
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004260 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004261}
4262
4263
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004264struct NumberConversion {
4265 char *szDescription;
4266 UsefulBufC CBOR;
4267 int64_t nConvertedToInt64;
4268 QCBORError uErrorInt64;
4269 uint64_t uConvertToUInt64;
4270 QCBORError uErrorUint64;
4271 double dConvertToDouble;
4272 QCBORError uErrorDouble;
4273};
4274
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004275static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004276 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004277 "negative bignum -1",
4278 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
4279 -1,
4280 QCBOR_SUCCESS,
4281 0,
4282 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4283 -1.0,
4284 QCBOR_SUCCESS
4285 },
4286 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004287 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004288 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4289 0xC2, 0x42, 0x01, 0x01}, 15},
4290 257000,
4291 QCBOR_SUCCESS,
4292 257000,
4293 QCBOR_SUCCESS,
4294 257000.0,
4295 QCBOR_SUCCESS
4296 },
4297 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004298 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004299 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4300 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07004301 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07004302 QCBOR_SUCCESS,
4303 0,
4304 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07004305 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07004306 QCBOR_SUCCESS
4307 },
4308 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004309 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004310 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4311 0xC2, 0x42, 0x01, 0x01}, 15},
4312 2056,
4313 QCBOR_SUCCESS,
4314 2056,
4315 QCBOR_SUCCESS,
4316 2056.0,
4317 QCBOR_SUCCESS
4318 },
4319 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004320 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07004321 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4322 0,
4323 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4324 0,
4325 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4326 -18446744073709551617.0,
4327 QCBOR_SUCCESS
4328 },
4329 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004330 "Positive bignum 0x01020304 indefinite length string",
4331 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
4332 0x01020304,
4333 QCBOR_SUCCESS,
4334 0x01020304,
4335 QCBOR_SUCCESS,
4336 16909060.0,
4337 QCBOR_SUCCESS
4338 },
4339 {
Laurence Lundblade887add82020-05-17 05:50:34 -07004340 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07004341 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4342 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
4343 0,
4344 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4345 0,
4346 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4347 -INFINITY,
4348 QCBOR_SUCCESS
4349 },
4350 {
4351 "big float [9223372036854775806, 9223372036854775806]",
4352 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4353 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
4354 0,
4355 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4356 0,
4357 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4358 INFINITY,
4359 QCBOR_SUCCESS
4360 },
4361 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004362 "Big float 3 * 2^^2",
4363 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
4364 12,
4365 QCBOR_SUCCESS,
4366 12,
4367 QCBOR_SUCCESS,
4368 12.0,
4369 QCBOR_SUCCESS
4370 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004371 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004372 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004373 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4374 0,
4375 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4376 18446744073709551615ULL,
4377 QCBOR_SUCCESS,
4378 18446744073709551615.0,
4379 QCBOR_SUCCESS
4380 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004381 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004382 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004383 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4384 65536-1,
4385 QCBOR_SUCCESS,
4386 0xffff,
4387 QCBOR_SUCCESS,
4388 65535.0,
4389 QCBOR_SUCCESS
4390 },
4391 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004392 "Postive integer 0",
4393 {(uint8_t[]){0x0}, 1},
4394 0LL,
4395 QCBOR_SUCCESS,
4396 0ULL,
4397 QCBOR_SUCCESS,
4398 0.0,
4399 QCBOR_SUCCESS
4400 },
4401 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004402 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004403 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4404 -9223372036854775807-1, // INT64_MIN
4405 QCBOR_SUCCESS,
4406 0ULL,
4407 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4408 -9223372036854775808.0,
4409 QCBOR_SUCCESS
4410 },
4411 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004412 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004413 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
4414 100L,
4415 QCBOR_SUCCESS,
4416 100ULL,
4417 QCBOR_SUCCESS,
4418 100.3,
4419 QCBOR_SUCCESS
4420 },
4421 {
4422 "Floating point value NaN 0xfa7fc00000",
4423 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
4424 0,
4425 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4426 0,
4427 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4428 NAN,
4429 QCBOR_SUCCESS
4430 },
4431 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004432 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004433 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
4434 -4,
4435 QCBOR_SUCCESS,
4436 0,
4437 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4438 -4.0,
4439 QCBOR_SUCCESS
4440 },
4441 {
4442 "Decimal fraction 3/10",
4443 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
4444 0,
4445 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4446 0,
4447 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4448 0.30000000000000004,
4449 QCBOR_SUCCESS
4450 }
4451};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004452
4453
4454
Laurence Lundblade313b2862020-05-16 01:23:06 -07004455int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004456{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004457 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004458
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004459 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
4460 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004461
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004462 // Set up the decoding context including a memory pool so that
4463 // indefinite length items can be checked
4464 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004465 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004466
4467 /* ----- test conversion to int64_t ------ */
4468 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004469 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4470 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004471 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004472 }
4473
4474 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004475 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004476 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004477 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004478 }
4479 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004480 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004481 }
4482
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004483 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004484 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4485 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4486 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004487 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004488 }
4489 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004490 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004491 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004492 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004493 }
4494 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004495 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004496 }
4497
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004498 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004499 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4500 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4501 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004502 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004503 }
4504 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004505 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004506 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004507 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004508 }
4509 if(pF->uErrorDouble == QCBOR_SUCCESS) {
4510 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004511 // NaN's can't be compared for equality. A NaN is
4512 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004513 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004514 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004515 }
4516 } else {
4517 // TODO: this comparison may need a margin of error
4518 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004519 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004520 }
4521 }
4522 }
4523 }
4524
4525 return 0;
4526}
4527
Laurence Lundblade313b2862020-05-16 01:23:06 -07004528int32_t IntegerConvertTestOld()
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004529{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004530 QCBORDecodeContext DCtx;
4531 QCBORError nCBORError;
4532
Laurence Lundbladea826c502020-05-10 21:07:00 -07004533 /* exponent, mantissa
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004534 [
4535 4([-1, 3]),
4536 4([-20, 4759477275222530853136]),
4537 4([9223372036854775807, -4759477275222530853137]),
4538 5([300, 100]),
4539 5([-20, 4759477275222530853136]),
4540 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004541 5([ 9223372036854775806, -4759477275222530853137])
4542 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004543 ]
4544 */
4545
4546 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4547
4548 QCBORItem Item;
4549 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4550 if(nCBORError) {
4551 return -1;
4552 }
4553
4554 int64_t integer;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004555 // 4([-1, 3]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004556 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004557 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004558 return -2;
4559 }
4560 DCtx.uLastError = 0; // TODO: a method for this
4561
Laurence Lundbladea826c502020-05-10 21:07:00 -07004562 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004563 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004564 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004565 return -2;
4566 }
4567 DCtx.uLastError = 0; // TODO: a method for this
4568
Laurence Lundbladea826c502020-05-10 21:07:00 -07004569 // 4([9223372036854775807, -4759477275222530853137]),
4570 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004571 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004572 return -2;
4573 }
4574 DCtx.uLastError = 0; // TODO: a method for this
4575
4576 // 5([300, 100]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004577 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004578 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004579 return -2;
4580 }
4581 DCtx.uLastError = 0; // TODO: a method for this
4582
Laurence Lundbladea826c502020-05-10 21:07:00 -07004583 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004584 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004585 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004586 return -2;
4587 }
4588 DCtx.uLastError = 0; // TODO: a method for this
4589
Laurence Lundbladea826c502020-05-10 21:07:00 -07004590 // 5([-9223372036854775807, -4759477275222530853137])
4591 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004592 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004593 return -2;
4594 }
4595 DCtx.uLastError = 0; // TODO: a method for this
4596
4597 // 5([ 9223372036854775806, -4759477275222530853137])
4598 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004599 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004600 return -2;
4601 }
4602 DCtx.uLastError = 0; // TODO: a method for this
4603
4604 // 5([ 9223372036854775806, 9223372036854775806])]
4605 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004606 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004607 return -2;
4608 }
4609 DCtx.uLastError = 0; // TODO: a method for this
4610
4611
4612
4613 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4614
4615 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4616 if(nCBORError) {
4617 return -1;
4618 }
4619
4620 uint64_t uinteger;
4621 // 4([-1, 3]),
4622 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004623 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004624 return -2;
4625 }
4626 DCtx.uLastError = 0; // TODO: a method for this
4627
4628 // 4([-20, 4759477275222530853136]),
4629 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004630 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004631 return -2;
4632 }
4633 DCtx.uLastError = 0; // TODO: a method for this
4634
4635 // 4([9223372036854775807, -4759477275222530853137]),
4636 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004637 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004638 return -2;
4639 }
4640 DCtx.uLastError = 0; // TODO: a method for this
4641
4642 // 5([300, 100]),
4643 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004644 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004645 return -2;
4646 }
4647 DCtx.uLastError = 0; // TODO: a method for this
4648
4649 // 5([-20, 4759477275222530853136]),
4650 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004651 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004652 return -2;
4653 }
4654 DCtx.uLastError = 0; // TODO: a method for this
4655
4656 // 5([-9223372036854775807, -4759477275222530853137])
4657 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004658 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004659 return -2;
4660 }
4661 DCtx.uLastError = 0; // TODO: a method for this
4662
4663 // 5([ 9223372036854775806, -4759477275222530853137])
4664 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004665 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004666 return -2;
4667 }
4668 DCtx.uLastError = 0; // TODO: a method for this
4669
4670 // 5([ 9223372036854775806, 9223372036854775806])]
4671 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004672 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004673 return -2;
4674 }
4675 DCtx.uLastError = 0; // TODO: a method for this
4676
4677
4678
4679 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4680 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4681 if(nCBORError) {
4682 return -1;
4683 }
4684
4685 double dResult;
4686 // 4([-1, 3]),
4687 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004688 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004689 dResult != 0.3) {
4690 return -2;
4691 }
4692
4693 // 4([-20, 4759477275222530853136]),
4694 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004695 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004696 dResult != 47.408855671161923) {
4697 return -2;
4698 }
4699
4700 // 4([9223372036854775807, -4759477275222530853137]),
4701 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004702 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004703 dResult != -INFINITY) {
4704 return -2;
4705 }
4706
4707 // 5([300, 100]),
4708 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004709 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004710 dResult != -INFINITY) {
4711 return -2;
4712 }
4713
4714 // 5([-20, 4759477275222530853136]),
4715 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004716 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004717 dResult != 4521260802379792.0) {
4718 return -2;
4719 }
4720
4721 // 5([-9223372036854775807, -4759477275222530853137])
4722 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004723 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004724 dResult != -0.0) {
4725 return -2;
4726 }
4727
4728 // 5([9223372036854775806, 9223372036854775806])]
4729 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004730 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004731 dResult != INFINITY) {
4732 return -2;
4733 }
4734
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004735 return 0;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004736}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07004737
4738
Laurence Lundbladee3553422020-05-02 11:11:17 -07004739int32_t CBORSequenceDecodeTests(void)
4740{
4741 QCBORDecodeContext DCtx;
4742 QCBORItem Item;
4743 QCBORError uCBORError;
4744
4745 // --- Test a sequence with extra bytes ---
4746
4747 // The input for the date test happens to be a sequence so it
4748 // is reused. It is a sequence because it doesn't start as
4749 // an array or map.
4750 QCBORDecode_Init(&DCtx,
4751 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
4752 QCBOR_DECODE_MODE_NORMAL);
4753
4754 // Get the first item
4755 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4756 if(uCBORError != QCBOR_SUCCESS) {
4757 return 1;
4758 }
4759 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
4760 return 2;
4761 }
4762
4763 // Get a second item
4764 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4765 if(uCBORError != QCBOR_SUCCESS) {
4766 return 2;
4767 }
4768 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
4769 return 3;
4770 }
4771
4772 // A sequence can have stuff at the end that may
4773 // or may not be valid CBOR. The protocol decoder knows
4774 // when to stop by definition of the protocol, not
4775 // when the top-level map or array is ended.
4776 // Finish still has to be called to know that
4777 // maps and arrays (if there were any) were closed
4778 // off correctly. When called like this it
4779 // must return the error QCBOR_ERR_EXTRA_BYTES.
4780 uCBORError = QCBORDecode_Finish(&DCtx);
4781 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
4782 return 4;
4783 }
4784
4785
4786 // --- Test an empty input ----
4787 uint8_t empty[1];
4788 UsefulBufC Empty = {empty, 0};
4789 QCBORDecode_Init(&DCtx,
4790 Empty,
4791 QCBOR_DECODE_MODE_NORMAL);
4792
4793 uCBORError = QCBORDecode_Finish(&DCtx);
4794 if(uCBORError != QCBOR_SUCCESS) {
4795 return 5;
4796 }
4797
4798
4799 // --- Sequence with unclosed indefinite length array ---
4800 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
4801
4802 QCBORDecode_Init(&DCtx,
4803 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
4804 QCBOR_DECODE_MODE_NORMAL);
4805
4806 // Get the first item
4807 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4808 if(uCBORError != QCBOR_SUCCESS) {
4809 return 7;
4810 }
4811 if(Item.uDataType != QCBOR_TYPE_INT64) {
4812 return 8;
4813 }
4814
4815 // Get a second item
4816 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4817 if(uCBORError != QCBOR_SUCCESS) {
4818 return 9;
4819 }
4820 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4821 return 10;
4822 }
4823
4824 // Try to finish before consuming all bytes to confirm
4825 // that the still-open error is returned.
4826 uCBORError = QCBORDecode_Finish(&DCtx);
4827 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
4828 return 11;
4829 }
4830
4831
4832 // --- Sequence with a closed indefinite length array ---
4833 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
4834
4835 QCBORDecode_Init(&DCtx,
4836 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
4837 QCBOR_DECODE_MODE_NORMAL);
4838
4839 // Get the first item
4840 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4841 if(uCBORError != QCBOR_SUCCESS) {
4842 return 12;
4843 }
4844 if(Item.uDataType != QCBOR_TYPE_INT64) {
4845 return 13;
4846 }
4847
4848 // Get a second item
4849 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4850 if(uCBORError != QCBOR_SUCCESS) {
4851 return 14;
4852 }
4853 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4854 return 15;
4855 }
4856
4857 // Try to finish before consuming all bytes to confirm
4858 // that the still-open error is returned.
4859 uCBORError = QCBORDecode_Finish(&DCtx);
4860 if(uCBORError != QCBOR_SUCCESS) {
4861 return 16;
4862 }
4863
4864
4865 return 0;
4866}
4867
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004868
Laurence Lundblade70ecead2020-06-15 19:40:06 -07004869
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004870int32_t IntToTests()
4871{
4872 int nErrCode;
4873 int32_t n32;
4874 int16_t n16;
4875 int8_t n8;
4876 uint32_t u32;
4877 uint16_t u16;
4878 uint8_t u8;
4879 uint64_t u64;
4880
4881 nErrCode = QCBOR_Int64ToInt32(1, &n32);
4882 if(nErrCode == -1 || n32 != 1) {
4883 return 1;
4884 }
4885
4886 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
4887 if(nErrCode == -1 || n32 != INT32_MAX) {
4888 return 2;
4889 }
4890
4891 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
4892 if(nErrCode == -1 || n32 != INT32_MIN) {
4893 return 3;
4894 }
4895
4896 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
4897 if(nErrCode != -1) {
4898 return 4;
4899 }
4900
4901 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
4902 if(nErrCode != -1) {
4903 return 5;
4904 }
4905
4906
4907 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
4908 if(nErrCode == -1 || n16 != INT16_MAX) {
4909 return 6;
4910 }
4911
4912 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
4913 if(nErrCode == -1 || n16 != INT16_MIN) {
4914 return 7;
4915 }
4916
4917 nErrCode = QCBOR_Int64ToInt16(1, &n16);
4918 if(nErrCode == -1 || n16 != 1) {
4919 return 8;
4920 }
4921
4922 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
4923 if(nErrCode != -1) {
4924 return 9;
4925 }
4926
4927 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
4928 if(nErrCode != -1) {
4929 return 10;
4930 }
4931
4932
4933 nErrCode = QCBOR_Int64ToInt8(1, &n8);
4934 if(nErrCode == -1 || n8 != 1) {
4935 return 11;
4936 }
4937
4938 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
4939 if(nErrCode == -1 || n8 != INT8_MAX) {
4940 return 12;
4941 }
4942
4943 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
4944 if(nErrCode == -1 || n8 != INT8_MIN) {
4945 return 13;
4946 }
4947
4948 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
4949 if(nErrCode != -1) {
4950 return 14;
4951 }
4952
4953 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
4954 if(nErrCode != -1) {
4955 return 15;
4956 }
4957
4958
4959 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
4960 if(nErrCode == -1 || u32 != 1) {
4961 return 16;
4962 }
4963
4964 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
4965 if(nErrCode == -1 || u32 != UINT32_MAX) {
4966 return 17;
4967 }
4968
4969 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
4970 if(nErrCode == -1 || u32 != 0) {
4971 return 18;
4972 }
4973
4974 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
4975 if(nErrCode != -1) {
4976 return 19;
4977 }
4978
4979 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
4980 if(nErrCode != -1) {
4981 return 20;
4982 }
4983
4984
4985 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
4986 if(nErrCode == -1 || u16 != UINT16_MAX) {
4987 return 21;
4988 }
4989
4990 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
4991 if(nErrCode == -1 || u16 != 0) {
4992 return 22;
4993 }
4994
4995 nErrCode = QCBOR_Int64UToInt16(1, &u16);
4996 if(nErrCode == -1 || u16 != 1) {
4997 return 23;
4998 }
4999
5000 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5001 if(nErrCode != -1) {
5002 return 24;
5003 }
5004
5005 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5006 if(nErrCode != -1) {
5007 return 25;
5008 }
5009
5010
5011 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5012 if(nErrCode == -1 || u8 != UINT8_MAX) {
5013 return 26;
5014 }
5015
5016 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5017 if(nErrCode == -1 || u8 != 0) {
5018 return 27;
5019 }
5020
5021 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5022 if(nErrCode == -1 || u8 != 1) {
5023 return 28;
5024 }
5025
5026 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5027 if(nErrCode != -1) {
5028 return 29;
5029 }
5030
5031 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5032 if(nErrCode != -1) {
5033 return 30;
5034 }
5035
5036
5037 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5038 if(nErrCode == -1 || u64 != 1) {
5039 return 31;
5040 }
5041
5042 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
5043 if(nErrCode == -1 || u64 != INT64_MAX) {
5044 return 32;
5045 }
5046
5047 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
5048 if(nErrCode == -1 || u64 != 0) {
5049 return 33;
5050 }
5051
5052 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
5053 if(nErrCode != -1) {
5054 return 34;
5055 }
5056
5057 return 0;
5058}
5059
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005060
5061/*
5062A sequence with
5063 A wrapping bstr
5064 containing a map
5065 1
5066 2
5067 A wrapping bstr
5068 containing an array
5069 3
5070 wrapping bstr
5071 4
5072 5
5073 6
5074 array
5075 7
5076 8
5077
5078 */
5079
5080static UsefulBufC foo(UsefulBuf ffo)
5081{
5082 UsefulBufC Encoded;
5083 QCBOREncodeContext EC;
5084 QCBORError uErr;
5085
5086 QCBOREncode_Init(&EC, ffo);
5087
5088 QCBOREncode_BstrWrap(&EC);
5089 QCBOREncode_OpenMap(&EC);
5090 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
5091 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
5092 QCBOREncode_CloseMap(&EC);
5093 QCBOREncode_BstrWrap(&EC);
5094 QCBOREncode_OpenArray(&EC);
5095 QCBOREncode_AddInt64(&EC, 3);
5096 QCBOREncode_BstrWrap(&EC);
5097 QCBOREncode_AddInt64(&EC, 4);
5098 QCBOREncode_CloseBstrWrap(&EC, NULL);
5099 QCBOREncode_AddInt64(&EC, 5);
5100 QCBOREncode_CloseArray(&EC);
5101 QCBOREncode_CloseBstrWrap(&EC, NULL);
5102 QCBOREncode_AddInt64(&EC, 6);
5103 QCBOREncode_CloseBstrWrap(&EC, NULL);
5104 QCBOREncode_OpenArray(&EC);
5105 QCBOREncode_AddInt64(&EC, 7);
5106 QCBOREncode_AddInt64(&EC, 8);
5107 QCBOREncode_CloseArray(&EC);
5108
5109 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07005110 if(uErr) {
5111 Encoded = NULLUsefulBufC;
5112 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005113
5114 return Encoded;
5115}
5116
5117
5118int32_t EnterBstrTest()
5119{
5120 MakeUsefulBufOnStack(ffo, 100);
5121
5122 QCBORDecodeContext DC;
5123
5124 QCBORDecode_Init(&DC, foo(ffo), 0);
5125
5126 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
5127
5128
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005129 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005130 QCBORDecode_EnterMap(&DC);
5131 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
5132 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
5133 QCBORDecode_ExitMap(&DC);
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005134 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005135 QCBORDecode_EnterArray(&DC);
5136 QCBORDecode_GetInt64(&DC, &i3);
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005137 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005138 QCBORDecode_GetInt64(&DC, &i4);
5139 QCBORDecode_ExitBstrWrapped(&DC);
5140 QCBORDecode_GetInt64(&DC, &i5);
5141 QCBORDecode_ExitArray(&DC);
5142 QCBORDecode_ExitBstrWrapped(&DC);
5143 QCBORDecode_GetInt64(&DC, &i6);
5144 QCBORDecode_ExitBstrWrapped(&DC);
5145 QCBORDecode_EnterArray(&DC);
5146 QCBORDecode_GetInt64(&DC, &i7);
5147 QCBORDecode_GetInt64(&DC, &i8);
5148 QCBORDecode_ExitArray(&DC);
5149
5150 QCBORError uErr = QCBORDecode_Finish(&DC);
5151
5152 return (int32_t)uErr;
5153}