blob: 0d630b61ab9e5614ba34e44057e582c369f5a051 [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 Lundbladed4728fd2018-12-17 15:15:56 -080036#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080037#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070038#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080039
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080040
Laurence Lundbladea2e29072018-12-30 09:20:06 -080041#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080042#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080043
44static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045{
46 if(szLabel) {
47 printf("%s ", szLabel);
48 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080049
Laurence Lundblade570fab52018-10-13 18:28:27 +080050 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080051 for(i = 0; i < Buf.len; i++) {
52 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080053 printf("%02x ", Z);
54 }
55 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080056
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 fflush(stdout);
58}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080059#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080060
61
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070062static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080063 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
65 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
66 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
67 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
68 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
69 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
70 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
71 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
72 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
73 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
74 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
75 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
76 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
77 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
78 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
79 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
80 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
81 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
82 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
83 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
84 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0xff};
87
88
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080089// return CBOR error or -1 if type of value doesn't match
90
Laurence Lundbladec5fef682020-01-25 11:38:45 -080091static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070093 QCBORItem Item;
94 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080095
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080096 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070097 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080098 if(Item.uDataType != QCBOR_TYPE_ARRAY)
99 return -1;
100
101 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700102 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800103 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800104 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800105 return -1;
106
107 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700108 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800110 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800111 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800112
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700114 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800116 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800118
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700120 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800122 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800124
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700126 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800128 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800130
131
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800132 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700133 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800134 if(Item.uDataType != QCBOR_TYPE_INT64 ||
135 Item.val.int64 != -2147483648)
136 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800137
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700139 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800140 if(Item.uDataType != QCBOR_TYPE_INT64 ||
141 Item.val.int64 != -2147483647)
142 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800143
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700145 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800146 if(Item.uDataType != QCBOR_TYPE_INT64 ||
147 Item.val.int64 != -65538)
148 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800149
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700151 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800152 if(Item.uDataType != QCBOR_TYPE_INT64 ||
153 Item.val.int64 != -65537)
154 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800155
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700157 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800158 if(Item.uDataType != QCBOR_TYPE_INT64 ||
159 Item.val.int64 != -65536)
160 return -1;
161
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800163 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700164 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800165 if(Item.uDataType != QCBOR_TYPE_INT64 ||
166 Item.val.int64 != -65535)
167 return -1;
168
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800169
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700171 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800172 if(Item.uDataType != QCBOR_TYPE_INT64 ||
173 Item.val.int64 != -65534)
174 return -1;
175
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800177 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700178 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800179 if(Item.uDataType != QCBOR_TYPE_INT64 ||
180 Item.val.int64 != -257)
181 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700184 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 if(Item.uDataType != QCBOR_TYPE_INT64 ||
186 Item.val.int64 != -256)
187 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800188
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700190 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 if(Item.uDataType != QCBOR_TYPE_INT64 ||
192 Item.val.int64 != -255)
193 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700196 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if(Item.uDataType != QCBOR_TYPE_INT64 ||
198 Item.val.int64 != -254)
199 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
201
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800202 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700203 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800204 if(Item.uDataType != QCBOR_TYPE_INT64 ||
205 Item.val.int64 != -25)
206 return -1;
207
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800208
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800209 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700210 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800211 if(Item.uDataType != QCBOR_TYPE_INT64 ||
212 Item.val.int64 != -24)
213 return -1;
214
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800215
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800216 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700217 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218 if(Item.uDataType != QCBOR_TYPE_INT64 ||
219 Item.val.int64 != -23)
220 return -1;
221
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800222
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800223 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700224 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800225 if(Item.uDataType != QCBOR_TYPE_INT64 ||
226 Item.val.int64 != -1)
227 return -1;
228
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800230 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700231 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800232 if(Item.uDataType != QCBOR_TYPE_INT64 ||
233 Item.val.int64 != 0)
234 return -1;
235
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800236
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800237 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700238 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800239 if(Item.uDataType != QCBOR_TYPE_INT64 ||
240 Item.val.int64 != 0)
241 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700244 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800245 if(Item.uDataType != QCBOR_TYPE_INT64 ||
246 Item.val.int64 != 1)
247 return -1;
248
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800250 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700251 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800252 if(Item.uDataType != QCBOR_TYPE_INT64 ||
253 Item.val.int64 != 22)
254 return -1;
255
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700258 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800259 if(Item.uDataType != QCBOR_TYPE_INT64 ||
260 Item.val.int64 != 23)
261 return -1;
262
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800263
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800264 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700265 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800266 if(Item.uDataType != QCBOR_TYPE_INT64 ||
267 Item.val.int64 != 24)
268 return -1;
269
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800270
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800271 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700272 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800273 if(Item.uDataType != QCBOR_TYPE_INT64 ||
274 Item.val.int64 != 25)
275 return -1;
276
277 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700278 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800279 if(Item.uDataType != QCBOR_TYPE_INT64 ||
280 Item.val.int64 != 26)
281 return -1;
282
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800283
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800284 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700285 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800286 if(Item.uDataType != QCBOR_TYPE_INT64 ||
287 Item.val.int64 != 254)
288 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800289
290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800291 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700292 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800293 if(Item.uDataType != QCBOR_TYPE_INT64 ||
294 Item.val.int64 != 255)
295 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800296
297
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800298 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700299 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800300 if(Item.uDataType != QCBOR_TYPE_INT64 ||
301 Item.val.int64 != 256)
302 return -1;
303
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800304
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800305 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700306 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800307 if(Item.uDataType != QCBOR_TYPE_INT64 ||
308 Item.val.int64 != 257)
309 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800310
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700312 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800313 if(Item.uDataType != QCBOR_TYPE_INT64 ||
314 Item.val.int64 != 65534)
315 return -1;
316
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800317
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800318 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700319 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800320 if(Item.uDataType != QCBOR_TYPE_INT64 ||
321 Item.val.int64 != 65535)
322 return -1;
323
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800324
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800325 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700326 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800327 if(Item.uDataType != QCBOR_TYPE_INT64 ||
328 Item.val.int64 != 65536)
329 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800330
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700332 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800333 if(Item.uDataType != QCBOR_TYPE_INT64 ||
334 Item.val.int64 != 65537)
335 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700338 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800339 if(Item.uDataType != QCBOR_TYPE_INT64 ||
340 Item.val.int64 != 65538)
341 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700344 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800345 if(Item.uDataType != QCBOR_TYPE_INT64 ||
346 Item.val.int64 != 2147483647)
347 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800348
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700350 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
352 Item.val.int64 != 2147483647)
353 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700356 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800357 if(Item.uDataType != QCBOR_TYPE_INT64 ||
358 Item.val.int64 != 2147483648)
359 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700362 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800363 if(Item.uDataType != QCBOR_TYPE_INT64 ||
364 Item.val.int64 != 2147483649)
365 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700368 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800369 if(Item.uDataType != QCBOR_TYPE_INT64 ||
370 Item.val.int64 != 4294967294)
371 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
373
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700374 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
375 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800376 if(Item.uDataType != QCBOR_TYPE_INT64 ||
377 Item.val.int64 != 4294967295)
378 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800379
380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800381 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700382 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800383 if(Item.uDataType != QCBOR_TYPE_INT64 ||
384 Item.val.int64 != 4294967296)
385 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800386
387
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800388 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700389 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800390 if(Item.uDataType != QCBOR_TYPE_INT64 ||
391 Item.val.int64 != 4294967297)
392 return -1;
393
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800394
395
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800396 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700397 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800398 if(Item.uDataType != QCBOR_TYPE_INT64 ||
399 Item.val.int64 != 9223372036854775807LL)
400 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800401
402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800403 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700404 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800405 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
406 Item.val.uint64 != 18446744073709551615ULL)
407 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800408
409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800410 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
411 return -1;
412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 return 0;
415}
416
417
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800418// One less than the smallest negative integer allowed in C. Decoding
419// this should fail.
420static const uint8_t spTooSmallNegative[] = {
421 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000422};
423
424
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800425/*
426 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800427 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800428 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800429int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800430{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000431 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800433
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000434 QCBORDecode_Init(&DCtx,
435 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
436 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000438 // The really big test of all successes
439 nReturn = IntegerValuesParseTestInternal(&DCtx);
440 if(nReturn) {
441 return nReturn;
442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800443
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000444 // The one large negative integer that can be parsed
445 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800446 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000447 QCBOR_DECODE_MODE_NORMAL);
448
449 QCBORItem item;
450 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
451 nReturn = -4000;
452 }
453
454 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455}
456
457
458/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800459 Creates a simple CBOR array and returns it in *pEncoded. The array is
460 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800461
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800463
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800464 */
465
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800466static uint8_t spSimpleArrayBuffer[50];
467
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800468static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800469{
470 QCBOREncodeContext ECtx;
471 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473 *pEncoded = NULL;
474 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800475
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800476 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800477 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800478 // and last with the buffer to do the actual encoding
479 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700480 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800481 QCBOREncode_OpenArray(&ECtx);
482 QCBOREncode_AddInt64(&ECtx, nInt1);
483 QCBOREncode_AddInt64(&ECtx, nInt2);
484 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
485 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
486 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800487
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800488 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489 goto Done;
490
491 if(*pEncoded != NULL) {
492 nReturn = 0;
493 goto Done;
494 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800495
496 // Use static buffer to avoid dependency on malloc()
497 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800498 goto Done;
499 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800500 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800501
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800503
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800504Done:
505 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506}
507
508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800509/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800510 Some basic CBOR with map and array used in a lot of tests.
511 The map labels are all strings
512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900514 "an array of two strings": [
515 "string1", "string2"
516 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 "map in a map": {
518 "bytes 1": h'78787878',
519 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900520 "another int": 98,
521 "text 2": "lies, damn lies and statistics"
522 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523 }
524 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800525static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700526 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
527 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
528 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
529 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
530 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
531 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
532 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
533 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
534 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
535 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
536 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
537 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
538 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
539 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
540 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
541 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800542
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800543static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800544 size_t nLen,
545 int64_t *pInt1,
546 int64_t *pInt2,
547 const uint8_t **pBuf3,
548 size_t *pBuf3Len,
549 const uint8_t **pBuf4,
550 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551{
552 QCBORDecodeContext DCtx;
553 QCBORItem Item;
554 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800555
Laurence Lundbladeee851742020-01-08 08:37:05 -0800556 QCBORDecode_Init(&DCtx,
557 (UsefulBufC){pEncoded, nLen},
558 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800559
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800560 // Make sure the first thing is a map
561 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
562 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800564 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800565 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566 goto Done;
567 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800569 // Second integer
570 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
571 goto Done;
572 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800573
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800574 // First string
575 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
576 goto Done;
577 *pBuf3 = Item.val.string.ptr;
578 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 // Second string
581 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
582 goto Done;
583 *pBuf4 = Item.val.string.ptr;
584 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800586 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800588Done:
589 return(nReturn);
590}
591
592
593
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800594
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800595int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800596{
597 uint8_t *pEncoded;
598 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800599
Laurence Lundblade5e390822019-01-06 12:35:01 -0800600 int64_t i1=0, i2=0;
601 size_t i3=0, i4=0;
602 const uint8_t *s3= (uint8_t *)"";
603 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800604
605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
607 return(-1);
608 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800609
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800610 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 if(i1 != 23 ||
613 i2 != 6000 ||
614 i3 != 8 ||
615 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530616 memcmp("galactic", s3, 8) !=0 ||
617 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 return(-1);
619 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800620
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800621 return(0);
622}
623
624
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700625/*
626 [
627 0,
628 [],
629 [
630 [],
631 [
632 0
633 ],
634 {},
635 {
636 1: {},
637 2: {},
638 3: []
639 }
640 ]
641 ]
642 */
643static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
644 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
645
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800646int32_t EmptyMapsAndArraysTest()
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700647{
648 QCBORDecodeContext DCtx;
649 QCBORItem Item;
650
Laurence Lundbladeee851742020-01-08 08:37:05 -0800651 QCBORDecode_Init(&DCtx,
652 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
653 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700654
655 // Array with 3 items
656 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
657 Item.uDataType != QCBOR_TYPE_ARRAY ||
658 Item.uNestingLevel != 0 ||
659 Item.uNextNestLevel != 1 ||
660 Item.val.uCount != 3) {
661 return -1;
662 }
663
664 // An integer 0
665 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
666 Item.uDataType != QCBOR_TYPE_INT64 ||
667 Item.uNestingLevel != 1 ||
668 Item.uNextNestLevel != 1 ||
669 Item.val.uint64 != 0) {
670 return -2;
671 }
672
673 // An empty array
674 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
675 Item.uDataType != QCBOR_TYPE_ARRAY ||
676 Item.uNestingLevel != 1 ||
677 Item.uNextNestLevel != 1 ||
678 Item.val.uCount != 0) {
679 return -3;
680 }
681
682 // An array with 4 items
683 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
684 Item.uDataType != QCBOR_TYPE_ARRAY ||
685 Item.uNestingLevel != 1 ||
686 Item.uNextNestLevel != 2 ||
687 Item.val.uCount != 4) {
688 return -4;
689 }
690
691 // An empty array
692 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
693 Item.uDataType != QCBOR_TYPE_ARRAY ||
694 Item.uNestingLevel != 2 ||
695 Item.uNextNestLevel != 2 ||
696 Item.val.uCount != 0) {
697 return -5;
698 }
699
700 // An array with 1 item
701 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
702 Item.uDataType != QCBOR_TYPE_ARRAY ||
703 Item.uNestingLevel != 2 ||
704 Item.uNextNestLevel != 3 ||
705 Item.val.uCount != 1) {
706 return -6;
707 }
708
709 // An integer 0
710 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
711 Item.uDataType != QCBOR_TYPE_INT64 ||
712 Item.uNestingLevel != 3 ||
713 Item.uNextNestLevel != 2 ||
714 Item.val.uint64 != 0) {
715 return -7;
716 }
717
718 // An empty map
719 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
720 Item.uDataType != QCBOR_TYPE_MAP ||
721 Item.uNestingLevel != 2 ||
722 Item.uNextNestLevel != 2 ||
723 Item.val.uCount != 0) {
724 return -8;
725 }
726
727 // An map with 3 items
728 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
729 Item.uDataType != QCBOR_TYPE_MAP ||
730 Item.uNestingLevel != 2 ||
731 Item.uNextNestLevel != 3 ||
732 Item.val.uCount != 3) {
733 return -9;
734 }
735
736 // An empty map
737 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
738 Item.uDataType != QCBOR_TYPE_MAP ||
739 Item.uNestingLevel != 3 ||
740 Item.uNextNestLevel != 3 ||
741 Item.val.uCount != 0) {
742 return -10;
743 }
744
745 // An empty map
746 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
747 Item.uDataType != QCBOR_TYPE_MAP ||
748 Item.uNestingLevel != 3 ||
749 Item.uNextNestLevel != 3 ||
750 Item.val.uCount != 0) {
751 return -11;
752 }
753
754 // An empty array
755 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
756 Item.uDataType != QCBOR_TYPE_ARRAY ||
757 Item.uNestingLevel != 3 ||
758 Item.uNextNestLevel != 0 ||
759 Item.val.uCount != 0) {
760 return -12;
761 }
762
763 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
764 return -13;
765 }
766
767 return 0;
768}
769
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800770
Laurence Lundbladeee851742020-01-08 08:37:05 -0800771static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
772 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800773
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800774int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800775{
776 QCBORDecodeContext DCtx;
777 int nReturn = 0;
778 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800779
Laurence Lundbladeee851742020-01-08 08:37:05 -0800780 QCBORDecode_Init(&DCtx,
781 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
782 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800783
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784 for(i = 0; i < 10; i++) {
785 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800786
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800787 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
788 Item.uDataType != QCBOR_TYPE_ARRAY ||
789 Item.uNestingLevel != i) {
790 nReturn = -1;
791 break;
792 }
793 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800794
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800795 return(nReturn);
796}
797
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700798// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800799static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
800 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
801 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
802 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800803
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800804int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800805{
806 QCBORDecodeContext DCtx;
807 int nReturn = 0;
808 int i;
809 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800810
811
Laurence Lundbladeee851742020-01-08 08:37:05 -0800812 QCBORDecode_Init(&DCtx,
813 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
814 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800815
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700816 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800817
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800818 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
819 Item.uDataType != QCBOR_TYPE_ARRAY ||
820 Item.uNestingLevel != i) {
821 nReturn = -1;
822 break;
823 }
824 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800825
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800826 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
827 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800828
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800829 return(nReturn);
830}
831
832
833
834
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800835int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800836{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700837 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800838
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800839 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700840 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800841
Laurence Lundbladeee851742020-01-08 08:37:05 -0800842 QCBORDecode_Init(&DCtx,
843 (UsefulBufC){spExpectedEncodedInts, nNum},
844 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800845
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800846 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800847
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700848 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800849 nResult = -1;
850 goto Done;
851 }
852 }
853Done:
854 return nResult;
855}
856
857
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800858
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800859int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800860{
861 uint8_t *pEncoded;
862 int nReturn;
863 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800864
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800865 int64_t i1, i2;
866 size_t i3, i4;
867 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800868
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800869 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800870
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800871 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
872 return(-1);
873 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800874
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
876 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
877 if(nResult == 0) {
878 nReturn = -1;
879 }
880 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800881
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800882 return(nReturn);
883}
884
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530885/*
886 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800887 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
888 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530889 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800890static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800891{
892 QCBORDecodeContext DCtx;
893 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700894 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundbladeee851742020-01-08 08:37:05 -0800896 QCBORDecode_Init(&DCtx,
897 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
898 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800899
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900900 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700901 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900902 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800903 if(Item.uDataType != QCBOR_TYPE_MAP ||
904 Item.val.uCount != 3)
905 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900907 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700908 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900909 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800911 Item.uDataType != QCBOR_TYPE_INT64 ||
912 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530913 Item.uDataAlloc ||
914 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900915 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800916 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900917 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800918
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900919 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700920 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900921 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800922 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530923 Item.uDataAlloc ||
924 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900925 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800926 Item.uDataType != QCBOR_TYPE_ARRAY ||
927 Item.val.uCount != 2)
928 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800929
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900930 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700931 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900932 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800933 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530934 Item.uDataAlloc ||
935 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900936 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800937 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900938 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800939
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900940 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700941 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900942 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800943 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530944 Item.uDataAlloc ||
945 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900946 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800947 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800949
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900950 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700951 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900952 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800953 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530954 Item.uDataAlloc ||
955 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900956 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800957 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900958 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800959 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800961
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900962 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700963 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900964 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800965 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900966 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800967 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530968 Item.uDataAlloc ||
969 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900970 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800971 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800973
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900974 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700975 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900976 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900978 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800979 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530980 Item.uDataAlloc ||
981 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900982 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800983 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800985
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700987 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900988 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800989 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530990 Item.uDataAlloc ||
991 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900992 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800993 Item.uDataType != QCBOR_TYPE_INT64 ||
994 Item.val.int64 != 98)
995 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800996
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900997 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700998 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900999 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001000 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001001 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301003 Item.uDataAlloc ||
1004 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001005 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001006 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001008
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001009 return 0;
1010}
1011
1012
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001013/*
1014 Decode and thoroughly check a moderately complex
1015 set of maps
1016 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001017int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001018{
1019 QCBORDecodeContext DCtx;
1020 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001021 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001022
Laurence Lundbladeee851742020-01-08 08:37:05 -08001023 QCBORDecode_Init(&DCtx,
1024 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1025 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001026
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001028 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001029 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001030 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1031 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001032 return -1;
1033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001034
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001036 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001037 }
1038 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1039 Item.uDataAlloc ||
1040 Item.uLabelAlloc ||
1041 Item.uLabelType != QCBOR_TYPE_NONE ||
1042 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1043 return -2;
1044 }
1045
1046 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001047 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001048 }
1049 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1050 Item.uDataType != QCBOR_TYPE_INT64 ||
1051 Item.val.int64 != 42 ||
1052 Item.uDataAlloc ||
1053 Item.uLabelAlloc) {
1054 return -3;
1055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001056
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001058 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 }
1060 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1061 Item.uDataAlloc ||
1062 Item.uLabelAlloc ||
1063 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1064 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1065 return -4;
1066 }
1067
1068 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001069 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001070 }
1071 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1072 Item.uDataAlloc ||
1073 Item.uLabelAlloc ||
1074 Item.uDataType != QCBOR_TYPE_ARRAY ||
1075 Item.val.uCount != 2) {
1076 return -5;
1077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001079 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001080 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001081 }
1082 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1083 Item.val.string.len != 7 ||
1084 Item.uDataAlloc ||
1085 Item.uLabelAlloc ||
1086 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1087 return -6;
1088 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001089
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001090 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001091 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 }
1093 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1094 Item.uDataAlloc ||
1095 Item.uLabelAlloc ||
1096 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1097 return -7;
1098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001099
1100
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001102 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001103 }
1104 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1105 Item.uDataAlloc ||
1106 Item.uLabelAlloc ||
1107 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1108 return -8;
1109 }
1110
1111 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001112 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001113 }
1114 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1115 Item.uDataAlloc ||
1116 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001117 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1118 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 return -9;
1120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001121
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001123 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001124 }
1125 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1126 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1127 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1128 Item.uDataAlloc ||
1129 Item.uLabelAlloc) {
1130 return -10;
1131 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001132
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001134 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001135 }
1136 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1137 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1138 Item.uDataAlloc ||
1139 Item.uLabelAlloc ||
1140 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1141 return -11;
1142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001143
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001145 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001146 }
1147 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1148 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1149 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1150 Item.uDataAlloc ||
1151 Item.uLabelAlloc) {
1152 return -12;
1153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001154
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001156 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 }
1158 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1159 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1160 Item.uDataAlloc ||
1161 Item.uLabelAlloc ||
1162 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1163 return -13;
1164 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001165
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001167 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001168 }
1169 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1170 Item.uDataAlloc ||
1171 Item.uLabelAlloc ||
1172 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1173 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1174 return -14;
1175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001176
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001178 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 }
1180 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1181 Item.uDataAlloc ||
1182 Item.uLabelAlloc ||
1183 Item.uDataType != QCBOR_TYPE_INT64 ||
1184 Item.val.int64 != 98) {
1185 return -15;
1186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001187
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001189 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001190 }
1191 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1192 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1193 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1194 Item.uDataAlloc ||
1195 Item.uLabelAlloc) {
1196 return -16;
1197 }
1198
1199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001200 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001201 }
1202 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1203 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1204 Item.uDataAlloc ||
1205 Item.uLabelAlloc ||
1206 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1207 return -17;
1208 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001209
1210
1211 /*
1212 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1213 map that when interpreted as an array will be too many. Test
1214 data just has the start of the map, not all the items in the map.
1215 */
1216 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
1217
1218 QCBORDecode_Init(&DCtx,
1219 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1220 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1221
1222 if((QCBOR_ERR_ARRAY_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
1223 return -50;
1224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001225
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 return 0;
1227}
1228
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001229
1230/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301231 Fully or partially decode pValidMapEncoded. When
1232 partially decoding check for the right error code.
1233 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001234
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301235 The partial decodes test error conditions of
1236 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001237
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301238 This could be combined with the above test
1239 and made prettier and maybe a little more
1240 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001242static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001243{
1244 QCBORDecodeContext DCtx;
1245 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001246 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001247
Laurence Lundbladeee851742020-01-08 08:37:05 -08001248 QCBORDecode_Init(&DCtx,
1249 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1250 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001251
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001252 if(nLevel < 1) {
1253 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1254 return -1;
1255 } else {
1256 return 0;
1257 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001258 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301259
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001260
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001261 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001262 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001263 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001264 if(Item.uDataType != QCBOR_TYPE_MAP ||
1265 Item.val.uCount != 3)
1266 return -1;
1267
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001268 if(nLevel < 2) {
1269 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1270 return -1;
1271 } else {
1272 return 0;
1273 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001274 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001275
1276
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001277 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001278 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001279 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001280 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 Item.uDataType != QCBOR_TYPE_INT64 ||
1282 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001283 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001284 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001286
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001287 if(nLevel < 3) {
1288 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1289 return -1;
1290 } else {
1291 return 0;
1292 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001294
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001295 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001296 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001297 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001298 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001299 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001300 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001301 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001302 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
1305
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001306 if(nLevel < 4) {
1307 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1308 return -1;
1309 } else {
1310 return 0;
1311 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001313
1314
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001315 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001316 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001317 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001318 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001319 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001320 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001323 if(nLevel < 5) {
1324 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1325 return -1;
1326 } else {
1327 return 0;
1328 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 }
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_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001335 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001336 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001338
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001339 if(nLevel < 6) {
1340 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1341 return -1;
1342 } else {
1343 return 0;
1344 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001346
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence 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 Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 Item.uDataType != QCBOR_TYPE_MAP ||
1353 Item.val.uCount != 4)
1354 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001355
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001356 if(nLevel < 7) {
1357 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1358 return -1;
1359 } else {
1360 return 0;
1361 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001363
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001364 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001365 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001366 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001368 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001370 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001371 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001373
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001374 if(nLevel < 8) {
1375 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1376 return -1;
1377 } else {
1378 return 0;
1379 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001381
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001382 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001383 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001388 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001389 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001392 if(nLevel < 9) {
1393 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1394 return -1;
1395 } else {
1396 return 0;
1397 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001399
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001401 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001402 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 Item.uDataType != QCBOR_TYPE_INT64 ||
1406 Item.val.int64 != 98)
1407 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001408
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001409 if(nLevel < 10) {
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("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001423 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001424 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001426
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301427 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001428 return -1;
1429 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001431 return 0;
1432}
1433
1434
1435
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001436
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001437int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001439 // Parse a moderatly complex map structure very thoroughly
1440 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1441 if(nResult) {
1442 return nResult;
1443 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001444
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001445 // Again, but in strings-only mode. It should succeed since the input
1446 // map has only string labels.
1447 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1448 if(nResult) {
1449 return nResult;
1450 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001451
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001452 // Again, but try to finish the decoding before the end of the
1453 // input at 10 different place and see that the right error code
1454 // is returned.
1455 for(int i = 0; i < 10; i++) {
1456 nResult = ExtraBytesTest(i);
1457 if(nResult) {
1458 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001459 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001461
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001462 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463}
1464
1465
Laurence Lundbladeee851742020-01-08 08:37:05 -08001466static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1467 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1468 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001469
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001470int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001471{
1472 QCBORDecodeContext DCtx;
1473 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001474 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001475
1476
Laurence Lundbladeee851742020-01-08 08:37:05 -08001477 QCBORDecode_Init(&DCtx,
1478 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1479 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001480
1481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001482 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001483 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001484 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1485 Item.val.uCount != 10)
1486 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001489 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 if(Item.uDataType != QCBOR_TYPE_FALSE)
1491 return -1;
1492
1493 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001494 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001495 if(Item.uDataType != QCBOR_TYPE_TRUE)
1496 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001497
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001499 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001500 if(Item.uDataType != QCBOR_TYPE_NULL)
1501 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001502
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001504 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001505 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1506 return -1;
1507
1508 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001509 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 return -1;
1511
1512 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001513 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1515 return -1;
1516
1517 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001518 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001519 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1520 return -1;
1521
Laurence Lundblade077475f2019-04-26 09:06:33 -07001522 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
Laurence Lundblade077475f2019-04-26 09:06:33 -07001525 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526 return -1;
1527
Laurence Lundblade077475f2019-04-26 09:06:33 -07001528 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001529 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001531 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001532 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001533 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1534 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001535
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001536 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001537 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001538 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1539 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001540
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001541 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001542
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001543}
1544
1545
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001546static int IsNotWellFormedError(QCBORError nErr)
1547{
1548 switch(nErr){
1549 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1550 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1551 case QCBOR_ERR_UNSUPPORTED:
1552 case QCBOR_ERR_HIT_END:
1553 case QCBOR_ERR_BAD_TYPE_7:
1554 case QCBOR_ERR_BAD_BREAK:
1555 case QCBOR_ERR_EXTRA_BYTES:
1556 case QCBOR_ERR_BAD_INT:
1557 return 1;
1558 default:
1559 return 0;
1560 }
1561}
1562
1563
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001564int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001565{
1566 // Loop over all the not-well-formed instance of CBOR
1567 // that are test vectors in not_well_formed_cbor.h
1568 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1569 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1570 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1571 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1572
Laurence Lundbladeee851742020-01-08 08:37:05 -08001573 // Set up decoder context. String allocator needed for indefinite
1574 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001575 QCBORDecodeContext DCtx;
1576 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1577 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1578 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1579
1580 // Loop getting items until no more to get
1581 QCBORError nCBORError;
1582 do {
1583 QCBORItem Item;
1584
1585 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1586 } while(nCBORError == QCBOR_SUCCESS);
1587
1588 // Every test vector must fail with
1589 // a not-well-formed error. If not
1590 // this test fails.
1591 if(!IsNotWellFormedError(nCBORError)) {
1592 // Return index of failure in the error code
1593 return 2000 + nIterate;
1594 }
1595 }
1596 return 0;
1597}
1598
1599
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001600struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001601 UsefulBufC Input;
1602 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001603};
1604
Laurence Lundblade59289e52019-12-30 13:44:37 -08001605
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001606static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001607{
1608 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1609 // Set up the decoding context including a memory pool so that
1610 // indefinite length items can be checked
1611 QCBORDecodeContext DCtx;
1612 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1613 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1614 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1615 if(nCBORError) {
1616 return -9;
1617 }
1618
1619 // Iterate until there is an error of some sort error
1620 QCBORItem Item;
1621 do {
1622 // Set to something none-zero other than QCBOR_TYPE_NONE
1623 memset(&Item, 0x33, sizeof(Item));
1624
1625 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1626 } while(nCBORError == QCBOR_SUCCESS);
1627
1628 // Must get the expected error or the this test fails
1629 // The data and label type must also be QCBOR_TYPE_NONE
1630 if(nCBORError != pF->nError ||
1631 Item.uDataType != QCBOR_TYPE_NONE ||
1632 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001633 // return index of CBOR + 100
1634 const size_t nIndex = (size_t)(pF - pFailInputs)/sizeof(struct FailInput);
1635 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001636 }
1637 }
1638
1639 return 0;
1640}
1641
1642
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001643struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001644 // Most of this is copied from not_well_formed.h. Here the error code
1645 // returned is also checked.
1646
1647 // Indefinite length strings must be closed off
1648 // An indefinite length byte string not closed off
1649 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1650 // An indefinite length text string not closed off
1651 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1652
1653
1654 // All the chunks in an indefinite length string must be of the type of indefinite length string
1655 // indefinite length byte string with text string chunk
1656 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1657 // indefinite length text string with a byte string chunk
1658 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1659 // indefinite length byte string with an positive integer chunk
1660 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1661 // indefinite length byte string with an negative integer chunk
1662 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1663 // indefinite length byte string with an array chunk
1664 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1665 // indefinite length byte string with an map chunk
1666 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1667 // indefinite length byte string with tagged integer chunk
1668 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1669 // indefinite length byte string with an simple type chunk
1670 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1671 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1672 // indefinite length text string with indefinite string inside
1673 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1674
1675
1676 // Definte length maps and arrays must be closed by having the right number of items
1677 // A definte length array that is supposed to have 1 item, but has none
1678 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1679 // A definte length array that is supposed to have 2 items, but has only 1
1680 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1681 // A definte length array that is supposed to have 511 items, but has only 1
1682 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1683 // A definte length map that is supposed to have 1 item, but has none
1684 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1685 // A definte length map that is supposed to have s item, but has only 1
1686 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1687
1688
1689 // Indefinte length maps and arrays must be ended by a break
1690 // Indefinite length array with zero items and no break
1691 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1692 // Indefinite length array with two items and no break
1693 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1694 // Indefinite length map with zero items and no break
1695 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1696 // Indefinite length map with two items and no break
1697 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1698
1699
1700 // Nested maps and arrays must be closed off (some extra nested test vectors)
1701 // Unclosed indefinite array containing a close definite array
1702 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
1703 // Definite length array containing an unclosed indefinite array
1704 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1705 // Deeply nested definite length arrays with deepest one unclosed
1706 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1707 // Deeply nested indefinite length arrays with deepest one unclosed
1708 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1709 // Mixed nesting with indefinite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001710 // TODO: think through this one
1711 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001712 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001713 // TODO: think through this one
1714 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001715
1716
1717 // The "argument" for the data item is incomplete
1718 // Positive integer missing 1 byte argument
1719 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1720 // Positive integer missing 2 byte argument
1721 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1722 // Positive integer missing 4 byte argument
1723 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1724 // Positive integer missing 8 byte argument
1725 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1726 // Positive integer missing 1 byte of 2 byte argument
1727 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1728 // Positive integer missing 2 bytes of 4 byte argument
1729 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1730 // Positive integer missing 1 bytes of 7 byte argument
1731 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1732 // Negative integer missing 1 byte argument
1733 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1734 // Binary string missing 1 byte argument
1735 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1736 // Text string missing 1 byte argument
1737 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1738 // Array missing 1 byte argument
1739 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1740 // Map missing 1 byte argument
1741 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1742 // Tag missing 1 byte argument
1743 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1744 // Simple missing 1 byte argument
1745 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1746
1747
1748 // Breaks must not occur in definite length arrays and maps
1749 // Array of length 1 with sole member replaced by a break
1750 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1751 // Array of length 2 with 2nd member replaced by a break
1752 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1753 // Map of length 1 with sole member label replaced by a break
1754 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1755 // Map of length 1 with sole member label replaced by break
1756 // Alternate representation that some decoders handle difference
1757 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1758 // Array of length 1 with 2nd member value replaced by a break
1759 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1760 // Map of length 2 with 2nd member replaced by a break
1761 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1762
1763
1764 // Breaks must not occur on their own out of an indefinite length data item
1765 // A bare break is not well formed
1766 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1767 // A bare break after a zero length definite length array
1768 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1769 // A bare break after a zero length indefinite length map
1770 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1771
1772
1773 // Forbidden two byte encodings of simple types
1774 // Must use 0xe0 instead
1775 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1776 // Should use 0xe1 instead
1777 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1778 // Should use 0xe2 instead
1779 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1780 // Should use 0xe3 instead
1781 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1782 // Should use 0xe4 instead
1783 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1784 // Should use 0xe5 instead
1785 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1786 // Should use 0xe6 instead
1787 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1788 // Should use 0xe7 instead
1789 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1790 // Should use 0xe8 instead
1791 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1792 // Should use 0xe9 instead
1793 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1794 // Should use 0xea instead
1795 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1796 // Should use 0xeb instead
1797 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1798 // Should use 0xec instead
1799 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1800 // Should use 0xed instead
1801 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1802 // Should use 0xee instead
1803 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1804 // Should use 0xef instead
1805 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1806 // Should use 0xf0 instead
1807 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1808 // Should use 0xf1 instead
1809 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1810 // Should use 0xf2 instead
1811 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1812 // Must use 0xf3 instead
1813 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1814 // Must use 0xf4 instead
1815 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1816 // Must use 0xf5 instead
1817 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1818 // Must use 0xf6 instead
1819 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1820 // Must use 0xf7 instead
1821 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1822 // Must use 0xf8 instead
1823 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1824
1825
1826 // Integers with additional info indefinite length
1827 // Positive integer with additional info indefinite length
1828 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1829 // Negative integer with additional info indefinite length
1830 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1831 // CBOR tag with "argument" an indefinite length
1832 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1833 // CBOR tag with "argument" an indefinite length alternate vector
1834 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1835
1836
1837 // Missing bytes from a deterministic length string
1838 // A byte string is of length 1 without the 1 byte
1839 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1840 // A text string is of length 1 without the 1 byte
1841 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001842 // Byte string should have 2^32-15 bytes, but has one
1843 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1844 // Byte string should have 2^32-15 bytes, but has one
1845 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001846
1847
1848 // Use of unassigned additional information values
1849 // Major type positive integer with reserved value 28
1850 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1851 // Major type positive integer with reserved value 29
1852 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1853 // Major type positive integer with reserved value 30
1854 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1855 // Major type negative integer with reserved value 28
1856 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1857 // Major type negative integer with reserved value 29
1858 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1859 // Major type negative integer with reserved value 30
1860 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1861 // Major type byte string with reserved value 28 length
1862 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1863 // Major type byte string with reserved value 29 length
1864 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1865 // Major type byte string with reserved value 30 length
1866 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1867 // Major type text string with reserved value 28 length
1868 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1869 // Major type text string with reserved value 29 length
1870 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1871 // Major type text string with reserved value 30 length
1872 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1873 // Major type array with reserved value 28 length
1874 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1875 // Major type array with reserved value 29 length
1876 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1877 // Major type array with reserved value 30 length
1878 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1879 // Major type map with reserved value 28 length
1880 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1881 // Major type map with reserved value 29 length
1882 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1883 // Major type map with reserved value 30 length
1884 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1885 // Major type tag with reserved value 28 length
1886 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1887 // Major type tag with reserved value 29 length
1888 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1889 // Major type tag with reserved value 30 length
1890 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1891 // Major type simple with reserved value 28 length
1892 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1893 // Major type simple with reserved value 29 length
1894 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1895 // Major type simple with reserved value 30 length
1896 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1897
1898
1899 // Maps must have an even number of data items (key & value)
1900 // Map with 1 item when it should have 2
1901 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1902 // Map with 3 item when it should have 4
1903 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1904 // Map with 1 item when it should have 2
1905 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1906 // Map with 3 item when it should have 4
1907 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1908
1909
1910 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001911 // Text-based date, with an integer
1912 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1913 // Epoch date, with an byte string
1914 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1915 // tagged as both epoch and string dates
1916 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1917 // big num tagged an int, not a byte string
1918 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001919};
1920
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001921int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001922{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001923 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001924
Laurence Lundblade59289e52019-12-30 13:44:37 -08001925 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1926 if(nResult) {
1927 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001928 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001929
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001930 // Corrupt the UsefulInputBuf and see that
1931 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001932 {
1933 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001934 QCBORItem Item;
1935 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001936
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001937 QCBORDecode_Init(&DCtx,
1938 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1939 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001940
1941 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001942 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001943 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001944 Item.val.uCount != 10) {
1945 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001946 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001947 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001948
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001949 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001950
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001951 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001952 if(nCBORError != QCBOR_ERR_HIT_END) {
1953 // Did not get back the error expected
1954 return -2;
1955 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001957
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001958/*
1959 This test is disabled until QCBOREncode_EncodeHead() is brought in so
1960 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
1961
1962 This relies on the largest string allowed being SIZE_MAX -4 rather than
1963 SIZE_MAX. That way the test can be performed.
1964 {
1965 QCBORDecodeContext DCtx;
1966 QCBORItem Item;
1967
1968 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
1969 0xff, 0xff, 0xff, 0xff};
1970
1971 QCBORDecode_Init(&DCtx,
1972 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
1973 QCBOR_DECODE_MODE_NORMAL);
1974
1975 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
1976 return -4;
1977 }
1978 }
1979*/
1980
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001981 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001982}
1983
1984
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001985/* Try all 256 values of the byte at nLen including recursing for
1986 each of the values to try values at nLen+1 ... up to nLenMax
1987 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001988static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001989{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001990 if(nLen >= nLenMax) {
1991 return;
1992 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001993
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001994 for(int inputByte = 0; inputByte < 256; inputByte++) {
1995 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001996 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001997 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001998
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001999 // Get ready to parse
2000 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002001 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002002
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002003 // Parse by getting the next item until an error occurs
2004 // Just about every possible decoder error can occur here
2005 // The goal of this test is not to check for the correct
2006 // error since that is not really possible. It is to
2007 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002008 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002009 QCBORItem Item;
2010 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002011 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002012 break;
2013 }
2014 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002015
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002016 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002017 }
2018}
2019
2020
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002021int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002022{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002023 // Size 2 tests 64K inputs and runs quickly
2024 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002025
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002026 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002027
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002028 return 0;
2029}
2030
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002031
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002032int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002033{
2034 // size 3 tests 16 million inputs and runs OK
2035 // in seconds on fast machines. Size 4 takes
2036 // 10+ minutes and 5 half a day on fast
2037 // machines. This test is kept separate from
2038 // the others so as to no slow down the use
2039 // of them as a very frequent regression.
2040 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002041
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002042 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002043
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002044 return 0;
2045}
2046
2047
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002048static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002049 0xc0, // tag for string date
2050 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002051
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002052 0xc1, // tag for epoch date
2053 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2054
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002055 // CBOR_TAG_B64
2056 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002057 0x1a, 0x53, 0x72, 0x4E, 0x01,
2058
2059 0xc1, // tag for epoch date
2060 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002061
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002062 0xc1, // tag for epoch date
2063 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002064
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002065 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002066 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002067
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002068 0xc1, // tag for epoch date
2069 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2070 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2071
2072 0xc1, // tag for epoch date
2073 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002074};
2075
2076
2077// have to check float expected only to within an epsilon
2078int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002079
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002081
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002082 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002083
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002084 return diff > 0.0000001;
2085}
2086
2087
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002088int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002089{
2090 QCBORDecodeContext DCtx;
2091 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002092 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002093
Laurence Lundbladeee851742020-01-08 08:37:05 -08002094 QCBORDecode_Init(&DCtx,
2095 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2096 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002097
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002098 const uint64_t uTags[] = {15};
2099 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002100
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002101 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002102
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002103 // String date
2104 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2105 return -1;
2106 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002107 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002108 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002109 }
2110
2111 // Epoch date
2112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002113 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002114 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2115 Item.val.epochDate.nSeconds != 1400000000 ||
2116 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002117 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002119
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002120 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2121 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002123 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002124 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2125 Item.val.epochDate.nSeconds != 1400000001 ||
2126 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002127 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002128 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002129 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131 // Epoch date that is too large for our representation
2132 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002133 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002135
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002136 // Epoch date in float format with fractional seconds
2137 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002138 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002139 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2140 Item.val.epochDate.nSeconds != 1 ||
2141 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002142 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002143 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002144
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002145 // Epoch date float that is too large for our representation
2146 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002147 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002149
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002150 // Epoch date double that is just slightly too large
2151 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2152 return -11;
2153 }
2154
2155 // Largest double epoch date supported
2156 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2157 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2158 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2159 Item.val.epochDate.nSeconds == 0) {
2160 return -12;
2161 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002162 // TODO: could use a few more tests with float, double, and half precsion
2163 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164
2165 return 0;
2166}
2167
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002168// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002169static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002170 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002171 0x81, // Array of one
2172 0xd8, 0x04, // non-preferred serialization of tag 4
2173 0x82, 0x01, 0x03}; // fraction 1/3
2174
Laurence Lundblade59289e52019-12-30 13:44:37 -08002175/*
2176 DB 9192939495969798 # tag(10489608748473423768)
2177 80 # array(0)
2178 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002179static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2180 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002181
Laurence Lundblade59289e52019-12-30 13:44:37 -08002182/*
2183DB 9192939495969798 # tag(10489608748473423768)
2184 D8 88 # tag(136)
2185 C6 # tag(6)
2186 C7 # tag(7)
2187 80 # array(0)
2188*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002189static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2190 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002191
2192/*
2193 The cbor.me parse of this.
2194 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002195 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 +07002196 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2197 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2198 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2199 */
2200static uint8_t spCSRWithTags[] = {
2201 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2202 0xc6, 0xc7, 0x36,
2203 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2204 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2205 0xcb, 0xa5,
2206 0xd1, 0x31,
2207 0xd1, 0xd1, 0xd1, 0x6c,
2208 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2209 0xc9, 0x30,
2210 0xd9, 0x03, 0x05, 0x63,
2211 0x53, 0x53, 0x47,
2212 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002213 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002214 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2215 0xd1, 0x2f,
2216 0xd1, 0x69,
2217 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2218 0xd1, 0x2d,
2219 0xd1, 0x62,
2220 0x55, 0x53,
2221 0xd7, 0x32,
2222 0xd3, 0xa2,
2223 0x2a,
2224 0xc9, 0xa1,
2225 0x28,
2226 0x26,
2227 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2228 0xcc, 0x4a,
2229 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2230 0xd0, 0x35,
2231 0xd7, 0xa1,
2232 0xcb, 0xc8, 0xc7, 0x24,
2233 0xc8, 0x22};
2234
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002235static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002236
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002237
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002238int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002239{
2240 QCBORDecodeContext DCtx;
2241 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002242
Laurence Lundbladeee851742020-01-08 08:37:05 -08002243 QCBORDecode_Init(&DCtx,
2244 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2245 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002247 //-------------------------
2248 // This text matches the magic number tag and the fraction tag
2249 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2250 return -2;
2251 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002252 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002253 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2254 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002256
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002257 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2258 return -4;
2259 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002260
2261#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002262 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002263 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002264 Item.val.uCount != 2) {
2265 return -5;
2266 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002267#else
2268 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2269 return -6;
2270 }
2271#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002272
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002273 // --------------------------------
2274 // This test decodes the very large tag, but it is not in
2275 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002276 QCBORDecode_Init(&DCtx,
2277 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2278 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002279 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2280 return -6;
2281 }
2282 if(Item.uTagBits) {
2283 return -7;
2284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002285
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002286 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002287 // This test sets up a caller-config list that includes the very large
2288 // tage and then matches it.
2289 QCBORDecode_Init(&DCtx,
2290 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2291 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002292 const uint64_t puList[] = {0x9192939495969798, 257};
2293 const QCBORTagListIn TL = {2, puList};
2294 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002296 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2297 return -8;
2298 }
2299 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2300 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2301 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2302 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2303 Item.val.uCount != 0) {
2304 return -9;
2305 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002306
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002307 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002308 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002309 const uint64_t puLongList[17] = {1,2,1};
2310 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002311 QCBORDecode_Init(&DCtx,
2312 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2313 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002314 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2315 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2316 return -11;
2317 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002318
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002319 // -----------------------
2320 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002321 QCBORDecode_Init(&DCtx,
2322 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2323 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002324 uint64_t puTags[16];
2325 QCBORTagListOut Out = {0, 4, puTags};
2326 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2327 return -12;
2328 }
2329 if(puTags[0] != 0x9192939495969798 ||
2330 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002331 puTags[2] != 0x06 ||
2332 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002333 return -13;
2334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002335
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002336 // ----------------------
2337 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002338 QCBORDecode_Init(&DCtx,
2339 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2340 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002341 QCBORTagListOut OutSmall = {0, 3, puTags};
2342 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2343 return -14;
2344 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002345
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002346 // ---------------
2347 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002348 QCBORDecode_Init(&DCtx,
2349 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2350 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002351 int n = CheckCSRMaps(&DCtx);
2352 if(n) {
2353 return n-2000;
2354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
Laurence Lundblade59289e52019-12-30 13:44:37 -08002356 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002357 QCBORDecode_Init(&DCtx,
2358 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2359 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002360
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002361 const uint64_t puTagList[] = {773, 1, 90599561};
2362 const QCBORTagListIn TagList = {3, puTagList};
2363 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002364
2365
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002366 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2367 return -100;
2368 }
2369 if(Item.uDataType != QCBOR_TYPE_MAP ||
2370 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2371 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2372 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2373 Item.val.uCount != 2 ||
2374 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2375 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2376 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2377 Out.uNumUsed != 3) {
2378 return -101;
2379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002380
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002381 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2382 return -102;
2383 }
2384 if(Item.uDataType != QCBOR_TYPE_MAP ||
2385 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2386 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2387 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2388 Item.val.uCount != 2 ||
2389 puTags[0] != 5859837686836516696 ||
2390 puTags[1] != 7 ||
2391 Out.uNumUsed != 2) {
2392 return -103;
2393 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002395 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2396 return -104;
2397 }
2398 if(Item.uDataType != QCBOR_TYPE_MAP ||
2399 Item.uTagBits ||
2400 Item.val.uCount != 5 ||
2401 puTags[0] != 0x0b ||
2402 Out.uNumUsed != 1) {
2403 return -105;
2404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002405
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002406 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2407 return -106;
2408 }
2409 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2410 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2411 Item.val.string.len != 12 ||
2412 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2413 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2414 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2415 Out.uNumUsed != 3) {
2416 return -105;
2417 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002418
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002419 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2420 return -107;
2421 }
2422 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2423 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2424 Item.val.string.len != 3 ||
2425 puTags[0] != 773 ||
2426 Out.uNumUsed != 1) {
2427 return -108;
2428 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002429
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002430 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2431 return -109;
2432 }
2433 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002434 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002435 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002436 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002437 puTags[11] != 0x0f ||
2438 Out.uNumUsed != 12) {
2439 return -110;
2440 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002441
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002442 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2443 return -111;
2444 }
2445 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2446 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2447 Item.val.string.len != 9 ||
2448 puTags[0] != 17 ||
2449 Out.uNumUsed != 1) {
2450 return -112;
2451 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002452
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002453 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2454 return -111;
2455 }
2456 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2457 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2458 Item.val.string.len != 2 ||
2459 puTags[0] != 17 ||
2460 Out.uNumUsed != 1) {
2461 return -112;
2462 }
2463
2464 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2465 return -113;
2466 }
2467 if(Item.uDataType != QCBOR_TYPE_MAP ||
2468 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2469 Item.val.uCount != 2 ||
2470 puTags[0] != 19 ||
2471 Out.uNumUsed != 1) {
2472 return -114;
2473 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002474
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002475 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2476 return -115;
2477 }
2478 if(Item.uDataType != QCBOR_TYPE_MAP ||
2479 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2480 Item.uTagBits ||
2481 Item.val.uCount != 1 ||
2482 puTags[0] != 9 ||
2483 Out.uNumUsed != 1) {
2484 return -116;
2485 }
2486
2487 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2488 return -116;
2489 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002490 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002491 Item.val.int64 != -7 ||
2492 Item.uTagBits ||
2493 Out.uNumUsed != 0) {
2494 return -117;
2495 }
2496
2497 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2498 return -118;
2499 }
2500 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2501 Item.val.string.len != 10 ||
2502 Item.uTagBits ||
2503 puTags[0] != 12 ||
2504 Out.uNumUsed != 1) {
2505 return -119;
2506 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002507
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002508 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2509 return -120;
2510 }
2511 if(Item.uDataType != QCBOR_TYPE_MAP ||
2512 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2513 Item.val.uCount != 1 ||
2514 puTags[0] != 0x17 ||
2515 Out.uNumUsed != 1) {
2516 return -121;
2517 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002518
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002519 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2520 return -122;
2521 }
2522 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2523 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2524 Item.val.int64 != -3 ||
2525 puTags[0] != 8 ||
2526 Out.uNumUsed != 1) {
2527 return -123;
2528 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002529
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002530 if(QCBORDecode_Finish(&DCtx)) {
2531 return -124;
2532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002534 return 0;
2535}
2536
2537
2538
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002539
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002540static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002541 0x83,
2542 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2543 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2544 0xA4,
2545 0x63, 0x42, 0x4E, 0x2B,
2546 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2547 0x18, 0x40,
2548 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2549 0x63, 0x42, 0x4E, 0x2D,
2550 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2551 0x38, 0x3F,
2552 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2553
2554
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002555static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002556
2557
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002558int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002559{
2560 QCBORDecodeContext DCtx;
2561 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002562 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002563
Laurence Lundbladeee851742020-01-08 08:37:05 -08002564 QCBORDecode_Init(&DCtx,
2565 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2566 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002567
2568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002569 //
2570 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2571 return -1;
2572 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2573 return -1;
2574 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002575
2576 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002577 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2578 return -1;
2579 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002580 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002581 return -1;
2582 }
2583
2584 //
2585 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2586 return -1;
2587 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002588 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002589 return -1;
2590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002591
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002592 //
2593 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2594 return -1;
2595 if(Item.uDataType != QCBOR_TYPE_MAP) {
2596 return -1;
2597 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002598
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002599 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2600 return -1;
2601 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2602 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002603 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002604 return -1;
2605 }
2606
2607 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2608 return -1;
2609 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2610 Item.uLabelType != QCBOR_TYPE_INT64 ||
2611 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002612 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002613 return -1;
2614 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002615
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002616 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2617 return -1;
2618 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2619 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002620 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002621 return -1;
2622 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002624 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2625 return -1;
2626 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2627 Item.uLabelType != QCBOR_TYPE_INT64 ||
2628 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002629 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002630 return -1;
2631 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002632
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002633 return 0;
2634}
2635
2636
2637
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002638static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002639 uint8_t uDataType,
2640 uint8_t uNestingLevel,
2641 uint8_t uNextNest,
2642 int64_t nLabel,
2643 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002644{
2645 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002646 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002648 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2649 if(Item.uDataType != uDataType) return -1;
2650 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002651 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2652 Item.uLabelType != QCBOR_TYPE_UINT64) {
2653 return -1;
2654 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002655 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2656 if(Item.label.int64 != nLabel) return -1;
2657 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002658 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002659 }
2660 }
2661 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302662 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002663
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002664 if(pItem) {
2665 *pItem = Item;
2666 }
2667 return 0;
2668}
2669
2670
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002671// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002672static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002673{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302674 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002675
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302676 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002677
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302678 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002679
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302680 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2681 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2682 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2683 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2684 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002685
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302686 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2687 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002688
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302689 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2690 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002691
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302692 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2693 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002694
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002695 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002696
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002697 return 0;
2698}
2699
2700
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002701/*
2702// cbor.me decoded output
2703{
2704 -23: {
2705 -20: {
2706 -18: "Organization",
2707 -17: "SSG",
2708 -15: "Confusion",
2709 -16: "San Diego",
2710 -14: "US"
2711 },
2712 -19: {
2713 -11: {
2714 -9: -7
2715 },
2716 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2717 }
2718 },
2719 -22: {
2720 -5: -3
2721 }
2722}
2723 */
2724
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002725
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002726static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002727 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2728 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2729 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2730 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2731 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2732 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2733 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2734 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2735 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2736
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002737int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002738{
2739 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002740
Laurence Lundbladeee851742020-01-08 08:37:05 -08002741 QCBORDecode_Init(&DCtx,
2742 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2743 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002744
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002745 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002746}
2747
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002748
2749
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002750int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002751{
2752 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002753
Laurence Lundbladeee851742020-01-08 08:37:05 -08002754 QCBORDecode_Init(&DCtx,
2755 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2756 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002757
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002758 QCBORItem Item;
2759 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002760
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002761 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2762 return -1;
2763 }
2764 if(Item.uDataType != QCBOR_TYPE_MAP) {
2765 return -2;
2766 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002767
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002768 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2769 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2770 return -3;
2771 }
2772
2773 return 0;
2774}
2775
2776
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002777// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002778static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002779 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2780 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2781 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2782 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2783 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2784 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002785 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2786 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2787 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2788 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002789
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002790int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002791{
2792 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002793
Laurence Lundbladeee851742020-01-08 08:37:05 -08002794 QCBORDecode_Init(&DCtx,
2795 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2796 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002797
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002798 return CheckCSRMaps(&DCtx);
2799}
2800
2801
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002802
Laurence Lundblade17ede402018-10-13 11:43:07 +08002803static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2804{
2805 UsefulOutBuf UOB;
2806 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002807
Laurence Lundblade17ede402018-10-13 11:43:07 +08002808 int i;
2809 for(i = 0; i < n; i++) {
2810 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2811 }
2812
2813 for(i = 0; i < n; i++) {
2814 UsefulOutBuf_AppendByte(&UOB, 0xff);
2815 }
2816 return UsefulOutBuf_OutUBuf(&UOB);
2817}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002818
2819
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002820static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002821{
2822 QCBORDecodeContext DC;
2823 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002824
Laurence Lundblade17ede402018-10-13 11:43:07 +08002825 int j;
2826 for(j = 0; j < nNestLevel; j++) {
2827 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002828 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002829 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2830 // Should be in error
2831 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2832 return -4;
2833 } else {
2834 return 0; // Decoding doesn't recover after an error
2835 }
2836 } else {
2837 // Should be no error
2838 if(nReturn) {
2839 return -9; // Should not have got an error
2840 }
2841 }
2842 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2843 return -7;
2844 }
2845 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002846 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002847 if(nReturn) {
2848 return -3;
2849 }
2850 return 0;
2851}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002852
2853
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002854int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002855{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302856 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002857 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002858 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002859 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002860 int nReturn = parse_indeflen_nested(Nested, i);
2861 if(nReturn) {
2862 return nReturn;
2863 }
2864 }
2865 return 0;
2866}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002867
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002868
Laurence Lundbladeee851742020-01-08 08:37:05 -08002869// [1, [2, 3]]
2870static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2871// No closing break
2872static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2873// Not enough closing breaks
2874static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2875// Too many closing breaks
2876static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2877// Unclosed indeflen inside def len
2878static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2879// confused tag
2880static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002881
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002882int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002883{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002884 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002885 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002886 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002887
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002888 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302889 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002890 QCBORDecodeContext DC;
2891 QCBORItem Item;
2892 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002893
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002894 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002895
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002896 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302897
2898 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2899 Item.uNestingLevel != 0 ||
2900 Item.uNextNestLevel != 1) {
2901 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002902 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002903
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002904 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302905 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2906 Item.uNestingLevel != 1 ||
2907 Item.uNextNestLevel != 1) {
2908 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002909 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002910
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002911 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302912 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2913 Item.uNestingLevel != 1 ||
2914 Item.uNextNestLevel != 2) {
2915 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002916 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002917
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002918 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002919 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302920 Item.uNestingLevel != 2 ||
2921 Item.uNextNestLevel != 2) {
2922 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002923 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002924
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002925 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002926 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302927 Item.uNestingLevel != 2 ||
2928 Item.uNextNestLevel != 0) {
2929 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002930 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002931
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002932 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302933 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002934 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002935
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002936 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002937 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002938
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002939 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002940
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002941 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002942
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002943 nResult = QCBORDecode_GetNext(&DC, &Item);
2944 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302945 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002946 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002947
Laurence Lundblade570fab52018-10-13 18:28:27 +08002948 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302949 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2950 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002951 }
2952
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002953
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002954 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002955 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002956
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002957 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002958
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002959 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002960
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002961 nResult = QCBORDecode_GetNext(&DC, &Item);
2962 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302963 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002964 }
2965
2966 nResult = QCBORDecode_GetNext(&DC, &Item);
2967 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302968 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002970
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002971 nResult = QCBORDecode_GetNext(&DC, &Item);
2972 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302973 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002974 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002975
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002976 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302977 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2978 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002979 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002980
2981
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002982 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002983 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002984
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002985 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002986
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002987 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002988
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002989 nResult = QCBORDecode_GetNext(&DC, &Item);
2990 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302991 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002992 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002993
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002994 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302995 if(nResult != QCBOR_ERR_BAD_BREAK) {
2996 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002997 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302998
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002999
Laurence Lundblade570fab52018-10-13 18:28:27 +08003000 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003001 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003002
Laurence Lundblade570fab52018-10-13 18:28:27 +08003003 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003004
Laurence Lundblade570fab52018-10-13 18:28:27 +08003005 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundblade570fab52018-10-13 18:28:27 +08003007 nResult = QCBORDecode_GetNext(&DC, &Item);
3008 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303009 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003010 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003011
Laurence Lundblade570fab52018-10-13 18:28:27 +08003012 nResult = QCBORDecode_GetNext(&DC, &Item);
3013 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303014 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003015 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003016
Laurence Lundblade570fab52018-10-13 18:28:27 +08003017 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303018 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3019 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003020 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003021
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303022 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003023 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003024
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303025 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003026
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303027 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003028
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303029 nResult = QCBORDecode_GetNext(&DC, &Item);
3030 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303031 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303032 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003033
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303034 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303035 if(nResult != QCBOR_ERR_BAD_BREAK) {
3036 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303037 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003038
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003039 return 0;
3040}
3041
Laurence Lundblade17ede402018-10-13 11:43:07 +08003042
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003043static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003044 0x81, // Array of length one
3045 0x7f, // text string marked with indefinite length
3046 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3047 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3048 0xff // ending break
3049};
3050
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003051static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303052 0x81, // Array of length one
3053 0x7f, // text string marked with indefinite length
3054 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3055 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3056 0xff // ending break
3057};
3058
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003059static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303060 0x81, // Array of length one
3061 0x7f, // text string marked with indefinite length
3062 0x01, 0x02, // Not a string
3063 0xff // ending break
3064};
3065
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003066static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303067 0x81, // Array of length one
3068 0x7f, // text string marked with indefinite length
3069 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3070 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3071 // missing end of string
3072};
3073
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003074static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303075 0xa1, // Array of length one
3076 0x7f, // text string marked with indefinite length
3077 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3078 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3079 0xff, // ending break
3080 0x01 // integer being labeled.
3081};
3082
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003083/**
3084 Make an indefinite length string
3085
3086 @param Storage Storage for string, must be 144 bytes in size
3087 @return The indefinite length string
3088
3089 This makes an array with one indefinite length string that has 7 chunks
3090 from size of 1 byte up to 64 bytes.
3091 */
3092static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303093{
3094 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003095
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303096 UsefulOutBuf_Init(&UOB, Storage);
3097 UsefulOutBuf_AppendByte(&UOB, 0x81);
3098 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003099
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003100 uint8_t uStringByte = 0;
3101 // Use of type int is intentional
3102 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3103 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303104 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003105 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3106 for(int j = 0; j < uChunkSize; j++) {
3107 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3108 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303109 }
3110 }
3111 UsefulOutBuf_AppendByte(&UOB, 0xff);
3112
3113 return UsefulOutBuf_OutUBuf(&UOB);
3114}
3115
3116static int CheckBigString(UsefulBufC BigString)
3117{
3118 if(BigString.len != 255) {
3119 return 1;
3120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003121
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303122 for(uint8_t i = 0; i < 255; i++){
3123 if(((const uint8_t *)BigString.ptr)[i] != i) {
3124 return 1;
3125 }
3126 }
3127 return 0;
3128}
3129
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303130
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003131int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303132{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303133 QCBORDecodeContext DC;
3134 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303135 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003136 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003137
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303138 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003139 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303140 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003141
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303142 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303143 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003145
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303146 if(QCBORDecode_GetNext(&DC, &Item)) {
3147 return -2;
3148 }
3149 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3150 return -3;
3151 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003152
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303153 if(QCBORDecode_GetNext(&DC, &Item)) {
3154 return -4;
3155 }
3156 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3157 return -5;
3158 }
3159 if(QCBORDecode_Finish(&DC)) {
3160 return -6;
3161 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303162
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303163 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003164 QCBORDecode_Init(&DC,
3165 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3166 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003167
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303168 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3169 return -7;
3170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003171
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303172 if(QCBORDecode_GetNext(&DC, &Item)) {
3173 return -8;
3174 }
3175 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3176 return -9;
3177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003178
Laurence Lundblade30816f22018-11-10 13:40:22 +07003179 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303180 return -10;
3181 }
3182
3183 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003184 QCBORDecode_Init(&DC,
3185 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3186 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003187
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303188 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3189 return -11;
3190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003191
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303192 if(QCBORDecode_GetNext(&DC, &Item)) {
3193 return -12;
3194 }
3195 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3196 return -13;
3197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003198
Laurence Lundblade30816f22018-11-10 13:40:22 +07003199 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303200 return -14;
3201 }
3202
3203 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003204 QCBORDecode_Init(&DC,
3205 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3206 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003207
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303208 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3209 return -15;
3210 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003211
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303212 if(QCBORDecode_GetNext(&DC, &Item)) {
3213 return -16;
3214 }
3215 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3216 return -17;
3217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003218
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303219 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3220 return -18;
3221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003222
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303223 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303224 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003225
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303226 QCBORDecode_GetNext(&DC, &Item);
3227 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303228 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003230
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303231 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303232 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003234
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303235 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003236 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303237
3238 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3239 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303240 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003242
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303243 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303244 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003245 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003246
Laurence Lundbladeee851742020-01-08 08:37:05 -08003247 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3248 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003249
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303250 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303251 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303252 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003254
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303255 QCBORDecode_GetNext(&DC, &Item);
3256 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303257 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303258 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003259 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303260 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003262
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303263 // ---- big bstr -----
3264 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003265
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303266 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3267 return -25;
3268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003269
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303270 if(QCBORDecode_GetNext(&DC, &Item)) {
3271 return -26;
3272 }
3273 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303274 return -26;
3275 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003276
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303277 if(QCBORDecode_GetNext(&DC, &Item)) {
3278 return -27;
3279 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303280 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303281 return -28;
3282 }
3283 if(CheckBigString(Item.val.string)) {
3284 return -3;
3285 }
3286 if(QCBORDecode_Finish(&DC)) {
3287 return -29;
3288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003289
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303290 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003291 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003292
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303293 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3294 return -30;
3295 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003296
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303297 QCBORDecode_GetNext(&DC, &Item);
3298 if(Item.uDataType != QCBOR_TYPE_MAP) {
3299 return -31;
3300 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003301
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303302 if(QCBORDecode_GetNext(&DC, &Item)){
3303 return -32;
3304 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003305 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3306 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303307 Item.uDataAlloc || !Item.uLabelAlloc ||
3308 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3309 return -33;
3310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003311
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303312 if(QCBORDecode_Finish(&DC)) {
3313 return -34;
3314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003315
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003316 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003317}
3318
3319
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003320int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303321{
3322 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003323 QCBORError nCBORError;
3324
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003325
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303326 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003327 QCBORDecode_Init(&DC,
3328 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3329 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003330
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003331 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003332
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003333 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3334 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303335 return -1;
3336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003337
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003338 if(CheckCSRMaps(&DC)) {
3339 return -2;
3340 }
3341
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303342 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003343 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003344 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003345
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303346 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003347 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303348 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003349
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303350 QCBORItem Item1, Item2, Item3, Item4;
3351 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003352 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303353 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3354 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003355 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303356 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003357 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303358 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003359 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303360 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003361 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303362 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003363 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003364
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303365 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003366
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303367 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303368 Item1.uDataType != QCBOR_TYPE_INT64 ||
3369 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003370 Item1.uDataAlloc != 0 ||
3371 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003372 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003373 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003374 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303376
3377 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003378 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303379 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003380 Item2.uDataAlloc != 0 ||
3381 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303382 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003383 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003384
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303385 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003386 Item3.uDataAlloc == 0 ||
3387 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003388 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003389 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003391
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303392 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003393 Item4.uDataAlloc == 0 ||
3394 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003395 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003396 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003397 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003398
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303399 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003400 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003401 QCBORDecode_Init(&DC,
3402 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3403 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303404 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3405 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003406 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303407 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003408 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003409 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003410 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303411 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3412 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3413 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3414 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3415 }
3416 }
3417 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003418 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003419 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303420 }
3421
3422 return 0;
3423}
3424
Laurence Lundbladef6531662018-12-04 10:42:22 +09003425
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303426
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003427int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003428{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003429 // Set up the decoder with a tiny bit of CBOR to parse because
3430 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003431 QCBORDecodeContext DC;
3432 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3433 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003434
Laurence Lundbladef6531662018-12-04 10:42:22 +09003435 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003436 // Then fish into the internals of the decode context
3437 // to get the allocator function so it can be called directly.
3438 // Also figure out how much pool is available for use
3439 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003440 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003441 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3442 if(nError) {
3443 return -9;
3444 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003445 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3446 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3447 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003448
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003449 // First test -- ask for one more byte than available and see failure
3450 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003451 if(!UsefulBuf_IsNULL(Allocated)) {
3452 return -1;
3453 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003454
Laurence Lundbladef6531662018-12-04 10:42:22 +09003455 // Re do the set up for the next test that will do a successful alloc,
3456 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003457 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003458 pAlloc = DC.StringAllocator.pfAllocator;
3459 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3460 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003461
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003462 // Allocate one byte less than available and see success
3463 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003464 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3465 return -2;
3466 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003467 // Ask for some more and see failure
3468 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003469 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3470 return -3;
3471 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003472 // Free the first allocate, retry the second and see success
3473 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3474 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003475 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3476 return -4;
3477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003478
Laurence Lundbladef6531662018-12-04 10:42:22 +09003479 // Re do set up for next test that involves a successful alloc,
3480 // and a successful realloc and a failed realloc
3481 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003482 pAlloc = DC.StringAllocator.pfAllocator;
3483 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003484
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003485 // Allocate half the pool and see success
3486 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003487 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3488 return -5;
3489 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003490 // Reallocate to take up the whole pool and see success
3491 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003492 if(UsefulBuf_IsNULL(Allocated2)) {
3493 return -6;
3494 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003495 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003496 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3497 return -7;
3498 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003499 // Try to allocate more to be sure there is failure after a realloc
3500 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3501 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003502 return -8;
3503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003504
Laurence Lundbladef6531662018-12-04 10:42:22 +09003505 return 0;
3506}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003507
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003508
3509/* Just enough of an allocator to test configuration of one */
3510static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3511{
3512 (void)pOldMem; // unused variable
3513
3514 if(uNewSize) {
3515 // Assumes the context pointer is the buffer and
3516 // nothing too big will ever be asked for.
3517 // This is only good for this basic test!
3518 return (UsefulBuf) {pCtx, uNewSize};
3519 } else {
3520 return NULLUsefulBuf;
3521 }
3522}
3523
3524
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003525int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003526{
3527 // Set up the decoder with a tiny bit of CBOR to parse because
3528 // nothing can be done with it unless that is set up.
3529 QCBORDecodeContext DC;
3530 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3531 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3532
3533 uint8_t pAllocatorBuffer[50];
3534
3535 // This is really just to test that this call works.
3536 // The full functionality of string allocators is tested
3537 // elsewhere with the MemPool internal allocator.
3538 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3539
3540 QCBORItem Item;
3541 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3542 return -1;
3543 }
3544
3545 if(Item.uDataAlloc == 0 ||
3546 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3547 Item.val.string.ptr != pAllocatorBuffer) {
3548 return -2;
3549 }
3550
3551 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3552 return -3;
3553 }
3554
3555 return 0;
3556}
3557
Laurence Lundblade59289e52019-12-30 13:44:37 -08003558
3559#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladea826c502020-05-10 21:07:00 -07003560/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08003561 [
3562 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003563 4([-20, 4759477275222530853136]),
3564 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003565 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003566 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003567 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07003568 5([ 9223372036854775806, -4759477275222530853137])
3569 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003570 ]
3571 */
3572
3573static const uint8_t spExpectedExponentsAndMantissas[] = {
3574 0x87,
3575 0xC4, 0x82, 0x20,
3576 0x03,
3577 0xC4, 0x82, 0x33,
3578 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3579 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3580 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3581 0xC5, 0x82, 0x19, 0x01, 0x2C,
3582 0x18, 0x64,
3583 0xC5, 0x82, 0x33,
3584 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3585 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3586 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07003587 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3588 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003589 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3590 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3591};
3592
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003593int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003594{
3595 QCBORDecodeContext DC;
3596 QCBORError nCBORError;
3597 QCBORItem item;
3598
Laurence Lundblade17af4902020-01-07 19:11:55 -08003599 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3600 0x06, 0x07, 0x08, 0x09, 0x010};
3601 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003602
3603
3604 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3605
3606 nCBORError = QCBORDecode_GetNext(&DC, &item);
3607 if(nCBORError != QCBOR_SUCCESS) {
3608 return 1;
3609 }
3610
3611 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3612 return 2;
3613 }
3614
3615 nCBORError = QCBORDecode_GetNext(&DC, &item);
3616 if(nCBORError != QCBOR_SUCCESS) {
3617 return 3;
3618 }
3619
3620 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3621 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3622 item.val.expAndMantissa.nExponent != -1) {
3623 return 4;
3624 }
3625
3626 nCBORError = QCBORDecode_GetNext(&DC, &item);
3627 if(nCBORError != QCBOR_SUCCESS) {
3628 return 5;
3629 }
3630
3631 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3632 item.val.expAndMantissa.nExponent != -20 ||
3633 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3634 return 6;
3635 }
3636
3637 nCBORError = QCBORDecode_GetNext(&DC, &item);
3638 if(nCBORError != QCBOR_SUCCESS) {
3639 return 7;
3640 }
3641
3642 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3643 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3644 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3645 return 8;
3646 }
3647
3648 nCBORError = QCBORDecode_GetNext(&DC, &item);
3649 if(nCBORError != QCBOR_SUCCESS) {
3650 return 9;
3651 }
3652
3653 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3654 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3655 item.val.expAndMantissa.nExponent != 300) {
3656 return 10;
3657 }
3658
Laurence Lundbladea826c502020-05-10 21:07:00 -07003659 // 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003660 nCBORError = QCBORDecode_GetNext(&DC, &item);
3661 if(nCBORError != QCBOR_SUCCESS) {
3662 return 11;
3663 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003664 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3665 item.val.expAndMantissa.nExponent != -20 ||
3666 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3667 return 12;
3668 }
3669
Laurence Lundbladea826c502020-05-10 21:07:00 -07003670 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade59289e52019-12-30 13:44:37 -08003671 nCBORError = QCBORDecode_GetNext(&DC, &item);
3672 if(nCBORError != QCBOR_SUCCESS) {
3673 return 13;
3674 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003675 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3676 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3677 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3678 return 14;
3679 }
3680
Laurence Lundbladea826c502020-05-10 21:07:00 -07003681 // 5([ 9223372036854775806, -4759477275222530853137])
3682 nCBORError = QCBORDecode_GetNext(&DC, &item);
3683 if(nCBORError != QCBOR_SUCCESS) {
3684 return 13;
3685 }
3686 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3687 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3688 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3689 return 14;
3690 }
3691
3692
3693 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003694 nCBORError = QCBORDecode_GetNext(&DC, &item);
3695 if(nCBORError != QCBOR_SUCCESS) {
3696 return 15;
3697 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003698 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3699 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3700 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3701 return 16;
3702 }
3703
3704 /* Now encode some stuff and then decode it */
3705 uint8_t pBuf[40];
3706 QCBOREncodeContext EC;
3707 UsefulBufC Encoded;
3708
3709 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3710 QCBOREncode_OpenArray(&EC);
3711 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3712 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3713 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3714 QCBOREncode_CloseArray(&EC);
3715 QCBOREncode_Finish(&EC, &Encoded);
3716
3717
3718 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3719 nCBORError = QCBORDecode_GetNext(&DC, &item);
3720 if(nCBORError != QCBOR_SUCCESS) {
3721 return 13;
3722 }
3723
3724 nCBORError = QCBORDecode_GetNext(&DC, &item);
3725 if(nCBORError != QCBOR_SUCCESS) {
3726 return 13;
3727 }
3728
3729 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3730 item.val.expAndMantissa.nExponent != 1000 ||
3731 item.val.expAndMantissa.Mantissa.nInt != 999) {
3732 return 15;
3733 }
3734
3735 nCBORError = QCBORDecode_GetNext(&DC, &item);
3736 if(nCBORError != QCBOR_SUCCESS) {
3737 return 13;
3738 }
3739
3740 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3741 item.val.expAndMantissa.nExponent != INT32_MIN ||
3742 item.val.expAndMantissa.Mantissa.nInt != 100) {
3743 return 15;
3744 }
3745
3746 nCBORError = QCBORDecode_GetNext(&DC, &item);
3747 if(nCBORError != QCBOR_SUCCESS) {
3748 return 13;
3749 }
3750
3751 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3752 item.val.expAndMantissa.nExponent != INT32_MAX ||
3753 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3754 return 12;
3755 }
3756
3757 return 0;
3758}
3759
3760
3761static struct FailInput ExponentAndMantissaFailures[] = {
3762 // Exponent > INT64_MAX
3763 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3764 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3765 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3766 // Mantissa > INT64_MAX
3767 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3768 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3769 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3770 // End of input
3771 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END},
3772 // End of input
3773 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END},
3774 // bad content for big num
3775 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3776 // bad content for big num
3777 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3778 // Bad integer for exponent
3779 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3780 // Bad integer for mantissa
3781 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3782 // 3 items in array
3783 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3784 // unterminated indefinite length array
3785 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3786 // Empty array
3787 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3788 // Second is not an integer
3789 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3790 // First is not an integer
3791 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3792 // Not an array
3793 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3794};
3795
3796
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003797int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003798{
3799 return ProcessFailures(ExponentAndMantissaFailures,
3800 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3801}
3802
3803#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003804
3805
3806
3807/*
3808 Some basic CBOR with map and array used in a lot of tests.
3809 The map labels are all strings
3810
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003811 {
3812 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003813 "an array of two strings": [
3814 "string1", "string2"
3815 ],
3816 "map in a map": {
3817 "bytes 1": h'78787878',
3818 "bytes 2": h'79797979',
3819 "another int": 98,
3820 "text 2": "lies, damn lies and statistics"
3821 }
3822 }
3823 */
3824
Laurence Lundbladeeb0b53d2020-04-14 18:34:25 -07003825#include "qcbor/qcbor_decode_map.h"
Laurence Lundblade1341c592020-04-11 14:19:05 -07003826#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003827
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003828static char strbuf[10];
3829const char *PrintType(uint8_t type) {
3830 switch(type) {
3831 case QCBOR_TYPE_INT64: return "INT64";
3832 case QCBOR_TYPE_UINT64: return "UINT64";
3833 case QCBOR_TYPE_ARRAY: return "ARRAY";
3834 case QCBOR_TYPE_MAP: return "MAP";
3835 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
3836 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
3837 default:
3838 sprintf(strbuf, "%d", type);
3839 return strbuf;
3840 }
3841}
3842
3843
3844void PrintItem(QCBORItem Item)
3845{
3846 printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":"");
3847 if(Item.uLabelType) {
3848 printf("Label: %s ", PrintType(Item.uLabelType));
3849 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3850 printf("%lld\n", Item.label.int64);
3851 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
3852 printf("\"%4.4s\"\n", Item.label.string.ptr);
3853 }
3854 }
3855}
3856
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003857
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003858int32_t EnterMapTest()
3859{
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003860 QCBORItem Item1, Item2, Item3;
3861 int64_t nDecodedInt1, nDecodedInt2;
3862 UsefulBufC B1, B2, S1, S2, S3;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003863
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003864 QCBORDecodeContext DCtx;
3865 QCBORError nCBORError;
3866
3867 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003868
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003869 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003870
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003871 QCBORDecode_GetIntInMapSZ(&DCtx, "first integer", &nDecodedInt1);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003872
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003873 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
3874 QCBORDecode_GetIntInMapSZ(&DCtx, "another int", &nDecodedInt2);
3875 QCBORDecode_GetBstrInMapSZ(&DCtx, "bytes 1", &B1);
3876 QCBORDecode_GetBstrInMapSZ(&DCtx, "bytes 2", &B2);
3877 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
3878 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003879
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003880 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3881 QCBORDecode_GetNext(&DCtx, &Item1);
3882 QCBORDecode_GetNext(&DCtx, &Item2);
3883 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
3884 return -400;
3885 }
3886 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade3f9ef042020-04-14 13:15:51 -07003887
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003888 // Parse the same array again using GetText() instead of GetItem()
3889 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3890 QCBORDecode_GetText(&DCtx, &S2);
3891 QCBORDecode_GetText(&DCtx, &S3);
3892 // TODO, check for end of array?
3893 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003894
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003895 QCBORDecode_ExitMap(&DCtx);
3896
3897 nCBORError = QCBORDecode_Finish(&DCtx);
3898
3899 if(nCBORError) {
3900 return (int32_t)nCBORError;
3901 }
3902
3903 if(nDecodedInt1 != 42) {
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003904 return 1001;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003905 }
3906
Laurence Lundblade1341c592020-04-11 14:19:05 -07003907 if(nDecodedInt2 != 98) {
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003908 return 1002;
3909 }
3910
3911 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
3912 UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){
3913 return 1003;
3914 }
3915
3916 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
3917 UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){
3918 return 1004;
3919 }
3920
3921 if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){
3922 return 1005;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003923 }
3924
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003925 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
3926 return 1006;
3927 }
3928
3929 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
3930 return 1007;
3931 }
3932
3933 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
3934 return 1008;
3935 }
3936
3937 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
3938 return 1009;
3939 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07003940
3941 // These tests confirm the cursor is at the right place after entering a map or array
3942
3943 // Confirm cursor is at right place
3944 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
3945 QCBORDecode_EnterMap(&DCtx);
3946 QCBORDecode_GetNext(&DCtx, &Item1);
3947 if(Item1.uDataType != QCBOR_TYPE_INT64) {
3948 return 2001;
3949 }
3950
3951
3952 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
3953 QCBORDecode_GetNext(&DCtx, &Item1);
3954 QCBORDecode_GetNext(&DCtx, &Item1);
3955 QCBORDecode_EnterArray(&DCtx);
3956 QCBORDecode_GetNext(&DCtx, &Item1);
3957 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
3958 return 2002;
3959 }
3960
3961 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
3962 QCBORDecode_EnterMap(&DCtx);
3963 //QCBORDecode_GetNext(&DCtx, &Item1);
3964 //QCBORDecode_GetNext(&DCtx, &Item1);
3965 //QCBORDecode_GetNext(&DCtx, &Item1);
3966 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
3967 QCBORDecode_GetNext(&DCtx, &Item1);
3968 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
3969 return 2003;
3970 }
3971
3972 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
3973 QCBORDecode_EnterMap(&DCtx);
3974 QCBORDecode_GetNext(&DCtx, &Item1);
3975 QCBORDecode_GetNext(&DCtx, &Item1);
3976 QCBORDecode_GetNext(&DCtx, &Item1);
3977 QCBORDecode_GetNext(&DCtx, &Item1);
3978 QCBORDecode_GetNext(&DCtx, &Item1);
3979 QCBORDecode_GetNext(&DCtx, &Item1);
3980 QCBORDecode_GetNext(&DCtx, &Item1);
3981 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3982 QCBORDecode_GetNext(&DCtx, &Item1);
3983 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
3984 return 2003;
3985 }
3986
3987
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003988 return 0;
3989}
3990
3991
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003992struct NumberConversion {
3993 char *szDescription;
3994 UsefulBufC CBOR;
3995 int64_t nConvertedToInt64;
3996 QCBORError uErrorInt64;
3997 uint64_t uConvertToUInt64;
3998 QCBORError uErrorUint64;
3999 double dConvertToDouble;
4000 QCBORError uErrorDouble;
4001};
4002
4003static struct NumberConversion NumberConversions[] = {
4004 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004005 "Big float 3 * 2^^2",
4006 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
4007 12,
4008 QCBOR_SUCCESS,
4009 12,
4010 QCBOR_SUCCESS,
4011 12.0,
4012 QCBOR_SUCCESS
4013 },
4014 // | 18446744073709551615 | 0x1bffffffffffffffff
4015 {
4016 "18446744073709551615",
4017 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4018 0,
4019 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4020 18446744073709551615ULL,
4021 QCBOR_SUCCESS,
4022 18446744073709551615.0,
4023 QCBOR_SUCCESS
4024 },
4025
4026 /* {
4027 "negative bignum 0xc349010000000000000000",
4028 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4029 0,
4030 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4031 0,
4032 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4033 -18446744073709551617.0,
4034 QCBOR_SUCCESS
4035 }, */
4036 {
4037 "positive bignum 0xffff",
4038 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4039 65536-1,
4040 QCBOR_SUCCESS,
4041 0xffff,
4042 QCBOR_SUCCESS,
4043 65535.0,
4044 QCBOR_SUCCESS
4045 },
4046 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004047 "Postive integer 0",
4048 {(uint8_t[]){0x0}, 1},
4049 0LL,
4050 QCBOR_SUCCESS,
4051 0ULL,
4052 QCBOR_SUCCESS,
4053 0.0,
4054 QCBOR_SUCCESS
4055 },
4056 {
4057 "-18446744073709551616",
4058 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4059 -9223372036854775807-1, // INT64_MIN
4060 QCBOR_SUCCESS,
4061 0ULL,
4062 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4063 -9223372036854775808.0,
4064 QCBOR_SUCCESS
4065 },
4066 {
4067 "Floating point value 100.3",
4068 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
4069 100L,
4070 QCBOR_SUCCESS,
4071 100ULL,
4072 QCBOR_SUCCESS,
4073 100.3,
4074 QCBOR_SUCCESS
4075 },
4076 {
4077 "Floating point value NaN 0xfa7fc00000",
4078 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
4079 0,
4080 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4081 0,
4082 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4083 NAN,
4084 QCBOR_SUCCESS
4085 },
4086 {
4087 "Floating point value -4",
4088 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
4089 -4,
4090 QCBOR_SUCCESS,
4091 0,
4092 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4093 -4.0,
4094 QCBOR_SUCCESS
4095 },
4096 {
4097 "Decimal fraction 3/10",
4098 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
4099 0,
4100 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4101 0,
4102 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4103 0.30000000000000004,
4104 QCBOR_SUCCESS
4105 }
4106};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004107
4108
4109
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004110
4111int32_t IntegerConvertTest2()
4112{
4113 const size_t nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
4114
4115 for(struct NumberConversion *pF = NumberConversions; pF < NumberConversions + nNumTests; pF++) {
4116 // Set up the decoding context including a memory pool so that
4117 // indefinite length items can be checked
4118 QCBORDecodeContext DCtx;
4119 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4120 UsefulBuf_MAKE_STACK_UB(Pool, 100);
4121 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4122 if(nCBORError) {
4123 return -9;
4124 }
4125
4126 int64_t nInt;
4127 DCtx.uLastError = 0;
4128 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
4129 if(QCBORDecode_GetLastError(&DCtx) != pF->uErrorInt64) {
4130 return -99;
4131 }
4132 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
4133 return -888;
4134 }
4135
4136
4137 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4138 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4139 if(nCBORError) {
4140 return -9;
4141 }
4142 uint64_t uInt;
4143 DCtx.uLastError = 0;
4144 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
4145 if(QCBORDecode_GetLastError(&DCtx) != pF->uErrorUint64) {
4146 return -99;
4147 }
4148 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
4149 return -888;
4150 }
4151
4152
4153 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4154 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4155 if(nCBORError) {
4156 return -9;
4157 }
4158 double d;
4159 DCtx.uLastError = 0;
4160 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
4161 if(QCBORDecode_GetLastError(&DCtx) != pF->uErrorDouble) {
4162 return -99;
4163 }
4164 if(pF->uErrorDouble == QCBOR_SUCCESS) {
4165 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004166 // NaN's can't be compared for equality. A NaN is
4167 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004168 if(!isnan(d)) {
4169 return -4;
4170 }
4171 } else {
4172 // TODO: this comparison may need a margin of error
4173 if(pF->dConvertToDouble != d) {
4174 return -5;
4175 }
4176 }
4177 }
4178 }
4179
4180 return 0;
4181}
4182
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004183int32_t IntegerConvertTest()
4184{
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004185 (void)IntegerConvertTest2();
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004186
4187 QCBORDecodeContext DCtx;
4188 QCBORError nCBORError;
4189
Laurence Lundbladea826c502020-05-10 21:07:00 -07004190 /* exponent, mantissa
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004191 [
4192 4([-1, 3]),
4193 4([-20, 4759477275222530853136]),
4194 4([9223372036854775807, -4759477275222530853137]),
4195 5([300, 100]),
4196 5([-20, 4759477275222530853136]),
4197 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004198 5([ 9223372036854775806, -4759477275222530853137])
4199 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004200 ]
4201 */
4202
4203 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4204
4205 QCBORItem Item;
4206 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4207 if(nCBORError) {
4208 return -1;
4209 }
4210
4211 int64_t integer;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004212 // 4([-1, 3]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004213 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
4214 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4215 return -2;
4216 }
4217 DCtx.uLastError = 0; // TODO: a method for this
4218
Laurence Lundbladea826c502020-05-10 21:07:00 -07004219 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004220 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
4221 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4222 return -2;
4223 }
4224 DCtx.uLastError = 0; // TODO: a method for this
4225
Laurence Lundbladea826c502020-05-10 21:07:00 -07004226 // 4([9223372036854775807, -4759477275222530853137]),
4227 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
4228 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4229 return -2;
4230 }
4231 DCtx.uLastError = 0; // TODO: a method for this
4232
4233 // 5([300, 100]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004234 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
4235 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4236 return -2;
4237 }
4238 DCtx.uLastError = 0; // TODO: a method for this
4239
Laurence Lundbladea826c502020-05-10 21:07:00 -07004240 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004241 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
4242 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4243 return -2;
4244 }
4245 DCtx.uLastError = 0; // TODO: a method for this
4246
Laurence Lundbladea826c502020-05-10 21:07:00 -07004247 // 5([-9223372036854775807, -4759477275222530853137])
4248 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
4249 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4250 return -2;
4251 }
4252 DCtx.uLastError = 0; // TODO: a method for this
4253
4254 // 5([ 9223372036854775806, -4759477275222530853137])
4255 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
4256 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4257 return -2;
4258 }
4259 DCtx.uLastError = 0; // TODO: a method for this
4260
4261 // 5([ 9223372036854775806, 9223372036854775806])]
4262 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
4263 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4264 return -2;
4265 }
4266 DCtx.uLastError = 0; // TODO: a method for this
4267
4268
4269
4270 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4271
4272 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4273 if(nCBORError) {
4274 return -1;
4275 }
4276
4277 uint64_t uinteger;
4278 // 4([-1, 3]),
4279 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
4280 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4281 return -2;
4282 }
4283 DCtx.uLastError = 0; // TODO: a method for this
4284
4285 // 4([-20, 4759477275222530853136]),
4286 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
4287 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4288 return -2;
4289 }
4290 DCtx.uLastError = 0; // TODO: a method for this
4291
4292 // 4([9223372036854775807, -4759477275222530853137]),
4293 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
4294 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4295 return -2;
4296 }
4297 DCtx.uLastError = 0; // TODO: a method for this
4298
4299 // 5([300, 100]),
4300 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
4301 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4302 return -2;
4303 }
4304 DCtx.uLastError = 0; // TODO: a method for this
4305
4306 // 5([-20, 4759477275222530853136]),
4307 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
4308 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4309 return -2;
4310 }
4311 DCtx.uLastError = 0; // TODO: a method for this
4312
4313 // 5([-9223372036854775807, -4759477275222530853137])
4314 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
4315 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4316 return -2;
4317 }
4318 DCtx.uLastError = 0; // TODO: a method for this
4319
4320 // 5([ 9223372036854775806, -4759477275222530853137])
4321 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
4322 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4323 return -2;
4324 }
4325 DCtx.uLastError = 0; // TODO: a method for this
4326
4327 // 5([ 9223372036854775806, 9223372036854775806])]
4328 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
4329 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
4330 return -2;
4331 }
4332 DCtx.uLastError = 0; // TODO: a method for this
4333
4334
4335
4336 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4337 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4338 if(nCBORError) {
4339 return -1;
4340 }
4341
4342 double dResult;
4343 // 4([-1, 3]),
4344 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
4345 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_SUCCESS &&
4346 dResult != 0.3) {
4347 return -2;
4348 }
4349
4350 // 4([-20, 4759477275222530853136]),
4351 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
4352 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_SUCCESS &&
4353 dResult != 47.408855671161923) {
4354 return -2;
4355 }
4356
4357 // 4([9223372036854775807, -4759477275222530853137]),
4358 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
4359 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_SUCCESS &&
4360 dResult != -INFINITY) {
4361 return -2;
4362 }
4363
4364 // 5([300, 100]),
4365 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
4366 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_SUCCESS &&
4367 dResult != -INFINITY) {
4368 return -2;
4369 }
4370
4371 // 5([-20, 4759477275222530853136]),
4372 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
4373 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_SUCCESS &&
4374 dResult != 4521260802379792.0) {
4375 return -2;
4376 }
4377
4378 // 5([-9223372036854775807, -4759477275222530853137])
4379 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
4380 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_SUCCESS &&
4381 dResult != -0.0) {
4382 return -2;
4383 }
4384
4385 // 5([9223372036854775806, 9223372036854775806])]
4386 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
4387 if(QCBORDecode_GetLastError(&DCtx) != QCBOR_SUCCESS &&
4388 dResult != INFINITY) {
4389 return -2;
4390 }
4391
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004392 return 0;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004393}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07004394
4395
Laurence Lundbladee3553422020-05-02 11:11:17 -07004396int32_t CBORSequenceDecodeTests(void)
4397{
4398 QCBORDecodeContext DCtx;
4399 QCBORItem Item;
4400 QCBORError uCBORError;
4401
4402 // --- Test a sequence with extra bytes ---
4403
4404 // The input for the date test happens to be a sequence so it
4405 // is reused. It is a sequence because it doesn't start as
4406 // an array or map.
4407 QCBORDecode_Init(&DCtx,
4408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
4409 QCBOR_DECODE_MODE_NORMAL);
4410
4411 // Get the first item
4412 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4413 if(uCBORError != QCBOR_SUCCESS) {
4414 return 1;
4415 }
4416 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
4417 return 2;
4418 }
4419
4420 // Get a second item
4421 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4422 if(uCBORError != QCBOR_SUCCESS) {
4423 return 2;
4424 }
4425 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
4426 return 3;
4427 }
4428
4429 // A sequence can have stuff at the end that may
4430 // or may not be valid CBOR. The protocol decoder knows
4431 // when to stop by definition of the protocol, not
4432 // when the top-level map or array is ended.
4433 // Finish still has to be called to know that
4434 // maps and arrays (if there were any) were closed
4435 // off correctly. When called like this it
4436 // must return the error QCBOR_ERR_EXTRA_BYTES.
4437 uCBORError = QCBORDecode_Finish(&DCtx);
4438 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
4439 return 4;
4440 }
4441
4442
4443 // --- Test an empty input ----
4444 uint8_t empty[1];
4445 UsefulBufC Empty = {empty, 0};
4446 QCBORDecode_Init(&DCtx,
4447 Empty,
4448 QCBOR_DECODE_MODE_NORMAL);
4449
4450 uCBORError = QCBORDecode_Finish(&DCtx);
4451 if(uCBORError != QCBOR_SUCCESS) {
4452 return 5;
4453 }
4454
4455
4456 // --- Sequence with unclosed indefinite length array ---
4457 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
4458
4459 QCBORDecode_Init(&DCtx,
4460 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
4461 QCBOR_DECODE_MODE_NORMAL);
4462
4463 // Get the first item
4464 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4465 if(uCBORError != QCBOR_SUCCESS) {
4466 return 7;
4467 }
4468 if(Item.uDataType != QCBOR_TYPE_INT64) {
4469 return 8;
4470 }
4471
4472 // Get a second item
4473 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4474 if(uCBORError != QCBOR_SUCCESS) {
4475 return 9;
4476 }
4477 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4478 return 10;
4479 }
4480
4481 // Try to finish before consuming all bytes to confirm
4482 // that the still-open error is returned.
4483 uCBORError = QCBORDecode_Finish(&DCtx);
4484 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
4485 return 11;
4486 }
4487
4488
4489 // --- Sequence with a closed indefinite length array ---
4490 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
4491
4492 QCBORDecode_Init(&DCtx,
4493 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
4494 QCBOR_DECODE_MODE_NORMAL);
4495
4496 // Get the first item
4497 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4498 if(uCBORError != QCBOR_SUCCESS) {
4499 return 12;
4500 }
4501 if(Item.uDataType != QCBOR_TYPE_INT64) {
4502 return 13;
4503 }
4504
4505 // Get a second item
4506 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4507 if(uCBORError != QCBOR_SUCCESS) {
4508 return 14;
4509 }
4510 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4511 return 15;
4512 }
4513
4514 // Try to finish before consuming all bytes to confirm
4515 // that the still-open error is returned.
4516 uCBORError = QCBORDecode_Finish(&DCtx);
4517 if(uCBORError != QCBOR_SUCCESS) {
4518 return 16;
4519 }
4520
4521
4522 return 0;
4523}