blob: c3dcc1e54c4669a35dc18a439291b00e776dac85 [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 Lundblade2ded3d92018-10-09 21:36:11 +08002104 0xc1, // tag for epoch date
2105 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2106
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002107 // CBOR_TAG_B64
Laurence Lundblade8fa7d5d2020-07-11 16:30:47 -07002108 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002109 0x1a, 0x53, 0x72, 0x4E, 0x01,
2110
2111 0xc1, // tag for epoch date
2112 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002113
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002114 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002115 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002116
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002117 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002118 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002119
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002120 0xc1, // tag for epoch date
2121 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2122 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2123
2124 0xc1, // tag for epoch date
2125 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002126};
2127
2128
2129// have to check float expected only to within an epsilon
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002130static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002131
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002132 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002133
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002134 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002135
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002136 return diff > 0.0000001;
2137}
2138
2139
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002140int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002141{
2142 QCBORDecodeContext DCtx;
2143 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002144 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002145
Laurence Lundbladeee851742020-01-08 08:37:05 -08002146 QCBORDecode_Init(&DCtx,
2147 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2148 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002149
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002150 const uint64_t uTags[] = {15};
2151 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002152
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002153 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002154
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002155 // String date
2156 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2157 return -1;
2158 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002159 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002160 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161 }
2162
Laurence Lundblade8fa7d5d2020-07-11 16:30:47 -07002163 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002165 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002166 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2167 Item.val.epochDate.nSeconds != 1400000000 ||
2168 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002169 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002171
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002172 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2173 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002174 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002175 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002176 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2177 Item.val.epochDate.nSeconds != 1400000001 ||
2178 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002179 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002180 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002182
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002183 // Epoch date that is too large for our representation
2184 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002185 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002187
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002188 // Epoch date in float format with fractional seconds
Laurence Lundblade9682a532020-06-06 18:33:04 -07002189#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002190 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002191 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002192 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2193 Item.val.epochDate.nSeconds != 1 ||
2194 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002195 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002196 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002197
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002198 // Epoch date float that is too large for our representation
2199 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002200 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002202
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002203 // Epoch date double that is just slightly too large
2204 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2205 return -11;
2206 }
2207
2208 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002209 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2210 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2211 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2212 Item.val.epochDate.nSeconds == 0) {
2213 return -12;
2214 }
2215#else
2216 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_UNSUPPORTED) {
2217 return -80;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002218 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002219 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_UNSUPPORTED) {
2220 return -80;
2221 }
2222 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_UNSUPPORTED) {
2223 return -80;
2224 }
2225 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_UNSUPPORTED) {
2226 return -80;
2227 }
2228#endif
2229
2230
Laurence Lundbladeee851742020-01-08 08:37:05 -08002231 // TODO: could use a few more tests with float, double, and half precsion
2232 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002233
2234 return 0;
2235}
2236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002237// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002238static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002239 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002240 0x81, // Array of one
2241 0xd8, 0x04, // non-preferred serialization of tag 4
2242 0x82, 0x01, 0x03}; // fraction 1/3
2243
Laurence Lundblade59289e52019-12-30 13:44:37 -08002244/*
2245 DB 9192939495969798 # tag(10489608748473423768)
2246 80 # array(0)
2247 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002248static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2249 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002250
Laurence Lundblade59289e52019-12-30 13:44:37 -08002251/*
2252DB 9192939495969798 # tag(10489608748473423768)
2253 D8 88 # tag(136)
2254 C6 # tag(6)
2255 C7 # tag(7)
2256 80 # array(0)
2257*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002258static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2259 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002260
2261/*
2262 The cbor.me parse of this.
2263 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002264 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 +07002265 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2266 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2267 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2268 */
2269static uint8_t spCSRWithTags[] = {
2270 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2271 0xc6, 0xc7, 0x36,
2272 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2273 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2274 0xcb, 0xa5,
2275 0xd1, 0x31,
2276 0xd1, 0xd1, 0xd1, 0x6c,
2277 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2278 0xc9, 0x30,
2279 0xd9, 0x03, 0x05, 0x63,
2280 0x53, 0x53, 0x47,
2281 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002282 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002283 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2284 0xd1, 0x2f,
2285 0xd1, 0x69,
2286 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2287 0xd1, 0x2d,
2288 0xd1, 0x62,
2289 0x55, 0x53,
2290 0xd7, 0x32,
2291 0xd3, 0xa2,
2292 0x2a,
2293 0xc9, 0xa1,
2294 0x28,
2295 0x26,
2296 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2297 0xcc, 0x4a,
2298 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2299 0xd0, 0x35,
2300 0xd7, 0xa1,
2301 0xcb, 0xc8, 0xc7, 0x24,
2302 0xc8, 0x22};
2303
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002304static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002305
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002306
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002307int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002308{
2309 QCBORDecodeContext DCtx;
2310 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002311
Laurence Lundbladeee851742020-01-08 08:37:05 -08002312 QCBORDecode_Init(&DCtx,
2313 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2314 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002315
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002316 //-------------------------
2317 // This text matches the magic number tag and the fraction tag
2318 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2319 return -2;
2320 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002321 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002322 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2323 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002325
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002326 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2327 return -4;
2328 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002329
2330#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002331 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002332 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002333 Item.val.uCount != 2) {
2334 return -5;
2335 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002336#else
2337 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2338 return -6;
2339 }
2340#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002341
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002342 // --------------------------------
2343 // This test decodes the very large tag, but it is not in
2344 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002345 QCBORDecode_Init(&DCtx,
2346 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2347 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002348 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2349 return -6;
2350 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002351 /*
2352 if(Item.uTagBits) { // TODO: make sure it is OK to remove this
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002353 return -7;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002354 }*/
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002356 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002357 // This test sets up a caller-config list that includes the very large
2358 // tage and then matches it.
2359 QCBORDecode_Init(&DCtx,
2360 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2361 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002362 const uint64_t puList[] = {0x9192939495969798, 257};
2363 const QCBORTagListIn TL = {2, puList};
2364 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002365
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002366 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2367 return -8;
2368 }
2369 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2370 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2371 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2372 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2373 Item.val.uCount != 0) {
2374 return -9;
2375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002376
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002377 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002378 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002379 const uint64_t puLongList[17] = {1,2,1};
2380 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002381 QCBORDecode_Init(&DCtx,
2382 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2383 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002384 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2385 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2386 return -11;
2387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002388
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002389 // -----------------------
2390 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002391 QCBORDecode_Init(&DCtx,
2392 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2393 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002394 uint64_t puTags[16];
2395 QCBORTagListOut Out = {0, 4, puTags};
2396 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2397 return -12;
2398 }
2399 if(puTags[0] != 0x9192939495969798 ||
2400 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002401 puTags[2] != 0x06 ||
2402 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002403 return -13;
2404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002405
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002406 // ----------------------
2407 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002408 QCBORDecode_Init(&DCtx,
2409 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2410 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002411 QCBORTagListOut OutSmall = {0, 3, puTags};
2412 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2413 return -14;
2414 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002415
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002416#if 0
2417 // TODO: this test needs to be re evaluated
2418
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002419 // ---------------
2420 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002421 QCBORDecode_Init(&DCtx,
2422 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2423 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002424 int n = CheckCSRMaps(&DCtx);
2425 if(n) {
2426 return n-2000;
2427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002428
Laurence Lundblade59289e52019-12-30 13:44:37 -08002429 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002430 QCBORDecode_Init(&DCtx,
2431 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2432 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002433
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002434 const uint64_t puTagList[] = {773, 1, 90599561};
2435 const QCBORTagListIn TagList = {3, puTagList};
2436 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002437
2438
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002439 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2440 return -100;
2441 }
2442 if(Item.uDataType != QCBOR_TYPE_MAP ||
2443 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2444 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2445 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2446 Item.val.uCount != 2 ||
2447 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2448 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2449 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2450 Out.uNumUsed != 3) {
2451 return -101;
2452 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002453
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002454 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2455 return -102;
2456 }
2457 if(Item.uDataType != QCBOR_TYPE_MAP ||
2458 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2459 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2460 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2461 Item.val.uCount != 2 ||
2462 puTags[0] != 5859837686836516696 ||
2463 puTags[1] != 7 ||
2464 Out.uNumUsed != 2) {
2465 return -103;
2466 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002467
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002468 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2469 return -104;
2470 }
2471 if(Item.uDataType != QCBOR_TYPE_MAP ||
2472 Item.uTagBits ||
2473 Item.val.uCount != 5 ||
2474 puTags[0] != 0x0b ||
2475 Out.uNumUsed != 1) {
2476 return -105;
2477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002478
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002479 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2480 return -106;
2481 }
2482 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2483 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2484 Item.val.string.len != 12 ||
2485 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2486 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2487 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2488 Out.uNumUsed != 3) {
2489 return -105;
2490 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002491
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002492 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2493 return -107;
2494 }
2495 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2496 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2497 Item.val.string.len != 3 ||
2498 puTags[0] != 773 ||
2499 Out.uNumUsed != 1) {
2500 return -108;
2501 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002502
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002503 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2504 return -109;
2505 }
2506 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002507 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002508 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002509 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002510 puTags[11] != 0x0f ||
2511 Out.uNumUsed != 12) {
2512 return -110;
2513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002514
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002515 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2516 return -111;
2517 }
2518 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2519 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2520 Item.val.string.len != 9 ||
2521 puTags[0] != 17 ||
2522 Out.uNumUsed != 1) {
2523 return -112;
2524 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002525
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002526 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2527 return -111;
2528 }
2529 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2530 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2531 Item.val.string.len != 2 ||
2532 puTags[0] != 17 ||
2533 Out.uNumUsed != 1) {
2534 return -112;
2535 }
2536
2537 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2538 return -113;
2539 }
2540 if(Item.uDataType != QCBOR_TYPE_MAP ||
2541 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2542 Item.val.uCount != 2 ||
2543 puTags[0] != 19 ||
2544 Out.uNumUsed != 1) {
2545 return -114;
2546 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002547
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002548 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2549 return -115;
2550 }
2551 if(Item.uDataType != QCBOR_TYPE_MAP ||
2552 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2553 Item.uTagBits ||
2554 Item.val.uCount != 1 ||
2555 puTags[0] != 9 ||
2556 Out.uNumUsed != 1) {
2557 return -116;
2558 }
2559
2560 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2561 return -116;
2562 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002563 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002564 Item.val.int64 != -7 ||
2565 Item.uTagBits ||
2566 Out.uNumUsed != 0) {
2567 return -117;
2568 }
2569
2570 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2571 return -118;
2572 }
2573 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2574 Item.val.string.len != 10 ||
2575 Item.uTagBits ||
2576 puTags[0] != 12 ||
2577 Out.uNumUsed != 1) {
2578 return -119;
2579 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002580
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002581 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2582 return -120;
2583 }
2584 if(Item.uDataType != QCBOR_TYPE_MAP ||
2585 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2586 Item.val.uCount != 1 ||
2587 puTags[0] != 0x17 ||
2588 Out.uNumUsed != 1) {
2589 return -121;
2590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002591
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002592 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2593 return -122;
2594 }
2595 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2596 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2597 Item.val.int64 != -3 ||
2598 puTags[0] != 8 ||
2599 Out.uNumUsed != 1) {
2600 return -123;
2601 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002602
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002603 if(QCBORDecode_Finish(&DCtx)) {
2604 return -124;
2605 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002606#else
2607 (void)spCSRWithTags;
2608#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002609 return 0;
2610}
2611
2612
2613
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002614
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002615static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002616 0x83,
2617 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2618 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2619 0xA4,
2620 0x63, 0x42, 0x4E, 0x2B,
2621 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2622 0x18, 0x40,
2623 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2624 0x63, 0x42, 0x4E, 0x2D,
2625 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2626 0x38, 0x3F,
2627 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2628
2629
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002630static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002631
2632
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002633int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002634{
2635 QCBORDecodeContext DCtx;
2636 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002637 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002638
Laurence Lundbladeee851742020-01-08 08:37:05 -08002639 QCBORDecode_Init(&DCtx,
2640 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2641 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002642
2643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002644 //
2645 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2646 return -1;
2647 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002648 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002649 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002650
2651 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002652 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002653 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002654 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002655 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002656 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002657 }
2658
2659 //
2660 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002661 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002662 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002663 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002664 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002665 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002666
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002667 //
2668 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002669 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002670 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002671 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002672 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002674 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002675 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002676 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2677 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002678 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002679 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002680 }
2681
2682 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002683 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002684 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2685 Item.uLabelType != QCBOR_TYPE_INT64 ||
2686 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002687 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002688 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002689 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002691 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002692 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002693 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2694 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002695 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002696 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002698
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002699 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002700 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002701 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2702 Item.uLabelType != QCBOR_TYPE_INT64 ||
2703 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002704 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002705 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002706 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002707
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002708 return 0;
2709}
2710
2711
2712
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002713static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002714 uint8_t uDataType,
2715 uint8_t uNestingLevel,
2716 uint8_t uNextNest,
2717 int64_t nLabel,
2718 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002719{
2720 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002721 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002722
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002723 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2724 if(Item.uDataType != uDataType) return -1;
2725 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002726 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2727 Item.uLabelType != QCBOR_TYPE_UINT64) {
2728 return -1;
2729 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002730 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2731 if(Item.label.int64 != nLabel) return -1;
2732 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002733 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002734 }
2735 }
2736 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302737 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002738
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002739 if(pItem) {
2740 *pItem = Item;
2741 }
2742 return 0;
2743}
2744
2745
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002746// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002747static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002748{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302749 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002750
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302751 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002752
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302753 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002754
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302755 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2756 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2757 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2758 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2759 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002760
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302761 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2762 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002763
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302764 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2765 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002766
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302767 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2768 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002769
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002770 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002771
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002772 return 0;
2773}
2774
2775
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002776/*
2777// cbor.me decoded output
2778{
2779 -23: {
2780 -20: {
2781 -18: "Organization",
2782 -17: "SSG",
2783 -15: "Confusion",
2784 -16: "San Diego",
2785 -14: "US"
2786 },
2787 -19: {
2788 -11: {
2789 -9: -7
2790 },
2791 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2792 }
2793 },
2794 -22: {
2795 -5: -3
2796 }
2797}
2798 */
2799
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002800
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002801static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002802 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2803 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2804 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2805 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2806 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2807 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2808 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2809 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2810 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2811
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002812int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002813{
2814 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002815
Laurence Lundbladeee851742020-01-08 08:37:05 -08002816 QCBORDecode_Init(&DCtx,
2817 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2818 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002819
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002820 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002821}
2822
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002823
2824
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002825int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002826{
2827 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002828
Laurence Lundbladeee851742020-01-08 08:37:05 -08002829 QCBORDecode_Init(&DCtx,
2830 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2831 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002832
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002833 QCBORItem Item;
2834 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002835
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002836 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2837 return -1;
2838 }
2839 if(Item.uDataType != QCBOR_TYPE_MAP) {
2840 return -2;
2841 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002842
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002843 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2844 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2845 return -3;
2846 }
2847
2848 return 0;
2849}
2850
2851
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002852// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002853static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002854 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2855 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2856 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2857 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2858 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2859 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002860 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2861 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2862 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2863 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002864
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002865int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002866{
2867 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002868
Laurence Lundbladeee851742020-01-08 08:37:05 -08002869 QCBORDecode_Init(&DCtx,
2870 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2871 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002872
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002873 return CheckCSRMaps(&DCtx);
2874}
2875
2876
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002877
Laurence Lundblade17ede402018-10-13 11:43:07 +08002878static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2879{
2880 UsefulOutBuf UOB;
2881 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002882
Laurence Lundblade17ede402018-10-13 11:43:07 +08002883 int i;
2884 for(i = 0; i < n; i++) {
2885 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2886 }
2887
2888 for(i = 0; i < n; i++) {
2889 UsefulOutBuf_AppendByte(&UOB, 0xff);
2890 }
2891 return UsefulOutBuf_OutUBuf(&UOB);
2892}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002893
2894
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002895static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002896{
2897 QCBORDecodeContext DC;
2898 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002899
Laurence Lundblade17ede402018-10-13 11:43:07 +08002900 int j;
2901 for(j = 0; j < nNestLevel; j++) {
2902 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002903 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002904 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2905 // Should be in error
2906 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2907 return -4;
2908 } else {
2909 return 0; // Decoding doesn't recover after an error
2910 }
2911 } else {
2912 // Should be no error
2913 if(nReturn) {
2914 return -9; // Should not have got an error
2915 }
2916 }
2917 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2918 return -7;
2919 }
2920 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002921 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002922 if(nReturn) {
2923 return -3;
2924 }
2925 return 0;
2926}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002927
2928
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002929int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002930{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302931 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002932 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002933 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002934 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002935 int nReturn = parse_indeflen_nested(Nested, i);
2936 if(nReturn) {
2937 return nReturn;
2938 }
2939 }
2940 return 0;
2941}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002942
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002943
Laurence Lundbladeee851742020-01-08 08:37:05 -08002944// [1, [2, 3]]
2945static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2946// No closing break
2947static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2948// Not enough closing breaks
2949static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2950// Too many closing breaks
2951static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2952// Unclosed indeflen inside def len
2953static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2954// confused tag
2955static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002956
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002957int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002958{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002959 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002960 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002961 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002962
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002963 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302964 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002965 QCBORDecodeContext DC;
2966 QCBORItem Item;
2967 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002968
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002969 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002970
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002971 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302972
2973 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2974 Item.uNestingLevel != 0 ||
2975 Item.uNextNestLevel != 1) {
2976 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002977 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002978
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002979 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302980 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2981 Item.uNestingLevel != 1 ||
2982 Item.uNextNestLevel != 1) {
2983 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002985
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002986 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302987 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2988 Item.uNestingLevel != 1 ||
2989 Item.uNextNestLevel != 2) {
2990 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002991 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002992
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002993 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002994 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302995 Item.uNestingLevel != 2 ||
2996 Item.uNextNestLevel != 2) {
2997 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002999
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003000 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003001 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303002 Item.uNestingLevel != 2 ||
3003 Item.uNextNestLevel != 0) {
3004 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003007 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303008 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003009 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003010
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003011 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003012 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003013
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003014 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003015
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003016 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003017
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003018 nResult = QCBORDecode_GetNext(&DC, &Item);
3019 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303020 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003022
Laurence Lundblade570fab52018-10-13 18:28:27 +08003023 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303024 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3025 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003026 }
3027
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003028
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003029 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003030 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003031
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003032 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003033
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003034 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003035
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003036 nResult = QCBORDecode_GetNext(&DC, &Item);
3037 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303038 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003039 }
3040
3041 nResult = QCBORDecode_GetNext(&DC, &Item);
3042 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303043 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003044 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003045
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003046 nResult = QCBORDecode_GetNext(&DC, &Item);
3047 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303048 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003049 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003050
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003051 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303052 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3053 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003055
3056
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003057 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003058 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003059
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003060 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003061
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003062 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003063
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003064 nResult = QCBORDecode_GetNext(&DC, &Item);
3065 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303066 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003067 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003068
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003069 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003070 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303071 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003072 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303073
Laurence Lundblade642282a2020-06-23 12:00:33 -07003074 nResult = QCBORDecode_GetNext(&DC, &Item);
3075 if(nResult != QCBOR_ERR_BAD_BREAK) {
3076 return -140;
3077 }
3078
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003079
Laurence Lundblade570fab52018-10-13 18:28:27 +08003080 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003081 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003082
Laurence Lundblade570fab52018-10-13 18:28:27 +08003083 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003084
Laurence Lundblade570fab52018-10-13 18:28:27 +08003085 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003086
Laurence Lundblade570fab52018-10-13 18:28:27 +08003087 nResult = QCBORDecode_GetNext(&DC, &Item);
3088 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303089 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003091
Laurence Lundblade570fab52018-10-13 18:28:27 +08003092 nResult = QCBORDecode_GetNext(&DC, &Item);
3093 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303094 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003096
Laurence Lundblade570fab52018-10-13 18:28:27 +08003097 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303098 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3099 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003101
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303102 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003103 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003104
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303105 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003106
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303107 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003108
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303109 nResult = QCBORDecode_GetNext(&DC, &Item);
3110 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303111 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003113
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303114 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303115 if(nResult != QCBOR_ERR_BAD_BREAK) {
3116 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303117 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003118
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003119 return 0;
3120}
3121
Laurence Lundblade17ede402018-10-13 11:43:07 +08003122
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003123static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003124 0x81, // Array of length one
3125 0x7f, // text string marked with indefinite length
3126 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3127 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3128 0xff // ending break
3129};
3130
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003131static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303132 0x81, // Array of length one
3133 0x7f, // text string marked with indefinite length
3134 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3135 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3136 0xff // ending break
3137};
3138
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003139static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303140 0x81, // Array of length one
3141 0x7f, // text string marked with indefinite length
3142 0x01, 0x02, // Not a string
3143 0xff // ending break
3144};
3145
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003146static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303147 0x81, // Array of length one
3148 0x7f, // text string marked with indefinite length
3149 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3150 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3151 // missing end of string
3152};
3153
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003154static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303155 0xa1, // Array of length one
3156 0x7f, // text string marked with indefinite length
3157 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3158 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3159 0xff, // ending break
3160 0x01 // integer being labeled.
3161};
3162
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003163/**
3164 Make an indefinite length string
3165
3166 @param Storage Storage for string, must be 144 bytes in size
3167 @return The indefinite length string
3168
3169 This makes an array with one indefinite length string that has 7 chunks
3170 from size of 1 byte up to 64 bytes.
3171 */
3172static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303173{
3174 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003175
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303176 UsefulOutBuf_Init(&UOB, Storage);
3177 UsefulOutBuf_AppendByte(&UOB, 0x81);
3178 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003179
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003180 uint8_t uStringByte = 0;
3181 // Use of type int is intentional
3182 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3183 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303184 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003185 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3186 for(int j = 0; j < uChunkSize; j++) {
3187 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3188 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303189 }
3190 }
3191 UsefulOutBuf_AppendByte(&UOB, 0xff);
3192
3193 return UsefulOutBuf_OutUBuf(&UOB);
3194}
3195
3196static int CheckBigString(UsefulBufC BigString)
3197{
3198 if(BigString.len != 255) {
3199 return 1;
3200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003201
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303202 for(uint8_t i = 0; i < 255; i++){
3203 if(((const uint8_t *)BigString.ptr)[i] != i) {
3204 return 1;
3205 }
3206 }
3207 return 0;
3208}
3209
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303210
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003211int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303212{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303213 QCBORDecodeContext DC;
3214 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303215 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003216 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003217
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303218 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003219 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303220 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003221
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303222 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303223 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003225
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303226 if(QCBORDecode_GetNext(&DC, &Item)) {
3227 return -2;
3228 }
3229 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3230 return -3;
3231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003232
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303233 if(QCBORDecode_GetNext(&DC, &Item)) {
3234 return -4;
3235 }
3236 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3237 return -5;
3238 }
3239 if(QCBORDecode_Finish(&DC)) {
3240 return -6;
3241 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303242
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303243 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003244 QCBORDecode_Init(&DC,
3245 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3246 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003247
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303248 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3249 return -7;
3250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003251
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303252 if(QCBORDecode_GetNext(&DC, &Item)) {
3253 return -8;
3254 }
3255 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3256 return -9;
3257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003258
Laurence Lundblade30816f22018-11-10 13:40:22 +07003259 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303260 return -10;
3261 }
3262
3263 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003264 QCBORDecode_Init(&DC,
3265 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3266 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003267
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303268 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3269 return -11;
3270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003271
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303272 if(QCBORDecode_GetNext(&DC, &Item)) {
3273 return -12;
3274 }
3275 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3276 return -13;
3277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003278
Laurence Lundblade30816f22018-11-10 13:40:22 +07003279 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303280 return -14;
3281 }
3282
3283 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003284 QCBORDecode_Init(&DC,
3285 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3286 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003287
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303288 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3289 return -15;
3290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003291
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303292 if(QCBORDecode_GetNext(&DC, &Item)) {
3293 return -16;
3294 }
3295 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3296 return -17;
3297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003298
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303299 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3300 return -18;
3301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303303 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303304 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003305
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303306 QCBORDecode_GetNext(&DC, &Item);
3307 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303308 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003310
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303311 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303312 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003314
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303315 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003316 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303317
3318 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3319 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303320 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003322
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303323 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303324 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003325 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003326
Laurence Lundbladeee851742020-01-08 08:37:05 -08003327 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3328 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003329
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303330 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303331 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303332 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303333 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003334
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303335 QCBORDecode_GetNext(&DC, &Item);
3336 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303337 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303338 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003339 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303340 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003342
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303343 // ---- big bstr -----
3344 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003345
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303346 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3347 return -25;
3348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003349
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303350 if(QCBORDecode_GetNext(&DC, &Item)) {
3351 return -26;
3352 }
3353 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303354 return -26;
3355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003356
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303357 if(QCBORDecode_GetNext(&DC, &Item)) {
3358 return -27;
3359 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303360 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303361 return -28;
3362 }
3363 if(CheckBigString(Item.val.string)) {
3364 return -3;
3365 }
3366 if(QCBORDecode_Finish(&DC)) {
3367 return -29;
3368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003369
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303370 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003371 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003372
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303373 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3374 return -30;
3375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003376
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303377 QCBORDecode_GetNext(&DC, &Item);
3378 if(Item.uDataType != QCBOR_TYPE_MAP) {
3379 return -31;
3380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003381
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303382 if(QCBORDecode_GetNext(&DC, &Item)){
3383 return -32;
3384 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003385 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3386 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303387 Item.uDataAlloc || !Item.uLabelAlloc ||
3388 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3389 return -33;
3390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003391
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303392 if(QCBORDecode_Finish(&DC)) {
3393 return -34;
3394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003395
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003396 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003397}
3398
3399
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003400int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303401{
3402 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003403 QCBORError nCBORError;
3404
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003405
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303406 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003407 QCBORDecode_Init(&DC,
3408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3409 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003410
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003411 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003412
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003413 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3414 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303415 return -1;
3416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003417
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003418 if(CheckCSRMaps(&DC)) {
3419 return -2;
3420 }
3421
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303422 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003423 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003424 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003425
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303426 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003427 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303428 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003429
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303430 QCBORItem Item1, Item2, Item3, Item4;
3431 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003432 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303433 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3434 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003435 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303436 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003437 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303438 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003439 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303440 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003441 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303442 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003443 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003444
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303445 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003446
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303447 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303448 Item1.uDataType != QCBOR_TYPE_INT64 ||
3449 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003450 Item1.uDataAlloc != 0 ||
3451 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003452 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003453 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003454 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003455
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303456
3457 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003458 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303459 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003460 Item2.uDataAlloc != 0 ||
3461 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303462 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003463 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003464
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303465 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003466 Item3.uDataAlloc == 0 ||
3467 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003468 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003469 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003470 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003471
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303472 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003473 Item4.uDataAlloc == 0 ||
3474 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003475 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003476 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003478
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303479 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003480 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003481 QCBORDecode_Init(&DC,
3482 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3483 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303484 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3485 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003486 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303487 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003488 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003489 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003490 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303491 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3492 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3493 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3494 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3495 }
3496 }
3497 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003498 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003499 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303500 }
3501
3502 return 0;
3503}
3504
Laurence Lundbladef6531662018-12-04 10:42:22 +09003505
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303506
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003507int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003508{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003509 // Set up the decoder with a tiny bit of CBOR to parse because
3510 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003511 QCBORDecodeContext DC;
3512 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3513 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003514
Laurence Lundbladef6531662018-12-04 10:42:22 +09003515 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003516 // Then fish into the internals of the decode context
3517 // to get the allocator function so it can be called directly.
3518 // Also figure out how much pool is available for use
3519 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003520 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003521 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3522 if(nError) {
3523 return -9;
3524 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003525 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3526 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3527 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003528
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003529 // First test -- ask for one more byte than available and see failure
3530 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003531 if(!UsefulBuf_IsNULL(Allocated)) {
3532 return -1;
3533 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003534
Laurence Lundbladef6531662018-12-04 10:42:22 +09003535 // Re do the set up for the next test that will do a successful alloc,
3536 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003537 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003538 pAlloc = DC.StringAllocator.pfAllocator;
3539 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3540 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003541
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003542 // Allocate one byte less than available and see success
3543 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003544 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3545 return -2;
3546 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003547 // Ask for some more and see failure
3548 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003549 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3550 return -3;
3551 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003552 // Free the first allocate, retry the second and see success
3553 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3554 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003555 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3556 return -4;
3557 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003558
Laurence Lundbladef6531662018-12-04 10:42:22 +09003559 // Re do set up for next test that involves a successful alloc,
3560 // and a successful realloc and a failed realloc
3561 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003562 pAlloc = DC.StringAllocator.pfAllocator;
3563 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003564
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003565 // Allocate half the pool and see success
3566 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003567 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3568 return -5;
3569 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003570 // Reallocate to take up the whole pool and see success
3571 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003572 if(UsefulBuf_IsNULL(Allocated2)) {
3573 return -6;
3574 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003575 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003576 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3577 return -7;
3578 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003579 // Try to allocate more to be sure there is failure after a realloc
3580 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3581 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003582 return -8;
3583 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003584
Laurence Lundbladef6531662018-12-04 10:42:22 +09003585 return 0;
3586}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003587
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003588
3589/* Just enough of an allocator to test configuration of one */
3590static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3591{
3592 (void)pOldMem; // unused variable
3593
3594 if(uNewSize) {
3595 // Assumes the context pointer is the buffer and
3596 // nothing too big will ever be asked for.
3597 // This is only good for this basic test!
3598 return (UsefulBuf) {pCtx, uNewSize};
3599 } else {
3600 return NULLUsefulBuf;
3601 }
3602}
3603
3604
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003605int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003606{
3607 // Set up the decoder with a tiny bit of CBOR to parse because
3608 // nothing can be done with it unless that is set up.
3609 QCBORDecodeContext DC;
3610 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3611 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3612
3613 uint8_t pAllocatorBuffer[50];
3614
3615 // This is really just to test that this call works.
3616 // The full functionality of string allocators is tested
3617 // elsewhere with the MemPool internal allocator.
3618 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3619
3620 QCBORItem Item;
3621 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3622 return -1;
3623 }
3624
3625 if(Item.uDataAlloc == 0 ||
3626 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3627 Item.val.string.ptr != pAllocatorBuffer) {
3628 return -2;
3629 }
3630
3631 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3632 return -3;
3633 }
3634
3635 return 0;
3636}
3637
Laurence Lundblade59289e52019-12-30 13:44:37 -08003638
Laurence Lundbladea826c502020-05-10 21:07:00 -07003639/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08003640 [
3641 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003642 4([-20, 4759477275222530853136]),
3643 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003644 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003645 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003646 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07003647 5([ 9223372036854775806, -4759477275222530853137])
3648 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003649 ]
3650 */
3651
3652static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003653 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003654 0xC4, 0x82, 0x20,
3655 0x03,
3656 0xC4, 0x82, 0x33,
3657 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3658 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3659 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3660 0xC5, 0x82, 0x19, 0x01, 0x2C,
3661 0x18, 0x64,
3662 0xC5, 0x82, 0x33,
3663 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3664 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3665 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07003666 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3667 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003668 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3669 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3670};
3671
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07003672#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3673
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003674int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003675{
3676 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003677 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003678 QCBORItem item;
3679
Laurence Lundblade17af4902020-01-07 19:11:55 -08003680 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3681 0x06, 0x07, 0x08, 0x09, 0x010};
3682 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003683
3684
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003685 QCBORDecode_Init(&DC,
3686 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
3687 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003688
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003689 uErr = QCBORDecode_GetNext(&DC, &item);
3690 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003691 return 1;
3692 }
3693
3694 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3695 return 2;
3696 }
3697
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003698 uErr = QCBORDecode_GetNext(&DC, &item);
3699 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003700 return 3;
3701 }
3702
3703 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3704 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3705 item.val.expAndMantissa.nExponent != -1) {
3706 return 4;
3707 }
3708
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003709 uErr = QCBORDecode_GetNext(&DC, &item);
3710 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003711 return 5;
3712 }
3713
3714 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3715 item.val.expAndMantissa.nExponent != -20 ||
3716 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3717 return 6;
3718 }
3719
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003720 uErr = QCBORDecode_GetNext(&DC, &item);
3721 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003722 return 7;
3723 }
3724
3725 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3726 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3727 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3728 return 8;
3729 }
3730
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003731 uErr = QCBORDecode_GetNext(&DC, &item);
3732 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003733 return 9;
3734 }
3735
3736 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3737 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3738 item.val.expAndMantissa.nExponent != 300) {
3739 return 10;
3740 }
3741
Laurence Lundbladea826c502020-05-10 21:07:00 -07003742 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003743 uErr = QCBORDecode_GetNext(&DC, &item);
3744 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003745 return 11;
3746 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003747 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3748 item.val.expAndMantissa.nExponent != -20 ||
3749 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3750 return 12;
3751 }
3752
Laurence Lundbladea826c502020-05-10 21:07:00 -07003753 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003754 uErr = QCBORDecode_GetNext(&DC, &item);
3755 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003756 return 13;
3757 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003758 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3759 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3760 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3761 return 14;
3762 }
3763
Laurence Lundbladea826c502020-05-10 21:07:00 -07003764 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003765 uErr = QCBORDecode_GetNext(&DC, &item);
3766 if(uErr != QCBOR_SUCCESS) {
3767 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07003768 }
3769 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3770 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3771 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003772 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07003773 }
3774
Laurence Lundbladea826c502020-05-10 21:07:00 -07003775 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003776 uErr = QCBORDecode_GetNext(&DC, &item);
3777 if(uErr != QCBOR_SUCCESS) {
3778 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003779 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003780 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3781 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3782 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003783 return 18;
3784 }
3785
3786 uErr = QCBORDecode_Finish(&DC);
3787 if(uErr != QCBOR_SUCCESS) {
3788 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003789 }
3790
3791 /* Now encode some stuff and then decode it */
3792 uint8_t pBuf[40];
3793 QCBOREncodeContext EC;
3794 UsefulBufC Encoded;
3795
3796 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3797 QCBOREncode_OpenArray(&EC);
3798 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3799 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3800 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3801 QCBOREncode_CloseArray(&EC);
3802 QCBOREncode_Finish(&EC, &Encoded);
3803
3804
3805 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003806 uErr = QCBORDecode_GetNext(&DC, &item);
3807 if(uErr != QCBOR_SUCCESS) {
3808 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003809 }
3810
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003811 uErr = QCBORDecode_GetNext(&DC, &item);
3812 if(uErr != QCBOR_SUCCESS) {
3813 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003814 }
3815
3816 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3817 item.val.expAndMantissa.nExponent != 1000 ||
3818 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003819 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003820 }
3821
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003822 uErr = QCBORDecode_GetNext(&DC, &item);
3823 if(uErr != QCBOR_SUCCESS) {
3824 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003825 }
3826
3827 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3828 item.val.expAndMantissa.nExponent != INT32_MIN ||
3829 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003830 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003831 }
3832
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003833 uErr = QCBORDecode_GetNext(&DC, &item);
3834 if(uErr != QCBOR_SUCCESS) {
3835 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003836 }
3837
3838 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3839 item.val.expAndMantissa.nExponent != INT32_MAX ||
3840 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003841 return 106;
3842 }
3843
3844
3845 int64_t nExp, nMant;
3846 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
3847 UsefulBufC Mant;
3848 bool bIsNeg;
3849
3850 QCBORDecode_Init(&DC,
3851 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
3852 QCBOR_DECODE_MODE_NORMAL);
3853 QCBORDecode_EnterArray(&DC);
3854
3855 // 4([-1, 3]),
3856 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nExp, &nMant);
3857
3858 // 4([-20, 4759477275222530853136]),
3859 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
3860
3861 // 4([9223372036854775807, -4759477275222530853137]),
3862 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
3863
3864 // 5([300, 100]),
3865 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nExp, &nMant);
3866
3867 // 5([-20, 4759477275222530853136]),
3868 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
3869
3870 // 5([-9223372036854775807, -4759477275222530853137])
3871 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
3872
3873 // 5([ 9223372036854775806, -4759477275222530853137])
3874 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
3875
3876 // 5([ 9223372036854775806, 9223372036854775806])]
3877 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
3878
3879 QCBORDecode_ExitArray(&DC);
3880
3881 uErr = QCBORDecode_Finish(&DC);
3882 if(uErr != QCBOR_SUCCESS) {
3883 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003884 }
3885
3886 return 0;
3887}
3888
3889
3890static struct FailInput ExponentAndMantissaFailures[] = {
3891 // Exponent > INT64_MAX
3892 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3893 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3894 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3895 // Mantissa > INT64_MAX
3896 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3897 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3898 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3899 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003900 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08003901 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003902 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08003903 // bad content for big num
3904 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3905 // bad content for big num
3906 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3907 // Bad integer for exponent
3908 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3909 // Bad integer for mantissa
3910 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3911 // 3 items in array
3912 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3913 // unterminated indefinite length array
3914 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3915 // Empty array
3916 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3917 // Second is not an integer
3918 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3919 // First is not an integer
3920 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3921 // Not an array
3922 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3923};
3924
3925
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003926int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003927{
3928 return ProcessFailures(ExponentAndMantissaFailures,
3929 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3930}
3931
3932#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003933
3934
3935
3936/*
3937 Some basic CBOR with map and array used in a lot of tests.
3938 The map labels are all strings
3939
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003940 {
3941 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003942 "an array of two strings": [
3943 "string1", "string2"
3944 ],
3945 "map in a map": {
3946 "bytes 1": h'78787878',
3947 "bytes 2": h'79797979',
3948 "another int": 98,
3949 "text 2": "lies, damn lies and statistics"
3950 }
3951 }
3952 */
3953
Laurence Lundblade1341c592020-04-11 14:19:05 -07003954#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003955
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003956static char strbuf[10];
3957const char *PrintType(uint8_t type) {
3958 switch(type) {
3959 case QCBOR_TYPE_INT64: return "INT64";
3960 case QCBOR_TYPE_UINT64: return "UINT64";
3961 case QCBOR_TYPE_ARRAY: return "ARRAY";
3962 case QCBOR_TYPE_MAP: return "MAP";
3963 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
3964 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
3965 default:
3966 sprintf(strbuf, "%d", type);
3967 return strbuf;
3968 }
3969}
3970
3971
3972void PrintItem(QCBORItem Item)
3973{
3974 printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":"");
3975 if(Item.uLabelType) {
3976 printf("Label: %s ", PrintType(Item.uLabelType));
3977 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3978 printf("%lld\n", Item.label.int64);
3979 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07003980 // TODO: proper conversion to null-terminated string
3981 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003982 }
3983 }
3984}
3985
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003986
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07003987int32_t EMap(UsefulBufC input)
3988{
3989 QCBORItem Item1, Item2, Item3;
3990 int64_t nDecodedInt1, nDecodedInt2;
3991 UsefulBufC B1, B2, S1, S2, S3;
3992
3993 QCBORDecodeContext DCtx;
3994 QCBORError nCBORError;
3995
3996 QCBORDecode_Init(&DCtx, input, 0);
3997
3998 QCBORDecode_EnterMap(&DCtx);
3999
4000 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4001
4002 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4003 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4004 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1);
4005 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2);
4006 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
4007 QCBORDecode_ExitMap(&DCtx);
4008
4009 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4010 QCBORDecode_GetNext(&DCtx, &Item1);
4011 QCBORDecode_GetNext(&DCtx, &Item2);
4012 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4013 return -400;
4014 }
4015 QCBORDecode_ExitArray(&DCtx);
4016
4017 // Parse the same array again using GetText() instead of GetItem()
4018 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4019 QCBORDecode_GetText(&DCtx, &S2);
4020 QCBORDecode_GetText(&DCtx, &S3);
4021 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4022 return 5000;
4023 }
4024 /* QCBORDecode_GetText(&DCtx, &S3);
4025 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4026 return 5001;
4027 } */
4028
4029 QCBORDecode_ExitArray(&DCtx);
4030
4031 QCBORDecode_ExitMap(&DCtx);
4032
4033 nCBORError = QCBORDecode_Finish(&DCtx);
4034
4035 if(nCBORError) {
4036 return (int32_t)nCBORError;
4037 }
4038
4039 if(nDecodedInt1 != 42) {
4040 return 1001;
4041 }
4042
4043 if(nDecodedInt2 != 98) {
4044 return 1002;
4045 }
4046
4047 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4048 UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){
4049 return 1003;
4050 }
4051
4052 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4053 UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){
4054 return 1004;
4055 }
4056
4057 if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){
4058 return 1005;
4059 }
4060
4061 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4062 return 1006;
4063 }
4064
4065 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4066 return 1007;
4067 }
4068
4069 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4070 return 1008;
4071 }
4072
4073 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4074 return 1009;
4075 }
4076
4077 return 0;
4078}
4079
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004080
4081/*
4082 [23,
4083 6000,
4084 h'67616C6163746963',
4085 h'686176656E20746F6B656E'
4086 ]
4087 */
4088static const uint8_t spSimpleArray[] = {
40890x84, 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};
4090
4091
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004092static const uint8_t spEmptyMap[] = {0xa0};
4093
4094static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004095
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004096static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4097
Laurence Lundbladef0499502020-08-01 11:55:57 -07004098/*
4099 {
4100 0: [],
4101 9: [
4102 [],
4103 []
4104 ],
4105 8: {
4106 1: [],
4107 2: {},
4108 3: []
4109 },
4110 4: {},
4111 5: [],
4112 6: [
4113 [],
4114 []
4115 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004116 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004117 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004118
4119
Laurence Lundbladef0499502020-08-01 11:55:57 -07004120static const uint8_t spMapOfEmpty[] = {
4121 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4122 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4123 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004124
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004125int32_t EnterMapTest()
4126{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004127 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004128 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004129 int32_t nReturn;
4130 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004131
4132
4133 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4134 QCBORDecode_EnterMap(&DCtx);
4135
Laurence Lundbladef0499502020-08-01 11:55:57 -07004136
4137 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004138 QCBORDecode_ExitArray(&DCtx);
4139
Laurence Lundbladef0499502020-08-01 11:55:57 -07004140 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004141 QCBORDecode_EnterArray(&DCtx);
4142 QCBORDecode_ExitArray(&DCtx);
4143 QCBORDecode_EnterArray(&DCtx);
4144 QCBORDecode_ExitArray(&DCtx);
4145 QCBORDecode_ExitArray(&DCtx);
4146
Laurence Lundbladef0499502020-08-01 11:55:57 -07004147 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004148 QCBORDecode_EnterArray(&DCtx);
4149 QCBORDecode_ExitArray(&DCtx);
4150 QCBORDecode_EnterMap(&DCtx);
4151 QCBORDecode_ExitMap(&DCtx);
4152 QCBORDecode_EnterArray(&DCtx);
4153 QCBORDecode_ExitArray(&DCtx);
4154 QCBORDecode_ExitMap(&DCtx);
4155
Laurence Lundbladef0499502020-08-01 11:55:57 -07004156 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004157 QCBORDecode_ExitMap(&DCtx);
4158
Laurence Lundbladef0499502020-08-01 11:55:57 -07004159 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004160 QCBORDecode_ExitArray(&DCtx);
4161
Laurence Lundbladef0499502020-08-01 11:55:57 -07004162 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004163 QCBORDecode_EnterArray(&DCtx);
4164 QCBORDecode_ExitArray(&DCtx);
4165 QCBORDecode_EnterArray(&DCtx);
4166 QCBORDecode_ExitArray(&DCtx);
4167 QCBORDecode_ExitArray(&DCtx);
4168
4169 QCBORDecode_ExitMap(&DCtx);
4170
4171 uErr = QCBORDecode_Finish(&DCtx);
4172 if(uErr != QCBOR_SUCCESS){
4173 return 3011;
4174 }
4175
4176
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004177 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004178 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004179 if(nReturn) {
4180 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004181 }
4182
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004183 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004184 if(nReturn) {
4185 return nReturn;
4186 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004187
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004188
Laurence Lundblade937ea812020-05-08 11:38:23 -07004189
4190 // These tests confirm the cursor is at the right place after entering a map or array
4191
4192 // Confirm cursor is at right place
4193 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4194 QCBORDecode_EnterMap(&DCtx);
4195 QCBORDecode_GetNext(&DCtx, &Item1);
4196 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4197 return 2001;
4198 }
4199
4200
4201 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4202 QCBORDecode_GetNext(&DCtx, &Item1);
4203 QCBORDecode_GetNext(&DCtx, &Item1);
4204 QCBORDecode_EnterArray(&DCtx);
4205 QCBORDecode_GetNext(&DCtx, &Item1);
4206 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4207 return 2002;
4208 }
4209
4210 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4211 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004212 QCBORDecode_GetNext(&DCtx, &Item1);
4213 QCBORDecode_GetNext(&DCtx, &Item1);
4214 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004215 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4216 QCBORDecode_GetNext(&DCtx, &Item1);
4217 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4218 return 2003;
4219 }
4220
4221 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4222 QCBORDecode_EnterMap(&DCtx);
4223 QCBORDecode_GetNext(&DCtx, &Item1);
4224 QCBORDecode_GetNext(&DCtx, &Item1);
4225 QCBORDecode_GetNext(&DCtx, &Item1);
4226 QCBORDecode_GetNext(&DCtx, &Item1);
4227 QCBORDecode_GetNext(&DCtx, &Item1);
4228 QCBORDecode_GetNext(&DCtx, &Item1);
4229 QCBORDecode_GetNext(&DCtx, &Item1);
4230 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4231 QCBORDecode_GetNext(&DCtx, &Item1);
4232 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004233 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004234 }
4235
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004236 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4237 QCBORDecode_EnterMap(&DCtx);
4238 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4239 QCBORDecode_ExitArray(&DCtx);
4240 QCBORDecode_GetNext(&DCtx, &Item1);
4241 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4242 return 2006;
4243 }
4244 QCBORDecode_ExitMap(&DCtx);
4245 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4246 return 2007;
4247 }
4248
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004249 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4250 QCBORDecode_EnterArray(&DCtx);
4251 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004252 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4253 uErr = QCBORDecode_GetAndResetError(&DCtx);
4254 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004255 return 2008;
4256 }
4257 UsefulBufC String;
4258 QCBORDecode_GetTextInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004259 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004260 return 2009;
4261 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004262
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004263
4264 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4265 QCBORDecode_EnterMap(&DCtx);
4266 // This will fail because the map is empty.
4267 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4268 uErr = QCBORDecode_GetAndResetError(&DCtx);
4269 if(uErr != QCBOR_ERR_NOT_FOUND){
4270 return 2010;
4271 }
4272 QCBORDecode_ExitMap(&DCtx);
4273 uErr = QCBORDecode_Finish(&DCtx);
4274 if(uErr != QCBOR_SUCCESS){
4275 return 2011;
4276 }
4277
4278
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004279 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4280 QCBORDecode_EnterMap(&DCtx);
4281 // This will fail because the map is empty.
4282 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4283 uErr = QCBORDecode_GetAndResetError(&DCtx);
4284 if(uErr != QCBOR_ERR_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004285 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004286 }
4287 QCBORDecode_ExitMap(&DCtx);
4288 uErr = QCBORDecode_Finish(&DCtx);
4289 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004290 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004291 }
4292
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004293
4294 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
4295 QCBORDecode_EnterArray(&DCtx);
4296 QCBORDecode_GetBytes(&DCtx, &String);
4297 QCBORDecode_EnterMap(&DCtx);
4298 QCBORDecode_ExitMap(&DCtx);
4299 QCBORDecode_EnterArray(&DCtx);
4300 QCBORDecode_ExitArray(&DCtx);
4301 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
4302 QCBORDecode_ExitArray(&DCtx);
4303 uErr = QCBORDecode_Finish(&DCtx);
4304 if(uErr != QCBOR_SUCCESS){
4305 return 2014;
4306 }
4307
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07004308 // TODO: more testing of entered mapps and arrays with problems
4309 // TODO: document error handling better (maybe improve error handling)
4310
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004311 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004312}
4313
4314
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004315struct NumberConversion {
4316 char *szDescription;
4317 UsefulBufC CBOR;
4318 int64_t nConvertedToInt64;
4319 QCBORError uErrorInt64;
4320 uint64_t uConvertToUInt64;
4321 QCBORError uErrorUint64;
4322 double dConvertToDouble;
4323 QCBORError uErrorDouble;
4324};
4325
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004326static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004327 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004328 "negative bignum -1",
4329 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
4330 -1,
4331 QCBOR_SUCCESS,
4332 0,
4333 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4334 -1.0,
4335 QCBOR_SUCCESS
4336 },
4337 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004338 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004339 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4340 0xC2, 0x42, 0x01, 0x01}, 15},
4341 257000,
4342 QCBOR_SUCCESS,
4343 257000,
4344 QCBOR_SUCCESS,
4345 257000.0,
4346 QCBOR_SUCCESS
4347 },
4348 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004349 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004350 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4351 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07004352 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07004353 QCBOR_SUCCESS,
4354 0,
4355 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07004356 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07004357 QCBOR_SUCCESS
4358 },
4359 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004360 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004361 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4362 0xC2, 0x42, 0x01, 0x01}, 15},
4363 2056,
4364 QCBOR_SUCCESS,
4365 2056,
4366 QCBOR_SUCCESS,
4367 2056.0,
4368 QCBOR_SUCCESS
4369 },
4370 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004371 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07004372 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4373 0,
4374 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4375 0,
4376 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4377 -18446744073709551617.0,
4378 QCBOR_SUCCESS
4379 },
4380 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004381 "Positive bignum 0x01020304 indefinite length string",
4382 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
4383 0x01020304,
4384 QCBOR_SUCCESS,
4385 0x01020304,
4386 QCBOR_SUCCESS,
4387 16909060.0,
4388 QCBOR_SUCCESS
4389 },
4390 {
Laurence Lundblade887add82020-05-17 05:50:34 -07004391 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07004392 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4393 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
4394 0,
4395 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4396 0,
4397 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4398 -INFINITY,
4399 QCBOR_SUCCESS
4400 },
4401 {
4402 "big float [9223372036854775806, 9223372036854775806]",
4403 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4404 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
4405 0,
4406 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4407 0,
4408 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4409 INFINITY,
4410 QCBOR_SUCCESS
4411 },
4412 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004413 "Big float 3 * 2^^2",
4414 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
4415 12,
4416 QCBOR_SUCCESS,
4417 12,
4418 QCBOR_SUCCESS,
4419 12.0,
4420 QCBOR_SUCCESS
4421 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004422 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004423 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004424 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4425 0,
4426 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4427 18446744073709551615ULL,
4428 QCBOR_SUCCESS,
4429 18446744073709551615.0,
4430 QCBOR_SUCCESS
4431 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004432 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004433 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004434 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4435 65536-1,
4436 QCBOR_SUCCESS,
4437 0xffff,
4438 QCBOR_SUCCESS,
4439 65535.0,
4440 QCBOR_SUCCESS
4441 },
4442 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004443 "Postive integer 0",
4444 {(uint8_t[]){0x0}, 1},
4445 0LL,
4446 QCBOR_SUCCESS,
4447 0ULL,
4448 QCBOR_SUCCESS,
4449 0.0,
4450 QCBOR_SUCCESS
4451 },
4452 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004453 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004454 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4455 -9223372036854775807-1, // INT64_MIN
4456 QCBOR_SUCCESS,
4457 0ULL,
4458 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4459 -9223372036854775808.0,
4460 QCBOR_SUCCESS
4461 },
4462 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004463 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004464 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
4465 100L,
4466 QCBOR_SUCCESS,
4467 100ULL,
4468 QCBOR_SUCCESS,
4469 100.3,
4470 QCBOR_SUCCESS
4471 },
4472 {
4473 "Floating point value NaN 0xfa7fc00000",
4474 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
4475 0,
4476 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4477 0,
4478 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4479 NAN,
4480 QCBOR_SUCCESS
4481 },
4482 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004483 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004484 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
4485 -4,
4486 QCBOR_SUCCESS,
4487 0,
4488 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4489 -4.0,
4490 QCBOR_SUCCESS
4491 },
4492 {
4493 "Decimal fraction 3/10",
4494 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
4495 0,
4496 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4497 0,
4498 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4499 0.30000000000000004,
4500 QCBOR_SUCCESS
4501 }
4502};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004503
4504
4505
Laurence Lundblade313b2862020-05-16 01:23:06 -07004506int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004507{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004508 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004509
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004510 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
4511 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004512
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004513 // Set up the decoding context including a memory pool so that
4514 // indefinite length items can be checked
4515 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004516 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004517
4518 /* ----- test conversion to int64_t ------ */
4519 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004520 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4521 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004522 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004523 }
4524
4525 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004526 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004527 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004528 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004529 }
4530 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004531 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004532 }
4533
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004534 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004535 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4536 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4537 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004538 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004539 }
4540 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004541 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004542 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004543 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004544 }
4545 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004546 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004547 }
4548
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004549 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004550 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4551 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4552 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004553 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004554 }
4555 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004556 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004557 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004558 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004559 }
4560 if(pF->uErrorDouble == QCBOR_SUCCESS) {
4561 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004562 // NaN's can't be compared for equality. A NaN is
4563 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004564 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004565 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004566 }
4567 } else {
4568 // TODO: this comparison may need a margin of error
4569 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004570 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004571 }
4572 }
4573 }
4574 }
4575
4576 return 0;
4577}
4578
Laurence Lundblade313b2862020-05-16 01:23:06 -07004579int32_t IntegerConvertTestOld()
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004580{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004581 QCBORDecodeContext DCtx;
4582 QCBORError nCBORError;
4583
Laurence Lundbladea826c502020-05-10 21:07:00 -07004584 /* exponent, mantissa
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004585 [
4586 4([-1, 3]),
4587 4([-20, 4759477275222530853136]),
4588 4([9223372036854775807, -4759477275222530853137]),
4589 5([300, 100]),
4590 5([-20, 4759477275222530853136]),
4591 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004592 5([ 9223372036854775806, -4759477275222530853137])
4593 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004594 ]
4595 */
4596
4597 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4598
4599 QCBORItem Item;
4600 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4601 if(nCBORError) {
4602 return -1;
4603 }
4604
4605 int64_t integer;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004606 // 4([-1, 3]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004607 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004608 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004609 return -2;
4610 }
4611 DCtx.uLastError = 0; // TODO: a method for this
4612
Laurence Lundbladea826c502020-05-10 21:07:00 -07004613 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004614 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004615 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004616 return -2;
4617 }
4618 DCtx.uLastError = 0; // TODO: a method for this
4619
Laurence Lundbladea826c502020-05-10 21:07:00 -07004620 // 4([9223372036854775807, -4759477275222530853137]),
4621 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004622 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004623 return -2;
4624 }
4625 DCtx.uLastError = 0; // TODO: a method for this
4626
4627 // 5([300, 100]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004628 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004629 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004630 return -2;
4631 }
4632 DCtx.uLastError = 0; // TODO: a method for this
4633
Laurence Lundbladea826c502020-05-10 21:07:00 -07004634 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004635 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004636 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004637 return -2;
4638 }
4639 DCtx.uLastError = 0; // TODO: a method for this
4640
Laurence Lundbladea826c502020-05-10 21:07:00 -07004641 // 5([-9223372036854775807, -4759477275222530853137])
4642 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004643 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004644 return -2;
4645 }
4646 DCtx.uLastError = 0; // TODO: a method for this
4647
4648 // 5([ 9223372036854775806, -4759477275222530853137])
4649 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004650 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004651 return -2;
4652 }
4653 DCtx.uLastError = 0; // TODO: a method for this
4654
4655 // 5([ 9223372036854775806, 9223372036854775806])]
4656 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004657 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004658 return -2;
4659 }
4660 DCtx.uLastError = 0; // TODO: a method for this
4661
4662
4663
4664 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4665
4666 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4667 if(nCBORError) {
4668 return -1;
4669 }
4670
4671 uint64_t uinteger;
4672 // 4([-1, 3]),
4673 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004674 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004675 return -2;
4676 }
4677 DCtx.uLastError = 0; // TODO: a method for this
4678
4679 // 4([-20, 4759477275222530853136]),
4680 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004681 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004682 return -2;
4683 }
4684 DCtx.uLastError = 0; // TODO: a method for this
4685
4686 // 4([9223372036854775807, -4759477275222530853137]),
4687 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004688 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004689 return -2;
4690 }
4691 DCtx.uLastError = 0; // TODO: a method for this
4692
4693 // 5([300, 100]),
4694 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004695 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004696 return -2;
4697 }
4698 DCtx.uLastError = 0; // TODO: a method for this
4699
4700 // 5([-20, 4759477275222530853136]),
4701 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004702 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004703 return -2;
4704 }
4705 DCtx.uLastError = 0; // TODO: a method for this
4706
4707 // 5([-9223372036854775807, -4759477275222530853137])
4708 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004709 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004710 return -2;
4711 }
4712 DCtx.uLastError = 0; // TODO: a method for this
4713
4714 // 5([ 9223372036854775806, -4759477275222530853137])
4715 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004716 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004717 return -2;
4718 }
4719 DCtx.uLastError = 0; // TODO: a method for this
4720
4721 // 5([ 9223372036854775806, 9223372036854775806])]
4722 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004723 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004724 return -2;
4725 }
4726 DCtx.uLastError = 0; // TODO: a method for this
4727
4728
4729
4730 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4731 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4732 if(nCBORError) {
4733 return -1;
4734 }
4735
4736 double dResult;
4737 // 4([-1, 3]),
4738 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004739 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004740 dResult != 0.3) {
4741 return -2;
4742 }
4743
4744 // 4([-20, 4759477275222530853136]),
4745 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004746 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004747 dResult != 47.408855671161923) {
4748 return -2;
4749 }
4750
4751 // 4([9223372036854775807, -4759477275222530853137]),
4752 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004753 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004754 dResult != -INFINITY) {
4755 return -2;
4756 }
4757
4758 // 5([300, 100]),
4759 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004760 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004761 dResult != -INFINITY) {
4762 return -2;
4763 }
4764
4765 // 5([-20, 4759477275222530853136]),
4766 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004767 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004768 dResult != 4521260802379792.0) {
4769 return -2;
4770 }
4771
4772 // 5([-9223372036854775807, -4759477275222530853137])
4773 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004774 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004775 dResult != -0.0) {
4776 return -2;
4777 }
4778
4779 // 5([9223372036854775806, 9223372036854775806])]
4780 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004781 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004782 dResult != INFINITY) {
4783 return -2;
4784 }
4785
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004786 return 0;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004787}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07004788
4789
Laurence Lundbladee3553422020-05-02 11:11:17 -07004790int32_t CBORSequenceDecodeTests(void)
4791{
4792 QCBORDecodeContext DCtx;
4793 QCBORItem Item;
4794 QCBORError uCBORError;
4795
4796 // --- Test a sequence with extra bytes ---
4797
4798 // The input for the date test happens to be a sequence so it
4799 // is reused. It is a sequence because it doesn't start as
4800 // an array or map.
4801 QCBORDecode_Init(&DCtx,
4802 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
4803 QCBOR_DECODE_MODE_NORMAL);
4804
4805 // Get the first item
4806 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4807 if(uCBORError != QCBOR_SUCCESS) {
4808 return 1;
4809 }
4810 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
4811 return 2;
4812 }
4813
4814 // Get a second item
4815 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4816 if(uCBORError != QCBOR_SUCCESS) {
4817 return 2;
4818 }
4819 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
4820 return 3;
4821 }
4822
4823 // A sequence can have stuff at the end that may
4824 // or may not be valid CBOR. The protocol decoder knows
4825 // when to stop by definition of the protocol, not
4826 // when the top-level map or array is ended.
4827 // Finish still has to be called to know that
4828 // maps and arrays (if there were any) were closed
4829 // off correctly. When called like this it
4830 // must return the error QCBOR_ERR_EXTRA_BYTES.
4831 uCBORError = QCBORDecode_Finish(&DCtx);
4832 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
4833 return 4;
4834 }
4835
4836
4837 // --- Test an empty input ----
4838 uint8_t empty[1];
4839 UsefulBufC Empty = {empty, 0};
4840 QCBORDecode_Init(&DCtx,
4841 Empty,
4842 QCBOR_DECODE_MODE_NORMAL);
4843
4844 uCBORError = QCBORDecode_Finish(&DCtx);
4845 if(uCBORError != QCBOR_SUCCESS) {
4846 return 5;
4847 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004848
4849
Laurence Lundbladee3553422020-05-02 11:11:17 -07004850 // --- Sequence with unclosed indefinite length array ---
4851 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004852
Laurence Lundbladee3553422020-05-02 11:11:17 -07004853 QCBORDecode_Init(&DCtx,
4854 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
4855 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004856
Laurence Lundbladee3553422020-05-02 11:11:17 -07004857 // Get the first item
4858 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4859 if(uCBORError != QCBOR_SUCCESS) {
4860 return 7;
4861 }
4862 if(Item.uDataType != QCBOR_TYPE_INT64) {
4863 return 8;
4864 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004865
Laurence Lundbladee3553422020-05-02 11:11:17 -07004866 // Get a second item
4867 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4868 if(uCBORError != QCBOR_SUCCESS) {
4869 return 9;
4870 }
4871 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4872 return 10;
4873 }
4874
4875 // Try to finish before consuming all bytes to confirm
4876 // that the still-open error is returned.
4877 uCBORError = QCBORDecode_Finish(&DCtx);
4878 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
4879 return 11;
4880 }
4881
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004882
Laurence Lundbladee3553422020-05-02 11:11:17 -07004883 // --- Sequence with a closed indefinite length array ---
4884 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004885
Laurence Lundbladee3553422020-05-02 11:11:17 -07004886 QCBORDecode_Init(&DCtx,
4887 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
4888 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004889
Laurence Lundbladee3553422020-05-02 11:11:17 -07004890 // Get the first item
4891 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4892 if(uCBORError != QCBOR_SUCCESS) {
4893 return 12;
4894 }
4895 if(Item.uDataType != QCBOR_TYPE_INT64) {
4896 return 13;
4897 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004898
Laurence Lundbladee3553422020-05-02 11:11:17 -07004899 // Get a second item
4900 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4901 if(uCBORError != QCBOR_SUCCESS) {
4902 return 14;
4903 }
4904 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4905 return 15;
4906 }
4907
4908 // Try to finish before consuming all bytes to confirm
4909 // that the still-open error is returned.
4910 uCBORError = QCBORDecode_Finish(&DCtx);
4911 if(uCBORError != QCBOR_SUCCESS) {
4912 return 16;
4913 }
4914
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004915
Laurence Lundbladee3553422020-05-02 11:11:17 -07004916 return 0;
4917}
4918
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004919
Laurence Lundblade70ecead2020-06-15 19:40:06 -07004920
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004921int32_t IntToTests()
4922{
4923 int nErrCode;
4924 int32_t n32;
4925 int16_t n16;
4926 int8_t n8;
4927 uint32_t u32;
4928 uint16_t u16;
4929 uint8_t u8;
4930 uint64_t u64;
4931
4932 nErrCode = QCBOR_Int64ToInt32(1, &n32);
4933 if(nErrCode == -1 || n32 != 1) {
4934 return 1;
4935 }
4936
4937 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
4938 if(nErrCode == -1 || n32 != INT32_MAX) {
4939 return 2;
4940 }
4941
4942 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
4943 if(nErrCode == -1 || n32 != INT32_MIN) {
4944 return 3;
4945 }
4946
4947 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
4948 if(nErrCode != -1) {
4949 return 4;
4950 }
4951
4952 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
4953 if(nErrCode != -1) {
4954 return 5;
4955 }
4956
4957
4958 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
4959 if(nErrCode == -1 || n16 != INT16_MAX) {
4960 return 6;
4961 }
4962
4963 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
4964 if(nErrCode == -1 || n16 != INT16_MIN) {
4965 return 7;
4966 }
4967
4968 nErrCode = QCBOR_Int64ToInt16(1, &n16);
4969 if(nErrCode == -1 || n16 != 1) {
4970 return 8;
4971 }
4972
4973 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
4974 if(nErrCode != -1) {
4975 return 9;
4976 }
4977
4978 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
4979 if(nErrCode != -1) {
4980 return 10;
4981 }
4982
4983
4984 nErrCode = QCBOR_Int64ToInt8(1, &n8);
4985 if(nErrCode == -1 || n8 != 1) {
4986 return 11;
4987 }
4988
4989 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
4990 if(nErrCode == -1 || n8 != INT8_MAX) {
4991 return 12;
4992 }
4993
4994 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
4995 if(nErrCode == -1 || n8 != INT8_MIN) {
4996 return 13;
4997 }
4998
4999 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5000 if(nErrCode != -1) {
5001 return 14;
5002 }
5003
5004 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5005 if(nErrCode != -1) {
5006 return 15;
5007 }
5008
5009
5010 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5011 if(nErrCode == -1 || u32 != 1) {
5012 return 16;
5013 }
5014
5015 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5016 if(nErrCode == -1 || u32 != UINT32_MAX) {
5017 return 17;
5018 }
5019
5020 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5021 if(nErrCode == -1 || u32 != 0) {
5022 return 18;
5023 }
5024
5025 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5026 if(nErrCode != -1) {
5027 return 19;
5028 }
5029
5030 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5031 if(nErrCode != -1) {
5032 return 20;
5033 }
5034
5035
5036 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5037 if(nErrCode == -1 || u16 != UINT16_MAX) {
5038 return 21;
5039 }
5040
5041 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5042 if(nErrCode == -1 || u16 != 0) {
5043 return 22;
5044 }
5045
5046 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5047 if(nErrCode == -1 || u16 != 1) {
5048 return 23;
5049 }
5050
5051 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5052 if(nErrCode != -1) {
5053 return 24;
5054 }
5055
5056 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5057 if(nErrCode != -1) {
5058 return 25;
5059 }
5060
5061
5062 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5063 if(nErrCode == -1 || u8 != UINT8_MAX) {
5064 return 26;
5065 }
5066
5067 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5068 if(nErrCode == -1 || u8 != 0) {
5069 return 27;
5070 }
5071
5072 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5073 if(nErrCode == -1 || u8 != 1) {
5074 return 28;
5075 }
5076
5077 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5078 if(nErrCode != -1) {
5079 return 29;
5080 }
5081
5082 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5083 if(nErrCode != -1) {
5084 return 30;
5085 }
5086
5087
5088 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5089 if(nErrCode == -1 || u64 != 1) {
5090 return 31;
5091 }
5092
5093 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
5094 if(nErrCode == -1 || u64 != INT64_MAX) {
5095 return 32;
5096 }
5097
5098 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
5099 if(nErrCode == -1 || u64 != 0) {
5100 return 33;
5101 }
5102
5103 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
5104 if(nErrCode != -1) {
5105 return 34;
5106 }
5107
5108 return 0;
5109}
5110
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005111
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005112
5113
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005114/*
5115A sequence with
5116 A wrapping bstr
5117 containing a map
5118 1
5119 2
5120 A wrapping bstr
5121 containing an array
5122 3
5123 wrapping bstr
5124 4
5125 5
5126 6
5127 array
5128 7
5129 8
5130
5131 */
5132
5133static UsefulBufC foo(UsefulBuf ffo)
5134{
5135 UsefulBufC Encoded;
5136 QCBOREncodeContext EC;
5137 QCBORError uErr;
5138
5139 QCBOREncode_Init(&EC, ffo);
5140
5141 QCBOREncode_BstrWrap(&EC);
5142 QCBOREncode_OpenMap(&EC);
5143 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
5144 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
5145 QCBOREncode_CloseMap(&EC);
5146 QCBOREncode_BstrWrap(&EC);
5147 QCBOREncode_OpenArray(&EC);
5148 QCBOREncode_AddInt64(&EC, 3);
5149 QCBOREncode_BstrWrap(&EC);
5150 QCBOREncode_AddInt64(&EC, 4);
5151 QCBOREncode_CloseBstrWrap(&EC, NULL);
5152 QCBOREncode_AddInt64(&EC, 5);
5153 QCBOREncode_CloseArray(&EC);
5154 QCBOREncode_CloseBstrWrap(&EC, NULL);
5155 QCBOREncode_AddInt64(&EC, 6);
5156 QCBOREncode_CloseBstrWrap(&EC, NULL);
5157 QCBOREncode_OpenArray(&EC);
5158 QCBOREncode_AddInt64(&EC, 7);
5159 QCBOREncode_AddInt64(&EC, 8);
5160 QCBOREncode_CloseArray(&EC);
5161
5162 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07005163 if(uErr) {
5164 Encoded = NULLUsefulBufC;
5165 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005166
5167 return Encoded;
5168}
5169
5170
5171int32_t EnterBstrTest()
5172{
5173 MakeUsefulBufOnStack(ffo, 100);
5174
5175 QCBORDecodeContext DC;
5176
5177 QCBORDecode_Init(&DC, foo(ffo), 0);
5178
5179 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
5180
5181
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005182 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005183 QCBORDecode_EnterMap(&DC);
5184 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
5185 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
5186 QCBORDecode_ExitMap(&DC);
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005187 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005188 QCBORDecode_EnterArray(&DC);
5189 QCBORDecode_GetInt64(&DC, &i3);
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005190 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005191 QCBORDecode_GetInt64(&DC, &i4);
5192 QCBORDecode_ExitBstrWrapped(&DC);
5193 QCBORDecode_GetInt64(&DC, &i5);
5194 QCBORDecode_ExitArray(&DC);
5195 QCBORDecode_ExitBstrWrapped(&DC);
5196 QCBORDecode_GetInt64(&DC, &i6);
5197 QCBORDecode_ExitBstrWrapped(&DC);
5198 QCBORDecode_EnterArray(&DC);
5199 QCBORDecode_GetInt64(&DC, &i7);
5200 QCBORDecode_GetInt64(&DC, &i8);
5201 QCBORDecode_ExitArray(&DC);
5202
5203 QCBORError uErr = QCBORDecode_Finish(&DC);
5204
5205 return (int32_t)uErr;
5206}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005207
5208
5209
5210
5211static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005212 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005213
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005214 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005215 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005216 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
5217 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
5218 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005219
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005220 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005221 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
5222 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
5223 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005224
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005225 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005226 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005227 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
5228 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005229
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005230 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005231 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
5232 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005233
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005234 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005235 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005236 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
5237 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005238
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005239 21,
5240 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
5241 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
5242
5243 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005244 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005245 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
5246 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005247
5248 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005249 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
5250 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005251
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005252 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005253 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005254 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
5255 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005256
5257 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005258 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
5259 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005260
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005261 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005262 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005263 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
5264 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005265
5266 0x18, 0x33,
5267 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
5268
5269 // MIME
5270 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005271 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
5272 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
5273 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005274
5275 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005276 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
5277 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005278
5279 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005280 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
5281 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
5282 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005283
5284 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005285 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
5286 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005287
5288 // UUID
5289 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005290 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
5291 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005292
5293 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005294 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
5295 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005296};
5297
5298int32_t DecodeTaggedTypeTests()
5299{
5300 QCBORDecodeContext DC;
5301 QCBORError uErr;
5302
5303 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
5304
5305 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005306 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005307
5308 QCBORDecode_EnterMap(&DC);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005309 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5310 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005311 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
5312 return 1;
5313 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005314 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005315 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5316 return 2;
5317 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005318 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005319 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5320 return 3;
5321 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005322 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
5323 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005324 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5325 return 4;
5326 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005327 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
5328 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5329 return 5;
5330 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005331
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005332 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5333 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5334 bNeg != false) {
5335 return 10;
5336 }
5337 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5338 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5339 bNeg != true) {
5340 return 11;
5341 }
5342 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5343 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5344 return 12;
5345 }
5346 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5347 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5348 return 13;
5349 }
5350 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5351 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5352 return 14;
5353 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005354
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005355 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5356 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5357 return 20;
5358 }
5359 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5360 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5361 return 21;
5362 }
5363 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5364 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5365 return 22;
5366 }
5367 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5368 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5369 return 23;
5370 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005371
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005372 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5373 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5374 return 30;
5375 }
5376 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5377 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5378 return 31;
5379 }
5380 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5381 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5382 return 32;
5383 }
5384 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5385 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5386 return 33;
5387 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005388
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005389 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5390 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5391 return 40;
5392 }
5393 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5394 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5395 return 41;
5396 }
5397 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5398 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5399 return 42;
5400 }
5401 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5402 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5403 return 43;
5404 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005405
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005406 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5407 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5408 return 50;
5409 }
5410 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5411 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5412 return 51;
5413 }
5414 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5415 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5416 return 52;
5417 }
5418 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5419 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5420 return 53;
5421 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005422
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005423 // MIME
5424 bool bIsNot7Bit;
5425 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bIsNot7Bit);
5426 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5427 bIsNot7Bit == true) {
5428 return 60;
5429 }
5430 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bIsNot7Bit);
5431 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5432 bIsNot7Bit == true) {
5433 return 61;
5434 }
5435 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bIsNot7Bit);
5436 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5437 bIsNot7Bit == false) {
5438 return 62;
5439 }
5440 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bIsNot7Bit);
5441 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5442 bIsNot7Bit == false) {
5443 return 63;
5444 }
5445 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5446 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5447 return 64;
5448 }
5449 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5450 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5451 return 65;
5452 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005453
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005454 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5455 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5456 return 70;
5457 }
5458 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5459 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5460 return 71;
5461 }
5462 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5463 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5464 return 72;
5465 }
5466 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5467 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5468 return 73;
5469 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005470
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005471 // Improvement: add some more error test cases
5472
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005473 QCBORDecode_ExitMap(&DC);
5474
5475 uErr = QCBORDecode_Finish(&DC);
5476 if(uErr != QCBOR_SUCCESS) {
5477 return 100;
5478 }
5479
5480 return 0;
5481}