blob: c78b008918c267362f25443f9b545a0c8e14cb6b [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 Lundblade2ded3d92018-10-09 21:36:11 +0800375 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700376 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 Lundblade2feb1e12020-07-15 03:50:45 -07001279
1280
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001281 /*
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};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001287
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001288 QCBORDecode_Init(&DCtx,
1289 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1290 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001291
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001292 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 Lundbladec5fef682020-01-25 11:38:45 -08001616int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001617{
1618 // Loop over all the not-well-formed instance of CBOR
1619 // that are test vectors in not_well_formed_cbor.h
1620 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1621 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1622 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1623 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1624
Laurence Lundbladeee851742020-01-08 08:37:05 -08001625 // Set up decoder context. String allocator needed for indefinite
1626 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001627 QCBORDecodeContext DCtx;
1628 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1629 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1630 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1631
1632 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001633 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001634 do {
1635 QCBORItem Item;
1636
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001637 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1638 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001639
1640 // Every test vector must fail with
1641 // a not-well-formed error. If not
1642 // this test fails.
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001643 if(!QCBORDecode_IsNotWellFormed(uCBORError) &&
1644 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001645 // Return index of failure in the error code
1646 return 2000 + nIterate;
1647 }
1648 }
1649 return 0;
1650}
1651
1652
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001653struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001654 UsefulBufC Input;
1655 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001656};
1657
Laurence Lundblade59289e52019-12-30 13:44:37 -08001658
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001659static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001660{
1661 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1662 // Set up the decoding context including a memory pool so that
1663 // indefinite length items can be checked
1664 QCBORDecodeContext DCtx;
1665 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1666 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001667
Laurence Lundblade59289e52019-12-30 13:44:37 -08001668 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1669 if(nCBORError) {
1670 return -9;
1671 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001672
Laurence Lundblade59289e52019-12-30 13:44:37 -08001673 // Iterate until there is an error of some sort error
1674 QCBORItem Item;
1675 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001676 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001677 memset(&Item, 0x33, sizeof(Item));
1678
1679 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1680 } while(nCBORError == QCBOR_SUCCESS);
1681
1682 // Must get the expected error or the this test fails
1683 // The data and label type must also be QCBOR_TYPE_NONE
1684 if(nCBORError != pF->nError ||
1685 Item.uDataType != QCBOR_TYPE_NONE ||
1686 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001687 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001688 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001689 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001690 }
1691 }
1692
1693 return 0;
1694}
1695
1696
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001697struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001698 // Most of this is copied from not_well_formed.h. Here the error code
1699 // returned is also checked.
1700
1701 // Indefinite length strings must be closed off
1702 // An indefinite length byte string not closed off
1703 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1704 // An indefinite length text string not closed off
1705 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1706
1707
1708 // All the chunks in an indefinite length string must be of the type of indefinite length string
1709 // indefinite length byte string with text string chunk
1710 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1711 // indefinite length text string with a byte string chunk
1712 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1713 // indefinite length byte string with an positive integer chunk
1714 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1715 // indefinite length byte string with an negative integer chunk
1716 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1717 // indefinite length byte string with an array chunk
1718 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1719 // indefinite length byte string with an map chunk
1720 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1721 // indefinite length byte string with tagged integer chunk
1722 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1723 // indefinite length byte string with an simple type chunk
1724 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1725 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1726 // indefinite length text string with indefinite string inside
1727 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1728
1729
1730 // Definte length maps and arrays must be closed by having the right number of items
1731 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001732 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001733 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001734 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001735 // A definte length array that is supposed to have 511 items, but has only 1
1736 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1737 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001738 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001739 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001740 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001741
1742
1743 // Indefinte length maps and arrays must be ended by a break
1744 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001745 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001746 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001747 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001748 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001749 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001750 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001751 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752
1753
1754 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001755 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001756 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001757 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001758 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001759 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001760 { {(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 -07001761 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001762 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001764 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001766 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001767 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001768
1769
1770 // The "argument" for the data item is incomplete
1771 // Positive integer missing 1 byte argument
1772 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1773 // Positive integer missing 2 byte argument
1774 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1775 // Positive integer missing 4 byte argument
1776 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1777 // Positive integer missing 8 byte argument
1778 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1779 // Positive integer missing 1 byte of 2 byte argument
1780 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1781 // Positive integer missing 2 bytes of 4 byte argument
1782 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1783 // Positive integer missing 1 bytes of 7 byte argument
1784 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1785 // Negative integer missing 1 byte argument
1786 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1787 // Binary string missing 1 byte argument
1788 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1789 // Text string missing 1 byte argument
1790 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1791 // Array missing 1 byte argument
1792 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1793 // Map missing 1 byte argument
1794 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1795 // Tag missing 1 byte argument
1796 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1797 // Simple missing 1 byte argument
1798 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1799
1800
1801 // Breaks must not occur in definite length arrays and maps
1802 // Array of length 1 with sole member replaced by a break
1803 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1804 // Array of length 2 with 2nd member replaced by a break
1805 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1806 // Map of length 1 with sole member label replaced by a break
1807 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1808 // Map of length 1 with sole member label replaced by break
1809 // Alternate representation that some decoders handle difference
1810 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1811 // Array of length 1 with 2nd member value replaced by a break
1812 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1813 // Map of length 2 with 2nd member replaced by a break
1814 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1815
1816
1817 // Breaks must not occur on their own out of an indefinite length data item
1818 // A bare break is not well formed
1819 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1820 // A bare break after a zero length definite length array
1821 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1822 // A bare break after a zero length indefinite length map
1823 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1824
1825
1826 // Forbidden two byte encodings of simple types
1827 // Must use 0xe0 instead
1828 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1829 // Should use 0xe1 instead
1830 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1831 // Should use 0xe2 instead
1832 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1833 // Should use 0xe3 instead
1834 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1835 // Should use 0xe4 instead
1836 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1837 // Should use 0xe5 instead
1838 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1839 // Should use 0xe6 instead
1840 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1841 // Should use 0xe7 instead
1842 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1843 // Should use 0xe8 instead
1844 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1845 // Should use 0xe9 instead
1846 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1847 // Should use 0xea instead
1848 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1849 // Should use 0xeb instead
1850 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1851 // Should use 0xec instead
1852 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1853 // Should use 0xed instead
1854 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1855 // Should use 0xee instead
1856 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1857 // Should use 0xef instead
1858 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1859 // Should use 0xf0 instead
1860 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1861 // Should use 0xf1 instead
1862 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1863 // Should use 0xf2 instead
1864 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1865 // Must use 0xf3 instead
1866 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1867 // Must use 0xf4 instead
1868 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1869 // Must use 0xf5 instead
1870 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1871 // Must use 0xf6 instead
1872 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1873 // Must use 0xf7 instead
1874 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1875 // Must use 0xf8 instead
1876 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1877
1878
1879 // Integers with additional info indefinite length
1880 // Positive integer with additional info indefinite length
1881 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1882 // Negative integer with additional info indefinite length
1883 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1884 // CBOR tag with "argument" an indefinite length
1885 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1886 // CBOR tag with "argument" an indefinite length alternate vector
1887 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1888
1889
1890 // Missing bytes from a deterministic length string
1891 // A byte string is of length 1 without the 1 byte
1892 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1893 // A text string is of length 1 without the 1 byte
1894 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001895 // Byte string should have 2^32-15 bytes, but has one
1896 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1897 // Byte string should have 2^32-15 bytes, but has one
1898 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001899
1900
1901 // Use of unassigned additional information values
1902 // Major type positive integer with reserved value 28
1903 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1904 // Major type positive integer with reserved value 29
1905 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1906 // Major type positive integer with reserved value 30
1907 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1908 // Major type negative integer with reserved value 28
1909 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1910 // Major type negative integer with reserved value 29
1911 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1912 // Major type negative integer with reserved value 30
1913 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1914 // Major type byte string with reserved value 28 length
1915 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1916 // Major type byte string with reserved value 29 length
1917 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1918 // Major type byte string with reserved value 30 length
1919 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1920 // Major type text string with reserved value 28 length
1921 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1922 // Major type text string with reserved value 29 length
1923 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1924 // Major type text string with reserved value 30 length
1925 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1926 // Major type array with reserved value 28 length
1927 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1928 // Major type array with reserved value 29 length
1929 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1930 // Major type array with reserved value 30 length
1931 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1932 // Major type map with reserved value 28 length
1933 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1934 // Major type map with reserved value 29 length
1935 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1936 // Major type map with reserved value 30 length
1937 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1938 // Major type tag with reserved value 28 length
1939 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1940 // Major type tag with reserved value 29 length
1941 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1942 // Major type tag with reserved value 30 length
1943 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1944 // Major type simple with reserved value 28 length
1945 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1946 // Major type simple with reserved value 29 length
1947 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1948 // Major type simple with reserved value 30 length
1949 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1950
1951
1952 // Maps must have an even number of data items (key & value)
1953 // Map with 1 item when it should have 2
1954 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1955 // Map with 3 item when it should have 4
1956 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1957 // Map with 1 item when it should have 2
1958 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1959 // Map with 3 item when it should have 4
1960 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1961
1962
1963 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001964 // Text-based date, with an integer
1965 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1966 // Epoch date, with an byte string
1967 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1968 // tagged as both epoch and string dates
1969 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1970 // big num tagged an int, not a byte string
1971 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001972};
1973
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001974int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001975{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001976 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001977
Laurence Lundblade59289e52019-12-30 13:44:37 -08001978 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1979 if(nResult) {
1980 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001982
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001983 // Corrupt the UsefulInputBuf and see that
1984 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001985 QCBORDecodeContext DCtx;
1986 QCBORItem Item;
1987 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001988
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001989 QCBORDecode_Init(&DCtx,
1990 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1991 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001993 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1994 return (int32_t)uQCBORError;
1995 }
1996 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
1997 // This wasn't supposed to happen
1998 return -1;
1999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002000
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002001 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002002
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002003 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2004 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2005 // Did not get back the error expected
2006 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002008
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002009/*
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002010 TODO: fix this
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002011 This test is disabled until QCBOREncode_EncodeHead() is brought in so
2012 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
2013
2014 This relies on the largest string allowed being SIZE_MAX -4 rather than
2015 SIZE_MAX. That way the test can be performed.
2016 {
2017 QCBORDecodeContext DCtx;
2018 QCBORItem Item;
2019
2020 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
2021 0xff, 0xff, 0xff, 0xff};
2022
2023 QCBORDecode_Init(&DCtx,
2024 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
2025 QCBOR_DECODE_MODE_NORMAL);
2026
2027 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2028 return -4;
2029 }
2030 }
2031*/
2032
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002033 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002034}
2035
2036
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002037/* Try all 256 values of the byte at nLen including recursing for
2038 each of the values to try values at nLen+1 ... up to nLenMax
2039 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002040static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002041{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002042 if(nLen >= nLenMax) {
2043 return;
2044 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002045
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002046 for(int inputByte = 0; inputByte < 256; inputByte++) {
2047 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002048 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002049 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002050
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002051 // Get ready to parse
2052 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002053 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002054
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002055 // Parse by getting the next item until an error occurs
2056 // Just about every possible decoder error can occur here
2057 // The goal of this test is not to check for the correct
2058 // error since that is not really possible. It is to
2059 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002060 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002061 QCBORItem Item;
2062 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002063 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002064 break;
2065 }
2066 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002067
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002068 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002069 }
2070}
2071
2072
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002073int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002074{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002075 // Size 2 tests 64K inputs and runs quickly
2076 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002077
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002078 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002079
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080 return 0;
2081}
2082
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002083
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002084int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002085{
2086 // size 3 tests 16 million inputs and runs OK
2087 // in seconds on fast machines. Size 4 takes
2088 // 10+ minutes and 5 half a day on fast
2089 // machines. This test is kept separate from
2090 // the others so as to no slow down the use
2091 // of them as a very frequent regression.
2092 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002093
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002094 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002095
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002096 return 0;
2097}
2098
2099
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002100static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002101 0xc0, // tag for string date
2102 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002103
Laurence Lundbladec7114722020-08-13 05:11:40 -07002104 0xc0, // tag for string date
2105 0x00, // Wrong type for a string date
2106
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002107 0xc1, // tag for epoch date
2108 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2109
Laurence Lundbladec7114722020-08-13 05:11:40 -07002110 0xc1,
2111 0x62, 'h', 'i', // wrong type tagged
2112
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002113 // CBOR_TAG_B64
Laurence Lundblade8fa7d5d2020-07-11 16:30:47 -07002114 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002115 0x1a, 0x53, 0x72, 0x4E, 0x01,
2116
2117 0xc1, // tag for epoch date
2118 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002120 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002121 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002123 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002124 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002125
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002126 0xc1, // tag for epoch date
2127 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2128 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2129
2130 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002131 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2132
2133 0xc1, // tag for epoch date
2134 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2135
2136 0xc1,
2137 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2138
2139 0xc1, // tag for epoch date
2140 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002141};
2142
2143
Laurence Lundbladec7114722020-08-13 05:11:40 -07002144
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002145// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002146#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002147static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002148
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002149 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002150
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002151 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002152
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002153 return diff > 0.0000001;
2154}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002155#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002156
2157
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002158int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002159{
2160 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002161 QCBORItem Item;
2162 QCBORError uError;
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 Lundblade2ded3d92018-10-09 21:36:11 +08002168 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002169 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002170 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002171 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002172 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002173 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002174 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002175 }
2176
Laurence Lundbladec7114722020-08-13 05:11:40 -07002177 // Wrong type for a string date
2178 uError = QCBORDecode_GetNext(&DCtx, &Item);
2179 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002180 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002181 }
2182
2183 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2184 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2185 return -4;
2186 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002187 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2188 Item.val.epochDate.nSeconds != 1400000000 ||
2189 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002190 return -5;
2191 }
2192
2193 // Wrong type for an epoch date
2194 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2195 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002196 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002197
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002198 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2199 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002200 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2201 return -7;
2202 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002203 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2204 Item.val.epochDate.nSeconds != 1400000001 ||
2205 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002206 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002207 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002209
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002210 // Epoch date that is too large for our representation
2211 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002212 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002214
Laurence Lundblade9682a532020-06-06 18:33:04 -07002215#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002216 // Epoch date in float format with fractional seconds
2217 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2218 return -10;
2219 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002220 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2221 Item.val.epochDate.nSeconds != 1 ||
2222 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002223 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002225
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002226 // Epoch date float that is too large for our representation
2227 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002228 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002230
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002231 // Epoch date double that is just slightly too large
2232 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002233 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002234 }
2235
2236 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002237 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2238 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2239 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2240 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002241 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002242 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002243
2244 // Nan
2245 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2246 return -15;
2247 }
2248
2249 // +Inifinity double-precision
2250 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2251 return -16;
2252 }
2253
2254#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2255 // -Inifinity half-precision
2256 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2257 return -17;
2258 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002259#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002260 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2261 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002262 }
2263#endif
2264
Laurence Lundbladec7114722020-08-13 05:11:40 -07002265#else
2266 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2267 return -19;
2268 }
2269 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2270 return -20;
2271 }
2272 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2273 return -21;
2274 }
2275 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2276 return -22;
2277 }
2278 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2279 return -23;
2280 }
2281 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2282 return -24;
2283 }
2284#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2285 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2286 return -25;
2287 }
2288#else
2289 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2290 return -26;
2291 }
2292#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002293
Laurence Lundbladec7114722020-08-13 05:11:40 -07002294#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002295
2296 return 0;
2297}
2298
Laurence Lundblade4b270642020-08-14 12:53:07 -07002299/*
2300 Test cases covered here. Some items cover more than one of these.
2301 positive integer (zero counts as a positive integer)
2302 negative integer
2303 half-precision float
2304 single-precision float
2305 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002306
Laurence Lundblade4b270642020-08-14 12:53:07 -07002307 float Overflow error
2308 Wrong type error for epoch
2309 Wrong type error for date string
2310 float disabled error
2311 half-precision disabled error
2312 -Infinity
2313 Slightly too large integer
2314 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002315
Laurence Lundblade4b270642020-08-14 12:53:07 -07002316 Get epoch by int
2317 Get string by int
2318 Get epoch by string
2319 Get string by string
2320 Fail to get epoch by wrong int label
2321 Fail to get string by wrong string label
2322 Fail to get epoch by string because it is invalid
2323 Fail to get epoch by int because it is invalid
2324
2325 Untagged values
2326 */
2327static uint8_t spSpiffyDateTestInput[] = {
2328 0x86,
2329
2330 0xc1,
2331 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2332
Laurence Lundbladec7114722020-08-13 05:11:40 -07002333 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002334 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2335
2336 0xc1, // tag for epoch date
2337 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2338
2339 0xc1, // tag for epoch date
2340 0x9f, 0xff, // Erroneous empty array as content for date
2341
2342 0xc0, // tag for string date
2343 0xbf, 0xff, // Erroneous empty map as content for date
2344
2345 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002346
2347 0x00,
2348 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002349 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002350
2351 0x01,
2352 0xc1, // tag for epoch date
2353 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2354
2355 // Untagged integer 0
2356 0x08,
2357 0x00,
2358
2359 // Utagged date string with string label y
2360 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002361 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002362
2363 // Untagged -1000 with label z
2364 0x61, 0x7a,
2365 0x39, 0x03, 0xe7,
2366
Laurence Lundbladec7114722020-08-13 05:11:40 -07002367 0x07,
2368 0xc1, // tag for epoch date
2369 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2370
Laurence Lundblade4b270642020-08-14 12:53:07 -07002371 0x05,
2372 0xc1,
2373 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2374
Laurence Lundbladec7114722020-08-13 05:11:40 -07002375 // Untagged single-precision float with value 3.14 with string label x
2376 0x61, 0x78,
2377 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2378
Laurence Lundbladec7114722020-08-13 05:11:40 -07002379 // Untagged half-precision float with value -2
2380 0x09,
2381 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002382
2383 0xff,
2384};
2385
2386int32_t SpiffyDateDecodeTest()
2387{
2388 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002389 QCBORError uError;
2390 int64_t nEpochDate1, nEpochDate2, nEpochDate3, nEpochDate5,
2391 nEpochDate4, nEpochDate6;
2392 UsefulBufC StringDate1, StringDate2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002393
2394 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002395 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002396 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002397 QCBORDecode_EnterArray(&DC);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002398
Laurence Lundblade4b270642020-08-14 12:53:07 -07002399 // Too-negative integer, -9.2233720368547748E+18
2400 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nEpochDate1);
2401 uError = QCBORDecode_GetAndResetError(&DC);
2402 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2403 return 1111;
2404 }
2405
2406 // Too-large integer
2407 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nEpochDate1);
2408 uError = QCBORDecode_GetAndResetError(&DC);
2409 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002410 return 1;
2411 }
2412
Laurence Lundblade4b270642020-08-14 12:53:07 -07002413 // Half-precision minus infinity
2414 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nEpochDate1);
2415 uError = QCBORDecode_GetAndResetError(&DC);
2416#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2417#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2418 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2419#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2420 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2421#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2422#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2423 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2424#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2425 if(uError != uExpectedforHalfMinusInfinity) {
2426 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002427 }
2428
Laurence Lundblade4b270642020-08-14 12:53:07 -07002429 // Bad content for epoch date
2430 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nEpochDate1);
2431 uError = QCBORDecode_GetAndResetError(&DC);
2432 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2433 return 3;
2434 }
2435
2436 // Bad content for string date
2437 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &StringDate1);
2438 uError = QCBORDecode_GetAndResetError(&DC);
2439 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2440 return 4;
2441 }
2442
2443 QCBORDecode_EnterMap(&DC);
2444
2445 // Get largest negative double precision epoch date allowed
2446 QCBORDecode_GetEpochDateInMapN(&DC, 5, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &nEpochDate2);
2447#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2448 if(nEpochDate2 != -9223372036854773760LL) {
2449 return 101;
2450 }
2451#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2452 uError = QCBORDecode_GetAndResetError(&DC);
2453 if(uError != QCBOR_ERR_NOT_FOUND) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002454 return 102;
2455 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002456#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002457
Laurence Lundblade4b270642020-08-14 12:53:07 -07002458 // Get largest double precision epoch date allowed
2459 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002460#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2461 if(nEpochDate2 != 9223372036854773760ULL) {
2462 return 101;
2463 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002464#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2465 uError = QCBORDecode_GetAndResetError(&DC);
2466 if(uError != QCBOR_ERR_NOT_FOUND) {
2467 return 102;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002468 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002469#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2470
2471 // A single-precision date
2472 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &nEpochDate5);
2473#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2474 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002475 return 103;
2476 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002477#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2478 uError = QCBORDecode_GetAndResetError(&DC);
2479 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2480 return 104;
2481 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002482#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2483
Laurence Lundblade4b270642020-08-14 12:53:07 -07002484 // A half-precision date with value -2
2485 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &nEpochDate4);
2486#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2487 if(nEpochDate4 != -2) {
2488 return 105;
2489 }
2490#else
2491 uError = QCBORDecode_GetAndResetError(&DC);
2492 if(uError == QCBOR_SUCCESS) {
2493 return 106;
2494 }
2495#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2496
2497 // Fail to get an epoch date by string label
2498 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label", QCBOR_TAG_REQUIREMENT_NO_TAG, &nEpochDate6);
2499 uError = QCBORDecode_GetAndResetError(&DC);
2500 if(uError != QCBOR_ERR_NOT_FOUND) {
2501 return 107;
2502 }
2503
2504 // Fail to get an epoch date by integer label
2505 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NO_TAG, &nEpochDate6);
2506 uError = QCBORDecode_GetAndResetError(&DC);
2507 if(uError != QCBOR_ERR_NOT_FOUND) {
2508 return 108;
2509 }
2510
2511 // Fail to get a string date by string label
2512 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label", QCBOR_TAG_REQUIREMENT_NO_TAG, &StringDate1);
2513 uError = QCBORDecode_GetAndResetError(&DC);
2514 if(uError != QCBOR_ERR_NOT_FOUND) {
2515 return 109;
2516 }
2517
2518 // Fail to get a string date by integer label
2519 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NO_TAG, &StringDate1);
2520 uError = QCBORDecode_GetAndResetError(&DC);
2521 if(uError != QCBOR_ERR_NOT_FOUND) {
2522 return 110;
2523 }
2524
2525 // The rest of these succeed even if float features are disabled
2526 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2527 QCBORDecode_GetEpochDateInMapN( &DC, 1, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nEpochDate1);
2528 // Tagged date string
2529 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &StringDate1);
2530 // Untagged integer 0
2531 QCBORDecode_GetEpochDateInMapN( &DC, 8, QCBOR_TAG_REQUIREMENT_NO_TAG, &nEpochDate3);
2532 // Untagged date string
2533 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NO_TAG, &StringDate2);
2534 // Untagged -1000 with label z
2535 QCBORDecode_GetEpochDateInMapSZ( &DC, "z", QCBOR_TAG_REQUIREMENT_NO_TAG, &nEpochDate6);
2536
2537 QCBORDecode_ExitMap(&DC);
2538 QCBORDecode_ExitArray(&DC);
2539 uError = QCBORDecode_Finish(&DC);
2540 if(uError) {
2541 return 1000;
2542 }
2543
2544 if(nEpochDate1 != 1400000000) {
2545 return 200;
2546 }
2547
2548 if(nEpochDate3 != 0) {
2549 return 201;
2550 }
2551
2552 if(nEpochDate6 != -1000) {
2553 return 202;
2554 }
2555
2556 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2557 return 203;
2558 }
2559
2560 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2561 return 204;
2562 }
2563
Laurence Lundbladec7114722020-08-13 05:11:40 -07002564 return 0;
2565}
2566
2567
2568
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002569// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002570static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002571 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002572 0x81, // Array of one
2573 0xd8, 0x04, // non-preferred serialization of tag 4
2574 0x82, 0x01, 0x03}; // fraction 1/3
2575
Laurence Lundblade59289e52019-12-30 13:44:37 -08002576/*
2577 DB 9192939495969798 # tag(10489608748473423768)
2578 80 # array(0)
2579 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002580static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2581 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002582
Laurence Lundblade59289e52019-12-30 13:44:37 -08002583/*
2584DB 9192939495969798 # tag(10489608748473423768)
2585 D8 88 # tag(136)
2586 C6 # tag(6)
2587 C7 # tag(7)
2588 80 # array(0)
2589*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002590static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2591 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002592
2593/*
2594 The cbor.me parse of this.
2595 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002596 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 +07002597 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2598 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2599 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2600 */
2601static uint8_t spCSRWithTags[] = {
2602 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2603 0xc6, 0xc7, 0x36,
2604 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2605 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2606 0xcb, 0xa5,
2607 0xd1, 0x31,
2608 0xd1, 0xd1, 0xd1, 0x6c,
2609 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2610 0xc9, 0x30,
2611 0xd9, 0x03, 0x05, 0x63,
2612 0x53, 0x53, 0x47,
2613 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002614 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002615 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2616 0xd1, 0x2f,
2617 0xd1, 0x69,
2618 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2619 0xd1, 0x2d,
2620 0xd1, 0x62,
2621 0x55, 0x53,
2622 0xd7, 0x32,
2623 0xd3, 0xa2,
2624 0x2a,
2625 0xc9, 0xa1,
2626 0x28,
2627 0x26,
2628 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2629 0xcc, 0x4a,
2630 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2631 0xd0, 0x35,
2632 0xd7, 0xa1,
2633 0xcb, 0xc8, 0xc7, 0x24,
2634 0xc8, 0x22};
2635
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002636static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002637
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002638
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002639int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002640{
2641 QCBORDecodeContext DCtx;
2642 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002643
Laurence Lundbladeee851742020-01-08 08:37:05 -08002644 QCBORDecode_Init(&DCtx,
2645 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2646 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002647
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002648 //-------------------------
2649 // This text matches the magic number tag and the fraction tag
2650 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2651 return -2;
2652 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002653 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002654 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2655 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002656 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002657
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002658 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2659 return -4;
2660 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002661
2662#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002663 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002664 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002665 Item.val.uCount != 2) {
2666 return -5;
2667 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002668#else
2669 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2670 return -6;
2671 }
2672#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002673
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002674 // --------------------------------
2675 // This test decodes the very large tag, but it is not in
2676 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002677 QCBORDecode_Init(&DCtx,
2678 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2679 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002680 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2681 return -6;
2682 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002683 /*
2684 if(Item.uTagBits) { // TODO: make sure it is OK to remove this
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002685 return -7;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002686 }*/
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002687
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002688 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002689 // This test sets up a caller-config list that includes the very large
2690 // tage and then matches it.
2691 QCBORDecode_Init(&DCtx,
2692 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2693 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002694 const uint64_t puList[] = {0x9192939495969798, 257};
2695 const QCBORTagListIn TL = {2, puList};
2696 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002697
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002698 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2699 return -8;
2700 }
2701 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2702 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2703 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2704 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2705 Item.val.uCount != 0) {
2706 return -9;
2707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002708
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002709 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002710 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002711 const uint64_t puLongList[17] = {1,2,1};
2712 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002713 QCBORDecode_Init(&DCtx,
2714 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2715 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002716 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2717 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2718 return -11;
2719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002720
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002721 // -----------------------
2722 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002723 QCBORDecode_Init(&DCtx,
2724 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2725 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002726 uint64_t puTags[16];
2727 QCBORTagListOut Out = {0, 4, puTags};
2728 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2729 return -12;
2730 }
2731 if(puTags[0] != 0x9192939495969798 ||
2732 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002733 puTags[2] != 0x06 ||
2734 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002735 return -13;
2736 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002737
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002738 // ----------------------
2739 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002740 QCBORDecode_Init(&DCtx,
2741 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2742 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002743 QCBORTagListOut OutSmall = {0, 3, puTags};
2744 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2745 return -14;
2746 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002747
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002748#if 0
2749 // TODO: this test needs to be re evaluated
2750
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002751 // ---------------
2752 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002753 QCBORDecode_Init(&DCtx,
2754 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2755 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002756 int n = CheckCSRMaps(&DCtx);
2757 if(n) {
2758 return n-2000;
2759 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002760
Laurence Lundblade59289e52019-12-30 13:44:37 -08002761 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002762 QCBORDecode_Init(&DCtx,
2763 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2764 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002765
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002766 const uint64_t puTagList[] = {773, 1, 90599561};
2767 const QCBORTagListIn TagList = {3, puTagList};
2768 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002769
2770
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002771 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2772 return -100;
2773 }
2774 if(Item.uDataType != QCBOR_TYPE_MAP ||
2775 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2776 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2777 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2778 Item.val.uCount != 2 ||
2779 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2780 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2781 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2782 Out.uNumUsed != 3) {
2783 return -101;
2784 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002785
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002786 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2787 return -102;
2788 }
2789 if(Item.uDataType != QCBOR_TYPE_MAP ||
2790 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2791 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2792 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2793 Item.val.uCount != 2 ||
2794 puTags[0] != 5859837686836516696 ||
2795 puTags[1] != 7 ||
2796 Out.uNumUsed != 2) {
2797 return -103;
2798 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002799
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002800 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2801 return -104;
2802 }
2803 if(Item.uDataType != QCBOR_TYPE_MAP ||
2804 Item.uTagBits ||
2805 Item.val.uCount != 5 ||
2806 puTags[0] != 0x0b ||
2807 Out.uNumUsed != 1) {
2808 return -105;
2809 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002810
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002811 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2812 return -106;
2813 }
2814 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2815 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2816 Item.val.string.len != 12 ||
2817 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2818 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2819 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2820 Out.uNumUsed != 3) {
2821 return -105;
2822 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002823
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002824 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2825 return -107;
2826 }
2827 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2828 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2829 Item.val.string.len != 3 ||
2830 puTags[0] != 773 ||
2831 Out.uNumUsed != 1) {
2832 return -108;
2833 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002834
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002835 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2836 return -109;
2837 }
2838 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002839 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002840 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002841 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002842 puTags[11] != 0x0f ||
2843 Out.uNumUsed != 12) {
2844 return -110;
2845 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002846
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002847 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2848 return -111;
2849 }
2850 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2851 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2852 Item.val.string.len != 9 ||
2853 puTags[0] != 17 ||
2854 Out.uNumUsed != 1) {
2855 return -112;
2856 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002857
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002858 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2859 return -111;
2860 }
2861 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2862 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2863 Item.val.string.len != 2 ||
2864 puTags[0] != 17 ||
2865 Out.uNumUsed != 1) {
2866 return -112;
2867 }
2868
2869 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2870 return -113;
2871 }
2872 if(Item.uDataType != QCBOR_TYPE_MAP ||
2873 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2874 Item.val.uCount != 2 ||
2875 puTags[0] != 19 ||
2876 Out.uNumUsed != 1) {
2877 return -114;
2878 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002879
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002880 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2881 return -115;
2882 }
2883 if(Item.uDataType != QCBOR_TYPE_MAP ||
2884 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2885 Item.uTagBits ||
2886 Item.val.uCount != 1 ||
2887 puTags[0] != 9 ||
2888 Out.uNumUsed != 1) {
2889 return -116;
2890 }
2891
2892 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2893 return -116;
2894 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002895 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002896 Item.val.int64 != -7 ||
2897 Item.uTagBits ||
2898 Out.uNumUsed != 0) {
2899 return -117;
2900 }
2901
2902 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2903 return -118;
2904 }
2905 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2906 Item.val.string.len != 10 ||
2907 Item.uTagBits ||
2908 puTags[0] != 12 ||
2909 Out.uNumUsed != 1) {
2910 return -119;
2911 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002912
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002913 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2914 return -120;
2915 }
2916 if(Item.uDataType != QCBOR_TYPE_MAP ||
2917 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2918 Item.val.uCount != 1 ||
2919 puTags[0] != 0x17 ||
2920 Out.uNumUsed != 1) {
2921 return -121;
2922 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002923
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002924 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2925 return -122;
2926 }
2927 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2928 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2929 Item.val.int64 != -3 ||
2930 puTags[0] != 8 ||
2931 Out.uNumUsed != 1) {
2932 return -123;
2933 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002934
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002935 if(QCBORDecode_Finish(&DCtx)) {
2936 return -124;
2937 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002938#else
2939 (void)spCSRWithTags;
2940#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002941 return 0;
2942}
2943
2944
2945
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002946
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002947static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002948 0x83,
2949 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2950 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2951 0xA4,
2952 0x63, 0x42, 0x4E, 0x2B,
2953 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2954 0x18, 0x40,
2955 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2956 0x63, 0x42, 0x4E, 0x2D,
2957 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2958 0x38, 0x3F,
2959 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2960
2961
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002962static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002963
2964
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002965int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002966{
2967 QCBORDecodeContext DCtx;
2968 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002969 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002970
Laurence Lundbladeee851742020-01-08 08:37:05 -08002971 QCBORDecode_Init(&DCtx,
2972 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2973 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002974
2975
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002976 //
2977 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2978 return -1;
2979 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002980 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002982
2983 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002984 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002985 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002986 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002987 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002988 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002989 }
2990
2991 //
2992 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002993 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002994 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002995 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002996 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002998
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002999 //
3000 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003001 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003002 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003003 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003005
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003006 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003007 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003008 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3009 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003010 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003011 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003012 }
3013
3014 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003015 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003016 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3017 Item.uLabelType != QCBOR_TYPE_INT64 ||
3018 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003019 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003020 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003022
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003023 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003024 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003025 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3026 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003027 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003028 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003030
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003031 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003032 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003033 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3034 Item.uLabelType != QCBOR_TYPE_INT64 ||
3035 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003036 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003037 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003038 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003039
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003040 return 0;
3041}
3042
3043
3044
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003045static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003046 uint8_t uDataType,
3047 uint8_t uNestingLevel,
3048 uint8_t uNextNest,
3049 int64_t nLabel,
3050 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003051{
3052 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003053 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003055 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3056 if(Item.uDataType != uDataType) return -1;
3057 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003058 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3059 Item.uLabelType != QCBOR_TYPE_UINT64) {
3060 return -1;
3061 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003062 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3063 if(Item.label.int64 != nLabel) return -1;
3064 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003065 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003066 }
3067 }
3068 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303069 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003071 if(pItem) {
3072 *pItem = Item;
3073 }
3074 return 0;
3075}
3076
3077
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003078// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003079static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003080{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303081 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003082
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303083 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003084
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303085 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003086
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303087 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
3088 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
3089 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
3090 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
3091 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003092
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303093 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
3094 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003095
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303096 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
3097 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003098
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303099 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
3100 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003101
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003102 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003103
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003104 return 0;
3105}
3106
3107
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003108/*
3109// cbor.me decoded output
3110{
3111 -23: {
3112 -20: {
3113 -18: "Organization",
3114 -17: "SSG",
3115 -15: "Confusion",
3116 -16: "San Diego",
3117 -14: "US"
3118 },
3119 -19: {
3120 -11: {
3121 -9: -7
3122 },
3123 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3124 }
3125 },
3126 -22: {
3127 -5: -3
3128 }
3129}
3130 */
3131
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003132
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003133static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003134 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3135 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3136 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3137 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3138 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3139 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3140 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3141 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3142 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3143
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003144int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003145{
3146 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003147
Laurence Lundbladeee851742020-01-08 08:37:05 -08003148 QCBORDecode_Init(&DCtx,
3149 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3150 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003151
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003152 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003153}
3154
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003155
3156
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003157int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003158{
3159 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003160
Laurence Lundbladeee851742020-01-08 08:37:05 -08003161 QCBORDecode_Init(&DCtx,
3162 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3163 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003164
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003165 QCBORItem Item;
3166 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003167
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003168 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3169 return -1;
3170 }
3171 if(Item.uDataType != QCBOR_TYPE_MAP) {
3172 return -2;
3173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003174
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003175 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3176 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3177 return -3;
3178 }
3179
3180 return 0;
3181}
3182
3183
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003184// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003185static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003186 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3187 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3188 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3189 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3190 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3191 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003192 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3193 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3194 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3195 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003196
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003197int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003198{
3199 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003200
Laurence Lundbladeee851742020-01-08 08:37:05 -08003201 QCBORDecode_Init(&DCtx,
3202 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3203 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003204
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003205 return CheckCSRMaps(&DCtx);
3206}
3207
3208
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003209
Laurence Lundblade17ede402018-10-13 11:43:07 +08003210static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3211{
3212 UsefulOutBuf UOB;
3213 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003214
Laurence Lundblade17ede402018-10-13 11:43:07 +08003215 int i;
3216 for(i = 0; i < n; i++) {
3217 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3218 }
3219
3220 for(i = 0; i < n; i++) {
3221 UsefulOutBuf_AppendByte(&UOB, 0xff);
3222 }
3223 return UsefulOutBuf_OutUBuf(&UOB);
3224}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003225
3226
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003227static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003228{
3229 QCBORDecodeContext DC;
3230 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003231
Laurence Lundblade17ede402018-10-13 11:43:07 +08003232 int j;
3233 for(j = 0; j < nNestLevel; j++) {
3234 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003235 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003236 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3237 // Should be in error
3238 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
3239 return -4;
3240 } else {
3241 return 0; // Decoding doesn't recover after an error
3242 }
3243 } else {
3244 // Should be no error
3245 if(nReturn) {
3246 return -9; // Should not have got an error
3247 }
3248 }
3249 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3250 return -7;
3251 }
3252 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003253 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003254 if(nReturn) {
3255 return -3;
3256 }
3257 return 0;
3258}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003259
3260
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003261int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003262{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303263 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003264 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003265 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003266 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003267 int nReturn = parse_indeflen_nested(Nested, i);
3268 if(nReturn) {
3269 return nReturn;
3270 }
3271 }
3272 return 0;
3273}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003274
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003275
Laurence Lundbladeee851742020-01-08 08:37:05 -08003276// [1, [2, 3]]
3277static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3278// No closing break
3279static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3280// Not enough closing breaks
3281static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3282// Too many closing breaks
3283static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3284// Unclosed indeflen inside def len
3285static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3286// confused tag
3287static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003288
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003289int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003290{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003291 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003292 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003293 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003294
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003295 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303296 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003297 QCBORDecodeContext DC;
3298 QCBORItem Item;
3299 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003300
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003301 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003303 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303304
3305 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3306 Item.uNestingLevel != 0 ||
3307 Item.uNextNestLevel != 1) {
3308 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003310
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003311 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303312 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3313 Item.uNestingLevel != 1 ||
3314 Item.uNextNestLevel != 1) {
3315 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003316 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003317
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003318 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303319 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3320 Item.uNestingLevel != 1 ||
3321 Item.uNextNestLevel != 2) {
3322 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003323 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003324
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003325 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003326 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303327 Item.uNestingLevel != 2 ||
3328 Item.uNextNestLevel != 2) {
3329 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003331
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003332 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003333 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303334 Item.uNestingLevel != 2 ||
3335 Item.uNextNestLevel != 0) {
3336 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003338
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003339 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303340 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003342
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003343 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003344 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003345
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003346 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003347
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003348 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003349
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003350 nResult = QCBORDecode_GetNext(&DC, &Item);
3351 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303352 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003353 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003354
Laurence Lundblade570fab52018-10-13 18:28:27 +08003355 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303356 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3357 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003358 }
3359
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003360
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003361 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003362 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003363
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003364 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003365
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003366 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003367
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003368 nResult = QCBORDecode_GetNext(&DC, &Item);
3369 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303370 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003371 }
3372
3373 nResult = QCBORDecode_GetNext(&DC, &Item);
3374 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303375 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003377
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003378 nResult = QCBORDecode_GetNext(&DC, &Item);
3379 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303380 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003382
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003383 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303384 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3385 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003387
3388
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003389 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003390 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003391
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003392 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003393
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003394 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003395
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003396 nResult = QCBORDecode_GetNext(&DC, &Item);
3397 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303398 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003400
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003401 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003402 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303403 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003404 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303405
Laurence Lundblade642282a2020-06-23 12:00:33 -07003406 nResult = QCBORDecode_GetNext(&DC, &Item);
3407 if(nResult != QCBOR_ERR_BAD_BREAK) {
3408 return -140;
3409 }
3410
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003411
Laurence Lundblade570fab52018-10-13 18:28:27 +08003412 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003413 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003414
Laurence Lundblade570fab52018-10-13 18:28:27 +08003415 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003416
Laurence Lundblade570fab52018-10-13 18:28:27 +08003417 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003418
Laurence Lundblade570fab52018-10-13 18:28:27 +08003419 nResult = QCBORDecode_GetNext(&DC, &Item);
3420 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303421 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003422 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003423
Laurence Lundblade570fab52018-10-13 18:28:27 +08003424 nResult = QCBORDecode_GetNext(&DC, &Item);
3425 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303426 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003428
Laurence Lundblade570fab52018-10-13 18:28:27 +08003429 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303430 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3431 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003432 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003433
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303434 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003435 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003436
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303437 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003438
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303439 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003440
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303441 nResult = QCBORDecode_GetNext(&DC, &Item);
3442 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303443 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303444 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003445
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303446 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303447 if(nResult != QCBOR_ERR_BAD_BREAK) {
3448 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303449 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003450
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003451 return 0;
3452}
3453
Laurence Lundblade17ede402018-10-13 11:43:07 +08003454
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003455static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003456 0x81, // Array of length one
3457 0x7f, // text string marked with indefinite length
3458 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3459 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3460 0xff // ending break
3461};
3462
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003463static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303464 0x81, // Array of length one
3465 0x7f, // text string marked with indefinite length
3466 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3467 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3468 0xff // ending break
3469};
3470
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003471static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303472 0x81, // Array of length one
3473 0x7f, // text string marked with indefinite length
3474 0x01, 0x02, // Not a string
3475 0xff // ending break
3476};
3477
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003478static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303479 0x81, // Array of length one
3480 0x7f, // text string marked with indefinite length
3481 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3482 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3483 // missing end of string
3484};
3485
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003486static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303487 0xa1, // Array of length one
3488 0x7f, // text string marked with indefinite length
3489 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3490 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3491 0xff, // ending break
3492 0x01 // integer being labeled.
3493};
3494
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003495/**
3496 Make an indefinite length string
3497
3498 @param Storage Storage for string, must be 144 bytes in size
3499 @return The indefinite length string
3500
3501 This makes an array with one indefinite length string that has 7 chunks
3502 from size of 1 byte up to 64 bytes.
3503 */
3504static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303505{
3506 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003507
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303508 UsefulOutBuf_Init(&UOB, Storage);
3509 UsefulOutBuf_AppendByte(&UOB, 0x81);
3510 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003511
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003512 uint8_t uStringByte = 0;
3513 // Use of type int is intentional
3514 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3515 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303516 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003517 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3518 for(int j = 0; j < uChunkSize; j++) {
3519 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3520 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303521 }
3522 }
3523 UsefulOutBuf_AppendByte(&UOB, 0xff);
3524
3525 return UsefulOutBuf_OutUBuf(&UOB);
3526}
3527
3528static int CheckBigString(UsefulBufC BigString)
3529{
3530 if(BigString.len != 255) {
3531 return 1;
3532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003533
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303534 for(uint8_t i = 0; i < 255; i++){
3535 if(((const uint8_t *)BigString.ptr)[i] != i) {
3536 return 1;
3537 }
3538 }
3539 return 0;
3540}
3541
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303542
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003543int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303544{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303545 QCBORDecodeContext DC;
3546 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303547 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003548 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003549
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303550 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003551 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303552 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003553
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303554 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303555 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303556 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003557
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303558 if(QCBORDecode_GetNext(&DC, &Item)) {
3559 return -2;
3560 }
3561 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3562 return -3;
3563 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003564
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303565 if(QCBORDecode_GetNext(&DC, &Item)) {
3566 return -4;
3567 }
3568 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3569 return -5;
3570 }
3571 if(QCBORDecode_Finish(&DC)) {
3572 return -6;
3573 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303574
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303575 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003576 QCBORDecode_Init(&DC,
3577 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3578 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003579
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303580 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3581 return -7;
3582 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003583
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303584 if(QCBORDecode_GetNext(&DC, &Item)) {
3585 return -8;
3586 }
3587 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3588 return -9;
3589 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003590
Laurence Lundblade30816f22018-11-10 13:40:22 +07003591 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303592 return -10;
3593 }
3594
3595 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003596 QCBORDecode_Init(&DC,
3597 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3598 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003599
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303600 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3601 return -11;
3602 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003603
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303604 if(QCBORDecode_GetNext(&DC, &Item)) {
3605 return -12;
3606 }
3607 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3608 return -13;
3609 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003610
Laurence Lundblade30816f22018-11-10 13:40:22 +07003611 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303612 return -14;
3613 }
3614
3615 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003616 QCBORDecode_Init(&DC,
3617 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3618 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003619
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303620 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3621 return -15;
3622 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003623
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303624 if(QCBORDecode_GetNext(&DC, &Item)) {
3625 return -16;
3626 }
3627 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3628 return -17;
3629 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003630
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303631 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3632 return -18;
3633 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003634
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303635 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303636 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003637
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303638 QCBORDecode_GetNext(&DC, &Item);
3639 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303640 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303641 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003642
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303643 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303644 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303645 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003646
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303647 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003648 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303649
3650 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3651 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303652 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003654
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303655 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303656 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003657 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003658
Laurence Lundbladeee851742020-01-08 08:37:05 -08003659 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3660 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003661
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303662 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303663 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303664 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303665 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003666
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303667 QCBORDecode_GetNext(&DC, &Item);
3668 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303669 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303670 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003671 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303672 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303673 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003674
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303675 // ---- big bstr -----
3676 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003677
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303678 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3679 return -25;
3680 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003681
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303682 if(QCBORDecode_GetNext(&DC, &Item)) {
3683 return -26;
3684 }
3685 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303686 return -26;
3687 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003688
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303689 if(QCBORDecode_GetNext(&DC, &Item)) {
3690 return -27;
3691 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303692 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303693 return -28;
3694 }
3695 if(CheckBigString(Item.val.string)) {
3696 return -3;
3697 }
3698 if(QCBORDecode_Finish(&DC)) {
3699 return -29;
3700 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003701
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303702 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003703 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003704
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303705 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3706 return -30;
3707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003708
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303709 QCBORDecode_GetNext(&DC, &Item);
3710 if(Item.uDataType != QCBOR_TYPE_MAP) {
3711 return -31;
3712 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003713
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303714 if(QCBORDecode_GetNext(&DC, &Item)){
3715 return -32;
3716 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003717 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3718 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303719 Item.uDataAlloc || !Item.uLabelAlloc ||
3720 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3721 return -33;
3722 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003723
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303724 if(QCBORDecode_Finish(&DC)) {
3725 return -34;
3726 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003727
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003728 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003729}
3730
3731
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003732int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303733{
3734 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003735 QCBORError nCBORError;
3736
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003737
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303738 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003739 QCBORDecode_Init(&DC,
3740 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3741 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003742
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003743 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003744
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003745 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3746 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303747 return -1;
3748 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003749
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003750 if(CheckCSRMaps(&DC)) {
3751 return -2;
3752 }
3753
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303754 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003755 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003756 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003757
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303758 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003759 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303760 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003761
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303762 QCBORItem Item1, Item2, Item3, Item4;
3763 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003764 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303765 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3766 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003767 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303768 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003769 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303770 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003771 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303772 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003773 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303774 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003775 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003776
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303777 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003778
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303779 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303780 Item1.uDataType != QCBOR_TYPE_INT64 ||
3781 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003782 Item1.uDataAlloc != 0 ||
3783 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003784 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003785 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003786 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003787
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303788
3789 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003790 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303791 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003792 Item2.uDataAlloc != 0 ||
3793 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303794 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003795 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003796
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303797 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003798 Item3.uDataAlloc == 0 ||
3799 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003800 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003801 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003802 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003803
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303804 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003805 Item4.uDataAlloc == 0 ||
3806 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003807 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003808 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003809 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003810
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303811 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003812 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003813 QCBORDecode_Init(&DC,
3814 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3815 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303816 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3817 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003818 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303819 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003820 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003821 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003822 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303823 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3824 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3825 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3826 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3827 }
3828 }
3829 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003830 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003831 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303832 }
3833
3834 return 0;
3835}
3836
Laurence Lundbladef6531662018-12-04 10:42:22 +09003837
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303838
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003839int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003840{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003841 // Set up the decoder with a tiny bit of CBOR to parse because
3842 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003843 QCBORDecodeContext DC;
3844 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3845 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003846
Laurence Lundbladef6531662018-12-04 10:42:22 +09003847 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003848 // Then fish into the internals of the decode context
3849 // to get the allocator function so it can be called directly.
3850 // Also figure out how much pool is available for use
3851 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003852 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003853 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3854 if(nError) {
3855 return -9;
3856 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003857 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3858 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3859 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003860
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003861 // First test -- ask for one more byte than available and see failure
3862 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003863 if(!UsefulBuf_IsNULL(Allocated)) {
3864 return -1;
3865 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003866
Laurence Lundbladef6531662018-12-04 10:42:22 +09003867 // Re do the set up for the next test that will do a successful alloc,
3868 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003869 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003870 pAlloc = DC.StringAllocator.pfAllocator;
3871 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3872 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003873
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003874 // Allocate one byte less than available and see success
3875 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003876 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3877 return -2;
3878 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003879 // Ask for some more and see failure
3880 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003881 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3882 return -3;
3883 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003884 // Free the first allocate, retry the second and see success
3885 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3886 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003887 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3888 return -4;
3889 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003890
Laurence Lundbladef6531662018-12-04 10:42:22 +09003891 // Re do set up for next test that involves a successful alloc,
3892 // and a successful realloc and a failed realloc
3893 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003894 pAlloc = DC.StringAllocator.pfAllocator;
3895 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003896
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003897 // Allocate half the pool and see success
3898 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003899 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3900 return -5;
3901 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003902 // Reallocate to take up the whole pool and see success
3903 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003904 if(UsefulBuf_IsNULL(Allocated2)) {
3905 return -6;
3906 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003907 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003908 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3909 return -7;
3910 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003911 // Try to allocate more to be sure there is failure after a realloc
3912 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3913 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003914 return -8;
3915 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003916
Laurence Lundbladef6531662018-12-04 10:42:22 +09003917 return 0;
3918}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003919
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003920
3921/* Just enough of an allocator to test configuration of one */
3922static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3923{
3924 (void)pOldMem; // unused variable
3925
3926 if(uNewSize) {
3927 // Assumes the context pointer is the buffer and
3928 // nothing too big will ever be asked for.
3929 // This is only good for this basic test!
3930 return (UsefulBuf) {pCtx, uNewSize};
3931 } else {
3932 return NULLUsefulBuf;
3933 }
3934}
3935
3936
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003937int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003938{
3939 // Set up the decoder with a tiny bit of CBOR to parse because
3940 // nothing can be done with it unless that is set up.
3941 QCBORDecodeContext DC;
3942 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3943 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3944
3945 uint8_t pAllocatorBuffer[50];
3946
3947 // This is really just to test that this call works.
3948 // The full functionality of string allocators is tested
3949 // elsewhere with the MemPool internal allocator.
3950 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3951
3952 QCBORItem Item;
3953 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3954 return -1;
3955 }
3956
3957 if(Item.uDataAlloc == 0 ||
3958 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3959 Item.val.string.ptr != pAllocatorBuffer) {
3960 return -2;
3961 }
3962
3963 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3964 return -3;
3965 }
3966
3967 return 0;
3968}
3969
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003970#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08003971
Laurence Lundbladea826c502020-05-10 21:07:00 -07003972/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08003973 [
3974 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003975 4([-20, 4759477275222530853136]),
3976 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003977 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003978 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003979 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07003980 5([ 9223372036854775806, -4759477275222530853137])
3981 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003982 ]
3983 */
3984
3985static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003986 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003987 0xC4, 0x82, 0x20,
3988 0x03,
3989 0xC4, 0x82, 0x33,
3990 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3991 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3992 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3993 0xC5, 0x82, 0x19, 0x01, 0x2C,
3994 0x18, 0x64,
3995 0xC5, 0x82, 0x33,
3996 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3997 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3998 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07003999 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4000 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004001 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4002 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4003};
4004
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004005
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004006int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004007{
4008 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004009 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004010 QCBORItem item;
4011
Laurence Lundblade17af4902020-01-07 19:11:55 -08004012 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4013 0x06, 0x07, 0x08, 0x09, 0x010};
4014 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004015
4016
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004017 QCBORDecode_Init(&DC,
4018 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4019 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004020
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004021 uErr = QCBORDecode_GetNext(&DC, &item);
4022 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004023 return 1;
4024 }
4025
4026 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4027 return 2;
4028 }
4029
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004030 uErr = QCBORDecode_GetNext(&DC, &item);
4031 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004032 return 3;
4033 }
4034
4035 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4036 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4037 item.val.expAndMantissa.nExponent != -1) {
4038 return 4;
4039 }
4040
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004041 uErr = QCBORDecode_GetNext(&DC, &item);
4042 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004043 return 5;
4044 }
4045
4046 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4047 item.val.expAndMantissa.nExponent != -20 ||
4048 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4049 return 6;
4050 }
4051
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004052 uErr = QCBORDecode_GetNext(&DC, &item);
4053 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004054 return 7;
4055 }
4056
4057 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4058 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4059 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4060 return 8;
4061 }
4062
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004063 uErr = QCBORDecode_GetNext(&DC, &item);
4064 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004065 return 9;
4066 }
4067
4068 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4069 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4070 item.val.expAndMantissa.nExponent != 300) {
4071 return 10;
4072 }
4073
Laurence Lundbladea826c502020-05-10 21:07:00 -07004074 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004075 uErr = QCBORDecode_GetNext(&DC, &item);
4076 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004077 return 11;
4078 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004079 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4080 item.val.expAndMantissa.nExponent != -20 ||
4081 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4082 return 12;
4083 }
4084
Laurence Lundbladea826c502020-05-10 21:07:00 -07004085 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004086 uErr = QCBORDecode_GetNext(&DC, &item);
4087 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004088 return 13;
4089 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004090 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4091 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4092 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4093 return 14;
4094 }
4095
Laurence Lundbladea826c502020-05-10 21:07:00 -07004096 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004097 uErr = QCBORDecode_GetNext(&DC, &item);
4098 if(uErr != QCBOR_SUCCESS) {
4099 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004100 }
4101 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4102 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4103 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004104 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004105 }
4106
Laurence Lundbladea826c502020-05-10 21:07:00 -07004107 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004108 uErr = QCBORDecode_GetNext(&DC, &item);
4109 if(uErr != QCBOR_SUCCESS) {
4110 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004111 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004112 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4113 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4114 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004115 return 18;
4116 }
4117
4118 uErr = QCBORDecode_Finish(&DC);
4119 if(uErr != QCBOR_SUCCESS) {
4120 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004121 }
4122
4123 /* Now encode some stuff and then decode it */
4124 uint8_t pBuf[40];
4125 QCBOREncodeContext EC;
4126 UsefulBufC Encoded;
4127
4128 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4129 QCBOREncode_OpenArray(&EC);
4130 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4131 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4132 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4133 QCBOREncode_CloseArray(&EC);
4134 QCBOREncode_Finish(&EC, &Encoded);
4135
4136
4137 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004138 uErr = QCBORDecode_GetNext(&DC, &item);
4139 if(uErr != QCBOR_SUCCESS) {
4140 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004141 }
4142
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004143 uErr = QCBORDecode_GetNext(&DC, &item);
4144 if(uErr != QCBOR_SUCCESS) {
4145 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004146 }
4147
4148 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4149 item.val.expAndMantissa.nExponent != 1000 ||
4150 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004151 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004152 }
4153
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004154 uErr = QCBORDecode_GetNext(&DC, &item);
4155 if(uErr != QCBOR_SUCCESS) {
4156 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004157 }
4158
4159 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4160 item.val.expAndMantissa.nExponent != INT32_MIN ||
4161 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004162 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004163 }
4164
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004165 uErr = QCBORDecode_GetNext(&DC, &item);
4166 if(uErr != QCBOR_SUCCESS) {
4167 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004168 }
4169
4170 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4171 item.val.expAndMantissa.nExponent != INT32_MAX ||
4172 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004173 return 106;
4174 }
4175
4176
4177 int64_t nExp, nMant;
4178 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4179 UsefulBufC Mant;
4180 bool bIsNeg;
4181
4182 QCBORDecode_Init(&DC,
4183 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4184 QCBOR_DECODE_MODE_NORMAL);
4185 QCBORDecode_EnterArray(&DC);
4186
4187 // 4([-1, 3]),
4188 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nExp, &nMant);
4189
4190 // 4([-20, 4759477275222530853136]),
4191 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4192
4193 // 4([9223372036854775807, -4759477275222530853137]),
4194 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4195
4196 // 5([300, 100]),
4197 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nExp, &nMant);
4198
4199 // 5([-20, 4759477275222530853136]),
4200 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4201
4202 // 5([-9223372036854775807, -4759477275222530853137])
4203 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4204
4205 // 5([ 9223372036854775806, -4759477275222530853137])
4206 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4207
4208 // 5([ 9223372036854775806, 9223372036854775806])]
4209 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4210
4211 QCBORDecode_ExitArray(&DC);
4212
4213 uErr = QCBORDecode_Finish(&DC);
4214 if(uErr != QCBOR_SUCCESS) {
4215 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004216 }
4217
4218 return 0;
4219}
4220
4221
4222static struct FailInput ExponentAndMantissaFailures[] = {
4223 // Exponent > INT64_MAX
4224 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4225 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4226 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4227 // Mantissa > INT64_MAX
4228 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4229 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4230 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4231 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004232 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004233 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004234 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004235 // bad content for big num
4236 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4237 // bad content for big num
4238 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4239 // Bad integer for exponent
4240 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4241 // Bad integer for mantissa
4242 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4243 // 3 items in array
4244 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4245 // unterminated indefinite length array
4246 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4247 // Empty array
4248 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4249 // Second is not an integer
4250 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4251 // First is not an integer
4252 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4253 // Not an array
4254 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4255};
4256
4257
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004258int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004259{
4260 return ProcessFailures(ExponentAndMantissaFailures,
4261 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4262}
4263
4264#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004265
4266
4267
4268/*
4269 Some basic CBOR with map and array used in a lot of tests.
4270 The map labels are all strings
4271
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004272 {
4273 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004274 "an array of two strings": [
4275 "string1", "string2"
4276 ],
4277 "map in a map": {
4278 "bytes 1": h'78787878',
4279 "bytes 2": h'79797979',
4280 "another int": 98,
4281 "text 2": "lies, damn lies and statistics"
4282 }
4283 }
4284 */
4285
Laurence Lundblade1341c592020-04-11 14:19:05 -07004286#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004287
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004288static char strbuf[10];
4289const char *PrintType(uint8_t type) {
4290 switch(type) {
4291 case QCBOR_TYPE_INT64: return "INT64";
4292 case QCBOR_TYPE_UINT64: return "UINT64";
4293 case QCBOR_TYPE_ARRAY: return "ARRAY";
4294 case QCBOR_TYPE_MAP: return "MAP";
4295 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
4296 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
4297 default:
4298 sprintf(strbuf, "%d", type);
4299 return strbuf;
4300 }
4301}
4302
4303
4304void PrintItem(QCBORItem Item)
4305{
4306 printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":"");
4307 if(Item.uLabelType) {
4308 printf("Label: %s ", PrintType(Item.uLabelType));
4309 if(Item.uLabelType == QCBOR_TYPE_INT64) {
4310 printf("%lld\n", Item.label.int64);
4311 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07004312 // TODO: proper conversion to null-terminated string
4313 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004314 }
4315 }
4316}
4317
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004318
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004319int32_t EMap(UsefulBufC input)
4320{
4321 QCBORItem Item1, Item2, Item3;
4322 int64_t nDecodedInt1, nDecodedInt2;
4323 UsefulBufC B1, B2, S1, S2, S3;
4324
4325 QCBORDecodeContext DCtx;
4326 QCBORError nCBORError;
4327
4328 QCBORDecode_Init(&DCtx, input, 0);
4329
4330 QCBORDecode_EnterMap(&DCtx);
4331
4332 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4333
4334 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4335 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4336 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1);
4337 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2);
4338 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
4339 QCBORDecode_ExitMap(&DCtx);
4340
4341 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4342 QCBORDecode_GetNext(&DCtx, &Item1);
4343 QCBORDecode_GetNext(&DCtx, &Item2);
4344 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4345 return -400;
4346 }
4347 QCBORDecode_ExitArray(&DCtx);
4348
4349 // Parse the same array again using GetText() instead of GetItem()
4350 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4351 QCBORDecode_GetText(&DCtx, &S2);
4352 QCBORDecode_GetText(&DCtx, &S3);
4353 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4354 return 5000;
4355 }
4356 /* QCBORDecode_GetText(&DCtx, &S3);
4357 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4358 return 5001;
4359 } */
4360
4361 QCBORDecode_ExitArray(&DCtx);
4362
4363 QCBORDecode_ExitMap(&DCtx);
4364
4365 nCBORError = QCBORDecode_Finish(&DCtx);
4366
4367 if(nCBORError) {
4368 return (int32_t)nCBORError;
4369 }
4370
4371 if(nDecodedInt1 != 42) {
4372 return 1001;
4373 }
4374
4375 if(nDecodedInt2 != 98) {
4376 return 1002;
4377 }
4378
4379 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4380 UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){
4381 return 1003;
4382 }
4383
4384 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4385 UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){
4386 return 1004;
4387 }
4388
4389 if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){
4390 return 1005;
4391 }
4392
4393 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4394 return 1006;
4395 }
4396
4397 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4398 return 1007;
4399 }
4400
4401 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4402 return 1008;
4403 }
4404
4405 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4406 return 1009;
4407 }
4408
4409 return 0;
4410}
4411
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004412
4413/*
4414 [23,
4415 6000,
4416 h'67616C6163746963',
4417 h'686176656E20746F6B656E'
4418 ]
4419 */
4420static const uint8_t spSimpleArray[] = {
44210x84, 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};
4422
4423
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004424static const uint8_t spEmptyMap[] = {0xa0};
4425
4426static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004427
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004428static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4429
Laurence Lundbladef0499502020-08-01 11:55:57 -07004430/*
4431 {
4432 0: [],
4433 9: [
4434 [],
4435 []
4436 ],
4437 8: {
4438 1: [],
4439 2: {},
4440 3: []
4441 },
4442 4: {},
4443 5: [],
4444 6: [
4445 [],
4446 []
4447 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004448 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004449 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004450
4451
Laurence Lundbladef0499502020-08-01 11:55:57 -07004452static const uint8_t spMapOfEmpty[] = {
4453 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4454 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4455 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004456
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004457int32_t EnterMapTest()
4458{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004459 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004460 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004461 int32_t nReturn;
4462 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004463
4464
4465 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4466 QCBORDecode_EnterMap(&DCtx);
4467
Laurence Lundbladef0499502020-08-01 11:55:57 -07004468
4469 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004470 QCBORDecode_ExitArray(&DCtx);
4471
Laurence Lundbladef0499502020-08-01 11:55:57 -07004472 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004473 QCBORDecode_EnterArray(&DCtx);
4474 QCBORDecode_ExitArray(&DCtx);
4475 QCBORDecode_EnterArray(&DCtx);
4476 QCBORDecode_ExitArray(&DCtx);
4477 QCBORDecode_ExitArray(&DCtx);
4478
Laurence Lundbladef0499502020-08-01 11:55:57 -07004479 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004480 QCBORDecode_EnterArray(&DCtx);
4481 QCBORDecode_ExitArray(&DCtx);
4482 QCBORDecode_EnterMap(&DCtx);
4483 QCBORDecode_ExitMap(&DCtx);
4484 QCBORDecode_EnterArray(&DCtx);
4485 QCBORDecode_ExitArray(&DCtx);
4486 QCBORDecode_ExitMap(&DCtx);
4487
Laurence Lundbladef0499502020-08-01 11:55:57 -07004488 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004489 QCBORDecode_ExitMap(&DCtx);
4490
Laurence Lundbladef0499502020-08-01 11:55:57 -07004491 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004492 QCBORDecode_ExitArray(&DCtx);
4493
Laurence Lundbladef0499502020-08-01 11:55:57 -07004494 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004495 QCBORDecode_EnterArray(&DCtx);
4496 QCBORDecode_ExitArray(&DCtx);
4497 QCBORDecode_EnterArray(&DCtx);
4498 QCBORDecode_ExitArray(&DCtx);
4499 QCBORDecode_ExitArray(&DCtx);
4500
4501 QCBORDecode_ExitMap(&DCtx);
4502
4503 uErr = QCBORDecode_Finish(&DCtx);
4504 if(uErr != QCBOR_SUCCESS){
4505 return 3011;
4506 }
4507
4508
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004509 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004510 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004511 if(nReturn) {
4512 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004513 }
4514
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004515 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004516 if(nReturn) {
4517 return nReturn;
4518 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004519
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004520
Laurence Lundblade937ea812020-05-08 11:38:23 -07004521
4522 // These tests confirm the cursor is at the right place after entering a map or array
4523
4524 // Confirm cursor is at right place
4525 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4526 QCBORDecode_EnterMap(&DCtx);
4527 QCBORDecode_GetNext(&DCtx, &Item1);
4528 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4529 return 2001;
4530 }
4531
4532
4533 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4534 QCBORDecode_GetNext(&DCtx, &Item1);
4535 QCBORDecode_GetNext(&DCtx, &Item1);
4536 QCBORDecode_EnterArray(&DCtx);
4537 QCBORDecode_GetNext(&DCtx, &Item1);
4538 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4539 return 2002;
4540 }
4541
4542 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4543 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004544 QCBORDecode_GetNext(&DCtx, &Item1);
4545 QCBORDecode_GetNext(&DCtx, &Item1);
4546 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004547 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4548 QCBORDecode_GetNext(&DCtx, &Item1);
4549 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4550 return 2003;
4551 }
4552
4553 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4554 QCBORDecode_EnterMap(&DCtx);
4555 QCBORDecode_GetNext(&DCtx, &Item1);
4556 QCBORDecode_GetNext(&DCtx, &Item1);
4557 QCBORDecode_GetNext(&DCtx, &Item1);
4558 QCBORDecode_GetNext(&DCtx, &Item1);
4559 QCBORDecode_GetNext(&DCtx, &Item1);
4560 QCBORDecode_GetNext(&DCtx, &Item1);
4561 QCBORDecode_GetNext(&DCtx, &Item1);
4562 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4563 QCBORDecode_GetNext(&DCtx, &Item1);
4564 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004565 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004566 }
4567
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004568 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4569 QCBORDecode_EnterMap(&DCtx);
4570 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4571 QCBORDecode_ExitArray(&DCtx);
4572 QCBORDecode_GetNext(&DCtx, &Item1);
4573 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4574 return 2006;
4575 }
4576 QCBORDecode_ExitMap(&DCtx);
4577 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4578 return 2007;
4579 }
4580
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004581 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4582 QCBORDecode_EnterArray(&DCtx);
4583 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004584 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4585 uErr = QCBORDecode_GetAndResetError(&DCtx);
4586 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004587 return 2008;
4588 }
4589 UsefulBufC String;
4590 QCBORDecode_GetTextInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004591 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004592 return 2009;
4593 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004594
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004595
4596 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4597 QCBORDecode_EnterMap(&DCtx);
4598 // This will fail because the map is empty.
4599 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4600 uErr = QCBORDecode_GetAndResetError(&DCtx);
4601 if(uErr != QCBOR_ERR_NOT_FOUND){
4602 return 2010;
4603 }
4604 QCBORDecode_ExitMap(&DCtx);
4605 uErr = QCBORDecode_Finish(&DCtx);
4606 if(uErr != QCBOR_SUCCESS){
4607 return 2011;
4608 }
4609
4610
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004611 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4612 QCBORDecode_EnterMap(&DCtx);
4613 // This will fail because the map is empty.
4614 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4615 uErr = QCBORDecode_GetAndResetError(&DCtx);
4616 if(uErr != QCBOR_ERR_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004617 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004618 }
4619 QCBORDecode_ExitMap(&DCtx);
4620 uErr = QCBORDecode_Finish(&DCtx);
4621 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004622 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004623 }
4624
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004625
4626 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
4627 QCBORDecode_EnterArray(&DCtx);
4628 QCBORDecode_GetBytes(&DCtx, &String);
4629 QCBORDecode_EnterMap(&DCtx);
4630 QCBORDecode_ExitMap(&DCtx);
4631 QCBORDecode_EnterArray(&DCtx);
4632 QCBORDecode_ExitArray(&DCtx);
4633 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
4634 QCBORDecode_ExitArray(&DCtx);
4635 uErr = QCBORDecode_Finish(&DCtx);
4636 if(uErr != QCBOR_SUCCESS){
4637 return 2014;
4638 }
4639
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07004640 // TODO: more testing of entered mapps and arrays with problems
4641 // TODO: document error handling better (maybe improve error handling)
4642
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004643 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004644}
4645
4646
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004647struct NumberConversion {
4648 char *szDescription;
4649 UsefulBufC CBOR;
4650 int64_t nConvertedToInt64;
4651 QCBORError uErrorInt64;
4652 uint64_t uConvertToUInt64;
4653 QCBORError uErrorUint64;
4654 double dConvertToDouble;
4655 QCBORError uErrorDouble;
4656};
4657
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004658static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004659 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07004660 "too large to fit into int64_t",
4661 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
4662 0,
4663 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4664 0,
4665 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4666 ((double)INT64_MIN) + 1 ,
4667 QCBOR_SUCCESS
4668 },
4669 {
4670 "largest negative int that fits in int64_t",
4671 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
4672 INT64_MIN,
4673 QCBOR_SUCCESS,
4674 0,
4675 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4676 (double)INT64_MIN,
4677 QCBOR_SUCCESS
4678 },
4679 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004680 "negative bignum -1",
4681 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
4682 -1,
4683 QCBOR_SUCCESS,
4684 0,
4685 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4686 -1.0,
4687 QCBOR_SUCCESS
4688 },
4689 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004690 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004691 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4692 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004693#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07004694 257000,
4695 QCBOR_SUCCESS,
4696 257000,
4697 QCBOR_SUCCESS,
4698 257000.0,
4699 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004700#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4701 0,
4702 QCBOR_ERR_UNEXPECTED_TYPE,
4703 0,
4704 QCBOR_ERR_UNEXPECTED_TYPE,
4705 0.0,
4706 QCBOR_ERR_UNEXPECTED_TYPE
4707#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07004708 },
4709 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004710 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004711 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4712 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004713#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07004714 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07004715 QCBOR_SUCCESS,
4716 0,
4717 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07004718 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07004719 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004720#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4721 0,
4722 QCBOR_ERR_UNEXPECTED_TYPE,
4723 0,
4724 QCBOR_ERR_UNEXPECTED_TYPE,
4725 0.0,
4726 QCBOR_ERR_UNEXPECTED_TYPE
4727#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07004728 },
4729 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004730 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004731 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4732 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004733#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07004734 2056,
4735 QCBOR_SUCCESS,
4736 2056,
4737 QCBOR_SUCCESS,
4738 2056.0,
4739 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004740#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4741 0,
4742 QCBOR_ERR_UNEXPECTED_TYPE,
4743 0,
4744 QCBOR_ERR_UNEXPECTED_TYPE,
4745 0.0,
4746 QCBOR_ERR_UNEXPECTED_TYPE
4747#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07004748 },
4749 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004750 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07004751 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4752 0,
4753 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4754 0,
4755 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4756 -18446744073709551617.0,
4757 QCBOR_SUCCESS
4758 },
4759 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004760 "Positive bignum 0x01020304 indefinite length string",
4761 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
4762 0x01020304,
4763 QCBOR_SUCCESS,
4764 0x01020304,
4765 QCBOR_SUCCESS,
4766 16909060.0,
4767 QCBOR_SUCCESS
4768 },
4769 {
Laurence Lundblade887add82020-05-17 05:50:34 -07004770 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07004771 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4772 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004773#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07004774 0,
4775 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4776 0,
4777 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4778 -INFINITY,
4779 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004780#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4781 0,
4782 QCBOR_ERR_UNEXPECTED_TYPE,
4783 0,
4784 QCBOR_ERR_UNEXPECTED_TYPE,
4785 0.0,
4786 QCBOR_ERR_UNEXPECTED_TYPE
4787#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07004788 },
4789 {
4790 "big float [9223372036854775806, 9223372036854775806]",
4791 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4792 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004793#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07004794 0,
4795 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4796 0,
4797 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4798 INFINITY,
4799 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004800#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4801 0,
4802 QCBOR_ERR_UNEXPECTED_TYPE,
4803 0,
4804 QCBOR_ERR_UNEXPECTED_TYPE,
4805 0.0,
4806 QCBOR_ERR_UNEXPECTED_TYPE
4807#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07004808 },
4809 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004810 "Big float 3 * 2^^2",
4811 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004812#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07004813 12,
4814 QCBOR_SUCCESS,
4815 12,
4816 QCBOR_SUCCESS,
4817 12.0,
4818 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004819#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4820 0,
4821 QCBOR_ERR_UNEXPECTED_TYPE,
4822 0,
4823 QCBOR_ERR_UNEXPECTED_TYPE,
4824 0.0,
4825 QCBOR_ERR_UNEXPECTED_TYPE
4826#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07004827 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004828 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004829 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004830 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4831 0,
4832 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4833 18446744073709551615ULL,
4834 QCBOR_SUCCESS,
4835 18446744073709551615.0,
4836 QCBOR_SUCCESS
4837 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004838 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004839 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004840 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4841 65536-1,
4842 QCBOR_SUCCESS,
4843 0xffff,
4844 QCBOR_SUCCESS,
4845 65535.0,
4846 QCBOR_SUCCESS
4847 },
4848 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004849 "Postive integer 0",
4850 {(uint8_t[]){0x0}, 1},
4851 0LL,
4852 QCBOR_SUCCESS,
4853 0ULL,
4854 QCBOR_SUCCESS,
4855 0.0,
4856 QCBOR_SUCCESS
4857 },
4858 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004859 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004860 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4861 -9223372036854775807-1, // INT64_MIN
4862 QCBOR_SUCCESS,
4863 0ULL,
4864 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4865 -9223372036854775808.0,
4866 QCBOR_SUCCESS
4867 },
4868 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004869 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004870 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004871#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004872 100L,
4873 QCBOR_SUCCESS,
4874 100ULL,
4875 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004876#else /* QCBOR_DISABLE_FLOAT_HW_USE */
4877 0,
4878 QCBOR_ERR_HW_FLOAT_DISABLED,
4879 0,
4880 QCBOR_ERR_HW_FLOAT_DISABLED,
4881#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004882 100.3,
4883 QCBOR_SUCCESS
4884 },
4885 {
4886 "Floating point value NaN 0xfa7fc00000",
4887 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004888#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004889 0,
4890 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4891 0,
4892 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004893#else /* QCBOR_DISABLE_FLOAT_HW_USE */
4894 0,
4895 QCBOR_ERR_HW_FLOAT_DISABLED,
4896 0,
4897 QCBOR_ERR_HW_FLOAT_DISABLED,
4898#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004899 NAN,
4900 QCBOR_SUCCESS
4901 },
4902 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004903 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004904 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004905#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
4906#ifndef QCBOR_DISABLE_FLOAT_HW_USE
4907 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004908 -4,
4909 QCBOR_SUCCESS,
4910 0,
4911 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4912 -4.0,
4913 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004914#else /* QCBOR_DISABLE_FLOAT_HW_USE */
4915 // Float HW disabled
4916 -4,
4917 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
4918 0,
4919 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
4920 -4.0,
4921 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
4922#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
4923#else
4924 // Half-precision disabled
4925 -4,
4926 QCBOR_ERR_HALF_PRECISION_DISABLED,
4927 0,
4928 QCBOR_ERR_HALF_PRECISION_DISABLED,
4929 -4.0,
4930 QCBOR_ERR_HALF_PRECISION_DISABLED
4931#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004932 },
4933 {
4934 "Decimal fraction 3/10",
4935 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004936#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004937 0,
4938 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4939 0,
4940 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4941 0.30000000000000004,
4942 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004943#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4944 0,
4945 QCBOR_ERR_UNEXPECTED_TYPE,
4946 0,
4947 QCBOR_ERR_UNEXPECTED_TYPE,
4948 0.0,
4949 QCBOR_ERR_UNEXPECTED_TYPE
4950#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004951 }
4952};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004953
4954
4955
Laurence Lundblade313b2862020-05-16 01:23:06 -07004956int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004957{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004958 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004959
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004960 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
4961 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004962
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004963 // Set up the decoding context including a memory pool so that
4964 // indefinite length items can be checked
4965 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004966 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004967
4968 /* ----- test conversion to int64_t ------ */
4969 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004970 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4971 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004972 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004973 }
4974
4975 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004976 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004977 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004978 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004979 }
4980 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004981 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004982 }
4983
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004984 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004985 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4986 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4987 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004988 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004989 }
4990 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004991 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004992 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004993 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004994 }
4995 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004996 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004997 }
4998
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004999 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005000 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5001 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5002 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005003 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005004 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005005#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005006 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005007 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005008 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005009 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005010 }
5011 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5012 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005013 // NaN's can't be compared for equality. A NaN is
5014 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005015 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005016 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005017 }
5018 } else {
5019 // TODO: this comparison may need a margin of error
5020 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005021 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005022 }
5023 }
5024 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005025#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005026 }
5027
5028 return 0;
5029}
5030
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005031
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005032
5033
Laurence Lundbladee3553422020-05-02 11:11:17 -07005034int32_t CBORSequenceDecodeTests(void)
5035{
5036 QCBORDecodeContext DCtx;
5037 QCBORItem Item;
5038 QCBORError uCBORError;
5039
5040 // --- Test a sequence with extra bytes ---
5041
5042 // The input for the date test happens to be a sequence so it
5043 // is reused. It is a sequence because it doesn't start as
5044 // an array or map.
5045 QCBORDecode_Init(&DCtx,
5046 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5047 QCBOR_DECODE_MODE_NORMAL);
5048
5049 // Get the first item
5050 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5051 if(uCBORError != QCBOR_SUCCESS) {
5052 return 1;
5053 }
5054 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5055 return 2;
5056 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005057
Laurence Lundbladee3553422020-05-02 11:11:17 -07005058 // Get a second item
5059 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005060 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5061 return 66;
5062 }
5063
5064 // Get a third item
5065 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005066 if(uCBORError != QCBOR_SUCCESS) {
5067 return 2;
5068 }
5069 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5070 return 3;
5071 }
5072
5073 // A sequence can have stuff at the end that may
5074 // or may not be valid CBOR. The protocol decoder knows
5075 // when to stop by definition of the protocol, not
5076 // when the top-level map or array is ended.
5077 // Finish still has to be called to know that
5078 // maps and arrays (if there were any) were closed
5079 // off correctly. When called like this it
5080 // must return the error QCBOR_ERR_EXTRA_BYTES.
5081 uCBORError = QCBORDecode_Finish(&DCtx);
5082 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5083 return 4;
5084 }
5085
5086
5087 // --- Test an empty input ----
5088 uint8_t empty[1];
5089 UsefulBufC Empty = {empty, 0};
5090 QCBORDecode_Init(&DCtx,
5091 Empty,
5092 QCBOR_DECODE_MODE_NORMAL);
5093
5094 uCBORError = QCBORDecode_Finish(&DCtx);
5095 if(uCBORError != QCBOR_SUCCESS) {
5096 return 5;
5097 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005098
5099
Laurence Lundbladee3553422020-05-02 11:11:17 -07005100 // --- Sequence with unclosed indefinite length array ---
5101 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005102
Laurence Lundbladee3553422020-05-02 11:11:17 -07005103 QCBORDecode_Init(&DCtx,
5104 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5105 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005106
Laurence Lundbladee3553422020-05-02 11:11:17 -07005107 // Get the first item
5108 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5109 if(uCBORError != QCBOR_SUCCESS) {
5110 return 7;
5111 }
5112 if(Item.uDataType != QCBOR_TYPE_INT64) {
5113 return 8;
5114 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005115
Laurence Lundbladee3553422020-05-02 11:11:17 -07005116 // Get a second item
5117 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5118 if(uCBORError != QCBOR_SUCCESS) {
5119 return 9;
5120 }
5121 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5122 return 10;
5123 }
5124
5125 // Try to finish before consuming all bytes to confirm
5126 // that the still-open error is returned.
5127 uCBORError = QCBORDecode_Finish(&DCtx);
5128 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
5129 return 11;
5130 }
5131
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005132
Laurence Lundbladee3553422020-05-02 11:11:17 -07005133 // --- Sequence with a closed indefinite length array ---
5134 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005135
Laurence Lundbladee3553422020-05-02 11:11:17 -07005136 QCBORDecode_Init(&DCtx,
5137 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5138 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005139
Laurence Lundbladee3553422020-05-02 11:11:17 -07005140 // Get the first item
5141 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5142 if(uCBORError != QCBOR_SUCCESS) {
5143 return 12;
5144 }
5145 if(Item.uDataType != QCBOR_TYPE_INT64) {
5146 return 13;
5147 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005148
Laurence Lundbladee3553422020-05-02 11:11:17 -07005149 // Get a second item
5150 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5151 if(uCBORError != QCBOR_SUCCESS) {
5152 return 14;
5153 }
5154 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5155 return 15;
5156 }
5157
5158 // Try to finish before consuming all bytes to confirm
5159 // that the still-open error is returned.
5160 uCBORError = QCBORDecode_Finish(&DCtx);
5161 if(uCBORError != QCBOR_SUCCESS) {
5162 return 16;
5163 }
5164
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005165
Laurence Lundbladee3553422020-05-02 11:11:17 -07005166 return 0;
5167}
5168
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005169
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005170
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005171int32_t IntToTests()
5172{
5173 int nErrCode;
5174 int32_t n32;
5175 int16_t n16;
5176 int8_t n8;
5177 uint32_t u32;
5178 uint16_t u16;
5179 uint8_t u8;
5180 uint64_t u64;
5181
5182 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5183 if(nErrCode == -1 || n32 != 1) {
5184 return 1;
5185 }
5186
5187 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5188 if(nErrCode == -1 || n32 != INT32_MAX) {
5189 return 2;
5190 }
5191
5192 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5193 if(nErrCode == -1 || n32 != INT32_MIN) {
5194 return 3;
5195 }
5196
5197 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5198 if(nErrCode != -1) {
5199 return 4;
5200 }
5201
5202 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5203 if(nErrCode != -1) {
5204 return 5;
5205 }
5206
5207
5208 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5209 if(nErrCode == -1 || n16 != INT16_MAX) {
5210 return 6;
5211 }
5212
5213 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5214 if(nErrCode == -1 || n16 != INT16_MIN) {
5215 return 7;
5216 }
5217
5218 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5219 if(nErrCode == -1 || n16 != 1) {
5220 return 8;
5221 }
5222
5223 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5224 if(nErrCode != -1) {
5225 return 9;
5226 }
5227
5228 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5229 if(nErrCode != -1) {
5230 return 10;
5231 }
5232
5233
5234 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5235 if(nErrCode == -1 || n8 != 1) {
5236 return 11;
5237 }
5238
5239 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5240 if(nErrCode == -1 || n8 != INT8_MAX) {
5241 return 12;
5242 }
5243
5244 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5245 if(nErrCode == -1 || n8 != INT8_MIN) {
5246 return 13;
5247 }
5248
5249 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5250 if(nErrCode != -1) {
5251 return 14;
5252 }
5253
5254 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5255 if(nErrCode != -1) {
5256 return 15;
5257 }
5258
5259
5260 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5261 if(nErrCode == -1 || u32 != 1) {
5262 return 16;
5263 }
5264
5265 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5266 if(nErrCode == -1 || u32 != UINT32_MAX) {
5267 return 17;
5268 }
5269
5270 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5271 if(nErrCode == -1 || u32 != 0) {
5272 return 18;
5273 }
5274
5275 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5276 if(nErrCode != -1) {
5277 return 19;
5278 }
5279
5280 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5281 if(nErrCode != -1) {
5282 return 20;
5283 }
5284
5285
5286 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5287 if(nErrCode == -1 || u16 != UINT16_MAX) {
5288 return 21;
5289 }
5290
5291 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5292 if(nErrCode == -1 || u16 != 0) {
5293 return 22;
5294 }
5295
5296 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5297 if(nErrCode == -1 || u16 != 1) {
5298 return 23;
5299 }
5300
5301 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5302 if(nErrCode != -1) {
5303 return 24;
5304 }
5305
5306 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5307 if(nErrCode != -1) {
5308 return 25;
5309 }
5310
5311
5312 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5313 if(nErrCode == -1 || u8 != UINT8_MAX) {
5314 return 26;
5315 }
5316
5317 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5318 if(nErrCode == -1 || u8 != 0) {
5319 return 27;
5320 }
5321
5322 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5323 if(nErrCode == -1 || u8 != 1) {
5324 return 28;
5325 }
5326
5327 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5328 if(nErrCode != -1) {
5329 return 29;
5330 }
5331
5332 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5333 if(nErrCode != -1) {
5334 return 30;
5335 }
5336
5337
5338 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5339 if(nErrCode == -1 || u64 != 1) {
5340 return 31;
5341 }
5342
5343 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
5344 if(nErrCode == -1 || u64 != INT64_MAX) {
5345 return 32;
5346 }
5347
5348 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
5349 if(nErrCode == -1 || u64 != 0) {
5350 return 33;
5351 }
5352
5353 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
5354 if(nErrCode != -1) {
5355 return 34;
5356 }
5357
5358 return 0;
5359}
5360
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005361
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005362
5363
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005364/*
5365A sequence with
5366 A wrapping bstr
5367 containing a map
5368 1
5369 2
5370 A wrapping bstr
5371 containing an array
5372 3
5373 wrapping bstr
5374 4
5375 5
5376 6
5377 array
5378 7
5379 8
5380
5381 */
5382
5383static UsefulBufC foo(UsefulBuf ffo)
5384{
5385 UsefulBufC Encoded;
5386 QCBOREncodeContext EC;
5387 QCBORError uErr;
5388
5389 QCBOREncode_Init(&EC, ffo);
5390
5391 QCBOREncode_BstrWrap(&EC);
5392 QCBOREncode_OpenMap(&EC);
5393 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
5394 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
5395 QCBOREncode_CloseMap(&EC);
5396 QCBOREncode_BstrWrap(&EC);
5397 QCBOREncode_OpenArray(&EC);
5398 QCBOREncode_AddInt64(&EC, 3);
5399 QCBOREncode_BstrWrap(&EC);
5400 QCBOREncode_AddInt64(&EC, 4);
5401 QCBOREncode_CloseBstrWrap(&EC, NULL);
5402 QCBOREncode_AddInt64(&EC, 5);
5403 QCBOREncode_CloseArray(&EC);
5404 QCBOREncode_CloseBstrWrap(&EC, NULL);
5405 QCBOREncode_AddInt64(&EC, 6);
5406 QCBOREncode_CloseBstrWrap(&EC, NULL);
5407 QCBOREncode_OpenArray(&EC);
5408 QCBOREncode_AddInt64(&EC, 7);
5409 QCBOREncode_AddInt64(&EC, 8);
5410 QCBOREncode_CloseArray(&EC);
5411
5412 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07005413 if(uErr) {
5414 Encoded = NULLUsefulBufC;
5415 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005416
5417 return Encoded;
5418}
5419
5420
5421int32_t EnterBstrTest()
5422{
5423 MakeUsefulBufOnStack(ffo, 100);
5424
5425 QCBORDecodeContext DC;
5426
5427 QCBORDecode_Init(&DC, foo(ffo), 0);
5428
5429 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
5430
5431
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005432 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005433 QCBORDecode_EnterMap(&DC);
5434 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
5435 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
5436 QCBORDecode_ExitMap(&DC);
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005437 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005438 QCBORDecode_EnterArray(&DC);
5439 QCBORDecode_GetInt64(&DC, &i3);
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005440 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005441 QCBORDecode_GetInt64(&DC, &i4);
5442 QCBORDecode_ExitBstrWrapped(&DC);
5443 QCBORDecode_GetInt64(&DC, &i5);
5444 QCBORDecode_ExitArray(&DC);
5445 QCBORDecode_ExitBstrWrapped(&DC);
5446 QCBORDecode_GetInt64(&DC, &i6);
5447 QCBORDecode_ExitBstrWrapped(&DC);
5448 QCBORDecode_EnterArray(&DC);
5449 QCBORDecode_GetInt64(&DC, &i7);
5450 QCBORDecode_GetInt64(&DC, &i8);
5451 QCBORDecode_ExitArray(&DC);
5452
5453 QCBORError uErr = QCBORDecode_Finish(&DC);
5454
5455 return (int32_t)uErr;
5456}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005457
5458
5459
5460
5461static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005462 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005463
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005464 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005465 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005466 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
5467 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
5468 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005469
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005470 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005471 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
5472 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
5473 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005474
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005475 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005476 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005477 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
5478 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005479
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005480 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005481 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
5482 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005483
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005484 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005485 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005486 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
5487 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005488
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005489 21,
5490 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
5491 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
5492
5493 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005494 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005495 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
5496 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005497
5498 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005499 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
5500 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005501
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005502 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005503 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005504 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
5505 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005506
5507 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005508 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
5509 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005510
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005511 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005512 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005513 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
5514 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005515
5516 0x18, 0x33,
5517 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
5518
5519 // MIME
5520 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005521 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
5522 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
5523 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005524
5525 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005526 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
5527 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005528
5529 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005530 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
5531 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
5532 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005533
5534 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005535 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
5536 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005537
5538 // UUID
5539 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005540 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
5541 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005542
5543 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005544 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
5545 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005546};
5547
5548int32_t DecodeTaggedTypeTests()
5549{
5550 QCBORDecodeContext DC;
5551 QCBORError uErr;
5552
5553 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
5554
5555 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005556 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005557
5558 QCBORDecode_EnterMap(&DC);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005559 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5560 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005561 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
5562 return 1;
5563 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005564 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005565 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5566 return 2;
5567 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005568 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005569 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5570 return 3;
5571 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005572 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
5573 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005574 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5575 return 4;
5576 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005577 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
5578 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5579 return 5;
5580 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005581
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005582 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5583 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5584 bNeg != false) {
5585 return 10;
5586 }
5587 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5588 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5589 bNeg != true) {
5590 return 11;
5591 }
5592 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5593 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5594 return 12;
5595 }
5596 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5597 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5598 return 13;
5599 }
5600 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5601 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5602 return 14;
5603 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005604
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005605 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5606 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5607 return 20;
5608 }
5609 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5610 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5611 return 21;
5612 }
5613 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5614 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5615 return 22;
5616 }
5617 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5618 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5619 return 23;
5620 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005621
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005622 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5623 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5624 return 30;
5625 }
5626 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5627 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5628 return 31;
5629 }
5630 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5631 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5632 return 32;
5633 }
5634 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5635 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5636 return 33;
5637 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005638
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005639 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5640 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5641 return 40;
5642 }
5643 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5644 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5645 return 41;
5646 }
5647 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5648 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5649 return 42;
5650 }
5651 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5652 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5653 return 43;
5654 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005655
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005656 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5657 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5658 return 50;
5659 }
5660 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5661 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5662 return 51;
5663 }
5664 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5665 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5666 return 52;
5667 }
5668 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5669 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5670 return 53;
5671 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005672
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005673 // MIME
5674 bool bIsNot7Bit;
5675 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bIsNot7Bit);
5676 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5677 bIsNot7Bit == true) {
5678 return 60;
5679 }
5680 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bIsNot7Bit);
5681 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5682 bIsNot7Bit == true) {
5683 return 61;
5684 }
5685 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bIsNot7Bit);
5686 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5687 bIsNot7Bit == false) {
5688 return 62;
5689 }
5690 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bIsNot7Bit);
5691 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5692 bIsNot7Bit == false) {
5693 return 63;
5694 }
5695 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5696 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5697 return 64;
5698 }
5699 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5700 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5701 return 65;
5702 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005703
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005704 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5705 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5706 return 70;
5707 }
5708 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5709 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5710 return 71;
5711 }
5712 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5713 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5714 return 72;
5715 }
5716 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5717 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5718 return 73;
5719 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005720
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005721 // Improvement: add some more error test cases
5722
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005723 QCBORDecode_ExitMap(&DC);
5724
5725 uErr = QCBORDecode_Finish(&DC);
5726 if(uErr != QCBOR_SUCCESS) {
5727 return 100;
5728 }
5729
5730 return 0;
5731}