blob: d882d991b62c5c51abe4a940129410eb506c3ad2 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070036#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080037#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080038#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070039#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080040
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080041
Laurence Lundbladea2e29072018-12-30 09:20:06 -080042#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080043#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080044
45static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080046{
47 if(szLabel) {
48 printf("%s ", szLabel);
49 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080050
Laurence Lundblade570fab52018-10-13 18:28:27 +080051 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080052 for(i = 0; i < Buf.len; i++) {
53 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080054 printf("%02x ", Z);
55 }
56 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080057
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058 fflush(stdout);
59}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080060#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061
62
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070063static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
66 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
67 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
68 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
69 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
70 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
71 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
72 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
73 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
74 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
75 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
76 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
77 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
78 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
79 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
80 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
81 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
82 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
83 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
84 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
85 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff};
88
89
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080090// return CBOR error or -1 if type of value doesn't match
91
Laurence Lundbladec5fef682020-01-25 11:38:45 -080092static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080093{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070094 QCBORItem Item;
95 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080096
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080097 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070098 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080099 if(Item.uDataType != QCBOR_TYPE_ARRAY)
100 return -1;
101
102 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700103 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800104 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800105 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800106 return -1;
107
108 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700109 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800110 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800111 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800112 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800113
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700115 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800117 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800118 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700121 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800123 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800124 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800125
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700127 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800128 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800129 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800130 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800131
132
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700134 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 if(Item.uDataType != QCBOR_TYPE_INT64 ||
136 Item.val.int64 != -2147483648)
137 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800138
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800139 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700140 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800141 if(Item.uDataType != QCBOR_TYPE_INT64 ||
142 Item.val.int64 != -2147483647)
143 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800144
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800145 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700146 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147 if(Item.uDataType != QCBOR_TYPE_INT64 ||
148 Item.val.int64 != -65538)
149 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800150
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800151 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700152 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 if(Item.uDataType != QCBOR_TYPE_INT64 ||
154 Item.val.int64 != -65537)
155 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800157 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700158 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800159 if(Item.uDataType != QCBOR_TYPE_INT64 ||
160 Item.val.int64 != -65536)
161 return -1;
162
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800163
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700165 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166 if(Item.uDataType != QCBOR_TYPE_INT64 ||
167 Item.val.int64 != -65535)
168 return -1;
169
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800170
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700172 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800173 if(Item.uDataType != QCBOR_TYPE_INT64 ||
174 Item.val.int64 != -65534)
175 return -1;
176
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800177
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800178 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700179 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800180 if(Item.uDataType != QCBOR_TYPE_INT64 ||
181 Item.val.int64 != -257)
182 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800183
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800184 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700185 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800186 if(Item.uDataType != QCBOR_TYPE_INT64 ||
187 Item.val.int64 != -256)
188 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800189
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800190 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700191 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800192 if(Item.uDataType != QCBOR_TYPE_INT64 ||
193 Item.val.int64 != -255)
194 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800195
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800196 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700197 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800198 if(Item.uDataType != QCBOR_TYPE_INT64 ||
199 Item.val.int64 != -254)
200 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800201
202
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800203 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700204 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800205 if(Item.uDataType != QCBOR_TYPE_INT64 ||
206 Item.val.int64 != -25)
207 return -1;
208
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800209
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800210 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700211 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if(Item.uDataType != QCBOR_TYPE_INT64 ||
213 Item.val.int64 != -24)
214 return -1;
215
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800216
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800217 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700218 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800219 if(Item.uDataType != QCBOR_TYPE_INT64 ||
220 Item.val.int64 != -23)
221 return -1;
222
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800223
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700225 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 if(Item.uDataType != QCBOR_TYPE_INT64 ||
227 Item.val.int64 != -1)
228 return -1;
229
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800230
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800231 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700232 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800233 if(Item.uDataType != QCBOR_TYPE_INT64 ||
234 Item.val.int64 != 0)
235 return -1;
236
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800237
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700239 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800240 if(Item.uDataType != QCBOR_TYPE_INT64 ||
241 Item.val.int64 != 0)
242 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800243
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800244 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700245 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800246 if(Item.uDataType != QCBOR_TYPE_INT64 ||
247 Item.val.int64 != 1)
248 return -1;
249
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800250
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700252 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 if(Item.uDataType != QCBOR_TYPE_INT64 ||
254 Item.val.int64 != 22)
255 return -1;
256
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800257
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700259 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800260 if(Item.uDataType != QCBOR_TYPE_INT64 ||
261 Item.val.int64 != 23)
262 return -1;
263
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800264
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700266 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(Item.uDataType != QCBOR_TYPE_INT64 ||
268 Item.val.int64 != 24)
269 return -1;
270
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800271
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800272 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700273 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(Item.uDataType != QCBOR_TYPE_INT64 ||
275 Item.val.int64 != 25)
276 return -1;
277
278 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700279 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800280 if(Item.uDataType != QCBOR_TYPE_INT64 ||
281 Item.val.int64 != 26)
282 return -1;
283
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800284
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800285 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700286 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800287 if(Item.uDataType != QCBOR_TYPE_INT64 ||
288 Item.val.int64 != 254)
289 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800290
291
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800292 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700293 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800294 if(Item.uDataType != QCBOR_TYPE_INT64 ||
295 Item.val.int64 != 255)
296 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800297
298
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800299 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700300 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800301 if(Item.uDataType != QCBOR_TYPE_INT64 ||
302 Item.val.int64 != 256)
303 return -1;
304
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800305
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700307 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308 if(Item.uDataType != QCBOR_TYPE_INT64 ||
309 Item.val.int64 != 257)
310 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800311
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800312 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700313 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800314 if(Item.uDataType != QCBOR_TYPE_INT64 ||
315 Item.val.int64 != 65534)
316 return -1;
317
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800318
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800319 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700320 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800321 if(Item.uDataType != QCBOR_TYPE_INT64 ||
322 Item.val.int64 != 65535)
323 return -1;
324
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800325
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800326 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700327 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800328 if(Item.uDataType != QCBOR_TYPE_INT64 ||
329 Item.val.int64 != 65536)
330 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800331
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800332 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700333 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800334 if(Item.uDataType != QCBOR_TYPE_INT64 ||
335 Item.val.int64 != 65537)
336 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800337
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800338 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700339 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800340 if(Item.uDataType != QCBOR_TYPE_INT64 ||
341 Item.val.int64 != 65538)
342 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800343
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800344 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700345 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800346 if(Item.uDataType != QCBOR_TYPE_INT64 ||
347 Item.val.int64 != 2147483647)
348 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800349
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800350 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700351 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800352 if(Item.uDataType != QCBOR_TYPE_INT64 ||
353 Item.val.int64 != 2147483647)
354 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800356 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700357 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 if(Item.uDataType != QCBOR_TYPE_INT64 ||
359 Item.val.int64 != 2147483648)
360 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800361
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800362 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700363 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800364 if(Item.uDataType != QCBOR_TYPE_INT64 ||
365 Item.val.int64 != 2147483649)
366 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800367
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800368 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700369 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 if(Item.uDataType != QCBOR_TYPE_INT64 ||
371 Item.val.int64 != 4294967294)
372 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800373
374
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800375 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700376 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 if(Item.uDataType != QCBOR_TYPE_INT64 ||
378 Item.val.int64 != 4294967295)
379 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800380
381
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800382 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700383 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800384 if(Item.uDataType != QCBOR_TYPE_INT64 ||
385 Item.val.int64 != 4294967296)
386 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800387
388
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800389 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700390 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800391 if(Item.uDataType != QCBOR_TYPE_INT64 ||
392 Item.val.int64 != 4294967297)
393 return -1;
394
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800395
396
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800397 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700398 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(Item.uDataType != QCBOR_TYPE_INT64 ||
400 Item.val.int64 != 9223372036854775807LL)
401 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800402
403
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800404 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700405 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800406 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
407 Item.val.uint64 != 18446744073709551615ULL)
408 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800409
410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
412 return -1;
413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800414
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800415 return 0;
416}
417
418
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800419// One less than the smallest negative integer allowed in C. Decoding
420// this should fail.
421static const uint8_t spTooSmallNegative[] = {
422 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000423};
424
425
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800426/*
427 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800428 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800430int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800431{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000432 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000435 QCBORDecode_Init(&DCtx,
436 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
437 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800438
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000439 // The really big test of all successes
440 nReturn = IntegerValuesParseTestInternal(&DCtx);
441 if(nReturn) {
442 return nReturn;
443 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800444
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000445 // The one large negative integer that can be parsed
446 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800447 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000448 QCBOR_DECODE_MODE_NORMAL);
449
450 QCBORItem item;
451 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
452 nReturn = -4000;
453 }
454
455 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800456}
457
458
459/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800460 Creates a simple CBOR array and returns it in *pEncoded. The array is
461 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800462
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800463 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800464
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800465 */
466
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800467static uint8_t spSimpleArrayBuffer[50];
468
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800469static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470{
471 QCBOREncodeContext ECtx;
472 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800473
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800474 *pEncoded = NULL;
475 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800476
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800478 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800479 // and last with the buffer to do the actual encoding
480 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700481 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 QCBOREncode_OpenArray(&ECtx);
483 QCBOREncode_AddInt64(&ECtx, nInt1);
484 QCBOREncode_AddInt64(&ECtx, nInt2);
485 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
486 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
487 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800488
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800489 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800490 goto Done;
491
492 if(*pEncoded != NULL) {
493 nReturn = 0;
494 goto Done;
495 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800496
497 // Use static buffer to avoid dependency on malloc()
498 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800499 goto Done;
500 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800501 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800502
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800503 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800504
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800505Done:
506 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800507}
508
509
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800510/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800511 Some basic CBOR with map and array used in a lot of tests.
512 The map labels are all strings
513
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800514 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900515 "an array of two strings": [
516 "string1", "string2"
517 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800518 "map in a map": {
519 "bytes 1": h'78787878',
520 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900521 "another int": 98,
522 "text 2": "lies, damn lies and statistics"
523 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800524 }
525 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800526static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700527 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
528 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
529 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
530 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
531 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
532 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
533 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
534 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
535 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
536 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
537 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
538 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
539 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
540 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
541 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700542 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800543
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700544// Same as above, but with indefinite lengths.
545static const uint8_t pValidMapIndefEncoded[] = {
5460xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5470x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5480x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5490x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5500x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5510x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5520x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5530x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5540x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5550x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5560x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5570x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5580x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5590x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5600x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5610x73, 0xff, 0xff};
562
563
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800564static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700565 size_t nLen,
566 int64_t *pInt1,
567 int64_t *pInt2,
568 const uint8_t **pBuf3,
569 size_t *pBuf3Len,
570 const uint8_t **pBuf4,
571 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800572{
573 QCBORDecodeContext DCtx;
574 QCBORItem Item;
575 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800576
Laurence Lundbladeee851742020-01-08 08:37:05 -0800577 QCBORDecode_Init(&DCtx,
578 (UsefulBufC){pEncoded, nLen},
579 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581 // Make sure the first thing is a map
582 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
583 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800585 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800586 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 goto Done;
588 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800590 // Second integer
591 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
592 goto Done;
593 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800594
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 // First string
596 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
597 goto Done;
598 *pBuf3 = Item.val.string.ptr;
599 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800601 // Second string
602 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
603 goto Done;
604 *pBuf4 = Item.val.string.ptr;
605 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609Done:
610 return(nReturn);
611}
612
613
614
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800615
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800616int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617{
618 uint8_t *pEncoded;
619 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800620
Laurence Lundblade5e390822019-01-06 12:35:01 -0800621 int64_t i1=0, i2=0;
622 size_t i3=0, i4=0;
623 const uint8_t *s3= (uint8_t *)"";
624 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800625
626
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800627 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
628 return(-1);
629 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800630
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800631 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800632
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800633 if(i1 != 23 ||
634 i2 != 6000 ||
635 i3 != 8 ||
636 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530637 memcmp("galactic", s3, 8) !=0 ||
638 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800639 return(-1);
640 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800642 return(0);
643}
644
645
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700646/*
647 [
648 0,
649 [],
650 [
651 [],
652 [
653 0
654 ],
655 {},
656 {
657 1: {},
658 2: {},
659 3: []
660 }
661 ]
662 ]
663 */
664static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
665 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
666
Laurence Lundblade02625d42020-06-25 14:41:41 -0700667/* Same as above, but with indefinte lengths */
668static uint8_t sEmptiesIndef[] = {
6690x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700670 0x00,
671 0x9F,
672 0xFF,
673 0x9F,
674 0x9F,
675 0xFF,
676 0x9F,
677 0x00,
678 0xFF,
679 0xBF,
680 0xFF,
681 0xBF,
682 0x01,
683 0xBF,
684 0xFF,
685 0x02,
686 0xBF,
687 0xFF,
688 0x03,
689 0x9F,
690 0xFF,
691 0xFF,
692 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700693 0xFF};
694
695
696
697static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700698{
699 QCBORDecodeContext DCtx;
700 QCBORItem Item;
701
Laurence Lundbladeee851742020-01-08 08:37:05 -0800702 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700703 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800704 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700705
706 // Array with 3 items
707 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
708 Item.uDataType != QCBOR_TYPE_ARRAY ||
709 Item.uNestingLevel != 0 ||
710 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700711 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700712 return -1;
713 }
714
715 // An integer 0
716 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
717 Item.uDataType != QCBOR_TYPE_INT64 ||
718 Item.uNestingLevel != 1 ||
719 Item.uNextNestLevel != 1 ||
720 Item.val.uint64 != 0) {
721 return -2;
722 }
723
724 // An empty array
725 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
726 Item.uDataType != QCBOR_TYPE_ARRAY ||
727 Item.uNestingLevel != 1 ||
728 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700729 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700730 return -3;
731 }
732
733 // An array with 4 items
734 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
735 Item.uDataType != QCBOR_TYPE_ARRAY ||
736 Item.uNestingLevel != 1 ||
737 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700738 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700739 return -4;
740 }
741
742 // An empty array
743 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
744 Item.uDataType != QCBOR_TYPE_ARRAY ||
745 Item.uNestingLevel != 2 ||
746 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700747 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700748 return -5;
749 }
750
751 // An array with 1 item
752 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
753 Item.uDataType != QCBOR_TYPE_ARRAY ||
754 Item.uNestingLevel != 2 ||
755 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700756 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700757 return -6;
758 }
759
760 // An integer 0
761 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
762 Item.uDataType != QCBOR_TYPE_INT64 ||
763 Item.uNestingLevel != 3 ||
764 Item.uNextNestLevel != 2 ||
765 Item.val.uint64 != 0) {
766 return -7;
767 }
768
769 // An empty map
770 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
771 Item.uDataType != QCBOR_TYPE_MAP ||
772 Item.uNestingLevel != 2 ||
773 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700774 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700775 return -8;
776 }
777
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700778 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700779 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
780 Item.uDataType != QCBOR_TYPE_MAP ||
781 Item.uNestingLevel != 2 ||
782 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700783 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700784 return -9;
785 }
786
787 // An empty map
788 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
789 Item.uDataType != QCBOR_TYPE_MAP ||
790 Item.uNestingLevel != 3 ||
791 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700792 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700793 return -10;
794 }
795
796 // An empty map
797 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
798 Item.uDataType != QCBOR_TYPE_MAP ||
799 Item.uNestingLevel != 3 ||
800 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700801 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700802 return -11;
803 }
804
805 // An empty array
806 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
807 Item.uDataType != QCBOR_TYPE_ARRAY ||
808 Item.uNestingLevel != 3 ||
809 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700810 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700811 return -12;
812 }
813
814 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
815 return -13;
816 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700817 return 0;
818}
819
820
821int32_t EmptyMapsAndArraysTest()
822{
823 int nResult;
824 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
825 true);
826 if(nResult) {
827 return nResult;
828 }
829
830 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
831 false);
832
833 if(nResult) {
834 return nResult -100;
835 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700836
837 return 0;
838}
839
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800840
Laurence Lundbladeee851742020-01-08 08:37:05 -0800841static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
842 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800843
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800844int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800845{
846 QCBORDecodeContext DCtx;
847 int nReturn = 0;
848 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800849
Laurence Lundbladeee851742020-01-08 08:37:05 -0800850 QCBORDecode_Init(&DCtx,
851 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
852 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800853
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800854 for(i = 0; i < 10; i++) {
855 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800856
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800857 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
858 Item.uDataType != QCBOR_TYPE_ARRAY ||
859 Item.uNestingLevel != i) {
860 nReturn = -1;
861 break;
862 }
863 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800864
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800865 return(nReturn);
866}
867
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700868// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800869static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
870 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
871 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
872 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800873
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800874int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875{
876 QCBORDecodeContext DCtx;
877 int nReturn = 0;
878 int i;
879 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
881
Laurence Lundbladeee851742020-01-08 08:37:05 -0800882 QCBORDecode_Init(&DCtx,
883 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
884 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800885
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700886 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800887
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
889 Item.uDataType != QCBOR_TYPE_ARRAY ||
890 Item.uNestingLevel != i) {
891 nReturn = -1;
892 break;
893 }
894 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800896 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
897 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800899 return(nReturn);
900}
901
902
903
904
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800905int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800906{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700907 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800908
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800909 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700910 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800911
Laurence Lundbladeee851742020-01-08 08:37:05 -0800912 QCBORDecode_Init(&DCtx,
913 (UsefulBufC){spExpectedEncodedInts, nNum},
914 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800915
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800916 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800917
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700918 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919 nResult = -1;
920 goto Done;
921 }
922 }
923Done:
924 return nResult;
925}
926
927
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800928
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800929int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800930{
931 uint8_t *pEncoded;
932 int nReturn;
933 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800934
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800935 int64_t i1, i2;
936 size_t i3, i4;
937 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800938
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800939 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800940
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800941 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
942 return(-1);
943 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800944
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800945 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
946 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
947 if(nResult == 0) {
948 nReturn = -1;
949 }
950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800951
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 return(nReturn);
953}
954
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530955/*
956 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800957 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
958 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530959 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800960static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800961{
962 QCBORDecodeContext DCtx;
963 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700964 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800965
Laurence Lundbladeee851742020-01-08 08:37:05 -0800966 QCBORDecode_Init(&DCtx,
967 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
968 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800969
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900970 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700971 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900972 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800973 if(Item.uDataType != QCBOR_TYPE_MAP ||
974 Item.val.uCount != 3)
975 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800976
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900977 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700978 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900979 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800980 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800981 Item.uDataType != QCBOR_TYPE_INT64 ||
982 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530983 Item.uDataAlloc ||
984 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900985 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800986 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800988
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900989 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700990 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800992 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530993 Item.uDataAlloc ||
994 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900995 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800996 Item.uDataType != QCBOR_TYPE_ARRAY ||
997 Item.val.uCount != 2)
998 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800999
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001000 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001001 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001002 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001003 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301004 Item.uDataAlloc ||
1005 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001007 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001009
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001010 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001011 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001012 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001013 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301014 Item.uDataAlloc ||
1015 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001016 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001017 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001019
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001020 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001021 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001022 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001023 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301024 Item.uDataAlloc ||
1025 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001026 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001027 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001028 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001029 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001030 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001031
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001032 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001033 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001034 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001035 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001036 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001037 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301038 Item.uDataAlloc ||
1039 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001040 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001041 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001043
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001044 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001045 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001046 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001047 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001048 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001049 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301050 Item.uDataAlloc ||
1051 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001052 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001053 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001055
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001056 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001057 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001058 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001059 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301060 Item.uDataAlloc ||
1061 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001062 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001063 Item.uDataType != QCBOR_TYPE_INT64 ||
1064 Item.val.int64 != 98)
1065 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001066
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001067 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001068 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001069 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001071 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001072 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301073 Item.uDataAlloc ||
1074 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001075 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001076 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 return 0;
1080}
1081
1082
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001083/*
1084 Decode and thoroughly check a moderately complex
1085 set of maps
1086 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001087int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001088{
1089 QCBORDecodeContext DCtx;
1090 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001091 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001092
Laurence Lundbladeee851742020-01-08 08:37:05 -08001093 QCBORDecode_Init(&DCtx,
1094 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1095 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001096
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001097 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001098 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001099 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001100 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1101 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001102 return -1;
1103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001104
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001105 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001106 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001107 }
1108 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1109 Item.uDataAlloc ||
1110 Item.uLabelAlloc ||
1111 Item.uLabelType != QCBOR_TYPE_NONE ||
1112 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1113 return -2;
1114 }
1115
1116 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001117 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 }
1119 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1120 Item.uDataType != QCBOR_TYPE_INT64 ||
1121 Item.val.int64 != 42 ||
1122 Item.uDataAlloc ||
1123 Item.uLabelAlloc) {
1124 return -3;
1125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001126
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001127 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001128 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001129 }
1130 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1131 Item.uDataAlloc ||
1132 Item.uLabelAlloc ||
1133 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1134 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1135 return -4;
1136 }
1137
1138 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001139 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001140 }
1141 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1142 Item.uDataAlloc ||
1143 Item.uLabelAlloc ||
1144 Item.uDataType != QCBOR_TYPE_ARRAY ||
1145 Item.val.uCount != 2) {
1146 return -5;
1147 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001148
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001149 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001150 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001151 }
1152 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1153 Item.val.string.len != 7 ||
1154 Item.uDataAlloc ||
1155 Item.uLabelAlloc ||
1156 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1157 return -6;
1158 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001159
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001160 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001161 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001162 }
1163 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1164 Item.uDataAlloc ||
1165 Item.uLabelAlloc ||
1166 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1167 return -7;
1168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001169
1170
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001171 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001172 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001173 }
1174 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1175 Item.uDataAlloc ||
1176 Item.uLabelAlloc ||
1177 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1178 return -8;
1179 }
1180
1181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001182 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 }
1184 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1185 Item.uDataAlloc ||
1186 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001187 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1188 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 return -9;
1190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001191
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001192 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001193 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001194 }
1195 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1196 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1197 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1198 Item.uDataAlloc ||
1199 Item.uLabelAlloc) {
1200 return -10;
1201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001202
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001203 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001204 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001205 }
1206 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1207 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1208 Item.uDataAlloc ||
1209 Item.uLabelAlloc ||
1210 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1211 return -11;
1212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001213
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001215 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 }
1217 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1218 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1219 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1220 Item.uDataAlloc ||
1221 Item.uLabelAlloc) {
1222 return -12;
1223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001224
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001225 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001226 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001227 }
1228 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1229 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1230 Item.uDataAlloc ||
1231 Item.uLabelAlloc ||
1232 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1233 return -13;
1234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001235
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001236 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001237 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001238 }
1239 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1240 Item.uDataAlloc ||
1241 Item.uLabelAlloc ||
1242 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1243 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1244 return -14;
1245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001246
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001247 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001248 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001249 }
1250 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1251 Item.uDataAlloc ||
1252 Item.uLabelAlloc ||
1253 Item.uDataType != QCBOR_TYPE_INT64 ||
1254 Item.val.int64 != 98) {
1255 return -15;
1256 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001257
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001259 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 }
1261 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1262 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1263 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1264 Item.uDataAlloc ||
1265 Item.uLabelAlloc) {
1266 return -16;
1267 }
1268
1269 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001270 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001271 }
1272 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1273 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1274 Item.uDataAlloc ||
1275 Item.uLabelAlloc ||
1276 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1277 return -17;
1278 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001279
1280
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001281 /*
1282 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1283 map that when interpreted as an array will be too many. Test
1284 data just has the start of the map, not all the items in the map.
1285 */
1286 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001287
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001288 QCBORDecode_Init(&DCtx,
1289 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1290 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001291
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001292 if((QCBOR_ERR_ARRAY_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
1293 return -50;
1294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001295
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001296 return 0;
1297}
1298
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001299
1300/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301301 Fully or partially decode pValidMapEncoded. When
1302 partially decoding check for the right error code.
1303 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301305 The partial decodes test error conditions of
1306 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001307
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301308 This could be combined with the above test
1309 and made prettier and maybe a little more
1310 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001311 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001312static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001313{
1314 QCBORDecodeContext DCtx;
1315 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001316 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001317
Laurence Lundbladeee851742020-01-08 08:37:05 -08001318 QCBORDecode_Init(&DCtx,
1319 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1320 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001322 if(nLevel < 1) {
1323 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1324 return -1;
1325 } else {
1326 return 0;
1327 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301329
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001330
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001331 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001332 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001333 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 if(Item.uDataType != QCBOR_TYPE_MAP ||
1335 Item.val.uCount != 3)
1336 return -1;
1337
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001338 if(nLevel < 2) {
1339 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1340 return -1;
1341 } else {
1342 return 0;
1343 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001344 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001345
1346
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001348 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001351 Item.uDataType != QCBOR_TYPE_INT64 ||
1352 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001353 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001354 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001356
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001357 if(nLevel < 3) {
1358 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1359 return -1;
1360 } else {
1361 return 0;
1362 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001364
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001366 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001367 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001369 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001370 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001371 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001372 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001374
1375
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001376 if(nLevel < 4) {
1377 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1378 return -1;
1379 } else {
1380 return 0;
1381 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001382 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001383
1384
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001385 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001386 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001387 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001389 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001390 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001392
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001393 if(nLevel < 5) {
1394 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1395 return -1;
1396 } else {
1397 return 0;
1398 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001400
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001401 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001402 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001403 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001404 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001405 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001408
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001409 if(nLevel < 6) {
1410 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1411 return -1;
1412 } else {
1413 return 0;
1414 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001416
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001417 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001418 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001420 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001421 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422 Item.uDataType != QCBOR_TYPE_MAP ||
1423 Item.val.uCount != 4)
1424 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001425
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001426 if(nLevel < 7) {
1427 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1428 return -1;
1429 } else {
1430 return 0;
1431 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001432 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001433
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001434 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001435 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001436 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001438 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001439 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001440 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001441 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001443
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 if(nLevel < 8) {
1445 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1446 return -1;
1447 } else {
1448 return 0;
1449 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001451
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001452 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001453 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001454 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001455 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001456 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001458 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001459 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001461
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001462 if(nLevel < 9) {
1463 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1464 return -1;
1465 } else {
1466 return 0;
1467 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001469
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001470 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001471 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001472 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001474 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001475 Item.uDataType != QCBOR_TYPE_INT64 ||
1476 Item.val.int64 != 98)
1477 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001479 if(nLevel < 10) {
1480 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1481 return -1;
1482 } else {
1483 return 0;
1484 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001485 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001486
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001487 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001488 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001489 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001491 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001493 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001494 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001495 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001496
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301497 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 return -1;
1499 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001500
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001501 return 0;
1502}
1503
1504
1505
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001506
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001507int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001509 // Parse a moderatly complex map structure very thoroughly
1510 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1511 if(nResult) {
1512 return nResult;
1513 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001514
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001515 // Again, but in strings-only mode. It should succeed since the input
1516 // map has only string labels.
1517 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1518 if(nResult) {
1519 return nResult;
1520 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001521
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001522 // Again, but try to finish the decoding before the end of the
1523 // input at 10 different place and see that the right error code
1524 // is returned.
1525 for(int i = 0; i < 10; i++) {
1526 nResult = ExtraBytesTest(i);
1527 if(nResult) {
1528 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001529 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001531
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001532 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001533}
1534
1535
Laurence Lundbladeee851742020-01-08 08:37:05 -08001536static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1537 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1538 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001539
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001540int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001541{
1542 QCBORDecodeContext DCtx;
1543 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001544 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001545
1546
Laurence Lundbladeee851742020-01-08 08:37:05 -08001547 QCBORDecode_Init(&DCtx,
1548 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1549 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001550
1551
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001552 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001553 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1555 Item.val.uCount != 10)
1556 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001558 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001559 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001560 if(Item.uDataType != QCBOR_TYPE_FALSE)
1561 return -1;
1562
1563 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001564 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001565 if(Item.uDataType != QCBOR_TYPE_TRUE)
1566 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001567
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001568 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001569 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001570 if(Item.uDataType != QCBOR_TYPE_NULL)
1571 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001573 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001574 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1576 return -1;
1577
1578 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001579 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001580 return -1;
1581
1582 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001583 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001584 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1585 return -1;
1586
1587 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001588 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1590 return -1;
1591
Laurence Lundblade077475f2019-04-26 09:06:33 -07001592 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001593 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001594
Laurence Lundblade077475f2019-04-26 09:06:33 -07001595 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001596 return -1;
1597
Laurence Lundblade077475f2019-04-26 09:06:33 -07001598 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001599 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001601 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001602 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001603 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1604 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001606 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001607 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001608 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1609 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001610
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001611 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001612
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001613}
1614
1615
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001616int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001617{
1618 // Loop over all the not-well-formed instance of CBOR
1619 // that are test vectors in not_well_formed_cbor.h
1620 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1621 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1622 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1623 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1624
Laurence Lundbladeee851742020-01-08 08:37:05 -08001625 // Set up decoder context. String allocator needed for indefinite
1626 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001627 QCBORDecodeContext DCtx;
1628 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1629 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1630 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1631
1632 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001633 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001634 do {
1635 QCBORItem Item;
1636
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001637 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1638 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001639
1640 // Every test vector must fail with
1641 // a not-well-formed error. If not
1642 // this test fails.
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001643 if(!QCBORDecode_IsNotWellFormed(uCBORError) &&
1644 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001645 // Return index of failure in the error code
1646 return 2000 + nIterate;
1647 }
1648 }
1649 return 0;
1650}
1651
1652
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001653struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001654 UsefulBufC Input;
1655 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001656};
1657
Laurence Lundblade59289e52019-12-30 13:44:37 -08001658
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001659static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001660{
1661 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1662 // Set up the decoding context including a memory pool so that
1663 // indefinite length items can be checked
1664 QCBORDecodeContext DCtx;
1665 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1666 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001667
Laurence Lundblade59289e52019-12-30 13:44:37 -08001668 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1669 if(nCBORError) {
1670 return -9;
1671 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001672
Laurence Lundblade59289e52019-12-30 13:44:37 -08001673 // Iterate until there is an error of some sort error
1674 QCBORItem Item;
1675 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001676 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001677 memset(&Item, 0x33, sizeof(Item));
1678
1679 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1680 } while(nCBORError == QCBOR_SUCCESS);
1681
1682 // Must get the expected error or the this test fails
1683 // The data and label type must also be QCBOR_TYPE_NONE
1684 if(nCBORError != pF->nError ||
1685 Item.uDataType != QCBOR_TYPE_NONE ||
1686 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001687 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001688 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001689 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001690 }
1691 }
1692
1693 return 0;
1694}
1695
1696
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001697struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001698 // Most of this is copied from not_well_formed.h. Here the error code
1699 // returned is also checked.
1700
1701 // Indefinite length strings must be closed off
1702 // An indefinite length byte string not closed off
1703 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1704 // An indefinite length text string not closed off
1705 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1706
1707
1708 // All the chunks in an indefinite length string must be of the type of indefinite length string
1709 // indefinite length byte string with text string chunk
1710 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1711 // indefinite length text string with a byte string chunk
1712 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1713 // indefinite length byte string with an positive integer chunk
1714 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1715 // indefinite length byte string with an negative integer chunk
1716 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1717 // indefinite length byte string with an array chunk
1718 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1719 // indefinite length byte string with an map chunk
1720 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1721 // indefinite length byte string with tagged integer chunk
1722 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1723 // indefinite length byte string with an simple type chunk
1724 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1725 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1726 // indefinite length text string with indefinite string inside
1727 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1728
1729
1730 // Definte length maps and arrays must be closed by having the right number of items
1731 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001732 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001733 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001734 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001735 // A definte length array that is supposed to have 511 items, but has only 1
1736 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1737 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001738 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001739 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001740 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001741
1742
1743 // Indefinte length maps and arrays must be ended by a break
1744 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001745 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001746 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001747 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001748 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001749 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001750 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001751 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752
1753
1754 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001755 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001756 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001757 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001758 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001759 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001760 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, // TODO: 23
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001761 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001762 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001764 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001766 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001767 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001768
1769
1770 // The "argument" for the data item is incomplete
1771 // Positive integer missing 1 byte argument
1772 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1773 // Positive integer missing 2 byte argument
1774 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1775 // Positive integer missing 4 byte argument
1776 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1777 // Positive integer missing 8 byte argument
1778 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1779 // Positive integer missing 1 byte of 2 byte argument
1780 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1781 // Positive integer missing 2 bytes of 4 byte argument
1782 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1783 // Positive integer missing 1 bytes of 7 byte argument
1784 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1785 // Negative integer missing 1 byte argument
1786 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1787 // Binary string missing 1 byte argument
1788 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1789 // Text string missing 1 byte argument
1790 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1791 // Array missing 1 byte argument
1792 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1793 // Map missing 1 byte argument
1794 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1795 // Tag missing 1 byte argument
1796 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1797 // Simple missing 1 byte argument
1798 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1799
1800
1801 // Breaks must not occur in definite length arrays and maps
1802 // Array of length 1 with sole member replaced by a break
1803 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1804 // Array of length 2 with 2nd member replaced by a break
1805 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1806 // Map of length 1 with sole member label replaced by a break
1807 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1808 // Map of length 1 with sole member label replaced by break
1809 // Alternate representation that some decoders handle difference
1810 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1811 // Array of length 1 with 2nd member value replaced by a break
1812 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1813 // Map of length 2 with 2nd member replaced by a break
1814 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1815
1816
1817 // Breaks must not occur on their own out of an indefinite length data item
1818 // A bare break is not well formed
1819 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1820 // A bare break after a zero length definite length array
1821 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1822 // A bare break after a zero length indefinite length map
1823 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1824
1825
1826 // Forbidden two byte encodings of simple types
1827 // Must use 0xe0 instead
1828 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1829 // Should use 0xe1 instead
1830 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1831 // Should use 0xe2 instead
1832 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1833 // Should use 0xe3 instead
1834 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1835 // Should use 0xe4 instead
1836 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1837 // Should use 0xe5 instead
1838 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1839 // Should use 0xe6 instead
1840 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1841 // Should use 0xe7 instead
1842 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1843 // Should use 0xe8 instead
1844 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1845 // Should use 0xe9 instead
1846 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1847 // Should use 0xea instead
1848 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1849 // Should use 0xeb instead
1850 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1851 // Should use 0xec instead
1852 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1853 // Should use 0xed instead
1854 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1855 // Should use 0xee instead
1856 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1857 // Should use 0xef instead
1858 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1859 // Should use 0xf0 instead
1860 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1861 // Should use 0xf1 instead
1862 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1863 // Should use 0xf2 instead
1864 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1865 // Must use 0xf3 instead
1866 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1867 // Must use 0xf4 instead
1868 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1869 // Must use 0xf5 instead
1870 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1871 // Must use 0xf6 instead
1872 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1873 // Must use 0xf7 instead
1874 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1875 // Must use 0xf8 instead
1876 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1877
1878
1879 // Integers with additional info indefinite length
1880 // Positive integer with additional info indefinite length
1881 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1882 // Negative integer with additional info indefinite length
1883 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1884 // CBOR tag with "argument" an indefinite length
1885 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1886 // CBOR tag with "argument" an indefinite length alternate vector
1887 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1888
1889
1890 // Missing bytes from a deterministic length string
1891 // A byte string is of length 1 without the 1 byte
1892 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1893 // A text string is of length 1 without the 1 byte
1894 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001895 // Byte string should have 2^32-15 bytes, but has one
1896 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1897 // Byte string should have 2^32-15 bytes, but has one
1898 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001899
1900
1901 // Use of unassigned additional information values
1902 // Major type positive integer with reserved value 28
1903 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1904 // Major type positive integer with reserved value 29
1905 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1906 // Major type positive integer with reserved value 30
1907 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1908 // Major type negative integer with reserved value 28
1909 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1910 // Major type negative integer with reserved value 29
1911 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1912 // Major type negative integer with reserved value 30
1913 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1914 // Major type byte string with reserved value 28 length
1915 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1916 // Major type byte string with reserved value 29 length
1917 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1918 // Major type byte string with reserved value 30 length
1919 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1920 // Major type text string with reserved value 28 length
1921 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1922 // Major type text string with reserved value 29 length
1923 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1924 // Major type text string with reserved value 30 length
1925 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1926 // Major type array with reserved value 28 length
1927 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1928 // Major type array with reserved value 29 length
1929 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1930 // Major type array with reserved value 30 length
1931 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1932 // Major type map with reserved value 28 length
1933 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1934 // Major type map with reserved value 29 length
1935 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1936 // Major type map with reserved value 30 length
1937 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1938 // Major type tag with reserved value 28 length
1939 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1940 // Major type tag with reserved value 29 length
1941 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1942 // Major type tag with reserved value 30 length
1943 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1944 // Major type simple with reserved value 28 length
1945 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1946 // Major type simple with reserved value 29 length
1947 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1948 // Major type simple with reserved value 30 length
1949 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1950
1951
1952 // Maps must have an even number of data items (key & value)
1953 // Map with 1 item when it should have 2
1954 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1955 // Map with 3 item when it should have 4
1956 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1957 // Map with 1 item when it should have 2
1958 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1959 // Map with 3 item when it should have 4
1960 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1961
1962
1963 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001964 // Text-based date, with an integer
1965 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1966 // Epoch date, with an byte string
1967 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1968 // tagged as both epoch and string dates
1969 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1970 // big num tagged an int, not a byte string
1971 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001972};
1973
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001974int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001975{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001976 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001977
Laurence Lundblade59289e52019-12-30 13:44:37 -08001978 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1979 if(nResult) {
1980 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001982
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001983 // Corrupt the UsefulInputBuf and see that
1984 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001985 QCBORDecodeContext DCtx;
1986 QCBORItem Item;
1987 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001988
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001989 QCBORDecode_Init(&DCtx,
1990 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1991 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001993 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1994 return (int32_t)uQCBORError;
1995 }
1996 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
1997 // This wasn't supposed to happen
1998 return -1;
1999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002000
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002001 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002002
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002003 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2004 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2005 // Did not get back the error expected
2006 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002008
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002009/*
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002010 TODO: fix this
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002011 This test is disabled until QCBOREncode_EncodeHead() is brought in so
2012 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
2013
2014 This relies on the largest string allowed being SIZE_MAX -4 rather than
2015 SIZE_MAX. That way the test can be performed.
2016 {
2017 QCBORDecodeContext DCtx;
2018 QCBORItem Item;
2019
2020 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
2021 0xff, 0xff, 0xff, 0xff};
2022
2023 QCBORDecode_Init(&DCtx,
2024 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
2025 QCBOR_DECODE_MODE_NORMAL);
2026
2027 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2028 return -4;
2029 }
2030 }
2031*/
2032
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002033 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002034}
2035
2036
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002037/* Try all 256 values of the byte at nLen including recursing for
2038 each of the values to try values at nLen+1 ... up to nLenMax
2039 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002040static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002041{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002042 if(nLen >= nLenMax) {
2043 return;
2044 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002045
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002046 for(int inputByte = 0; inputByte < 256; inputByte++) {
2047 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002048 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002049 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002050
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002051 // Get ready to parse
2052 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002053 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002054
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002055 // Parse by getting the next item until an error occurs
2056 // Just about every possible decoder error can occur here
2057 // The goal of this test is not to check for the correct
2058 // error since that is not really possible. It is to
2059 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002060 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002061 QCBORItem Item;
2062 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002063 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002064 break;
2065 }
2066 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002067
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002068 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002069 }
2070}
2071
2072
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002073int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002074{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002075 // Size 2 tests 64K inputs and runs quickly
2076 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002077
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002078 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002079
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080 return 0;
2081}
2082
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002083
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002084int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002085{
2086 // size 3 tests 16 million inputs and runs OK
2087 // in seconds on fast machines. Size 4 takes
2088 // 10+ minutes and 5 half a day on fast
2089 // machines. This test is kept separate from
2090 // the others so as to no slow down the use
2091 // of them as a very frequent regression.
2092 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002093
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002094 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002095
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002096 return 0;
2097}
2098
2099
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002100static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002101 0xc0, // tag for string date
2102 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002103
Laurence Lundbladec7114722020-08-13 05:11:40 -07002104 0xc0, // tag for string date
2105 0x00, // Wrong type for a string date
2106
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002107 0xc1, // tag for epoch date
2108 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2109
Laurence Lundbladec7114722020-08-13 05:11:40 -07002110 0xc1,
2111 0x62, 'h', 'i', // wrong type tagged
2112
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002113 // CBOR_TAG_B64
Laurence Lundblade8fa7d5d2020-07-11 16:30:47 -07002114 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002115 0x1a, 0x53, 0x72, 0x4E, 0x01,
2116
2117 0xc1, // tag for epoch date
2118 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002120 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002121 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002123 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002124 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002125
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002126 0xc1, // tag for epoch date
2127 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2128 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2129
2130 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002131 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2132
2133 0xc1, // tag for epoch date
2134 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2135
2136 0xc1,
2137 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2138
2139 0xc1, // tag for epoch date
2140 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002141};
2142
2143
Laurence Lundbladec7114722020-08-13 05:11:40 -07002144
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002145// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002146#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002147static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002148
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002149 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002150
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002151 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002152
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002153 return diff > 0.0000001;
2154}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002155#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002156
2157
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002158int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002159{
2160 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002161 QCBORItem Item;
2162 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002163
Laurence Lundbladeee851742020-01-08 08:37:05 -08002164 QCBORDecode_Init(&DCtx,
2165 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2166 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002167
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002168 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002169 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002170 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002171 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002172 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002173 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002174 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002175 }
2176
Laurence Lundbladec7114722020-08-13 05:11:40 -07002177 // Wrong type for a string date
2178 uError = QCBORDecode_GetNext(&DCtx, &Item);
2179 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002180 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002181 }
2182
2183 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2184 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2185 return -4;
2186 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002187 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2188 Item.val.epochDate.nSeconds != 1400000000 ||
2189 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002190 return -5;
2191 }
2192
2193 // Wrong type for an epoch date
2194 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2195 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002196 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002197
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002198 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2199 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002200 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2201 return -7;
2202 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002203 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2204 Item.val.epochDate.nSeconds != 1400000001 ||
2205 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002206 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002207 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002209
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002210 // Epoch date that is too large for our representation
2211 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002212 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002214
Laurence Lundblade9682a532020-06-06 18:33:04 -07002215#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002216 // Epoch date in float format with fractional seconds
2217 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2218 return -10;
2219 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002220 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2221 Item.val.epochDate.nSeconds != 1 ||
2222 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002223 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002225
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002226 // Epoch date float that is too large for our representation
2227 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002228 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002230
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002231 // Epoch date double that is just slightly too large
2232 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002233 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002234 }
2235
2236 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002237 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2238 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2239 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2240 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002241 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002242 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002243
2244 // Nan
2245 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2246 return -15;
2247 }
2248
2249 // +Inifinity double-precision
2250 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2251 return -16;
2252 }
2253
2254#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2255 // -Inifinity half-precision
2256 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2257 return -17;
2258 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002259#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002260 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2261 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002262 }
2263#endif
2264
Laurence Lundbladec7114722020-08-13 05:11:40 -07002265#else
2266 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2267 return -19;
2268 }
2269 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2270 return -20;
2271 }
2272 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2273 return -21;
2274 }
2275 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2276 return -22;
2277 }
2278 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2279 return -23;
2280 }
2281 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2282 return -24;
2283 }
2284#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2285 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2286 return -25;
2287 }
2288#else
2289 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2290 return -26;
2291 }
2292#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002293
Laurence Lundbladec7114722020-08-13 05:11:40 -07002294#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002295
2296 return 0;
2297}
2298
Laurence Lundbladec7114722020-08-13 05:11:40 -07002299
2300static uint8_t spDateTestInput2[] = {
2301 0xbf,
2302
2303 0x05,
2304 0xc1, // tag for epoch date
2305 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
2306
2307 0x00,
2308 0xc0, // tag for string date
2309 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2310
2311 0x01,
2312 0xc1, // tag for epoch date
2313 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2314
2315 // Untagged integer 0
2316 0x08,
2317 0x00,
2318
2319 // Utagged date string with string label y
2320 0x61, 0x79,
2321 0x6a, '2','0','8','5','-','0','4','-','1','2', // Date string
2322
2323 // Untagged -1000 with label z
2324 0x61, 0x7a,
2325 0x39, 0x03, 0xe7,
2326
2327#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2328 0x07,
2329 0xc1, // tag for epoch date
2330 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2331
2332 // Untagged single-precision float with value 3.14 with string label x
2333 0x61, 0x78,
2334 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2335
2336#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2337 // Untagged half-precision float with value -2
2338 0x09,
2339 0xF9, 0xC0, 0x00,
2340#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2341#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2342
2343 0xff,
2344};
2345
2346int32_t SpiffyDateDecodeTest()
2347{
2348 QCBORDecodeContext DC;
2349 //QCBORItem Item;
2350 QCBORError uError;
2351 int64_t nEpochDate1, nEpochDate3, nEpochDate6;
2352 UsefulBufC StringDate1, StringDate2;
2353
2354 QCBORDecode_Init(&DC,
2355 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput2),
2356 QCBOR_DECODE_MODE_NORMAL);
2357 QCBORDecode_EnterMap(&DC);
2358 QCBORDecode_GetEpochDateInMapN(&DC, 5, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nEpochDate1);
2359 uError = QCBORDecode_GetAndResetError(&DC);
2360 if(uError == QCBOR_SUCCESS) {
2361 return -999;
2362 }
2363 QCBORDecode_GetEpochDateInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nEpochDate1);
2364 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &StringDate1);
2365 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NO_TAG, &nEpochDate3);
2366 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NO_TAG, &StringDate2);
2367 QCBORDecode_GetEpochDateInMapSZ(&DC, "z", QCBOR_TAG_REQUIREMENT_NO_TAG, &nEpochDate6);
2368#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2369 int64_t nEpochDate5, nEpochDate2;
2370 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &nEpochDate2);
2371 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &nEpochDate5);
2372#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2373 int64_t nEpochDate4;
2374 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &nEpochDate4);
2375#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2376#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2377
2378 QCBORDecode_ExitMap(&DC);
2379 uError = QCBORDecode_Finish(&DC);
2380 if(uError) {
2381 return 1;
2382 }
2383
2384 if(nEpochDate1 != 1400000000) {
2385 return 100;
2386 }
2387
2388 if(nEpochDate3 != 0) {
2389 return 102;
2390 }
2391
2392 if(nEpochDate6 != -1000) {
2393 return 105;
2394 }
2395
2396 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2397 return 106;
2398 }
2399
2400 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2401 return 107;
2402 }
2403
2404#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2405 if(nEpochDate2 != 9223372036854773760ULL) {
2406 return 101;
2407 }
2408 if(nEpochDate5 != 3) {
2409 return 104;
2410 }
2411#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2412 if(nEpochDate4 != -2) {
2413 return 103;
2414 }
2415#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2416#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2417
2418 return 0;
2419}
2420
2421
2422
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002423// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002424static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002425 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002426 0x81, // Array of one
2427 0xd8, 0x04, // non-preferred serialization of tag 4
2428 0x82, 0x01, 0x03}; // fraction 1/3
2429
Laurence Lundblade59289e52019-12-30 13:44:37 -08002430/*
2431 DB 9192939495969798 # tag(10489608748473423768)
2432 80 # array(0)
2433 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002434static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2435 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002436
Laurence Lundblade59289e52019-12-30 13:44:37 -08002437/*
2438DB 9192939495969798 # tag(10489608748473423768)
2439 D8 88 # tag(136)
2440 C6 # tag(6)
2441 C7 # tag(7)
2442 80 # array(0)
2443*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002444static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2445 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002446
2447/*
2448 The cbor.me parse of this.
2449 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002450 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 +07002451 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2452 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2453 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2454 */
2455static uint8_t spCSRWithTags[] = {
2456 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2457 0xc6, 0xc7, 0x36,
2458 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2459 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2460 0xcb, 0xa5,
2461 0xd1, 0x31,
2462 0xd1, 0xd1, 0xd1, 0x6c,
2463 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2464 0xc9, 0x30,
2465 0xd9, 0x03, 0x05, 0x63,
2466 0x53, 0x53, 0x47,
2467 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002468 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002469 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2470 0xd1, 0x2f,
2471 0xd1, 0x69,
2472 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2473 0xd1, 0x2d,
2474 0xd1, 0x62,
2475 0x55, 0x53,
2476 0xd7, 0x32,
2477 0xd3, 0xa2,
2478 0x2a,
2479 0xc9, 0xa1,
2480 0x28,
2481 0x26,
2482 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2483 0xcc, 0x4a,
2484 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2485 0xd0, 0x35,
2486 0xd7, 0xa1,
2487 0xcb, 0xc8, 0xc7, 0x24,
2488 0xc8, 0x22};
2489
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002490static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002491
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002492
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002493int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002494{
2495 QCBORDecodeContext DCtx;
2496 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002497
Laurence Lundbladeee851742020-01-08 08:37:05 -08002498 QCBORDecode_Init(&DCtx,
2499 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2500 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002501
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002502 //-------------------------
2503 // This text matches the magic number tag and the fraction tag
2504 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2505 return -2;
2506 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002507 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002508 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2509 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002510 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002511
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002512 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2513 return -4;
2514 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002515
2516#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002517 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002518 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002519 Item.val.uCount != 2) {
2520 return -5;
2521 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002522#else
2523 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2524 return -6;
2525 }
2526#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002527
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002528 // --------------------------------
2529 // This test decodes the very large tag, but it is not in
2530 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002531 QCBORDecode_Init(&DCtx,
2532 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2533 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002534 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2535 return -6;
2536 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002537 /*
2538 if(Item.uTagBits) { // TODO: make sure it is OK to remove this
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002539 return -7;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002540 }*/
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002541
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002542 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002543 // This test sets up a caller-config list that includes the very large
2544 // tage and then matches it.
2545 QCBORDecode_Init(&DCtx,
2546 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2547 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002548 const uint64_t puList[] = {0x9192939495969798, 257};
2549 const QCBORTagListIn TL = {2, puList};
2550 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002551
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002552 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2553 return -8;
2554 }
2555 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2556 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2557 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2558 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2559 Item.val.uCount != 0) {
2560 return -9;
2561 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002562
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002563 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002564 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002565 const uint64_t puLongList[17] = {1,2,1};
2566 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002567 QCBORDecode_Init(&DCtx,
2568 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2569 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002570 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2571 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2572 return -11;
2573 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002574
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002575 // -----------------------
2576 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002577 QCBORDecode_Init(&DCtx,
2578 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2579 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002580 uint64_t puTags[16];
2581 QCBORTagListOut Out = {0, 4, puTags};
2582 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2583 return -12;
2584 }
2585 if(puTags[0] != 0x9192939495969798 ||
2586 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002587 puTags[2] != 0x06 ||
2588 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002589 return -13;
2590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002591
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002592 // ----------------------
2593 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002594 QCBORDecode_Init(&DCtx,
2595 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2596 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002597 QCBORTagListOut OutSmall = {0, 3, puTags};
2598 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2599 return -14;
2600 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002601
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002602#if 0
2603 // TODO: this test needs to be re evaluated
2604
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002605 // ---------------
2606 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002607 QCBORDecode_Init(&DCtx,
2608 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2609 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002610 int n = CheckCSRMaps(&DCtx);
2611 if(n) {
2612 return n-2000;
2613 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002614
Laurence Lundblade59289e52019-12-30 13:44:37 -08002615 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002616 QCBORDecode_Init(&DCtx,
2617 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2618 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002619
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002620 const uint64_t puTagList[] = {773, 1, 90599561};
2621 const QCBORTagListIn TagList = {3, puTagList};
2622 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002623
2624
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002625 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2626 return -100;
2627 }
2628 if(Item.uDataType != QCBOR_TYPE_MAP ||
2629 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2630 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2631 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2632 Item.val.uCount != 2 ||
2633 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2634 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2635 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2636 Out.uNumUsed != 3) {
2637 return -101;
2638 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002639
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002640 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2641 return -102;
2642 }
2643 if(Item.uDataType != QCBOR_TYPE_MAP ||
2644 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2645 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2646 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2647 Item.val.uCount != 2 ||
2648 puTags[0] != 5859837686836516696 ||
2649 puTags[1] != 7 ||
2650 Out.uNumUsed != 2) {
2651 return -103;
2652 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002653
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002654 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2655 return -104;
2656 }
2657 if(Item.uDataType != QCBOR_TYPE_MAP ||
2658 Item.uTagBits ||
2659 Item.val.uCount != 5 ||
2660 puTags[0] != 0x0b ||
2661 Out.uNumUsed != 1) {
2662 return -105;
2663 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002664
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002665 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2666 return -106;
2667 }
2668 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2669 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2670 Item.val.string.len != 12 ||
2671 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2672 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2673 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2674 Out.uNumUsed != 3) {
2675 return -105;
2676 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002677
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002678 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2679 return -107;
2680 }
2681 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2682 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2683 Item.val.string.len != 3 ||
2684 puTags[0] != 773 ||
2685 Out.uNumUsed != 1) {
2686 return -108;
2687 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002688
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002689 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2690 return -109;
2691 }
2692 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002693 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002694 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002695 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002696 puTags[11] != 0x0f ||
2697 Out.uNumUsed != 12) {
2698 return -110;
2699 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002700
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002701 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2702 return -111;
2703 }
2704 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2705 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2706 Item.val.string.len != 9 ||
2707 puTags[0] != 17 ||
2708 Out.uNumUsed != 1) {
2709 return -112;
2710 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002711
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002712 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2713 return -111;
2714 }
2715 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2716 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2717 Item.val.string.len != 2 ||
2718 puTags[0] != 17 ||
2719 Out.uNumUsed != 1) {
2720 return -112;
2721 }
2722
2723 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2724 return -113;
2725 }
2726 if(Item.uDataType != QCBOR_TYPE_MAP ||
2727 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2728 Item.val.uCount != 2 ||
2729 puTags[0] != 19 ||
2730 Out.uNumUsed != 1) {
2731 return -114;
2732 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002733
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002734 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2735 return -115;
2736 }
2737 if(Item.uDataType != QCBOR_TYPE_MAP ||
2738 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2739 Item.uTagBits ||
2740 Item.val.uCount != 1 ||
2741 puTags[0] != 9 ||
2742 Out.uNumUsed != 1) {
2743 return -116;
2744 }
2745
2746 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2747 return -116;
2748 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002749 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002750 Item.val.int64 != -7 ||
2751 Item.uTagBits ||
2752 Out.uNumUsed != 0) {
2753 return -117;
2754 }
2755
2756 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2757 return -118;
2758 }
2759 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2760 Item.val.string.len != 10 ||
2761 Item.uTagBits ||
2762 puTags[0] != 12 ||
2763 Out.uNumUsed != 1) {
2764 return -119;
2765 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002766
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002767 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2768 return -120;
2769 }
2770 if(Item.uDataType != QCBOR_TYPE_MAP ||
2771 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2772 Item.val.uCount != 1 ||
2773 puTags[0] != 0x17 ||
2774 Out.uNumUsed != 1) {
2775 return -121;
2776 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002777
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002778 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2779 return -122;
2780 }
2781 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2782 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2783 Item.val.int64 != -3 ||
2784 puTags[0] != 8 ||
2785 Out.uNumUsed != 1) {
2786 return -123;
2787 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002788
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002789 if(QCBORDecode_Finish(&DCtx)) {
2790 return -124;
2791 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002792#else
2793 (void)spCSRWithTags;
2794#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002795 return 0;
2796}
2797
2798
2799
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002800
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002801static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002802 0x83,
2803 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2804 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2805 0xA4,
2806 0x63, 0x42, 0x4E, 0x2B,
2807 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2808 0x18, 0x40,
2809 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2810 0x63, 0x42, 0x4E, 0x2D,
2811 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2812 0x38, 0x3F,
2813 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2814
2815
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002816static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002817
2818
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002819int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002820{
2821 QCBORDecodeContext DCtx;
2822 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002823 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002824
Laurence Lundbladeee851742020-01-08 08:37:05 -08002825 QCBORDecode_Init(&DCtx,
2826 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2827 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002828
2829
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002830 //
2831 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2832 return -1;
2833 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002834 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002835 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002836
2837 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002838 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002839 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002840 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002841 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002842 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002843 }
2844
2845 //
2846 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002847 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002848 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002849 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002850 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002851 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002852
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002853 //
2854 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002855 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002856 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002857 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002858 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002859
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002860 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002861 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002862 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2863 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002864 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002865 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002866 }
2867
2868 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002869 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002870 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2871 Item.uLabelType != QCBOR_TYPE_INT64 ||
2872 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002873 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002874 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002875 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002876
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002877 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002878 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002879 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2880 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002881 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002882 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002883 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002884
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002885 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002886 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002887 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2888 Item.uLabelType != QCBOR_TYPE_INT64 ||
2889 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002890 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002891 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002892 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002893
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002894 return 0;
2895}
2896
2897
2898
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002899static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002900 uint8_t uDataType,
2901 uint8_t uNestingLevel,
2902 uint8_t uNextNest,
2903 int64_t nLabel,
2904 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002905{
2906 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002907 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002908
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002909 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2910 if(Item.uDataType != uDataType) return -1;
2911 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002912 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2913 Item.uLabelType != QCBOR_TYPE_UINT64) {
2914 return -1;
2915 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002916 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2917 if(Item.label.int64 != nLabel) return -1;
2918 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002919 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002920 }
2921 }
2922 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302923 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002924
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002925 if(pItem) {
2926 *pItem = Item;
2927 }
2928 return 0;
2929}
2930
2931
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002932// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002933static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002934{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302935 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002936
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302937 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002938
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302939 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002940
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302941 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2942 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2943 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2944 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2945 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002946
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302947 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2948 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002949
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302950 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2951 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002952
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302953 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2954 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002955
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002956 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002957
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002958 return 0;
2959}
2960
2961
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002962/*
2963// cbor.me decoded output
2964{
2965 -23: {
2966 -20: {
2967 -18: "Organization",
2968 -17: "SSG",
2969 -15: "Confusion",
2970 -16: "San Diego",
2971 -14: "US"
2972 },
2973 -19: {
2974 -11: {
2975 -9: -7
2976 },
2977 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2978 }
2979 },
2980 -22: {
2981 -5: -3
2982 }
2983}
2984 */
2985
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002986
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002987static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002988 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2989 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2990 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2991 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2992 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2993 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2994 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2995 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2996 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2997
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002998int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002999{
3000 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003001
Laurence Lundbladeee851742020-01-08 08:37:05 -08003002 QCBORDecode_Init(&DCtx,
3003 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3004 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003005
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003006 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003007}
3008
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003009
3010
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003011int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003012{
3013 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003014
Laurence Lundbladeee851742020-01-08 08:37:05 -08003015 QCBORDecode_Init(&DCtx,
3016 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3017 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003018
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003019 QCBORItem Item;
3020 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003021
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003022 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3023 return -1;
3024 }
3025 if(Item.uDataType != QCBOR_TYPE_MAP) {
3026 return -2;
3027 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003028
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003029 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3030 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3031 return -3;
3032 }
3033
3034 return 0;
3035}
3036
3037
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003038// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003039static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003040 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3041 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3042 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3043 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3044 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3045 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003046 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3047 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3048 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3049 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003050
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003051int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003052{
3053 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003054
Laurence Lundbladeee851742020-01-08 08:37:05 -08003055 QCBORDecode_Init(&DCtx,
3056 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3057 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003058
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003059 return CheckCSRMaps(&DCtx);
3060}
3061
3062
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003063
Laurence Lundblade17ede402018-10-13 11:43:07 +08003064static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3065{
3066 UsefulOutBuf UOB;
3067 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003068
Laurence Lundblade17ede402018-10-13 11:43:07 +08003069 int i;
3070 for(i = 0; i < n; i++) {
3071 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3072 }
3073
3074 for(i = 0; i < n; i++) {
3075 UsefulOutBuf_AppendByte(&UOB, 0xff);
3076 }
3077 return UsefulOutBuf_OutUBuf(&UOB);
3078}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003079
3080
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003081static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003082{
3083 QCBORDecodeContext DC;
3084 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003085
Laurence Lundblade17ede402018-10-13 11:43:07 +08003086 int j;
3087 for(j = 0; j < nNestLevel; j++) {
3088 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003089 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003090 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3091 // Should be in error
3092 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
3093 return -4;
3094 } else {
3095 return 0; // Decoding doesn't recover after an error
3096 }
3097 } else {
3098 // Should be no error
3099 if(nReturn) {
3100 return -9; // Should not have got an error
3101 }
3102 }
3103 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3104 return -7;
3105 }
3106 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003107 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003108 if(nReturn) {
3109 return -3;
3110 }
3111 return 0;
3112}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003113
3114
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003115int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003116{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303117 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003118 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003119 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003120 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003121 int nReturn = parse_indeflen_nested(Nested, i);
3122 if(nReturn) {
3123 return nReturn;
3124 }
3125 }
3126 return 0;
3127}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003128
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003129
Laurence Lundbladeee851742020-01-08 08:37:05 -08003130// [1, [2, 3]]
3131static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3132// No closing break
3133static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3134// Not enough closing breaks
3135static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3136// Too many closing breaks
3137static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3138// Unclosed indeflen inside def len
3139static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3140// confused tag
3141static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003142
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003143int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003144{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003145 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003146 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003147 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003148
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003149 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303150 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003151 QCBORDecodeContext DC;
3152 QCBORItem Item;
3153 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003154
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003155 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003156
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003157 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303158
3159 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3160 Item.uNestingLevel != 0 ||
3161 Item.uNextNestLevel != 1) {
3162 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003164
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003165 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303166 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3167 Item.uNestingLevel != 1 ||
3168 Item.uNextNestLevel != 1) {
3169 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003171
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003172 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303173 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3174 Item.uNestingLevel != 1 ||
3175 Item.uNextNestLevel != 2) {
3176 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003178
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003179 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003180 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303181 Item.uNestingLevel != 2 ||
3182 Item.uNextNestLevel != 2) {
3183 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003184 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003185
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003186 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003187 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303188 Item.uNestingLevel != 2 ||
3189 Item.uNextNestLevel != 0) {
3190 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003191 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003192
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003193 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303194 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003195 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003196
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003197 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003198 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003199
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003200 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003201
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003202 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003203
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003204 nResult = QCBORDecode_GetNext(&DC, &Item);
3205 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303206 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003208
Laurence Lundblade570fab52018-10-13 18:28:27 +08003209 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303210 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3211 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003212 }
3213
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003214
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003215 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003216 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003217
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003218 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003219
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003220 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003221
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003222 nResult = QCBORDecode_GetNext(&DC, &Item);
3223 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303224 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003225 }
3226
3227 nResult = QCBORDecode_GetNext(&DC, &Item);
3228 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303229 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003231
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003232 nResult = QCBORDecode_GetNext(&DC, &Item);
3233 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303234 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003236
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003237 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303238 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3239 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003241
3242
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003243 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003244 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003245
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003246 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003247
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003248 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003249
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003250 nResult = QCBORDecode_GetNext(&DC, &Item);
3251 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303252 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003254
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003255 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003256 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303257 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003258 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303259
Laurence Lundblade642282a2020-06-23 12:00:33 -07003260 nResult = QCBORDecode_GetNext(&DC, &Item);
3261 if(nResult != QCBOR_ERR_BAD_BREAK) {
3262 return -140;
3263 }
3264
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003265
Laurence Lundblade570fab52018-10-13 18:28:27 +08003266 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003267 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003268
Laurence Lundblade570fab52018-10-13 18:28:27 +08003269 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003270
Laurence Lundblade570fab52018-10-13 18:28:27 +08003271 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003272
Laurence Lundblade570fab52018-10-13 18:28:27 +08003273 nResult = QCBORDecode_GetNext(&DC, &Item);
3274 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303275 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003277
Laurence Lundblade570fab52018-10-13 18:28:27 +08003278 nResult = QCBORDecode_GetNext(&DC, &Item);
3279 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303280 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003282
Laurence Lundblade570fab52018-10-13 18:28:27 +08003283 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303284 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3285 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003287
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303288 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003289 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003290
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303291 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003292
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303293 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003294
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303295 nResult = QCBORDecode_GetNext(&DC, &Item);
3296 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303297 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003299
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303300 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303301 if(nResult != QCBOR_ERR_BAD_BREAK) {
3302 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003304
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003305 return 0;
3306}
3307
Laurence Lundblade17ede402018-10-13 11:43:07 +08003308
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003309static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003310 0x81, // Array of length one
3311 0x7f, // text string marked with indefinite length
3312 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3313 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3314 0xff // ending break
3315};
3316
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003317static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303318 0x81, // Array of length one
3319 0x7f, // text string marked with indefinite length
3320 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3321 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3322 0xff // ending break
3323};
3324
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003325static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303326 0x81, // Array of length one
3327 0x7f, // text string marked with indefinite length
3328 0x01, 0x02, // Not a string
3329 0xff // ending break
3330};
3331
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003332static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303333 0x81, // Array of length one
3334 0x7f, // text string marked with indefinite length
3335 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3336 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3337 // missing end of string
3338};
3339
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003340static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303341 0xa1, // Array of length one
3342 0x7f, // text string marked with indefinite length
3343 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3344 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3345 0xff, // ending break
3346 0x01 // integer being labeled.
3347};
3348
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003349/**
3350 Make an indefinite length string
3351
3352 @param Storage Storage for string, must be 144 bytes in size
3353 @return The indefinite length string
3354
3355 This makes an array with one indefinite length string that has 7 chunks
3356 from size of 1 byte up to 64 bytes.
3357 */
3358static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303359{
3360 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003361
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303362 UsefulOutBuf_Init(&UOB, Storage);
3363 UsefulOutBuf_AppendByte(&UOB, 0x81);
3364 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003365
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003366 uint8_t uStringByte = 0;
3367 // Use of type int is intentional
3368 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3369 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303370 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003371 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3372 for(int j = 0; j < uChunkSize; j++) {
3373 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3374 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303375 }
3376 }
3377 UsefulOutBuf_AppendByte(&UOB, 0xff);
3378
3379 return UsefulOutBuf_OutUBuf(&UOB);
3380}
3381
3382static int CheckBigString(UsefulBufC BigString)
3383{
3384 if(BigString.len != 255) {
3385 return 1;
3386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003387
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303388 for(uint8_t i = 0; i < 255; i++){
3389 if(((const uint8_t *)BigString.ptr)[i] != i) {
3390 return 1;
3391 }
3392 }
3393 return 0;
3394}
3395
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303396
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003397int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303398{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303399 QCBORDecodeContext DC;
3400 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303401 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003402 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003403
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303404 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003405 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303406 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003407
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303408 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303409 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303410 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003411
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303412 if(QCBORDecode_GetNext(&DC, &Item)) {
3413 return -2;
3414 }
3415 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3416 return -3;
3417 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003418
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303419 if(QCBORDecode_GetNext(&DC, &Item)) {
3420 return -4;
3421 }
3422 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3423 return -5;
3424 }
3425 if(QCBORDecode_Finish(&DC)) {
3426 return -6;
3427 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303428
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303429 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003430 QCBORDecode_Init(&DC,
3431 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3432 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003433
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303434 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3435 return -7;
3436 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003437
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303438 if(QCBORDecode_GetNext(&DC, &Item)) {
3439 return -8;
3440 }
3441 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3442 return -9;
3443 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003444
Laurence Lundblade30816f22018-11-10 13:40:22 +07003445 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303446 return -10;
3447 }
3448
3449 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003450 QCBORDecode_Init(&DC,
3451 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3452 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003453
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303454 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3455 return -11;
3456 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003457
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303458 if(QCBORDecode_GetNext(&DC, &Item)) {
3459 return -12;
3460 }
3461 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3462 return -13;
3463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003464
Laurence Lundblade30816f22018-11-10 13:40:22 +07003465 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303466 return -14;
3467 }
3468
3469 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003470 QCBORDecode_Init(&DC,
3471 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3472 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003473
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303474 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3475 return -15;
3476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003477
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303478 if(QCBORDecode_GetNext(&DC, &Item)) {
3479 return -16;
3480 }
3481 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3482 return -17;
3483 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003484
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303485 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3486 return -18;
3487 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003488
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303489 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303490 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003491
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303492 QCBORDecode_GetNext(&DC, &Item);
3493 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303494 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303495 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003496
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303497 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303498 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303499 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003500
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303501 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003502 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303503
3504 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3505 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303506 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303507 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003508
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303509 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303510 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003511 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003512
Laurence Lundbladeee851742020-01-08 08:37:05 -08003513 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3514 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003515
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303516 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303517 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303518 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303519 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003520
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303521 QCBORDecode_GetNext(&DC, &Item);
3522 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303523 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303524 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003525 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303526 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303527 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003528
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303529 // ---- big bstr -----
3530 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003531
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303532 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3533 return -25;
3534 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003535
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303536 if(QCBORDecode_GetNext(&DC, &Item)) {
3537 return -26;
3538 }
3539 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303540 return -26;
3541 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003542
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303543 if(QCBORDecode_GetNext(&DC, &Item)) {
3544 return -27;
3545 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303546 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303547 return -28;
3548 }
3549 if(CheckBigString(Item.val.string)) {
3550 return -3;
3551 }
3552 if(QCBORDecode_Finish(&DC)) {
3553 return -29;
3554 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003555
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303556 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003557 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003558
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303559 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3560 return -30;
3561 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003562
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303563 QCBORDecode_GetNext(&DC, &Item);
3564 if(Item.uDataType != QCBOR_TYPE_MAP) {
3565 return -31;
3566 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003567
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303568 if(QCBORDecode_GetNext(&DC, &Item)){
3569 return -32;
3570 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003571 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3572 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303573 Item.uDataAlloc || !Item.uLabelAlloc ||
3574 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3575 return -33;
3576 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003577
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303578 if(QCBORDecode_Finish(&DC)) {
3579 return -34;
3580 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003581
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003582 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003583}
3584
3585
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003586int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303587{
3588 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003589 QCBORError nCBORError;
3590
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003591
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303592 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003593 QCBORDecode_Init(&DC,
3594 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3595 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003596
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003597 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003598
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003599 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3600 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303601 return -1;
3602 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003603
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003604 if(CheckCSRMaps(&DC)) {
3605 return -2;
3606 }
3607
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303608 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003609 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003610 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003611
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303612 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003613 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303614 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003615
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303616 QCBORItem Item1, Item2, Item3, Item4;
3617 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003618 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303619 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3620 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003621 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303622 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003623 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303624 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003625 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303626 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003627 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303628 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003629 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003630
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303631 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003632
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303633 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303634 Item1.uDataType != QCBOR_TYPE_INT64 ||
3635 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003636 Item1.uDataAlloc != 0 ||
3637 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003638 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003639 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003640 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003641
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303642
3643 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003644 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303645 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003646 Item2.uDataAlloc != 0 ||
3647 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303648 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003649 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003650
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303651 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003652 Item3.uDataAlloc == 0 ||
3653 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003654 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003655 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003656 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003657
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303658 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003659 Item4.uDataAlloc == 0 ||
3660 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003661 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003662 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003663 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003664
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303665 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003666 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003667 QCBORDecode_Init(&DC,
3668 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3669 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303670 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3671 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003672 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303673 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003674 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003675 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003676 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303677 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3678 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3679 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3680 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3681 }
3682 }
3683 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003684 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003685 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303686 }
3687
3688 return 0;
3689}
3690
Laurence Lundbladef6531662018-12-04 10:42:22 +09003691
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303692
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003693int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003694{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003695 // Set up the decoder with a tiny bit of CBOR to parse because
3696 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003697 QCBORDecodeContext DC;
3698 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3699 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003700
Laurence Lundbladef6531662018-12-04 10:42:22 +09003701 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003702 // Then fish into the internals of the decode context
3703 // to get the allocator function so it can be called directly.
3704 // Also figure out how much pool is available for use
3705 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003706 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003707 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3708 if(nError) {
3709 return -9;
3710 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003711 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3712 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3713 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003714
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003715 // First test -- ask for one more byte than available and see failure
3716 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003717 if(!UsefulBuf_IsNULL(Allocated)) {
3718 return -1;
3719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003720
Laurence Lundbladef6531662018-12-04 10:42:22 +09003721 // Re do the set up for the next test that will do a successful alloc,
3722 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003723 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003724 pAlloc = DC.StringAllocator.pfAllocator;
3725 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3726 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003727
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003728 // Allocate one byte less than available and see success
3729 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003730 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3731 return -2;
3732 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003733 // Ask for some more and see failure
3734 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003735 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3736 return -3;
3737 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003738 // Free the first allocate, retry the second and see success
3739 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3740 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003741 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3742 return -4;
3743 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003744
Laurence Lundbladef6531662018-12-04 10:42:22 +09003745 // Re do set up for next test that involves a successful alloc,
3746 // and a successful realloc and a failed realloc
3747 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003748 pAlloc = DC.StringAllocator.pfAllocator;
3749 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003750
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003751 // Allocate half the pool and see success
3752 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003753 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3754 return -5;
3755 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003756 // Reallocate to take up the whole pool and see success
3757 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003758 if(UsefulBuf_IsNULL(Allocated2)) {
3759 return -6;
3760 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003761 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003762 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3763 return -7;
3764 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003765 // Try to allocate more to be sure there is failure after a realloc
3766 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3767 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003768 return -8;
3769 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003770
Laurence Lundbladef6531662018-12-04 10:42:22 +09003771 return 0;
3772}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003773
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003774
3775/* Just enough of an allocator to test configuration of one */
3776static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3777{
3778 (void)pOldMem; // unused variable
3779
3780 if(uNewSize) {
3781 // Assumes the context pointer is the buffer and
3782 // nothing too big will ever be asked for.
3783 // This is only good for this basic test!
3784 return (UsefulBuf) {pCtx, uNewSize};
3785 } else {
3786 return NULLUsefulBuf;
3787 }
3788}
3789
3790
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003791int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003792{
3793 // Set up the decoder with a tiny bit of CBOR to parse because
3794 // nothing can be done with it unless that is set up.
3795 QCBORDecodeContext DC;
3796 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3797 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3798
3799 uint8_t pAllocatorBuffer[50];
3800
3801 // This is really just to test that this call works.
3802 // The full functionality of string allocators is tested
3803 // elsewhere with the MemPool internal allocator.
3804 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3805
3806 QCBORItem Item;
3807 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3808 return -1;
3809 }
3810
3811 if(Item.uDataAlloc == 0 ||
3812 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3813 Item.val.string.ptr != pAllocatorBuffer) {
3814 return -2;
3815 }
3816
3817 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3818 return -3;
3819 }
3820
3821 return 0;
3822}
3823
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003824#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08003825
Laurence Lundbladea826c502020-05-10 21:07:00 -07003826/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08003827 [
3828 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003829 4([-20, 4759477275222530853136]),
3830 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003831 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003832 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003833 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07003834 5([ 9223372036854775806, -4759477275222530853137])
3835 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003836 ]
3837 */
3838
3839static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003840 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003841 0xC4, 0x82, 0x20,
3842 0x03,
3843 0xC4, 0x82, 0x33,
3844 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3845 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3846 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3847 0xC5, 0x82, 0x19, 0x01, 0x2C,
3848 0x18, 0x64,
3849 0xC5, 0x82, 0x33,
3850 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3851 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3852 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07003853 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3854 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003855 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3856 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3857};
3858
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07003859
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003860int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003861{
3862 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003863 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003864 QCBORItem item;
3865
Laurence Lundblade17af4902020-01-07 19:11:55 -08003866 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3867 0x06, 0x07, 0x08, 0x09, 0x010};
3868 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003869
3870
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003871 QCBORDecode_Init(&DC,
3872 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
3873 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003874
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003875 uErr = QCBORDecode_GetNext(&DC, &item);
3876 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003877 return 1;
3878 }
3879
3880 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3881 return 2;
3882 }
3883
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003884 uErr = QCBORDecode_GetNext(&DC, &item);
3885 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003886 return 3;
3887 }
3888
3889 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3890 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3891 item.val.expAndMantissa.nExponent != -1) {
3892 return 4;
3893 }
3894
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003895 uErr = QCBORDecode_GetNext(&DC, &item);
3896 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003897 return 5;
3898 }
3899
3900 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3901 item.val.expAndMantissa.nExponent != -20 ||
3902 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3903 return 6;
3904 }
3905
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003906 uErr = QCBORDecode_GetNext(&DC, &item);
3907 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003908 return 7;
3909 }
3910
3911 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3912 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3913 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3914 return 8;
3915 }
3916
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003917 uErr = QCBORDecode_GetNext(&DC, &item);
3918 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003919 return 9;
3920 }
3921
3922 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3923 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3924 item.val.expAndMantissa.nExponent != 300) {
3925 return 10;
3926 }
3927
Laurence Lundbladea826c502020-05-10 21:07:00 -07003928 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003929 uErr = QCBORDecode_GetNext(&DC, &item);
3930 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003931 return 11;
3932 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003933 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3934 item.val.expAndMantissa.nExponent != -20 ||
3935 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3936 return 12;
3937 }
3938
Laurence Lundbladea826c502020-05-10 21:07:00 -07003939 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003940 uErr = QCBORDecode_GetNext(&DC, &item);
3941 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08003942 return 13;
3943 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003944 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3945 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3946 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3947 return 14;
3948 }
3949
Laurence Lundbladea826c502020-05-10 21:07:00 -07003950 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003951 uErr = QCBORDecode_GetNext(&DC, &item);
3952 if(uErr != QCBOR_SUCCESS) {
3953 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07003954 }
3955 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3956 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3957 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003958 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07003959 }
3960
Laurence Lundbladea826c502020-05-10 21:07:00 -07003961 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003962 uErr = QCBORDecode_GetNext(&DC, &item);
3963 if(uErr != QCBOR_SUCCESS) {
3964 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003965 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003966 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3967 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3968 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003969 return 18;
3970 }
3971
3972 uErr = QCBORDecode_Finish(&DC);
3973 if(uErr != QCBOR_SUCCESS) {
3974 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003975 }
3976
3977 /* Now encode some stuff and then decode it */
3978 uint8_t pBuf[40];
3979 QCBOREncodeContext EC;
3980 UsefulBufC Encoded;
3981
3982 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3983 QCBOREncode_OpenArray(&EC);
3984 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3985 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3986 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3987 QCBOREncode_CloseArray(&EC);
3988 QCBOREncode_Finish(&EC, &Encoded);
3989
3990
3991 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003992 uErr = QCBORDecode_GetNext(&DC, &item);
3993 if(uErr != QCBOR_SUCCESS) {
3994 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003995 }
3996
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07003997 uErr = QCBORDecode_GetNext(&DC, &item);
3998 if(uErr != QCBOR_SUCCESS) {
3999 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004000 }
4001
4002 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4003 item.val.expAndMantissa.nExponent != 1000 ||
4004 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004005 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004006 }
4007
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004008 uErr = QCBORDecode_GetNext(&DC, &item);
4009 if(uErr != QCBOR_SUCCESS) {
4010 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004011 }
4012
4013 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4014 item.val.expAndMantissa.nExponent != INT32_MIN ||
4015 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004016 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004017 }
4018
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004019 uErr = QCBORDecode_GetNext(&DC, &item);
4020 if(uErr != QCBOR_SUCCESS) {
4021 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004022 }
4023
4024 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4025 item.val.expAndMantissa.nExponent != INT32_MAX ||
4026 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004027 return 106;
4028 }
4029
4030
4031 int64_t nExp, nMant;
4032 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4033 UsefulBufC Mant;
4034 bool bIsNeg;
4035
4036 QCBORDecode_Init(&DC,
4037 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4038 QCBOR_DECODE_MODE_NORMAL);
4039 QCBORDecode_EnterArray(&DC);
4040
4041 // 4([-1, 3]),
4042 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nExp, &nMant);
4043
4044 // 4([-20, 4759477275222530853136]),
4045 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4046
4047 // 4([9223372036854775807, -4759477275222530853137]),
4048 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4049
4050 // 5([300, 100]),
4051 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &nExp, &nMant);
4052
4053 // 5([-20, 4759477275222530853136]),
4054 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4055
4056 // 5([-9223372036854775807, -4759477275222530853137])
4057 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4058
4059 // 5([ 9223372036854775806, -4759477275222530853137])
4060 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4061
4062 // 5([ 9223372036854775806, 9223372036854775806])]
4063 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_MATCH_TAG, MantBuf, &Mant, &bIsNeg, &nExp);
4064
4065 QCBORDecode_ExitArray(&DC);
4066
4067 uErr = QCBORDecode_Finish(&DC);
4068 if(uErr != QCBOR_SUCCESS) {
4069 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004070 }
4071
4072 return 0;
4073}
4074
4075
4076static struct FailInput ExponentAndMantissaFailures[] = {
4077 // Exponent > INT64_MAX
4078 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4079 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4080 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4081 // Mantissa > INT64_MAX
4082 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4083 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4084 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4085 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004086 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004087 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004088 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004089 // bad content for big num
4090 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4091 // bad content for big num
4092 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4093 // Bad integer for exponent
4094 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4095 // Bad integer for mantissa
4096 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4097 // 3 items in array
4098 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4099 // unterminated indefinite length array
4100 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4101 // Empty array
4102 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4103 // Second is not an integer
4104 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4105 // First is not an integer
4106 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4107 // Not an array
4108 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4109};
4110
4111
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004112int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004113{
4114 return ProcessFailures(ExponentAndMantissaFailures,
4115 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4116}
4117
4118#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004119
4120
4121
4122/*
4123 Some basic CBOR with map and array used in a lot of tests.
4124 The map labels are all strings
4125
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004126 {
4127 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004128 "an array of two strings": [
4129 "string1", "string2"
4130 ],
4131 "map in a map": {
4132 "bytes 1": h'78787878',
4133 "bytes 2": h'79797979',
4134 "another int": 98,
4135 "text 2": "lies, damn lies and statistics"
4136 }
4137 }
4138 */
4139
Laurence Lundblade1341c592020-04-11 14:19:05 -07004140#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004141
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004142static char strbuf[10];
4143const char *PrintType(uint8_t type) {
4144 switch(type) {
4145 case QCBOR_TYPE_INT64: return "INT64";
4146 case QCBOR_TYPE_UINT64: return "UINT64";
4147 case QCBOR_TYPE_ARRAY: return "ARRAY";
4148 case QCBOR_TYPE_MAP: return "MAP";
4149 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
4150 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
4151 default:
4152 sprintf(strbuf, "%d", type);
4153 return strbuf;
4154 }
4155}
4156
4157
4158void PrintItem(QCBORItem Item)
4159{
4160 printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":"");
4161 if(Item.uLabelType) {
4162 printf("Label: %s ", PrintType(Item.uLabelType));
4163 if(Item.uLabelType == QCBOR_TYPE_INT64) {
4164 printf("%lld\n", Item.label.int64);
4165 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07004166 // TODO: proper conversion to null-terminated string
4167 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004168 }
4169 }
4170}
4171
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004172
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004173int32_t EMap(UsefulBufC input)
4174{
4175 QCBORItem Item1, Item2, Item3;
4176 int64_t nDecodedInt1, nDecodedInt2;
4177 UsefulBufC B1, B2, S1, S2, S3;
4178
4179 QCBORDecodeContext DCtx;
4180 QCBORError nCBORError;
4181
4182 QCBORDecode_Init(&DCtx, input, 0);
4183
4184 QCBORDecode_EnterMap(&DCtx);
4185
4186 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4187
4188 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4189 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4190 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1);
4191 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2);
4192 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
4193 QCBORDecode_ExitMap(&DCtx);
4194
4195 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4196 QCBORDecode_GetNext(&DCtx, &Item1);
4197 QCBORDecode_GetNext(&DCtx, &Item2);
4198 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4199 return -400;
4200 }
4201 QCBORDecode_ExitArray(&DCtx);
4202
4203 // Parse the same array again using GetText() instead of GetItem()
4204 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4205 QCBORDecode_GetText(&DCtx, &S2);
4206 QCBORDecode_GetText(&DCtx, &S3);
4207 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4208 return 5000;
4209 }
4210 /* QCBORDecode_GetText(&DCtx, &S3);
4211 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4212 return 5001;
4213 } */
4214
4215 QCBORDecode_ExitArray(&DCtx);
4216
4217 QCBORDecode_ExitMap(&DCtx);
4218
4219 nCBORError = QCBORDecode_Finish(&DCtx);
4220
4221 if(nCBORError) {
4222 return (int32_t)nCBORError;
4223 }
4224
4225 if(nDecodedInt1 != 42) {
4226 return 1001;
4227 }
4228
4229 if(nDecodedInt2 != 98) {
4230 return 1002;
4231 }
4232
4233 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4234 UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){
4235 return 1003;
4236 }
4237
4238 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4239 UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){
4240 return 1004;
4241 }
4242
4243 if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){
4244 return 1005;
4245 }
4246
4247 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4248 return 1006;
4249 }
4250
4251 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4252 return 1007;
4253 }
4254
4255 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4256 return 1008;
4257 }
4258
4259 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4260 return 1009;
4261 }
4262
4263 return 0;
4264}
4265
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004266
4267/*
4268 [23,
4269 6000,
4270 h'67616C6163746963',
4271 h'686176656E20746F6B656E'
4272 ]
4273 */
4274static const uint8_t spSimpleArray[] = {
42750x84, 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};
4276
4277
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004278static const uint8_t spEmptyMap[] = {0xa0};
4279
4280static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004281
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004282static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4283
Laurence Lundbladef0499502020-08-01 11:55:57 -07004284/*
4285 {
4286 0: [],
4287 9: [
4288 [],
4289 []
4290 ],
4291 8: {
4292 1: [],
4293 2: {},
4294 3: []
4295 },
4296 4: {},
4297 5: [],
4298 6: [
4299 [],
4300 []
4301 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004302 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004303 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004304
4305
Laurence Lundbladef0499502020-08-01 11:55:57 -07004306static const uint8_t spMapOfEmpty[] = {
4307 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4308 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4309 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004310
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004311int32_t EnterMapTest()
4312{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004313 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004314 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004315 int32_t nReturn;
4316 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004317
4318
4319 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4320 QCBORDecode_EnterMap(&DCtx);
4321
Laurence Lundbladef0499502020-08-01 11:55:57 -07004322
4323 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004324 QCBORDecode_ExitArray(&DCtx);
4325
Laurence Lundbladef0499502020-08-01 11:55:57 -07004326 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004327 QCBORDecode_EnterArray(&DCtx);
4328 QCBORDecode_ExitArray(&DCtx);
4329 QCBORDecode_EnterArray(&DCtx);
4330 QCBORDecode_ExitArray(&DCtx);
4331 QCBORDecode_ExitArray(&DCtx);
4332
Laurence Lundbladef0499502020-08-01 11:55:57 -07004333 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004334 QCBORDecode_EnterArray(&DCtx);
4335 QCBORDecode_ExitArray(&DCtx);
4336 QCBORDecode_EnterMap(&DCtx);
4337 QCBORDecode_ExitMap(&DCtx);
4338 QCBORDecode_EnterArray(&DCtx);
4339 QCBORDecode_ExitArray(&DCtx);
4340 QCBORDecode_ExitMap(&DCtx);
4341
Laurence Lundbladef0499502020-08-01 11:55:57 -07004342 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004343 QCBORDecode_ExitMap(&DCtx);
4344
Laurence Lundbladef0499502020-08-01 11:55:57 -07004345 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004346 QCBORDecode_ExitArray(&DCtx);
4347
Laurence Lundbladef0499502020-08-01 11:55:57 -07004348 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004349 QCBORDecode_EnterArray(&DCtx);
4350 QCBORDecode_ExitArray(&DCtx);
4351 QCBORDecode_EnterArray(&DCtx);
4352 QCBORDecode_ExitArray(&DCtx);
4353 QCBORDecode_ExitArray(&DCtx);
4354
4355 QCBORDecode_ExitMap(&DCtx);
4356
4357 uErr = QCBORDecode_Finish(&DCtx);
4358 if(uErr != QCBOR_SUCCESS){
4359 return 3011;
4360 }
4361
4362
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004363 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004364 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004365 if(nReturn) {
4366 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004367 }
4368
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004369 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004370 if(nReturn) {
4371 return nReturn;
4372 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004373
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004374
Laurence Lundblade937ea812020-05-08 11:38:23 -07004375
4376 // These tests confirm the cursor is at the right place after entering a map or array
4377
4378 // Confirm cursor is at right place
4379 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4380 QCBORDecode_EnterMap(&DCtx);
4381 QCBORDecode_GetNext(&DCtx, &Item1);
4382 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4383 return 2001;
4384 }
4385
4386
4387 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4388 QCBORDecode_GetNext(&DCtx, &Item1);
4389 QCBORDecode_GetNext(&DCtx, &Item1);
4390 QCBORDecode_EnterArray(&DCtx);
4391 QCBORDecode_GetNext(&DCtx, &Item1);
4392 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4393 return 2002;
4394 }
4395
4396 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4397 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004398 QCBORDecode_GetNext(&DCtx, &Item1);
4399 QCBORDecode_GetNext(&DCtx, &Item1);
4400 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004401 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4402 QCBORDecode_GetNext(&DCtx, &Item1);
4403 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4404 return 2003;
4405 }
4406
4407 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4408 QCBORDecode_EnterMap(&DCtx);
4409 QCBORDecode_GetNext(&DCtx, &Item1);
4410 QCBORDecode_GetNext(&DCtx, &Item1);
4411 QCBORDecode_GetNext(&DCtx, &Item1);
4412 QCBORDecode_GetNext(&DCtx, &Item1);
4413 QCBORDecode_GetNext(&DCtx, &Item1);
4414 QCBORDecode_GetNext(&DCtx, &Item1);
4415 QCBORDecode_GetNext(&DCtx, &Item1);
4416 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4417 QCBORDecode_GetNext(&DCtx, &Item1);
4418 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004419 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004420 }
4421
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004422 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4423 QCBORDecode_EnterMap(&DCtx);
4424 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4425 QCBORDecode_ExitArray(&DCtx);
4426 QCBORDecode_GetNext(&DCtx, &Item1);
4427 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4428 return 2006;
4429 }
4430 QCBORDecode_ExitMap(&DCtx);
4431 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4432 return 2007;
4433 }
4434
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004435 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4436 QCBORDecode_EnterArray(&DCtx);
4437 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004438 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4439 uErr = QCBORDecode_GetAndResetError(&DCtx);
4440 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004441 return 2008;
4442 }
4443 UsefulBufC String;
4444 QCBORDecode_GetTextInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004445 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004446 return 2009;
4447 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004448
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004449
4450 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4451 QCBORDecode_EnterMap(&DCtx);
4452 // This will fail because the map is empty.
4453 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4454 uErr = QCBORDecode_GetAndResetError(&DCtx);
4455 if(uErr != QCBOR_ERR_NOT_FOUND){
4456 return 2010;
4457 }
4458 QCBORDecode_ExitMap(&DCtx);
4459 uErr = QCBORDecode_Finish(&DCtx);
4460 if(uErr != QCBOR_SUCCESS){
4461 return 2011;
4462 }
4463
4464
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004465 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4466 QCBORDecode_EnterMap(&DCtx);
4467 // This will fail because the map is empty.
4468 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4469 uErr = QCBORDecode_GetAndResetError(&DCtx);
4470 if(uErr != QCBOR_ERR_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004471 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004472 }
4473 QCBORDecode_ExitMap(&DCtx);
4474 uErr = QCBORDecode_Finish(&DCtx);
4475 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004476 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004477 }
4478
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004479
4480 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
4481 QCBORDecode_EnterArray(&DCtx);
4482 QCBORDecode_GetBytes(&DCtx, &String);
4483 QCBORDecode_EnterMap(&DCtx);
4484 QCBORDecode_ExitMap(&DCtx);
4485 QCBORDecode_EnterArray(&DCtx);
4486 QCBORDecode_ExitArray(&DCtx);
4487 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
4488 QCBORDecode_ExitArray(&DCtx);
4489 uErr = QCBORDecode_Finish(&DCtx);
4490 if(uErr != QCBOR_SUCCESS){
4491 return 2014;
4492 }
4493
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07004494 // TODO: more testing of entered mapps and arrays with problems
4495 // TODO: document error handling better (maybe improve error handling)
4496
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004497 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004498}
4499
4500
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004501struct NumberConversion {
4502 char *szDescription;
4503 UsefulBufC CBOR;
4504 int64_t nConvertedToInt64;
4505 QCBORError uErrorInt64;
4506 uint64_t uConvertToUInt64;
4507 QCBORError uErrorUint64;
4508 double dConvertToDouble;
4509 QCBORError uErrorDouble;
4510};
4511
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004512static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004513 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07004514 "too large to fit into int64_t",
4515 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
4516 0,
4517 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4518 0,
4519 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4520 ((double)INT64_MIN) + 1 ,
4521 QCBOR_SUCCESS
4522 },
4523 {
4524 "largest negative int that fits in int64_t",
4525 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
4526 INT64_MIN,
4527 QCBOR_SUCCESS,
4528 0,
4529 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4530 (double)INT64_MIN,
4531 QCBOR_SUCCESS
4532 },
4533 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004534 "negative bignum -1",
4535 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
4536 -1,
4537 QCBOR_SUCCESS,
4538 0,
4539 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4540 -1.0,
4541 QCBOR_SUCCESS
4542 },
4543 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004544 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004545 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4546 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004547#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07004548 257000,
4549 QCBOR_SUCCESS,
4550 257000,
4551 QCBOR_SUCCESS,
4552 257000.0,
4553 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004554#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4555 0,
4556 QCBOR_ERR_UNEXPECTED_TYPE,
4557 0,
4558 QCBOR_ERR_UNEXPECTED_TYPE,
4559 0.0,
4560 QCBOR_ERR_UNEXPECTED_TYPE
4561#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07004562 },
4563 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004564 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004565 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4566 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004567#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07004568 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07004569 QCBOR_SUCCESS,
4570 0,
4571 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07004572 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07004573 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004574#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4575 0,
4576 QCBOR_ERR_UNEXPECTED_TYPE,
4577 0,
4578 QCBOR_ERR_UNEXPECTED_TYPE,
4579 0.0,
4580 QCBOR_ERR_UNEXPECTED_TYPE
4581#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07004582 },
4583 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004584 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004585 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4586 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004587#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07004588 2056,
4589 QCBOR_SUCCESS,
4590 2056,
4591 QCBOR_SUCCESS,
4592 2056.0,
4593 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004594#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4595 0,
4596 QCBOR_ERR_UNEXPECTED_TYPE,
4597 0,
4598 QCBOR_ERR_UNEXPECTED_TYPE,
4599 0.0,
4600 QCBOR_ERR_UNEXPECTED_TYPE
4601#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07004602 },
4603 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004604 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07004605 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4606 0,
4607 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4608 0,
4609 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4610 -18446744073709551617.0,
4611 QCBOR_SUCCESS
4612 },
4613 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004614 "Positive bignum 0x01020304 indefinite length string",
4615 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
4616 0x01020304,
4617 QCBOR_SUCCESS,
4618 0x01020304,
4619 QCBOR_SUCCESS,
4620 16909060.0,
4621 QCBOR_SUCCESS
4622 },
4623 {
Laurence Lundblade887add82020-05-17 05:50:34 -07004624 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07004625 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4626 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004627#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07004628 0,
4629 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4630 0,
4631 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4632 -INFINITY,
4633 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004634#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4635 0,
4636 QCBOR_ERR_UNEXPECTED_TYPE,
4637 0,
4638 QCBOR_ERR_UNEXPECTED_TYPE,
4639 0.0,
4640 QCBOR_ERR_UNEXPECTED_TYPE
4641#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07004642 },
4643 {
4644 "big float [9223372036854775806, 9223372036854775806]",
4645 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4646 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004647#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07004648 0,
4649 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4650 0,
4651 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4652 INFINITY,
4653 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004654#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4655 0,
4656 QCBOR_ERR_UNEXPECTED_TYPE,
4657 0,
4658 QCBOR_ERR_UNEXPECTED_TYPE,
4659 0.0,
4660 QCBOR_ERR_UNEXPECTED_TYPE
4661#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07004662 },
4663 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004664 "Big float 3 * 2^^2",
4665 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004666#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07004667 12,
4668 QCBOR_SUCCESS,
4669 12,
4670 QCBOR_SUCCESS,
4671 12.0,
4672 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004673#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4674 0,
4675 QCBOR_ERR_UNEXPECTED_TYPE,
4676 0,
4677 QCBOR_ERR_UNEXPECTED_TYPE,
4678 0.0,
4679 QCBOR_ERR_UNEXPECTED_TYPE
4680#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07004681 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004682 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004683 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004684 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4685 0,
4686 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4687 18446744073709551615ULL,
4688 QCBOR_SUCCESS,
4689 18446744073709551615.0,
4690 QCBOR_SUCCESS
4691 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004692 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004693 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004694 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4695 65536-1,
4696 QCBOR_SUCCESS,
4697 0xffff,
4698 QCBOR_SUCCESS,
4699 65535.0,
4700 QCBOR_SUCCESS
4701 },
4702 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004703 "Postive integer 0",
4704 {(uint8_t[]){0x0}, 1},
4705 0LL,
4706 QCBOR_SUCCESS,
4707 0ULL,
4708 QCBOR_SUCCESS,
4709 0.0,
4710 QCBOR_SUCCESS
4711 },
4712 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004713 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004714 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4715 -9223372036854775807-1, // INT64_MIN
4716 QCBOR_SUCCESS,
4717 0ULL,
4718 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4719 -9223372036854775808.0,
4720 QCBOR_SUCCESS
4721 },
4722 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004723 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004724 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004725#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004726 100L,
4727 QCBOR_SUCCESS,
4728 100ULL,
4729 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004730#else /* QCBOR_DISABLE_FLOAT_HW_USE */
4731 0,
4732 QCBOR_ERR_HW_FLOAT_DISABLED,
4733 0,
4734 QCBOR_ERR_HW_FLOAT_DISABLED,
4735#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004736 100.3,
4737 QCBOR_SUCCESS
4738 },
4739 {
4740 "Floating point value NaN 0xfa7fc00000",
4741 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004742#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004743 0,
4744 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4745 0,
4746 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004747#else /* QCBOR_DISABLE_FLOAT_HW_USE */
4748 0,
4749 QCBOR_ERR_HW_FLOAT_DISABLED,
4750 0,
4751 QCBOR_ERR_HW_FLOAT_DISABLED,
4752#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004753 NAN,
4754 QCBOR_SUCCESS
4755 },
4756 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004757 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004758 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004759#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
4760#ifndef QCBOR_DISABLE_FLOAT_HW_USE
4761 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004762 -4,
4763 QCBOR_SUCCESS,
4764 0,
4765 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4766 -4.0,
4767 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004768#else /* QCBOR_DISABLE_FLOAT_HW_USE */
4769 // Float HW disabled
4770 -4,
4771 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
4772 0,
4773 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
4774 -4.0,
4775 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
4776#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
4777#else
4778 // Half-precision disabled
4779 -4,
4780 QCBOR_ERR_HALF_PRECISION_DISABLED,
4781 0,
4782 QCBOR_ERR_HALF_PRECISION_DISABLED,
4783 -4.0,
4784 QCBOR_ERR_HALF_PRECISION_DISABLED
4785#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004786 },
4787 {
4788 "Decimal fraction 3/10",
4789 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004790#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004791 0,
4792 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4793 0,
4794 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4795 0.30000000000000004,
4796 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004797#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
4798 0,
4799 QCBOR_ERR_UNEXPECTED_TYPE,
4800 0,
4801 QCBOR_ERR_UNEXPECTED_TYPE,
4802 0.0,
4803 QCBOR_ERR_UNEXPECTED_TYPE
4804#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004805 }
4806};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004807
4808
4809
Laurence Lundblade313b2862020-05-16 01:23:06 -07004810int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004811{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004812 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004813
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004814 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
4815 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004816
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004817 // Set up the decoding context including a memory pool so that
4818 // indefinite length items can be checked
4819 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004820 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004821
4822 /* ----- test conversion to int64_t ------ */
4823 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004824 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4825 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004826 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004827 }
4828
4829 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004830 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004831 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004832 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004833 }
4834 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004835 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004836 }
4837
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004838 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004839 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4840 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4841 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004842 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004843 }
4844 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004845 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004846 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004847 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004848 }
4849 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004850 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004851 }
4852
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004853 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004854 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4855 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4856 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004857 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004858 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004859#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004860 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004861 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004862 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004863 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004864 }
4865 if(pF->uErrorDouble == QCBOR_SUCCESS) {
4866 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004867 // NaN's can't be compared for equality. A NaN is
4868 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004869 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004870 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004871 }
4872 } else {
4873 // TODO: this comparison may need a margin of error
4874 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004875 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004876 }
4877 }
4878 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004879#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004880 }
4881
4882 return 0;
4883}
4884
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004885
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07004886
4887
Laurence Lundbladee3553422020-05-02 11:11:17 -07004888int32_t CBORSequenceDecodeTests(void)
4889{
4890 QCBORDecodeContext DCtx;
4891 QCBORItem Item;
4892 QCBORError uCBORError;
4893
4894 // --- Test a sequence with extra bytes ---
4895
4896 // The input for the date test happens to be a sequence so it
4897 // is reused. It is a sequence because it doesn't start as
4898 // an array or map.
4899 QCBORDecode_Init(&DCtx,
4900 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
4901 QCBOR_DECODE_MODE_NORMAL);
4902
4903 // Get the first item
4904 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4905 if(uCBORError != QCBOR_SUCCESS) {
4906 return 1;
4907 }
4908 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
4909 return 2;
4910 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07004911
Laurence Lundbladee3553422020-05-02 11:11:17 -07004912 // Get a second item
4913 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07004914 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
4915 return 66;
4916 }
4917
4918 // Get a third item
4919 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07004920 if(uCBORError != QCBOR_SUCCESS) {
4921 return 2;
4922 }
4923 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
4924 return 3;
4925 }
4926
4927 // A sequence can have stuff at the end that may
4928 // or may not be valid CBOR. The protocol decoder knows
4929 // when to stop by definition of the protocol, not
4930 // when the top-level map or array is ended.
4931 // Finish still has to be called to know that
4932 // maps and arrays (if there were any) were closed
4933 // off correctly. When called like this it
4934 // must return the error QCBOR_ERR_EXTRA_BYTES.
4935 uCBORError = QCBORDecode_Finish(&DCtx);
4936 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
4937 return 4;
4938 }
4939
4940
4941 // --- Test an empty input ----
4942 uint8_t empty[1];
4943 UsefulBufC Empty = {empty, 0};
4944 QCBORDecode_Init(&DCtx,
4945 Empty,
4946 QCBOR_DECODE_MODE_NORMAL);
4947
4948 uCBORError = QCBORDecode_Finish(&DCtx);
4949 if(uCBORError != QCBOR_SUCCESS) {
4950 return 5;
4951 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004952
4953
Laurence Lundbladee3553422020-05-02 11:11:17 -07004954 // --- Sequence with unclosed indefinite length array ---
4955 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004956
Laurence Lundbladee3553422020-05-02 11:11:17 -07004957 QCBORDecode_Init(&DCtx,
4958 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
4959 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004960
Laurence Lundbladee3553422020-05-02 11:11:17 -07004961 // Get the first item
4962 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4963 if(uCBORError != QCBOR_SUCCESS) {
4964 return 7;
4965 }
4966 if(Item.uDataType != QCBOR_TYPE_INT64) {
4967 return 8;
4968 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004969
Laurence Lundbladee3553422020-05-02 11:11:17 -07004970 // Get a second item
4971 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4972 if(uCBORError != QCBOR_SUCCESS) {
4973 return 9;
4974 }
4975 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4976 return 10;
4977 }
4978
4979 // Try to finish before consuming all bytes to confirm
4980 // that the still-open error is returned.
4981 uCBORError = QCBORDecode_Finish(&DCtx);
4982 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
4983 return 11;
4984 }
4985
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004986
Laurence Lundbladee3553422020-05-02 11:11:17 -07004987 // --- Sequence with a closed indefinite length array ---
4988 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004989
Laurence Lundbladee3553422020-05-02 11:11:17 -07004990 QCBORDecode_Init(&DCtx,
4991 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
4992 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07004993
Laurence Lundbladee3553422020-05-02 11:11:17 -07004994 // Get the first item
4995 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4996 if(uCBORError != QCBOR_SUCCESS) {
4997 return 12;
4998 }
4999 if(Item.uDataType != QCBOR_TYPE_INT64) {
5000 return 13;
5001 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005002
Laurence Lundbladee3553422020-05-02 11:11:17 -07005003 // Get a second item
5004 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5005 if(uCBORError != QCBOR_SUCCESS) {
5006 return 14;
5007 }
5008 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5009 return 15;
5010 }
5011
5012 // Try to finish before consuming all bytes to confirm
5013 // that the still-open error is returned.
5014 uCBORError = QCBORDecode_Finish(&DCtx);
5015 if(uCBORError != QCBOR_SUCCESS) {
5016 return 16;
5017 }
5018
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005019
Laurence Lundbladee3553422020-05-02 11:11:17 -07005020 return 0;
5021}
5022
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005023
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005024
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005025int32_t IntToTests()
5026{
5027 int nErrCode;
5028 int32_t n32;
5029 int16_t n16;
5030 int8_t n8;
5031 uint32_t u32;
5032 uint16_t u16;
5033 uint8_t u8;
5034 uint64_t u64;
5035
5036 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5037 if(nErrCode == -1 || n32 != 1) {
5038 return 1;
5039 }
5040
5041 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5042 if(nErrCode == -1 || n32 != INT32_MAX) {
5043 return 2;
5044 }
5045
5046 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5047 if(nErrCode == -1 || n32 != INT32_MIN) {
5048 return 3;
5049 }
5050
5051 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5052 if(nErrCode != -1) {
5053 return 4;
5054 }
5055
5056 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5057 if(nErrCode != -1) {
5058 return 5;
5059 }
5060
5061
5062 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5063 if(nErrCode == -1 || n16 != INT16_MAX) {
5064 return 6;
5065 }
5066
5067 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5068 if(nErrCode == -1 || n16 != INT16_MIN) {
5069 return 7;
5070 }
5071
5072 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5073 if(nErrCode == -1 || n16 != 1) {
5074 return 8;
5075 }
5076
5077 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5078 if(nErrCode != -1) {
5079 return 9;
5080 }
5081
5082 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5083 if(nErrCode != -1) {
5084 return 10;
5085 }
5086
5087
5088 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5089 if(nErrCode == -1 || n8 != 1) {
5090 return 11;
5091 }
5092
5093 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5094 if(nErrCode == -1 || n8 != INT8_MAX) {
5095 return 12;
5096 }
5097
5098 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5099 if(nErrCode == -1 || n8 != INT8_MIN) {
5100 return 13;
5101 }
5102
5103 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5104 if(nErrCode != -1) {
5105 return 14;
5106 }
5107
5108 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5109 if(nErrCode != -1) {
5110 return 15;
5111 }
5112
5113
5114 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5115 if(nErrCode == -1 || u32 != 1) {
5116 return 16;
5117 }
5118
5119 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5120 if(nErrCode == -1 || u32 != UINT32_MAX) {
5121 return 17;
5122 }
5123
5124 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5125 if(nErrCode == -1 || u32 != 0) {
5126 return 18;
5127 }
5128
5129 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5130 if(nErrCode != -1) {
5131 return 19;
5132 }
5133
5134 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5135 if(nErrCode != -1) {
5136 return 20;
5137 }
5138
5139
5140 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5141 if(nErrCode == -1 || u16 != UINT16_MAX) {
5142 return 21;
5143 }
5144
5145 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5146 if(nErrCode == -1 || u16 != 0) {
5147 return 22;
5148 }
5149
5150 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5151 if(nErrCode == -1 || u16 != 1) {
5152 return 23;
5153 }
5154
5155 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5156 if(nErrCode != -1) {
5157 return 24;
5158 }
5159
5160 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5161 if(nErrCode != -1) {
5162 return 25;
5163 }
5164
5165
5166 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5167 if(nErrCode == -1 || u8 != UINT8_MAX) {
5168 return 26;
5169 }
5170
5171 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5172 if(nErrCode == -1 || u8 != 0) {
5173 return 27;
5174 }
5175
5176 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5177 if(nErrCode == -1 || u8 != 1) {
5178 return 28;
5179 }
5180
5181 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5182 if(nErrCode != -1) {
5183 return 29;
5184 }
5185
5186 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5187 if(nErrCode != -1) {
5188 return 30;
5189 }
5190
5191
5192 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5193 if(nErrCode == -1 || u64 != 1) {
5194 return 31;
5195 }
5196
5197 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
5198 if(nErrCode == -1 || u64 != INT64_MAX) {
5199 return 32;
5200 }
5201
5202 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
5203 if(nErrCode == -1 || u64 != 0) {
5204 return 33;
5205 }
5206
5207 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
5208 if(nErrCode != -1) {
5209 return 34;
5210 }
5211
5212 return 0;
5213}
5214
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005215
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005216
5217
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005218/*
5219A sequence with
5220 A wrapping bstr
5221 containing a map
5222 1
5223 2
5224 A wrapping bstr
5225 containing an array
5226 3
5227 wrapping bstr
5228 4
5229 5
5230 6
5231 array
5232 7
5233 8
5234
5235 */
5236
5237static UsefulBufC foo(UsefulBuf ffo)
5238{
5239 UsefulBufC Encoded;
5240 QCBOREncodeContext EC;
5241 QCBORError uErr;
5242
5243 QCBOREncode_Init(&EC, ffo);
5244
5245 QCBOREncode_BstrWrap(&EC);
5246 QCBOREncode_OpenMap(&EC);
5247 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
5248 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
5249 QCBOREncode_CloseMap(&EC);
5250 QCBOREncode_BstrWrap(&EC);
5251 QCBOREncode_OpenArray(&EC);
5252 QCBOREncode_AddInt64(&EC, 3);
5253 QCBOREncode_BstrWrap(&EC);
5254 QCBOREncode_AddInt64(&EC, 4);
5255 QCBOREncode_CloseBstrWrap(&EC, NULL);
5256 QCBOREncode_AddInt64(&EC, 5);
5257 QCBOREncode_CloseArray(&EC);
5258 QCBOREncode_CloseBstrWrap(&EC, NULL);
5259 QCBOREncode_AddInt64(&EC, 6);
5260 QCBOREncode_CloseBstrWrap(&EC, NULL);
5261 QCBOREncode_OpenArray(&EC);
5262 QCBOREncode_AddInt64(&EC, 7);
5263 QCBOREncode_AddInt64(&EC, 8);
5264 QCBOREncode_CloseArray(&EC);
5265
5266 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07005267 if(uErr) {
5268 Encoded = NULLUsefulBufC;
5269 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005270
5271 return Encoded;
5272}
5273
5274
5275int32_t EnterBstrTest()
5276{
5277 MakeUsefulBufOnStack(ffo, 100);
5278
5279 QCBORDecodeContext DC;
5280
5281 QCBORDecode_Init(&DC, foo(ffo), 0);
5282
5283 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
5284
5285
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005286 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005287 QCBORDecode_EnterMap(&DC);
5288 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
5289 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
5290 QCBORDecode_ExitMap(&DC);
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005291 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005292 QCBORDecode_EnterArray(&DC);
5293 QCBORDecode_GetInt64(&DC, &i3);
Laurence Lundbladef9175a92020-07-30 21:35:45 -07005294 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NO_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005295 QCBORDecode_GetInt64(&DC, &i4);
5296 QCBORDecode_ExitBstrWrapped(&DC);
5297 QCBORDecode_GetInt64(&DC, &i5);
5298 QCBORDecode_ExitArray(&DC);
5299 QCBORDecode_ExitBstrWrapped(&DC);
5300 QCBORDecode_GetInt64(&DC, &i6);
5301 QCBORDecode_ExitBstrWrapped(&DC);
5302 QCBORDecode_EnterArray(&DC);
5303 QCBORDecode_GetInt64(&DC, &i7);
5304 QCBORDecode_GetInt64(&DC, &i8);
5305 QCBORDecode_ExitArray(&DC);
5306
5307 QCBORError uErr = QCBORDecode_Finish(&DC);
5308
5309 return (int32_t)uErr;
5310}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005311
5312
5313
5314
5315static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005316 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005317
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005318 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005319 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005320 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
5321 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
5322 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005323
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005324 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005325 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
5326 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
5327 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005328
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005329 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005330 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005331 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
5332 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005333
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005334 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005335 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
5336 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005337
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005338 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005339 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005340 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
5341 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005342
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005343 21,
5344 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
5345 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
5346
5347 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005348 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005349 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
5350 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005351
5352 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005353 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
5354 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005355
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005356 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005357 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005358 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
5359 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005360
5361 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005362 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
5363 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005364
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005365 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005366 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005367 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
5368 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005369
5370 0x18, 0x33,
5371 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
5372
5373 // MIME
5374 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005375 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
5376 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
5377 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005378
5379 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005380 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
5381 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005382
5383 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005384 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
5385 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
5386 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005387
5388 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005389 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
5390 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005391
5392 // UUID
5393 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005394 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
5395 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005396
5397 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005398 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
5399 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005400};
5401
5402int32_t DecodeTaggedTypeTests()
5403{
5404 QCBORDecodeContext DC;
5405 QCBORError uErr;
5406
5407 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
5408
5409 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005410 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005411
5412 QCBORDecode_EnterMap(&DC);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005413 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5414 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005415 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
5416 return 1;
5417 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005418 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005419 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5420 return 2;
5421 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005422 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005423 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5424 return 3;
5425 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005426 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
5427 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005428 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5429 return 4;
5430 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005431 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
5432 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5433 return 5;
5434 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005435
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005436 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5437 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5438 bNeg != false) {
5439 return 10;
5440 }
5441 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5442 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5443 bNeg != true) {
5444 return 11;
5445 }
5446 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5447 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5448 return 12;
5449 }
5450 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5451 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5452 return 13;
5453 }
5454 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bNeg);
5455 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5456 return 14;
5457 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005458
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005459 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5460 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5461 return 20;
5462 }
5463 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5464 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5465 return 21;
5466 }
5467 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5468 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5469 return 22;
5470 }
5471 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5472 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5473 return 23;
5474 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005475
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005476 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5477 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5478 return 30;
5479 }
5480 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5481 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5482 return 31;
5483 }
5484 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5485 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5486 return 32;
5487 }
5488 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5489 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5490 return 33;
5491 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005492
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005493 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5494 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5495 return 40;
5496 }
5497 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5498 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5499 return 41;
5500 }
5501 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5502 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5503 return 42;
5504 }
5505 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5506 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5507 return 43;
5508 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005509
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005510 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5511 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5512 return 50;
5513 }
5514 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5515 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5516 return 51;
5517 }
5518 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5519 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5520 return 52;
5521 }
5522 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5523 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5524 return 53;
5525 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005526
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005527 // MIME
5528 bool bIsNot7Bit;
5529 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bIsNot7Bit);
5530 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5531 bIsNot7Bit == true) {
5532 return 60;
5533 }
5534 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bIsNot7Bit);
5535 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5536 bIsNot7Bit == true) {
5537 return 61;
5538 }
5539 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bIsNot7Bit);
5540 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5541 bIsNot7Bit == false) {
5542 return 62;
5543 }
5544 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NO_TAG, &String, &bIsNot7Bit);
5545 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5546 bIsNot7Bit == false) {
5547 return 63;
5548 }
5549 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5550 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5551 return 64;
5552 }
5553 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String, &bNeg);
5554 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5555 return 65;
5556 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005557
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005558 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5559 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5560 return 70;
5561 }
5562 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NO_TAG, &String);
5563 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5564 return 71;
5565 }
5566 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5567 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5568 return 72;
5569 }
5570 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_MATCH_TAG, &String);
5571 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5572 return 73;
5573 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005574
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005575 // Improvement: add some more error test cases
5576
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005577 QCBORDecode_ExitMap(&DC);
5578
5579 uErr = QCBORDecode_Finish(&DC);
5580 if(uErr != QCBOR_SUCCESS) {
5581 return 100;
5582 }
5583
5584 return 0;
5585}