blob: 16d2b2eead601a0f56894e90824f0da6756d9294 [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,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700541 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800542
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700543// Same as above, but with indefinite lengths.
544static const uint8_t pValidMapIndefEncoded[] = {
5450xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5460x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5470x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5480x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5490x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5500x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5510x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5520x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5530x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5540x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5550x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5560x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5570x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5580x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5590x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5600x73, 0xff, 0xff};
561
562
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800563static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700564 size_t nLen,
565 int64_t *pInt1,
566 int64_t *pInt2,
567 const uint8_t **pBuf3,
568 size_t *pBuf3Len,
569 const uint8_t **pBuf4,
570 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800571{
572 QCBORDecodeContext DCtx;
573 QCBORItem Item;
574 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800575
Laurence Lundbladeee851742020-01-08 08:37:05 -0800576 QCBORDecode_Init(&DCtx,
577 (UsefulBufC){pEncoded, nLen},
578 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 // Make sure the first thing is a map
581 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
582 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800583
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800584 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800585 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800586 goto Done;
587 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800588
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589 // Second integer
590 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
591 goto Done;
592 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800593
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800594 // First string
595 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
596 goto Done;
597 *pBuf3 = Item.val.string.ptr;
598 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800599
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800600 // Second string
601 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
602 goto Done;
603 *pBuf4 = Item.val.string.ptr;
604 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800607
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800608Done:
609 return(nReturn);
610}
611
612
613
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800614
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800615int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800616{
617 uint8_t *pEncoded;
618 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800619
Laurence Lundblade5e390822019-01-06 12:35:01 -0800620 int64_t i1=0, i2=0;
621 size_t i3=0, i4=0;
622 const uint8_t *s3= (uint8_t *)"";
623 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800624
625
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800626 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
627 return(-1);
628 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800629
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800630 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800631
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800632 if(i1 != 23 ||
633 i2 != 6000 ||
634 i3 != 8 ||
635 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530636 memcmp("galactic", s3, 8) !=0 ||
637 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800638 return(-1);
639 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800640
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800641 return(0);
642}
643
644
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700645/*
646 [
647 0,
648 [],
649 [
650 [],
651 [
652 0
653 ],
654 {},
655 {
656 1: {},
657 2: {},
658 3: []
659 }
660 ]
661 ]
662 */
663static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
664 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
665
Laurence Lundblade02625d42020-06-25 14:41:41 -0700666/* Same as above, but with indefinte lengths */
667static uint8_t sEmptiesIndef[] = {
6680x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700669 0x00,
670 0x9F,
671 0xFF,
672 0x9F,
673 0x9F,
674 0xFF,
675 0x9F,
676 0x00,
677 0xFF,
678 0xBF,
679 0xFF,
680 0xBF,
681 0x01,
682 0xBF,
683 0xFF,
684 0x02,
685 0xBF,
686 0xFF,
687 0x03,
688 0x9F,
689 0xFF,
690 0xFF,
691 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700692 0xFF};
693
694
695
696static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700697{
698 QCBORDecodeContext DCtx;
699 QCBORItem Item;
700
Laurence Lundbladeee851742020-01-08 08:37:05 -0800701 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700702 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800703 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700704
705 // Array with 3 items
706 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
707 Item.uDataType != QCBOR_TYPE_ARRAY ||
708 Item.uNestingLevel != 0 ||
709 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700710 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700711 return -1;
712 }
713
714 // An integer 0
715 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
716 Item.uDataType != QCBOR_TYPE_INT64 ||
717 Item.uNestingLevel != 1 ||
718 Item.uNextNestLevel != 1 ||
719 Item.val.uint64 != 0) {
720 return -2;
721 }
722
723 // An empty array
724 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
725 Item.uDataType != QCBOR_TYPE_ARRAY ||
726 Item.uNestingLevel != 1 ||
727 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700728 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700729 return -3;
730 }
731
732 // An array with 4 items
733 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
734 Item.uDataType != QCBOR_TYPE_ARRAY ||
735 Item.uNestingLevel != 1 ||
736 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700737 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700738 return -4;
739 }
740
741 // An empty array
742 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
743 Item.uDataType != QCBOR_TYPE_ARRAY ||
744 Item.uNestingLevel != 2 ||
745 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700746 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700747 return -5;
748 }
749
750 // An array with 1 item
751 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
752 Item.uDataType != QCBOR_TYPE_ARRAY ||
753 Item.uNestingLevel != 2 ||
754 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700755 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700756 return -6;
757 }
758
759 // An integer 0
760 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
761 Item.uDataType != QCBOR_TYPE_INT64 ||
762 Item.uNestingLevel != 3 ||
763 Item.uNextNestLevel != 2 ||
764 Item.val.uint64 != 0) {
765 return -7;
766 }
767
768 // An empty map
769 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
770 Item.uDataType != QCBOR_TYPE_MAP ||
771 Item.uNestingLevel != 2 ||
772 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700773 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700774 return -8;
775 }
776
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700777 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700778 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
779 Item.uDataType != QCBOR_TYPE_MAP ||
780 Item.uNestingLevel != 2 ||
781 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700782 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700783 return -9;
784 }
785
786 // An empty map
787 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
788 Item.uDataType != QCBOR_TYPE_MAP ||
789 Item.uNestingLevel != 3 ||
790 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700791 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700792 return -10;
793 }
794
795 // An empty map
796 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
797 Item.uDataType != QCBOR_TYPE_MAP ||
798 Item.uNestingLevel != 3 ||
799 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700800 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700801 return -11;
802 }
803
804 // An empty array
805 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
806 Item.uDataType != QCBOR_TYPE_ARRAY ||
807 Item.uNestingLevel != 3 ||
808 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700809 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700810 return -12;
811 }
812
813 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
814 return -13;
815 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700816 return 0;
817}
818
819
820int32_t EmptyMapsAndArraysTest()
821{
822 int nResult;
823 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
824 true);
825 if(nResult) {
826 return nResult;
827 }
828
829 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
830 false);
831
832 if(nResult) {
833 return nResult -100;
834 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700835
836 return 0;
837}
838
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800839
Laurence Lundbladeee851742020-01-08 08:37:05 -0800840static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
841 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800842
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800843int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800844{
845 QCBORDecodeContext DCtx;
846 int nReturn = 0;
847 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800848
Laurence Lundbladeee851742020-01-08 08:37:05 -0800849 QCBORDecode_Init(&DCtx,
850 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
851 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800852
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800853 for(i = 0; i < 10; i++) {
854 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800855
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800856 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
857 Item.uDataType != QCBOR_TYPE_ARRAY ||
858 Item.uNestingLevel != i) {
859 nReturn = -1;
860 break;
861 }
862 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800863
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800864 return(nReturn);
865}
866
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700867// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800868static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
869 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
870 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
871 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800872
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800873int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800874{
875 QCBORDecodeContext DCtx;
876 int nReturn = 0;
877 int i;
878 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800879
880
Laurence Lundbladeee851742020-01-08 08:37:05 -0800881 QCBORDecode_Init(&DCtx,
882 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
883 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800884
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700885 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800886
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800887 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
888 Item.uDataType != QCBOR_TYPE_ARRAY ||
889 Item.uNestingLevel != i) {
890 nReturn = -1;
891 break;
892 }
893 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800894
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800895 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
896 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800897
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800898 return(nReturn);
899}
900
901
902
903
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800904int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800905{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700906 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800907
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800908 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700909 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800910
Laurence Lundbladeee851742020-01-08 08:37:05 -0800911 QCBORDecode_Init(&DCtx,
912 (UsefulBufC){spExpectedEncodedInts, nNum},
913 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800914
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800915 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800916
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700917 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800918 nResult = -1;
919 goto Done;
920 }
921 }
922Done:
923 return nResult;
924}
925
926
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800927
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800928int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800929{
930 uint8_t *pEncoded;
931 int nReturn;
932 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800933
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800934 int64_t i1, i2;
935 size_t i3, i4;
936 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800937
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800938 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800939
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800940 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
941 return(-1);
942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800943
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800944 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
945 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
946 if(nResult == 0) {
947 nReturn = -1;
948 }
949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800950
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800951 return(nReturn);
952}
953
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530954/*
955 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800956 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
957 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530958 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800959static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960{
961 QCBORDecodeContext DCtx;
962 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700963 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800964
Laurence Lundbladeee851742020-01-08 08:37:05 -0800965 QCBORDecode_Init(&DCtx,
966 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
967 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800968
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900969 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700970 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900971 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800972 if(Item.uDataType != QCBOR_TYPE_MAP ||
973 Item.val.uCount != 3)
974 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800975
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900976 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700977 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900978 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800979 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800980 Item.uDataType != QCBOR_TYPE_INT64 ||
981 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530982 Item.uDataAlloc ||
983 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900984 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800985 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800987
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900988 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700989 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900990 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800991 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530992 Item.uDataAlloc ||
993 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900994 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800995 Item.uDataType != QCBOR_TYPE_ARRAY ||
996 Item.val.uCount != 2)
997 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800998
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900999 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001000 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001001 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002 if(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("string1"))) {
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 Lundbladeccfb8cd2018-12-07 21:11:30 +09001009 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001010 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001011 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001012 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301013 Item.uDataAlloc ||
1014 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001015 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001016 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001018
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001019 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001020 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001021 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001022 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301023 Item.uDataAlloc ||
1024 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001025 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001026 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001030
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001031 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001032 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001033 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001034 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001036 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301037 Item.uDataAlloc ||
1038 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001039 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001040 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001041 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001042
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001043 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001044 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001045 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001046 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001047 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001048 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301049 Item.uDataAlloc ||
1050 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001051 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001052 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001053 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001054
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001055 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001056 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001058 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301059 Item.uDataAlloc ||
1060 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001061 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001062 Item.uDataType != QCBOR_TYPE_INT64 ||
1063 Item.val.int64 != 98)
1064 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001065
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001066 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001067 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001068 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001069 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001070 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001071 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301072 Item.uDataAlloc ||
1073 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001074 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001075 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001077
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001078 return 0;
1079}
1080
1081
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001082/*
1083 Decode and thoroughly check a moderately complex
1084 set of maps
1085 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001086int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001087{
1088 QCBORDecodeContext DCtx;
1089 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001090 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001091
Laurence Lundbladeee851742020-01-08 08:37:05 -08001092 QCBORDecode_Init(&DCtx,
1093 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1094 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001095
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001096 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001097 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001099 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1100 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 return -1;
1102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001103
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001104 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001105 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001106 }
1107 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1108 Item.uDataAlloc ||
1109 Item.uLabelAlloc ||
1110 Item.uLabelType != QCBOR_TYPE_NONE ||
1111 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1112 return -2;
1113 }
1114
1115 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001116 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 }
1118 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1119 Item.uDataType != QCBOR_TYPE_INT64 ||
1120 Item.val.int64 != 42 ||
1121 Item.uDataAlloc ||
1122 Item.uLabelAlloc) {
1123 return -3;
1124 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001125
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001126 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001127 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 }
1129 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1130 Item.uDataAlloc ||
1131 Item.uLabelAlloc ||
1132 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1133 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1134 return -4;
1135 }
1136
1137 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001138 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001139 }
1140 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1141 Item.uDataAlloc ||
1142 Item.uLabelAlloc ||
1143 Item.uDataType != QCBOR_TYPE_ARRAY ||
1144 Item.val.uCount != 2) {
1145 return -5;
1146 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001147
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001148 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001149 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001150 }
1151 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1152 Item.val.string.len != 7 ||
1153 Item.uDataAlloc ||
1154 Item.uLabelAlloc ||
1155 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1156 return -6;
1157 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001158
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001159 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001160 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 }
1162 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1163 Item.uDataAlloc ||
1164 Item.uLabelAlloc ||
1165 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1166 return -7;
1167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001168
1169
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001170 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001171 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001172 }
1173 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1174 Item.uDataAlloc ||
1175 Item.uLabelAlloc ||
1176 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1177 return -8;
1178 }
1179
1180 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001181 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001182 }
1183 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1184 Item.uDataAlloc ||
1185 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001186 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1187 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 return -9;
1189 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001190
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001191 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001192 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 }
1194 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1195 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1196 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1197 Item.uDataAlloc ||
1198 Item.uLabelAlloc) {
1199 return -10;
1200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001201
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001202 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001203 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001204 }
1205 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1206 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1207 Item.uDataAlloc ||
1208 Item.uLabelAlloc ||
1209 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1210 return -11;
1211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001212
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001213 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001214 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001215 }
1216 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1217 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1218 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1219 Item.uDataAlloc ||
1220 Item.uLabelAlloc) {
1221 return -12;
1222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001223
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001224 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001225 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 }
1227 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1228 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1229 Item.uDataAlloc ||
1230 Item.uLabelAlloc ||
1231 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1232 return -13;
1233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001234
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001235 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001236 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001237 }
1238 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1239 Item.uDataAlloc ||
1240 Item.uLabelAlloc ||
1241 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1242 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1243 return -14;
1244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001245
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001246 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001247 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001248 }
1249 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1250 Item.uDataAlloc ||
1251 Item.uLabelAlloc ||
1252 Item.uDataType != QCBOR_TYPE_INT64 ||
1253 Item.val.int64 != 98) {
1254 return -15;
1255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001256
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001257 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001258 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001259 }
1260 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1261 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1262 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1263 Item.uDataAlloc ||
1264 Item.uLabelAlloc) {
1265 return -16;
1266 }
1267
1268 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001269 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001270 }
1271 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1272 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1273 Item.uDataAlloc ||
1274 Item.uLabelAlloc ||
1275 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1276 return -17;
1277 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001278
1279
1280 /*
1281 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1282 map that when interpreted as an array will be too many. Test
1283 data just has the start of the map, not all the items in the map.
1284 */
1285 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
1286
1287 QCBORDecode_Init(&DCtx,
1288 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1289 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1290
1291 if((QCBOR_ERR_ARRAY_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
1292 return -50;
1293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001294
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001295 return 0;
1296}
1297
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001298
1299/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301300 Fully or partially decode pValidMapEncoded. When
1301 partially decoding check for the right error code.
1302 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001303
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301304 The partial decodes test error conditions of
1305 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001306
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301307 This could be combined with the above test
1308 and made prettier and maybe a little more
1309 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001310 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001311static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312{
1313 QCBORDecodeContext DCtx;
1314 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001315 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001316
Laurence Lundbladeee851742020-01-08 08:37:05 -08001317 QCBORDecode_Init(&DCtx,
1318 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1319 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001320
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001321 if(nLevel < 1) {
1322 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1323 return -1;
1324 } else {
1325 return 0;
1326 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001327 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301328
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001329
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001331 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001332 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001333 if(Item.uDataType != QCBOR_TYPE_MAP ||
1334 Item.val.uCount != 3)
1335 return -1;
1336
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001337 if(nLevel < 2) {
1338 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1339 return -1;
1340 } else {
1341 return 0;
1342 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001344
1345
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001346 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001347 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001348 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350 Item.uDataType != QCBOR_TYPE_INT64 ||
1351 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001352 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001353 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001355
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001356 if(nLevel < 3) {
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("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001370 Item.val.uCount != 2) {
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
1374
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001375 if(nLevel < 4) {
1376 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1377 return -1;
1378 } else {
1379 return 0;
1380 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001382
1383
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001385 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001388 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
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 < 5) {
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.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001406 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001407
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001408 if(nLevel < 6) {
1409 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1410 return -1;
1411 } else {
1412 return 0;
1413 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001414 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001415
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001416 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001417 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001418 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001419 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001420 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001421 Item.uDataType != QCBOR_TYPE_MAP ||
1422 Item.val.uCount != 4)
1423 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001424
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001425 if(nLevel < 7) {
1426 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1427 return -1;
1428 } else {
1429 return 0;
1430 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001432
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001433 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001434 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001435 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001436 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001437 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001439 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001441 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001442
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001443 if(nLevel < 8) {
1444 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1445 return -1;
1446 } else {
1447 return 0;
1448 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001449 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001450
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001451 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001452 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001453 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001455 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001457 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001458 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001460
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001461 if(nLevel < 9) {
1462 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1463 return -1;
1464 } else {
1465 return 0;
1466 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001469 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001470 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001471 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001472 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001473 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001474 Item.uDataType != QCBOR_TYPE_INT64 ||
1475 Item.val.int64 != 98)
1476 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001477
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001478 if(nLevel < 10) {
1479 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1480 return -1;
1481 } else {
1482 return 0;
1483 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001484 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001485
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001486 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001487 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001488 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001489 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001490 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001491 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001492 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001493 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001494 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001495
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301496 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001497 return -1;
1498 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001499
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001500 return 0;
1501}
1502
1503
1504
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001505
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001506int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001507{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001508 // Parse a moderatly complex map structure very thoroughly
1509 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1510 if(nResult) {
1511 return nResult;
1512 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001513
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001514 // Again, but in strings-only mode. It should succeed since the input
1515 // map has only string labels.
1516 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1517 if(nResult) {
1518 return nResult;
1519 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001520
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001521 // Again, but try to finish the decoding before the end of the
1522 // input at 10 different place and see that the right error code
1523 // is returned.
1524 for(int i = 0; i < 10; i++) {
1525 nResult = ExtraBytesTest(i);
1526 if(nResult) {
1527 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001528 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001529 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001530
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001531 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001532}
1533
1534
Laurence Lundbladeee851742020-01-08 08:37:05 -08001535static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1536 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1537 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001538
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001539int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001540{
1541 QCBORDecodeContext DCtx;
1542 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001543 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001544
1545
Laurence Lundbladeee851742020-01-08 08:37:05 -08001546 QCBORDecode_Init(&DCtx,
1547 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1548 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001549
1550
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001552 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001553 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1554 Item.val.uCount != 10)
1555 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001556
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001558 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001559 if(Item.uDataType != QCBOR_TYPE_FALSE)
1560 return -1;
1561
1562 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001563 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001564 if(Item.uDataType != QCBOR_TYPE_TRUE)
1565 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001566
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001567 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001568 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 if(Item.uDataType != QCBOR_TYPE_NULL)
1570 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001571
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001572 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001573 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001574 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1575 return -1;
1576
1577 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001578 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001579 return -1;
1580
1581 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001582 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1584 return -1;
1585
1586 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001587 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1589 return -1;
1590
Laurence Lundblade077475f2019-04-26 09:06:33 -07001591 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001592 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001593
Laurence Lundblade077475f2019-04-26 09:06:33 -07001594 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001595 return -1;
1596
Laurence Lundblade077475f2019-04-26 09:06:33 -07001597 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001598 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001599
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001600 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001601 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001602 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1603 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001604
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001605 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001606 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001607 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1608 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001609
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001610 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001612}
1613
1614
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001615static int IsNotWellFormedError(QCBORError nErr)
1616{
1617 switch(nErr){
1618 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1619 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1620 case QCBOR_ERR_UNSUPPORTED:
1621 case QCBOR_ERR_HIT_END:
1622 case QCBOR_ERR_BAD_TYPE_7:
1623 case QCBOR_ERR_BAD_BREAK:
1624 case QCBOR_ERR_EXTRA_BYTES:
1625 case QCBOR_ERR_BAD_INT:
1626 return 1;
1627 default:
1628 return 0;
1629 }
1630}
1631
1632
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001633int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001634{
1635 // Loop over all the not-well-formed instance of CBOR
1636 // that are test vectors in not_well_formed_cbor.h
1637 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1638 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1639 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1640 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1641
Laurence Lundbladeee851742020-01-08 08:37:05 -08001642 // Set up decoder context. String allocator needed for indefinite
1643 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001644 QCBORDecodeContext DCtx;
1645 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1646 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1647 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1648
1649 // Loop getting items until no more to get
1650 QCBORError nCBORError;
1651 do {
1652 QCBORItem Item;
1653
1654 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1655 } while(nCBORError == QCBOR_SUCCESS);
1656
1657 // Every test vector must fail with
1658 // a not-well-formed error. If not
1659 // this test fails.
1660 if(!IsNotWellFormedError(nCBORError)) {
1661 // Return index of failure in the error code
1662 return 2000 + nIterate;
1663 }
1664 }
1665 return 0;
1666}
1667
1668
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001669struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001670 UsefulBufC Input;
1671 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001672};
1673
Laurence Lundblade59289e52019-12-30 13:44:37 -08001674
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001675static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001676{
1677 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1678 // Set up the decoding context including a memory pool so that
1679 // indefinite length items can be checked
1680 QCBORDecodeContext DCtx;
1681 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1682 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001683
Laurence Lundblade59289e52019-12-30 13:44:37 -08001684 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1685 if(nCBORError) {
1686 return -9;
1687 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001688
Laurence Lundblade59289e52019-12-30 13:44:37 -08001689 // Iterate until there is an error of some sort error
1690 QCBORItem Item;
1691 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001692 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001693 memset(&Item, 0x33, sizeof(Item));
1694
1695 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1696 } while(nCBORError == QCBOR_SUCCESS);
1697
1698 // Must get the expected error or the this test fails
1699 // The data and label type must also be QCBOR_TYPE_NONE
1700 if(nCBORError != pF->nError ||
1701 Item.uDataType != QCBOR_TYPE_NONE ||
1702 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001703 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001704 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001705 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001706 }
1707 }
1708
1709 return 0;
1710}
1711
1712
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001713struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001714 // Most of this is copied from not_well_formed.h. Here the error code
1715 // returned is also checked.
1716
1717 // Indefinite length strings must be closed off
1718 // An indefinite length byte string not closed off
1719 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1720 // An indefinite length text string not closed off
1721 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1722
1723
1724 // All the chunks in an indefinite length string must be of the type of indefinite length string
1725 // indefinite length byte string with text string chunk
1726 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1727 // indefinite length text string with a byte string chunk
1728 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1729 // indefinite length byte string with an positive integer chunk
1730 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1731 // indefinite length byte string with an negative integer chunk
1732 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1733 // indefinite length byte string with an array chunk
1734 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1735 // indefinite length byte string with an map chunk
1736 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1737 // indefinite length byte string with tagged integer chunk
1738 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1739 // indefinite length byte string with an simple type chunk
1740 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1741 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1742 // indefinite length text string with indefinite string inside
1743 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1744
1745
1746 // Definte length maps and arrays must be closed by having the right number of items
1747 // A definte length array that is supposed to have 1 item, but has none
1748 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1749 // A definte length array that is supposed to have 2 items, but has only 1
1750 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1751 // A definte length array that is supposed to have 511 items, but has only 1
1752 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1753 // A definte length map that is supposed to have 1 item, but has none
1754 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1755 // A definte length map that is supposed to have s item, but has only 1
1756 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1757
1758
1759 // Indefinte length maps and arrays must be ended by a break
1760 // Indefinite length array with zero items and no break
1761 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1762 // Indefinite length array with two items and no break
1763 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1764 // Indefinite length map with zero items and no break
1765 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1766 // Indefinite length map with two items and no break
1767 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1768
1769
1770 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001771 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001772 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001773 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001774 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1775 // Deeply nested definite length arrays with deepest one unclosed
1776 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1777 // Deeply nested indefinite length arrays with deepest one unclosed
1778 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1779 // Mixed nesting with indefinite unclosed
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001780 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001781 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001782 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001783 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001784
1785
1786 // The "argument" for the data item is incomplete
1787 // Positive integer missing 1 byte argument
1788 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1789 // Positive integer missing 2 byte argument
1790 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1791 // Positive integer missing 4 byte argument
1792 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1793 // Positive integer missing 8 byte argument
1794 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1795 // Positive integer missing 1 byte of 2 byte argument
1796 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1797 // Positive integer missing 2 bytes of 4 byte argument
1798 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1799 // Positive integer missing 1 bytes of 7 byte argument
1800 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1801 // Negative integer missing 1 byte argument
1802 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1803 // Binary string missing 1 byte argument
1804 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1805 // Text string missing 1 byte argument
1806 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1807 // Array missing 1 byte argument
1808 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1809 // Map missing 1 byte argument
1810 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1811 // Tag missing 1 byte argument
1812 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1813 // Simple missing 1 byte argument
1814 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1815
1816
1817 // Breaks must not occur in definite length arrays and maps
1818 // Array of length 1 with sole member replaced by a break
1819 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1820 // Array of length 2 with 2nd member replaced by a break
1821 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1822 // Map of length 1 with sole member label replaced by a break
1823 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1824 // Map of length 1 with sole member label replaced by break
1825 // Alternate representation that some decoders handle difference
1826 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1827 // Array of length 1 with 2nd member value replaced by a break
1828 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1829 // Map of length 2 with 2nd member replaced by a break
1830 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1831
1832
1833 // Breaks must not occur on their own out of an indefinite length data item
1834 // A bare break is not well formed
1835 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1836 // A bare break after a zero length definite length array
1837 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1838 // A bare break after a zero length indefinite length map
1839 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1840
1841
1842 // Forbidden two byte encodings of simple types
1843 // Must use 0xe0 instead
1844 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1845 // Should use 0xe1 instead
1846 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1847 // Should use 0xe2 instead
1848 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1849 // Should use 0xe3 instead
1850 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1851 // Should use 0xe4 instead
1852 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1853 // Should use 0xe5 instead
1854 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1855 // Should use 0xe6 instead
1856 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1857 // Should use 0xe7 instead
1858 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1859 // Should use 0xe8 instead
1860 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1861 // Should use 0xe9 instead
1862 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1863 // Should use 0xea instead
1864 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1865 // Should use 0xeb instead
1866 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1867 // Should use 0xec instead
1868 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1869 // Should use 0xed instead
1870 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1871 // Should use 0xee instead
1872 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1873 // Should use 0xef instead
1874 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1875 // Should use 0xf0 instead
1876 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1877 // Should use 0xf1 instead
1878 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1879 // Should use 0xf2 instead
1880 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1881 // Must use 0xf3 instead
1882 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1883 // Must use 0xf4 instead
1884 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1885 // Must use 0xf5 instead
1886 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1887 // Must use 0xf6 instead
1888 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1889 // Must use 0xf7 instead
1890 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1891 // Must use 0xf8 instead
1892 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1893
1894
1895 // Integers with additional info indefinite length
1896 // Positive integer with additional info indefinite length
1897 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1898 // Negative integer with additional info indefinite length
1899 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1900 // CBOR tag with "argument" an indefinite length
1901 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1902 // CBOR tag with "argument" an indefinite length alternate vector
1903 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1904
1905
1906 // Missing bytes from a deterministic length string
1907 // A byte string is of length 1 without the 1 byte
1908 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1909 // A text string is of length 1 without the 1 byte
1910 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001911 // Byte string should have 2^32-15 bytes, but has one
1912 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1913 // Byte string should have 2^32-15 bytes, but has one
1914 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001915
1916
1917 // Use of unassigned additional information values
1918 // Major type positive integer with reserved value 28
1919 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1920 // Major type positive integer with reserved value 29
1921 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1922 // Major type positive integer with reserved value 30
1923 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1924 // Major type negative integer with reserved value 28
1925 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1926 // Major type negative integer with reserved value 29
1927 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1928 // Major type negative integer with reserved value 30
1929 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1930 // Major type byte string with reserved value 28 length
1931 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1932 // Major type byte string with reserved value 29 length
1933 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1934 // Major type byte string with reserved value 30 length
1935 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1936 // Major type text string with reserved value 28 length
1937 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1938 // Major type text string with reserved value 29 length
1939 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1940 // Major type text string with reserved value 30 length
1941 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1942 // Major type array with reserved value 28 length
1943 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1944 // Major type array with reserved value 29 length
1945 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1946 // Major type array with reserved value 30 length
1947 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1948 // Major type map with reserved value 28 length
1949 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1950 // Major type map with reserved value 29 length
1951 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1952 // Major type map with reserved value 30 length
1953 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1954 // Major type tag with reserved value 28 length
1955 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1956 // Major type tag with reserved value 29 length
1957 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1958 // Major type tag with reserved value 30 length
1959 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1960 // Major type simple with reserved value 28 length
1961 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1962 // Major type simple with reserved value 29 length
1963 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1964 // Major type simple with reserved value 30 length
1965 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1966
1967
1968 // Maps must have an even number of data items (key & value)
1969 // Map with 1 item when it should have 2
1970 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1971 // Map with 3 item when it should have 4
1972 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1973 // Map with 1 item when it should have 2
1974 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1975 // Map with 3 item when it should have 4
1976 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1977
1978
1979 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001980 // Text-based date, with an integer
1981 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1982 // Epoch date, with an byte string
1983 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1984 // tagged as both epoch and string dates
1985 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1986 // big num tagged an int, not a byte string
1987 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001988};
1989
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001990int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001991{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001992 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001993
Laurence Lundblade59289e52019-12-30 13:44:37 -08001994 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1995 if(nResult) {
1996 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001998
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001999 // Corrupt the UsefulInputBuf and see that
2000 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002001 {
2002 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002003 QCBORItem Item;
2004 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002005
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002006 QCBORDecode_Init(&DCtx,
2007 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2008 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002009
2010 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002011 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002012 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002013 Item.val.uCount != 10) {
2014 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002015 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002017
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002018 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002019
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002020 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002021 if(nCBORError != QCBOR_ERR_HIT_END) {
2022 // Did not get back the error expected
2023 return -2;
2024 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002026
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002027/*
2028 This test is disabled until QCBOREncode_EncodeHead() is brought in so
2029 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
2030
2031 This relies on the largest string allowed being SIZE_MAX -4 rather than
2032 SIZE_MAX. That way the test can be performed.
2033 {
2034 QCBORDecodeContext DCtx;
2035 QCBORItem Item;
2036
2037 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
2038 0xff, 0xff, 0xff, 0xff};
2039
2040 QCBORDecode_Init(&DCtx,
2041 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
2042 QCBOR_DECODE_MODE_NORMAL);
2043
2044 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2045 return -4;
2046 }
2047 }
2048*/
2049
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002050 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002051}
2052
2053
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002054/* Try all 256 values of the byte at nLen including recursing for
2055 each of the values to try values at nLen+1 ... up to nLenMax
2056 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002057static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002058{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002059 if(nLen >= nLenMax) {
2060 return;
2061 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002062
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002063 for(int inputByte = 0; inputByte < 256; inputByte++) {
2064 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002065 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002066 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002067
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002068 // Get ready to parse
2069 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002070 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002071
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002072 // Parse by getting the next item until an error occurs
2073 // Just about every possible decoder error can occur here
2074 // The goal of this test is not to check for the correct
2075 // error since that is not really possible. It is to
2076 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002077 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002078 QCBORItem Item;
2079 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002081 break;
2082 }
2083 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002084
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002085 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002086 }
2087}
2088
2089
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002090int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002092 // Size 2 tests 64K inputs and runs quickly
2093 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002094
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002095 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002096
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002097 return 0;
2098}
2099
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002100
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002101int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002102{
2103 // size 3 tests 16 million inputs and runs OK
2104 // in seconds on fast machines. Size 4 takes
2105 // 10+ minutes and 5 half a day on fast
2106 // machines. This test is kept separate from
2107 // the others so as to no slow down the use
2108 // of them as a very frequent regression.
2109 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002110
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002111 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002112
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002113 return 0;
2114}
2115
2116
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002117static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002118 0xc0, // tag for string date
2119 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002120
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002121 0xc1, // tag for epoch date
2122 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2123
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002124 // CBOR_TAG_B64
2125 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002126 0x1a, 0x53, 0x72, 0x4E, 0x01,
2127
2128 0xc1, // tag for epoch date
2129 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131 0xc1, // tag for epoch date
2132 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002133
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002134 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002135 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002136
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002137 0xc1, // tag for epoch date
2138 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2139 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2140
2141 0xc1, // tag for epoch date
2142 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002143};
2144
2145
2146// have to check float expected only to within an epsilon
2147int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002148
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002149 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002150
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002151 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002152
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002153 return diff > 0.0000001;
2154}
2155
2156
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002157int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158{
2159 QCBORDecodeContext DCtx;
2160 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002161 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002162
Laurence Lundbladeee851742020-01-08 08:37:05 -08002163 QCBORDecode_Init(&DCtx,
2164 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2165 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002166
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002167 const uint64_t uTags[] = {15};
2168 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002169
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002170 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002171
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002172 // String date
2173 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2174 return -1;
2175 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002176 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002177 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002178 }
2179
2180 // Epoch date
2181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002182 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002183 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2184 Item.val.epochDate.nSeconds != 1400000000 ||
2185 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002186 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002188
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002189 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2190 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002191 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002192 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002193 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2194 Item.val.epochDate.nSeconds != 1400000001 ||
2195 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002196 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002197 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002198 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002199
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002200 // Epoch date that is too large for our representation
2201 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002202 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002203 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002204
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002205 // Epoch date in float format with fractional seconds
2206 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002207 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002208 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2209 Item.val.epochDate.nSeconds != 1 ||
2210 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002211 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002213
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002214 // Epoch date float that is too large for our representation
2215 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002216 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002218
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002219 // Epoch date double that is just slightly too large
2220 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2221 return -11;
2222 }
2223
2224 // Largest double epoch date supported
2225 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2226 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2227 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2228 Item.val.epochDate.nSeconds == 0) {
2229 return -12;
2230 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002231 // TODO: could use a few more tests with float, double, and half precsion
2232 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002233
2234 return 0;
2235}
2236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002237// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002238static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002239 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002240 0x81, // Array of one
2241 0xd8, 0x04, // non-preferred serialization of tag 4
2242 0x82, 0x01, 0x03}; // fraction 1/3
2243
Laurence Lundblade59289e52019-12-30 13:44:37 -08002244/*
2245 DB 9192939495969798 # tag(10489608748473423768)
2246 80 # array(0)
2247 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002248static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2249 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002250
Laurence Lundblade59289e52019-12-30 13:44:37 -08002251/*
2252DB 9192939495969798 # tag(10489608748473423768)
2253 D8 88 # tag(136)
2254 C6 # tag(6)
2255 C7 # tag(7)
2256 80 # array(0)
2257*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002258static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2259 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002260
2261/*
2262 The cbor.me parse of this.
2263 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002264 9(-17): 773("SSG"), -15: 16(17(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002265 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2266 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2267 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2268 */
2269static uint8_t spCSRWithTags[] = {
2270 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2271 0xc6, 0xc7, 0x36,
2272 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2273 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2274 0xcb, 0xa5,
2275 0xd1, 0x31,
2276 0xd1, 0xd1, 0xd1, 0x6c,
2277 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2278 0xc9, 0x30,
2279 0xd9, 0x03, 0x05, 0x63,
2280 0x53, 0x53, 0x47,
2281 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002282 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002283 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2284 0xd1, 0x2f,
2285 0xd1, 0x69,
2286 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2287 0xd1, 0x2d,
2288 0xd1, 0x62,
2289 0x55, 0x53,
2290 0xd7, 0x32,
2291 0xd3, 0xa2,
2292 0x2a,
2293 0xc9, 0xa1,
2294 0x28,
2295 0x26,
2296 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2297 0xcc, 0x4a,
2298 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2299 0xd0, 0x35,
2300 0xd7, 0xa1,
2301 0xcb, 0xc8, 0xc7, 0x24,
2302 0xc8, 0x22};
2303
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002304static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002305
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002306
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002307int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002308{
2309 QCBORDecodeContext DCtx;
2310 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002311
Laurence Lundbladeee851742020-01-08 08:37:05 -08002312 QCBORDecode_Init(&DCtx,
2313 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2314 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002315
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002316 //-------------------------
2317 // This text matches the magic number tag and the fraction tag
2318 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2319 return -2;
2320 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002321 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002322 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2323 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002325
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002326 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2327 return -4;
2328 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002329
2330#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002331 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002332 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002333 Item.val.uCount != 2) {
2334 return -5;
2335 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002336#else
2337 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2338 return -6;
2339 }
2340#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002341
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002342 // --------------------------------
2343 // This test decodes the very large tag, but it is not in
2344 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002345 QCBORDecode_Init(&DCtx,
2346 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2347 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002348 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2349 return -6;
2350 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002351 /*
2352 if(Item.uTagBits) { // TODO: make sure it is OK to remove this
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002353 return -7;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002354 }*/
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002356 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002357 // This test sets up a caller-config list that includes the very large
2358 // tage and then matches it.
2359 QCBORDecode_Init(&DCtx,
2360 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2361 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002362 const uint64_t puList[] = {0x9192939495969798, 257};
2363 const QCBORTagListIn TL = {2, puList};
2364 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002365
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002366 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2367 return -8;
2368 }
2369 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2370 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2371 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2372 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2373 Item.val.uCount != 0) {
2374 return -9;
2375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002376
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002377 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002378 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002379 const uint64_t puLongList[17] = {1,2,1};
2380 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002381 QCBORDecode_Init(&DCtx,
2382 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2383 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002384 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2385 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2386 return -11;
2387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002388
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002389 // -----------------------
2390 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002391 QCBORDecode_Init(&DCtx,
2392 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2393 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002394 uint64_t puTags[16];
2395 QCBORTagListOut Out = {0, 4, puTags};
2396 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2397 return -12;
2398 }
2399 if(puTags[0] != 0x9192939495969798 ||
2400 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002401 puTags[2] != 0x06 ||
2402 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002403 return -13;
2404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002405
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002406 // ----------------------
2407 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002408 QCBORDecode_Init(&DCtx,
2409 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2410 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002411 QCBORTagListOut OutSmall = {0, 3, puTags};
2412 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2413 return -14;
2414 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002415
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002416#if 0
2417 // TODO: this test needs to be re evaluated
2418
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002419 // ---------------
2420 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002421 QCBORDecode_Init(&DCtx,
2422 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2423 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002424 int n = CheckCSRMaps(&DCtx);
2425 if(n) {
2426 return n-2000;
2427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002428
Laurence Lundblade59289e52019-12-30 13:44:37 -08002429 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002430 QCBORDecode_Init(&DCtx,
2431 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2432 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002433
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002434 const uint64_t puTagList[] = {773, 1, 90599561};
2435 const QCBORTagListIn TagList = {3, puTagList};
2436 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002437
2438
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002439 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2440 return -100;
2441 }
2442 if(Item.uDataType != QCBOR_TYPE_MAP ||
2443 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2444 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2445 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2446 Item.val.uCount != 2 ||
2447 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2448 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2449 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2450 Out.uNumUsed != 3) {
2451 return -101;
2452 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002453
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002454 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2455 return -102;
2456 }
2457 if(Item.uDataType != QCBOR_TYPE_MAP ||
2458 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2459 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2460 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2461 Item.val.uCount != 2 ||
2462 puTags[0] != 5859837686836516696 ||
2463 puTags[1] != 7 ||
2464 Out.uNumUsed != 2) {
2465 return -103;
2466 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002467
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002468 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2469 return -104;
2470 }
2471 if(Item.uDataType != QCBOR_TYPE_MAP ||
2472 Item.uTagBits ||
2473 Item.val.uCount != 5 ||
2474 puTags[0] != 0x0b ||
2475 Out.uNumUsed != 1) {
2476 return -105;
2477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002478
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002479 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2480 return -106;
2481 }
2482 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2483 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2484 Item.val.string.len != 12 ||
2485 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2486 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2487 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2488 Out.uNumUsed != 3) {
2489 return -105;
2490 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002491
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002492 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2493 return -107;
2494 }
2495 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2496 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2497 Item.val.string.len != 3 ||
2498 puTags[0] != 773 ||
2499 Out.uNumUsed != 1) {
2500 return -108;
2501 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002502
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002503 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2504 return -109;
2505 }
2506 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002507 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002508 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002509 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002510 puTags[11] != 0x0f ||
2511 Out.uNumUsed != 12) {
2512 return -110;
2513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002514
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002515 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2516 return -111;
2517 }
2518 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2519 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2520 Item.val.string.len != 9 ||
2521 puTags[0] != 17 ||
2522 Out.uNumUsed != 1) {
2523 return -112;
2524 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002525
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002526 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2527 return -111;
2528 }
2529 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2530 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2531 Item.val.string.len != 2 ||
2532 puTags[0] != 17 ||
2533 Out.uNumUsed != 1) {
2534 return -112;
2535 }
2536
2537 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2538 return -113;
2539 }
2540 if(Item.uDataType != QCBOR_TYPE_MAP ||
2541 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2542 Item.val.uCount != 2 ||
2543 puTags[0] != 19 ||
2544 Out.uNumUsed != 1) {
2545 return -114;
2546 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002547
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002548 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2549 return -115;
2550 }
2551 if(Item.uDataType != QCBOR_TYPE_MAP ||
2552 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2553 Item.uTagBits ||
2554 Item.val.uCount != 1 ||
2555 puTags[0] != 9 ||
2556 Out.uNumUsed != 1) {
2557 return -116;
2558 }
2559
2560 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2561 return -116;
2562 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002563 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002564 Item.val.int64 != -7 ||
2565 Item.uTagBits ||
2566 Out.uNumUsed != 0) {
2567 return -117;
2568 }
2569
2570 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2571 return -118;
2572 }
2573 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2574 Item.val.string.len != 10 ||
2575 Item.uTagBits ||
2576 puTags[0] != 12 ||
2577 Out.uNumUsed != 1) {
2578 return -119;
2579 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002580
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002581 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2582 return -120;
2583 }
2584 if(Item.uDataType != QCBOR_TYPE_MAP ||
2585 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2586 Item.val.uCount != 1 ||
2587 puTags[0] != 0x17 ||
2588 Out.uNumUsed != 1) {
2589 return -121;
2590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002591
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002592 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2593 return -122;
2594 }
2595 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2596 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2597 Item.val.int64 != -3 ||
2598 puTags[0] != 8 ||
2599 Out.uNumUsed != 1) {
2600 return -123;
2601 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002602
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002603 if(QCBORDecode_Finish(&DCtx)) {
2604 return -124;
2605 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002606#else
2607 (void)spCSRWithTags;
2608#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002609 return 0;
2610}
2611
2612
2613
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002614
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002615static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002616 0x83,
2617 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2618 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2619 0xA4,
2620 0x63, 0x42, 0x4E, 0x2B,
2621 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2622 0x18, 0x40,
2623 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2624 0x63, 0x42, 0x4E, 0x2D,
2625 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2626 0x38, 0x3F,
2627 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2628
2629
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002630static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002631
2632
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002633int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002634{
2635 QCBORDecodeContext DCtx;
2636 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002637 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002638
Laurence Lundbladeee851742020-01-08 08:37:05 -08002639 QCBORDecode_Init(&DCtx,
2640 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2641 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002642
2643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002644 //
2645 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2646 return -1;
2647 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002648 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002649 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002650
2651 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002652 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002653 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002654 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002655 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002656 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002657 }
2658
2659 //
2660 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002661 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002662 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002663 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002664 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002665 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002666
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002667 //
2668 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002669 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002670 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002671 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002672 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002674 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002675 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002676 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2677 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002678 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002679 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002680 }
2681
2682 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002683 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002684 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2685 Item.uLabelType != QCBOR_TYPE_INT64 ||
2686 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002687 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002688 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002689 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002691 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002692 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002693 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2694 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002695 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002696 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002698
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002699 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002700 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002701 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2702 Item.uLabelType != QCBOR_TYPE_INT64 ||
2703 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002704 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002705 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002706 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002707
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002708 return 0;
2709}
2710
2711
2712
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002713static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002714 uint8_t uDataType,
2715 uint8_t uNestingLevel,
2716 uint8_t uNextNest,
2717 int64_t nLabel,
2718 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002719{
2720 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002721 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002722
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002723 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2724 if(Item.uDataType != uDataType) return -1;
2725 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002726 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2727 Item.uLabelType != QCBOR_TYPE_UINT64) {
2728 return -1;
2729 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002730 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2731 if(Item.label.int64 != nLabel) return -1;
2732 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002733 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002734 }
2735 }
2736 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302737 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002738
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002739 if(pItem) {
2740 *pItem = Item;
2741 }
2742 return 0;
2743}
2744
2745
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002746// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002747static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002748{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302749 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002750
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302751 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002752
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302753 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002754
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302755 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2756 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2757 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2758 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2759 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002760
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302761 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2762 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002763
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302764 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2765 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002766
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302767 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2768 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002769
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002770 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002771
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002772 return 0;
2773}
2774
2775
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002776/*
2777// cbor.me decoded output
2778{
2779 -23: {
2780 -20: {
2781 -18: "Organization",
2782 -17: "SSG",
2783 -15: "Confusion",
2784 -16: "San Diego",
2785 -14: "US"
2786 },
2787 -19: {
2788 -11: {
2789 -9: -7
2790 },
2791 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2792 }
2793 },
2794 -22: {
2795 -5: -3
2796 }
2797}
2798 */
2799
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002800
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002801static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002802 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2803 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2804 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2805 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2806 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2807 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2808 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2809 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2810 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2811
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002812int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002813{
2814 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002815
Laurence Lundbladeee851742020-01-08 08:37:05 -08002816 QCBORDecode_Init(&DCtx,
2817 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2818 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002819
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002820 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002821}
2822
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002823
2824
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002825int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002826{
2827 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002828
Laurence Lundbladeee851742020-01-08 08:37:05 -08002829 QCBORDecode_Init(&DCtx,
2830 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2831 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002832
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002833 QCBORItem Item;
2834 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002835
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002836 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2837 return -1;
2838 }
2839 if(Item.uDataType != QCBOR_TYPE_MAP) {
2840 return -2;
2841 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002842
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002843 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2844 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2845 return -3;
2846 }
2847
2848 return 0;
2849}
2850
2851
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002852// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002853static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002854 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2855 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2856 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2857 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2858 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2859 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002860 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2861 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2862 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2863 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002864
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002865int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002866{
2867 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002868
Laurence Lundbladeee851742020-01-08 08:37:05 -08002869 QCBORDecode_Init(&DCtx,
2870 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2871 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002872
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002873 return CheckCSRMaps(&DCtx);
2874}
2875
2876
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002877
Laurence Lundblade17ede402018-10-13 11:43:07 +08002878static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2879{
2880 UsefulOutBuf UOB;
2881 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002882
Laurence Lundblade17ede402018-10-13 11:43:07 +08002883 int i;
2884 for(i = 0; i < n; i++) {
2885 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2886 }
2887
2888 for(i = 0; i < n; i++) {
2889 UsefulOutBuf_AppendByte(&UOB, 0xff);
2890 }
2891 return UsefulOutBuf_OutUBuf(&UOB);
2892}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002893
2894
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002895static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002896{
2897 QCBORDecodeContext DC;
2898 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002899
Laurence Lundblade17ede402018-10-13 11:43:07 +08002900 int j;
2901 for(j = 0; j < nNestLevel; j++) {
2902 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002903 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002904 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2905 // Should be in error
2906 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2907 return -4;
2908 } else {
2909 return 0; // Decoding doesn't recover after an error
2910 }
2911 } else {
2912 // Should be no error
2913 if(nReturn) {
2914 return -9; // Should not have got an error
2915 }
2916 }
2917 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2918 return -7;
2919 }
2920 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002921 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002922 if(nReturn) {
2923 return -3;
2924 }
2925 return 0;
2926}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002927
2928
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002929int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002930{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302931 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002932 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002933 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002934 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002935 int nReturn = parse_indeflen_nested(Nested, i);
2936 if(nReturn) {
2937 return nReturn;
2938 }
2939 }
2940 return 0;
2941}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002942
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002943
Laurence Lundbladeee851742020-01-08 08:37:05 -08002944// [1, [2, 3]]
2945static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2946// No closing break
2947static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2948// Not enough closing breaks
2949static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2950// Too many closing breaks
2951static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2952// Unclosed indeflen inside def len
2953static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2954// confused tag
2955static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002956
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002957int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002958{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002959 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002960 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002961 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002962
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002963 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302964 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002965 QCBORDecodeContext DC;
2966 QCBORItem Item;
2967 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002968
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002969 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002970
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002971 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302972
2973 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2974 Item.uNestingLevel != 0 ||
2975 Item.uNextNestLevel != 1) {
2976 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002977 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002978
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002979 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302980 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2981 Item.uNestingLevel != 1 ||
2982 Item.uNextNestLevel != 1) {
2983 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002985
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002986 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302987 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2988 Item.uNestingLevel != 1 ||
2989 Item.uNextNestLevel != 2) {
2990 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002991 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002992
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002993 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002994 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302995 Item.uNestingLevel != 2 ||
2996 Item.uNextNestLevel != 2) {
2997 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002999
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003000 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003001 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303002 Item.uNestingLevel != 2 ||
3003 Item.uNextNestLevel != 0) {
3004 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003007 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303008 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003009 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003010
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003011 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003012 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003013
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003014 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003015
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003016 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003017
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003018 nResult = QCBORDecode_GetNext(&DC, &Item);
3019 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303020 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003022
Laurence Lundblade570fab52018-10-13 18:28:27 +08003023 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303024 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3025 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003026 }
3027
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003028
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003029 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003030 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003031
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003032 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003033
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003034 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003035
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003036 nResult = QCBORDecode_GetNext(&DC, &Item);
3037 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303038 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003039 }
3040
3041 nResult = QCBORDecode_GetNext(&DC, &Item);
3042 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303043 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003044 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003045
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003046 nResult = QCBORDecode_GetNext(&DC, &Item);
3047 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303048 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003049 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003050
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003051 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303052 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3053 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003055
3056
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003057 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003058 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003059
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003060 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003061
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003062 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003063
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003064 nResult = QCBORDecode_GetNext(&DC, &Item);
3065 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303066 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003067 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003068
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003069 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003070 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303071 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003072 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303073
Laurence Lundblade642282a2020-06-23 12:00:33 -07003074 nResult = QCBORDecode_GetNext(&DC, &Item);
3075 if(nResult != QCBOR_ERR_BAD_BREAK) {
3076 return -140;
3077 }
3078
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003079
Laurence Lundblade570fab52018-10-13 18:28:27 +08003080 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003081 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003082
Laurence Lundblade570fab52018-10-13 18:28:27 +08003083 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003084
Laurence Lundblade570fab52018-10-13 18:28:27 +08003085 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003086
Laurence Lundblade570fab52018-10-13 18:28:27 +08003087 nResult = QCBORDecode_GetNext(&DC, &Item);
3088 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303089 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003091
Laurence Lundblade570fab52018-10-13 18:28:27 +08003092 nResult = QCBORDecode_GetNext(&DC, &Item);
3093 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303094 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003096
Laurence Lundblade570fab52018-10-13 18:28:27 +08003097 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303098 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3099 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003101
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303102 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003103 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003104
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303105 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003106
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303107 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003108
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303109 nResult = QCBORDecode_GetNext(&DC, &Item);
3110 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303111 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003113
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303114 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303115 if(nResult != QCBOR_ERR_BAD_BREAK) {
3116 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303117 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003118
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003119 return 0;
3120}
3121
Laurence Lundblade17ede402018-10-13 11:43:07 +08003122
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003123static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003124 0x81, // Array of length one
3125 0x7f, // text string marked with indefinite length
3126 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3127 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3128 0xff // ending break
3129};
3130
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003131static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303132 0x81, // Array of length one
3133 0x7f, // text string marked with indefinite length
3134 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3135 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3136 0xff // ending break
3137};
3138
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003139static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303140 0x81, // Array of length one
3141 0x7f, // text string marked with indefinite length
3142 0x01, 0x02, // Not a string
3143 0xff // ending break
3144};
3145
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003146static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303147 0x81, // Array of length one
3148 0x7f, // text string marked with indefinite length
3149 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3150 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3151 // missing end of string
3152};
3153
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003154static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303155 0xa1, // Array of length one
3156 0x7f, // text string marked with indefinite length
3157 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3158 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3159 0xff, // ending break
3160 0x01 // integer being labeled.
3161};
3162
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003163/**
3164 Make an indefinite length string
3165
3166 @param Storage Storage for string, must be 144 bytes in size
3167 @return The indefinite length string
3168
3169 This makes an array with one indefinite length string that has 7 chunks
3170 from size of 1 byte up to 64 bytes.
3171 */
3172static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303173{
3174 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003175
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303176 UsefulOutBuf_Init(&UOB, Storage);
3177 UsefulOutBuf_AppendByte(&UOB, 0x81);
3178 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003179
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003180 uint8_t uStringByte = 0;
3181 // Use of type int is intentional
3182 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3183 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303184 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003185 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3186 for(int j = 0; j < uChunkSize; j++) {
3187 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3188 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303189 }
3190 }
3191 UsefulOutBuf_AppendByte(&UOB, 0xff);
3192
3193 return UsefulOutBuf_OutUBuf(&UOB);
3194}
3195
3196static int CheckBigString(UsefulBufC BigString)
3197{
3198 if(BigString.len != 255) {
3199 return 1;
3200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003201
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303202 for(uint8_t i = 0; i < 255; i++){
3203 if(((const uint8_t *)BigString.ptr)[i] != i) {
3204 return 1;
3205 }
3206 }
3207 return 0;
3208}
3209
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303210
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003211int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303212{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303213 QCBORDecodeContext DC;
3214 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303215 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003216 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003217
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303218 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003219 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303220 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003221
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303222 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303223 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003225
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303226 if(QCBORDecode_GetNext(&DC, &Item)) {
3227 return -2;
3228 }
3229 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3230 return -3;
3231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003232
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303233 if(QCBORDecode_GetNext(&DC, &Item)) {
3234 return -4;
3235 }
3236 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3237 return -5;
3238 }
3239 if(QCBORDecode_Finish(&DC)) {
3240 return -6;
3241 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303242
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303243 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003244 QCBORDecode_Init(&DC,
3245 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3246 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003247
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303248 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3249 return -7;
3250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003251
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303252 if(QCBORDecode_GetNext(&DC, &Item)) {
3253 return -8;
3254 }
3255 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3256 return -9;
3257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003258
Laurence Lundblade30816f22018-11-10 13:40:22 +07003259 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303260 return -10;
3261 }
3262
3263 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003264 QCBORDecode_Init(&DC,
3265 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3266 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003267
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303268 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3269 return -11;
3270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003271
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303272 if(QCBORDecode_GetNext(&DC, &Item)) {
3273 return -12;
3274 }
3275 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3276 return -13;
3277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003278
Laurence Lundblade30816f22018-11-10 13:40:22 +07003279 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303280 return -14;
3281 }
3282
3283 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003284 QCBORDecode_Init(&DC,
3285 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3286 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003287
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303288 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3289 return -15;
3290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003291
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303292 if(QCBORDecode_GetNext(&DC, &Item)) {
3293 return -16;
3294 }
3295 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3296 return -17;
3297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003298
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303299 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3300 return -18;
3301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303303 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303304 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003305
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303306 QCBORDecode_GetNext(&DC, &Item);
3307 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303308 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003310
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303311 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303312 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003314
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303315 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003316 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303317
3318 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3319 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303320 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003322
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303323 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303324 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003325 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003326
Laurence Lundbladeee851742020-01-08 08:37:05 -08003327 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3328 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003329
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303330 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303331 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303332 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303333 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003334
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303335 QCBORDecode_GetNext(&DC, &Item);
3336 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303337 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303338 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003339 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303340 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003342
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303343 // ---- big bstr -----
3344 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003345
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303346 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3347 return -25;
3348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003349
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303350 if(QCBORDecode_GetNext(&DC, &Item)) {
3351 return -26;
3352 }
3353 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303354 return -26;
3355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003356
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303357 if(QCBORDecode_GetNext(&DC, &Item)) {
3358 return -27;
3359 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303360 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303361 return -28;
3362 }
3363 if(CheckBigString(Item.val.string)) {
3364 return -3;
3365 }
3366 if(QCBORDecode_Finish(&DC)) {
3367 return -29;
3368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003369
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303370 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003371 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003372
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303373 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3374 return -30;
3375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003376
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303377 QCBORDecode_GetNext(&DC, &Item);
3378 if(Item.uDataType != QCBOR_TYPE_MAP) {
3379 return -31;
3380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003381
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303382 if(QCBORDecode_GetNext(&DC, &Item)){
3383 return -32;
3384 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003385 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3386 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303387 Item.uDataAlloc || !Item.uLabelAlloc ||
3388 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3389 return -33;
3390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003391
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303392 if(QCBORDecode_Finish(&DC)) {
3393 return -34;
3394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003395
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003396 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003397}
3398
3399
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003400int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303401{
3402 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003403 QCBORError nCBORError;
3404
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003405
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303406 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003407 QCBORDecode_Init(&DC,
3408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3409 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003410
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003411 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003412
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003413 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3414 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303415 return -1;
3416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003417
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003418 if(CheckCSRMaps(&DC)) {
3419 return -2;
3420 }
3421
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303422 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003423 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003424 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003425
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303426 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003427 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303428 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003429
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303430 QCBORItem Item1, Item2, Item3, Item4;
3431 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003432 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303433 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3434 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003435 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303436 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003437 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303438 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003439 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303440 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003441 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303442 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003443 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003444
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303445 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003446
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303447 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303448 Item1.uDataType != QCBOR_TYPE_INT64 ||
3449 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003450 Item1.uDataAlloc != 0 ||
3451 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003452 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003453 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003454 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003455
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303456
3457 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003458 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303459 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003460 Item2.uDataAlloc != 0 ||
3461 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303462 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003463 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003464
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303465 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003466 Item3.uDataAlloc == 0 ||
3467 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003468 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003469 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003470 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003471
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303472 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003473 Item4.uDataAlloc == 0 ||
3474 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003475 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003476 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003478
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303479 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003480 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003481 QCBORDecode_Init(&DC,
3482 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3483 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303484 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3485 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003486 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303487 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003488 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003489 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003490 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303491 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3492 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3493 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3494 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3495 }
3496 }
3497 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003498 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003499 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303500 }
3501
3502 return 0;
3503}
3504
Laurence Lundbladef6531662018-12-04 10:42:22 +09003505
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303506
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003507int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003508{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003509 // Set up the decoder with a tiny bit of CBOR to parse because
3510 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003511 QCBORDecodeContext DC;
3512 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3513 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003514
Laurence Lundbladef6531662018-12-04 10:42:22 +09003515 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003516 // Then fish into the internals of the decode context
3517 // to get the allocator function so it can be called directly.
3518 // Also figure out how much pool is available for use
3519 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003520 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003521 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3522 if(nError) {
3523 return -9;
3524 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003525 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3526 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3527 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003528
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003529 // First test -- ask for one more byte than available and see failure
3530 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003531 if(!UsefulBuf_IsNULL(Allocated)) {
3532 return -1;
3533 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003534
Laurence Lundbladef6531662018-12-04 10:42:22 +09003535 // Re do the set up for the next test that will do a successful alloc,
3536 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003537 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003538 pAlloc = DC.StringAllocator.pfAllocator;
3539 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3540 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003541
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003542 // Allocate one byte less than available and see success
3543 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003544 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3545 return -2;
3546 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003547 // Ask for some more and see failure
3548 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003549 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3550 return -3;
3551 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003552 // Free the first allocate, retry the second and see success
3553 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3554 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003555 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3556 return -4;
3557 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003558
Laurence Lundbladef6531662018-12-04 10:42:22 +09003559 // Re do set up for next test that involves a successful alloc,
3560 // and a successful realloc and a failed realloc
3561 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003562 pAlloc = DC.StringAllocator.pfAllocator;
3563 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003564
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003565 // Allocate half the pool and see success
3566 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003567 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3568 return -5;
3569 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003570 // Reallocate to take up the whole pool and see success
3571 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003572 if(UsefulBuf_IsNULL(Allocated2)) {
3573 return -6;
3574 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003575 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003576 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3577 return -7;
3578 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003579 // Try to allocate more to be sure there is failure after a realloc
3580 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3581 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003582 return -8;
3583 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003584
Laurence Lundbladef6531662018-12-04 10:42:22 +09003585 return 0;
3586}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003587
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003588
3589/* Just enough of an allocator to test configuration of one */
3590static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3591{
3592 (void)pOldMem; // unused variable
3593
3594 if(uNewSize) {
3595 // Assumes the context pointer is the buffer and
3596 // nothing too big will ever be asked for.
3597 // This is only good for this basic test!
3598 return (UsefulBuf) {pCtx, uNewSize};
3599 } else {
3600 return NULLUsefulBuf;
3601 }
3602}
3603
3604
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003605int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003606{
3607 // Set up the decoder with a tiny bit of CBOR to parse because
3608 // nothing can be done with it unless that is set up.
3609 QCBORDecodeContext DC;
3610 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3611 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3612
3613 uint8_t pAllocatorBuffer[50];
3614
3615 // This is really just to test that this call works.
3616 // The full functionality of string allocators is tested
3617 // elsewhere with the MemPool internal allocator.
3618 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3619
3620 QCBORItem Item;
3621 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3622 return -1;
3623 }
3624
3625 if(Item.uDataAlloc == 0 ||
3626 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3627 Item.val.string.ptr != pAllocatorBuffer) {
3628 return -2;
3629 }
3630
3631 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3632 return -3;
3633 }
3634
3635 return 0;
3636}
3637
Laurence Lundblade59289e52019-12-30 13:44:37 -08003638
3639#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladea826c502020-05-10 21:07:00 -07003640/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08003641 [
3642 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003643 4([-20, 4759477275222530853136]),
3644 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003645 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003646 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003647 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07003648 5([ 9223372036854775806, -4759477275222530853137])
3649 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003650 ]
3651 */
3652
3653static const uint8_t spExpectedExponentsAndMantissas[] = {
3654 0x87,
3655 0xC4, 0x82, 0x20,
3656 0x03,
3657 0xC4, 0x82, 0x33,
3658 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3659 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3660 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3661 0xC5, 0x82, 0x19, 0x01, 0x2C,
3662 0x18, 0x64,
3663 0xC5, 0x82, 0x33,
3664 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3665 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3666 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07003667 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3668 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003669 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3670 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3671};
3672
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003673int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003674{
3675 QCBORDecodeContext DC;
3676 QCBORError nCBORError;
3677 QCBORItem item;
3678
Laurence Lundblade17af4902020-01-07 19:11:55 -08003679 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3680 0x06, 0x07, 0x08, 0x09, 0x010};
3681 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003682
3683
3684 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3685
3686 nCBORError = QCBORDecode_GetNext(&DC, &item);
3687 if(nCBORError != QCBOR_SUCCESS) {
3688 return 1;
3689 }
3690
3691 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3692 return 2;
3693 }
3694
3695 nCBORError = QCBORDecode_GetNext(&DC, &item);
3696 if(nCBORError != QCBOR_SUCCESS) {
3697 return 3;
3698 }
3699
3700 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3701 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3702 item.val.expAndMantissa.nExponent != -1) {
3703 return 4;
3704 }
3705
3706 nCBORError = QCBORDecode_GetNext(&DC, &item);
3707 if(nCBORError != QCBOR_SUCCESS) {
3708 return 5;
3709 }
3710
3711 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3712 item.val.expAndMantissa.nExponent != -20 ||
3713 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3714 return 6;
3715 }
3716
3717 nCBORError = QCBORDecode_GetNext(&DC, &item);
3718 if(nCBORError != QCBOR_SUCCESS) {
3719 return 7;
3720 }
3721
3722 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3723 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3724 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3725 return 8;
3726 }
3727
3728 nCBORError = QCBORDecode_GetNext(&DC, &item);
3729 if(nCBORError != QCBOR_SUCCESS) {
3730 return 9;
3731 }
3732
3733 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3734 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3735 item.val.expAndMantissa.nExponent != 300) {
3736 return 10;
3737 }
3738
Laurence Lundbladea826c502020-05-10 21:07:00 -07003739 // 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003740 nCBORError = QCBORDecode_GetNext(&DC, &item);
3741 if(nCBORError != QCBOR_SUCCESS) {
3742 return 11;
3743 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003744 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3745 item.val.expAndMantissa.nExponent != -20 ||
3746 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3747 return 12;
3748 }
3749
Laurence Lundbladea826c502020-05-10 21:07:00 -07003750 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade59289e52019-12-30 13:44:37 -08003751 nCBORError = QCBORDecode_GetNext(&DC, &item);
3752 if(nCBORError != QCBOR_SUCCESS) {
3753 return 13;
3754 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003755 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3756 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3757 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3758 return 14;
3759 }
3760
Laurence Lundbladea826c502020-05-10 21:07:00 -07003761 // 5([ 9223372036854775806, -4759477275222530853137])
3762 nCBORError = QCBORDecode_GetNext(&DC, &item);
3763 if(nCBORError != QCBOR_SUCCESS) {
3764 return 13;
3765 }
3766 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3767 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3768 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3769 return 14;
3770 }
3771
3772
3773 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003774 nCBORError = QCBORDecode_GetNext(&DC, &item);
3775 if(nCBORError != QCBOR_SUCCESS) {
3776 return 15;
3777 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003778 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3779 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3780 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3781 return 16;
3782 }
3783
3784 /* Now encode some stuff and then decode it */
3785 uint8_t pBuf[40];
3786 QCBOREncodeContext EC;
3787 UsefulBufC Encoded;
3788
3789 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3790 QCBOREncode_OpenArray(&EC);
3791 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3792 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3793 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3794 QCBOREncode_CloseArray(&EC);
3795 QCBOREncode_Finish(&EC, &Encoded);
3796
3797
3798 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3799 nCBORError = QCBORDecode_GetNext(&DC, &item);
3800 if(nCBORError != QCBOR_SUCCESS) {
3801 return 13;
3802 }
3803
3804 nCBORError = QCBORDecode_GetNext(&DC, &item);
3805 if(nCBORError != QCBOR_SUCCESS) {
3806 return 13;
3807 }
3808
3809 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3810 item.val.expAndMantissa.nExponent != 1000 ||
3811 item.val.expAndMantissa.Mantissa.nInt != 999) {
3812 return 15;
3813 }
3814
3815 nCBORError = QCBORDecode_GetNext(&DC, &item);
3816 if(nCBORError != QCBOR_SUCCESS) {
3817 return 13;
3818 }
3819
3820 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3821 item.val.expAndMantissa.nExponent != INT32_MIN ||
3822 item.val.expAndMantissa.Mantissa.nInt != 100) {
3823 return 15;
3824 }
3825
3826 nCBORError = QCBORDecode_GetNext(&DC, &item);
3827 if(nCBORError != QCBOR_SUCCESS) {
3828 return 13;
3829 }
3830
3831 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3832 item.val.expAndMantissa.nExponent != INT32_MAX ||
3833 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3834 return 12;
3835 }
3836
3837 return 0;
3838}
3839
3840
3841static struct FailInput ExponentAndMantissaFailures[] = {
3842 // Exponent > INT64_MAX
3843 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3844 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3845 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3846 // Mantissa > INT64_MAX
3847 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3848 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3849 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3850 // End of input
3851 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END},
3852 // End of input
3853 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END},
3854 // bad content for big num
3855 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3856 // bad content for big num
3857 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3858 // Bad integer for exponent
3859 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3860 // Bad integer for mantissa
3861 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3862 // 3 items in array
3863 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3864 // unterminated indefinite length array
3865 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3866 // Empty array
3867 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3868 // Second is not an integer
3869 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3870 // First is not an integer
3871 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3872 // Not an array
3873 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3874};
3875
3876
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003877int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003878{
3879 return ProcessFailures(ExponentAndMantissaFailures,
3880 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3881}
3882
3883#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003884
3885
3886
3887/*
3888 Some basic CBOR with map and array used in a lot of tests.
3889 The map labels are all strings
3890
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003891 {
3892 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003893 "an array of two strings": [
3894 "string1", "string2"
3895 ],
3896 "map in a map": {
3897 "bytes 1": h'78787878',
3898 "bytes 2": h'79797979',
3899 "another int": 98,
3900 "text 2": "lies, damn lies and statistics"
3901 }
3902 }
3903 */
3904
Laurence Lundbladeeb0b53d2020-04-14 18:34:25 -07003905#include "qcbor/qcbor_decode_map.h"
Laurence Lundblade1341c592020-04-11 14:19:05 -07003906#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003907
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003908static char strbuf[10];
3909const char *PrintType(uint8_t type) {
3910 switch(type) {
3911 case QCBOR_TYPE_INT64: return "INT64";
3912 case QCBOR_TYPE_UINT64: return "UINT64";
3913 case QCBOR_TYPE_ARRAY: return "ARRAY";
3914 case QCBOR_TYPE_MAP: return "MAP";
3915 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
3916 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
3917 default:
3918 sprintf(strbuf, "%d", type);
3919 return strbuf;
3920 }
3921}
3922
3923
3924void PrintItem(QCBORItem Item)
3925{
3926 printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":"");
3927 if(Item.uLabelType) {
3928 printf("Label: %s ", PrintType(Item.uLabelType));
3929 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3930 printf("%lld\n", Item.label.int64);
3931 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07003932 // TODO: proper conversion to null-terminated string
3933 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003934 }
3935 }
3936}
3937
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003938
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07003939int32_t EMap(UsefulBufC input)
3940{
3941 QCBORItem Item1, Item2, Item3;
3942 int64_t nDecodedInt1, nDecodedInt2;
3943 UsefulBufC B1, B2, S1, S2, S3;
3944
3945 QCBORDecodeContext DCtx;
3946 QCBORError nCBORError;
3947
3948 QCBORDecode_Init(&DCtx, input, 0);
3949
3950 QCBORDecode_EnterMap(&DCtx);
3951
3952 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
3953
3954 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
3955 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
3956 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1);
3957 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2);
3958 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
3959 QCBORDecode_ExitMap(&DCtx);
3960
3961 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3962 QCBORDecode_GetNext(&DCtx, &Item1);
3963 QCBORDecode_GetNext(&DCtx, &Item2);
3964 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
3965 return -400;
3966 }
3967 QCBORDecode_ExitArray(&DCtx);
3968
3969 // Parse the same array again using GetText() instead of GetItem()
3970 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3971 QCBORDecode_GetText(&DCtx, &S2);
3972 QCBORDecode_GetText(&DCtx, &S3);
3973 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
3974 return 5000;
3975 }
3976 /* QCBORDecode_GetText(&DCtx, &S3);
3977 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
3978 return 5001;
3979 } */
3980
3981 QCBORDecode_ExitArray(&DCtx);
3982
3983 QCBORDecode_ExitMap(&DCtx);
3984
3985 nCBORError = QCBORDecode_Finish(&DCtx);
3986
3987 if(nCBORError) {
3988 return (int32_t)nCBORError;
3989 }
3990
3991 if(nDecodedInt1 != 42) {
3992 return 1001;
3993 }
3994
3995 if(nDecodedInt2 != 98) {
3996 return 1002;
3997 }
3998
3999 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4000 UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){
4001 return 1003;
4002 }
4003
4004 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
4005 UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){
4006 return 1004;
4007 }
4008
4009 if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){
4010 return 1005;
4011 }
4012
4013 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4014 return 1006;
4015 }
4016
4017 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4018 return 1007;
4019 }
4020
4021 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4022 return 1008;
4023 }
4024
4025 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4026 return 1009;
4027 }
4028
4029 return 0;
4030}
4031
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004032int32_t EnterMapTest()
4033{
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004034 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004035 QCBORDecodeContext DCtx;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004036
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004037 int32_t nReturn;
Laurence Lundblade1341c592020-04-11 14:19:05 -07004038
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004039 (void)pValidMapIndefEncoded;
4040 nReturn = EMap( UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
4041 if(nReturn) {
4042 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004043 }
4044
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004045 nReturn = EMap( UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
4046 if(nReturn) {
4047 return nReturn;
4048 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004049
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004050
Laurence Lundblade937ea812020-05-08 11:38:23 -07004051
4052 // These tests confirm the cursor is at the right place after entering a map or array
4053
4054 // Confirm cursor is at right place
4055 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4056 QCBORDecode_EnterMap(&DCtx);
4057 QCBORDecode_GetNext(&DCtx, &Item1);
4058 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4059 return 2001;
4060 }
4061
4062
4063 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4064 QCBORDecode_GetNext(&DCtx, &Item1);
4065 QCBORDecode_GetNext(&DCtx, &Item1);
4066 QCBORDecode_EnterArray(&DCtx);
4067 QCBORDecode_GetNext(&DCtx, &Item1);
4068 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4069 return 2002;
4070 }
4071
4072 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4073 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004074 QCBORDecode_GetNext(&DCtx, &Item1);
4075 QCBORDecode_GetNext(&DCtx, &Item1);
4076 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004077 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4078 QCBORDecode_GetNext(&DCtx, &Item1);
4079 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4080 return 2003;
4081 }
4082
4083 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4084 QCBORDecode_EnterMap(&DCtx);
4085 QCBORDecode_GetNext(&DCtx, &Item1);
4086 QCBORDecode_GetNext(&DCtx, &Item1);
4087 QCBORDecode_GetNext(&DCtx, &Item1);
4088 QCBORDecode_GetNext(&DCtx, &Item1);
4089 QCBORDecode_GetNext(&DCtx, &Item1);
4090 QCBORDecode_GetNext(&DCtx, &Item1);
4091 QCBORDecode_GetNext(&DCtx, &Item1);
4092 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4093 QCBORDecode_GetNext(&DCtx, &Item1);
4094 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004095 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004096 }
4097
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004098 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4099 QCBORDecode_EnterMap(&DCtx);
4100 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4101 QCBORDecode_ExitArray(&DCtx);
4102 QCBORDecode_GetNext(&DCtx, &Item1);
4103 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4104 return 2006;
4105 }
4106 QCBORDecode_ExitMap(&DCtx);
4107 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4108 return 2007;
4109 }
4110
Laurence Lundblade937ea812020-05-08 11:38:23 -07004111
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004112 return 0;
4113}
4114
4115
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004116struct NumberConversion {
4117 char *szDescription;
4118 UsefulBufC CBOR;
4119 int64_t nConvertedToInt64;
4120 QCBORError uErrorInt64;
4121 uint64_t uConvertToUInt64;
4122 QCBORError uErrorUint64;
4123 double dConvertToDouble;
4124 QCBORError uErrorDouble;
4125};
4126
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004127static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004128 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004129 "negative bignum -1",
4130 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
4131 -1,
4132 QCBOR_SUCCESS,
4133 0,
4134 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4135 -1.0,
4136 QCBOR_SUCCESS
4137 },
4138 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004139 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004140 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4141 0xC2, 0x42, 0x01, 0x01}, 15},
4142 257000,
4143 QCBOR_SUCCESS,
4144 257000,
4145 QCBOR_SUCCESS,
4146 257000.0,
4147 QCBOR_SUCCESS
4148 },
4149 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004150 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004151 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4152 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07004153 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07004154 QCBOR_SUCCESS,
4155 0,
4156 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07004157 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07004158 QCBOR_SUCCESS
4159 },
4160 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004161 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004162 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4163 0xC2, 0x42, 0x01, 0x01}, 15},
4164 2056,
4165 QCBOR_SUCCESS,
4166 2056,
4167 QCBOR_SUCCESS,
4168 2056.0,
4169 QCBOR_SUCCESS
4170 },
4171 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004172 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07004173 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4174 0,
4175 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4176 0,
4177 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4178 -18446744073709551617.0,
4179 QCBOR_SUCCESS
4180 },
4181 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004182 "Positive bignum 0x01020304 indefinite length string",
4183 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
4184 0x01020304,
4185 QCBOR_SUCCESS,
4186 0x01020304,
4187 QCBOR_SUCCESS,
4188 16909060.0,
4189 QCBOR_SUCCESS
4190 },
4191 {
Laurence Lundblade887add82020-05-17 05:50:34 -07004192 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07004193 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4194 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
4195 0,
4196 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4197 0,
4198 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4199 -INFINITY,
4200 QCBOR_SUCCESS
4201 },
4202 {
4203 "big float [9223372036854775806, 9223372036854775806]",
4204 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4205 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
4206 0,
4207 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4208 0,
4209 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4210 INFINITY,
4211 QCBOR_SUCCESS
4212 },
4213 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004214 "Big float 3 * 2^^2",
4215 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
4216 12,
4217 QCBOR_SUCCESS,
4218 12,
4219 QCBOR_SUCCESS,
4220 12.0,
4221 QCBOR_SUCCESS
4222 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004223 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004224 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004225 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4226 0,
4227 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4228 18446744073709551615ULL,
4229 QCBOR_SUCCESS,
4230 18446744073709551615.0,
4231 QCBOR_SUCCESS
4232 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004233 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004234 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004235 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4236 65536-1,
4237 QCBOR_SUCCESS,
4238 0xffff,
4239 QCBOR_SUCCESS,
4240 65535.0,
4241 QCBOR_SUCCESS
4242 },
4243 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004244 "Postive integer 0",
4245 {(uint8_t[]){0x0}, 1},
4246 0LL,
4247 QCBOR_SUCCESS,
4248 0ULL,
4249 QCBOR_SUCCESS,
4250 0.0,
4251 QCBOR_SUCCESS
4252 },
4253 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004254 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004255 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4256 -9223372036854775807-1, // INT64_MIN
4257 QCBOR_SUCCESS,
4258 0ULL,
4259 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4260 -9223372036854775808.0,
4261 QCBOR_SUCCESS
4262 },
4263 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004264 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004265 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
4266 100L,
4267 QCBOR_SUCCESS,
4268 100ULL,
4269 QCBOR_SUCCESS,
4270 100.3,
4271 QCBOR_SUCCESS
4272 },
4273 {
4274 "Floating point value NaN 0xfa7fc00000",
4275 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
4276 0,
4277 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4278 0,
4279 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4280 NAN,
4281 QCBOR_SUCCESS
4282 },
4283 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004284 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004285 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
4286 -4,
4287 QCBOR_SUCCESS,
4288 0,
4289 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4290 -4.0,
4291 QCBOR_SUCCESS
4292 },
4293 {
4294 "Decimal fraction 3/10",
4295 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
4296 0,
4297 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4298 0,
4299 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4300 0.30000000000000004,
4301 QCBOR_SUCCESS
4302 }
4303};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004304
4305
4306
Laurence Lundblade313b2862020-05-16 01:23:06 -07004307int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004308{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004309 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004310
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004311 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
4312 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004313
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004314 // Set up the decoding context including a memory pool so that
4315 // indefinite length items can be checked
4316 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004317 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004318
4319 /* ----- test conversion to int64_t ------ */
4320 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004321 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4322 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004323 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004324 }
4325
4326 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004327 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004328 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004329 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004330 }
4331 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004332 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004333 }
4334
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004335 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004336 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4337 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4338 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004339 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004340 }
4341 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004342 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004343 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004344 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004345 }
4346 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004347 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004348 }
4349
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004350 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004351 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4352 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4353 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004354 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004355 }
4356 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004357 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004358 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004359 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004360 }
4361 if(pF->uErrorDouble == QCBOR_SUCCESS) {
4362 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004363 // NaN's can't be compared for equality. A NaN is
4364 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004365 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004366 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004367 }
4368 } else {
4369 // TODO: this comparison may need a margin of error
4370 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004371 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004372 }
4373 }
4374 }
4375 }
4376
4377 return 0;
4378}
4379
Laurence Lundblade313b2862020-05-16 01:23:06 -07004380int32_t IntegerConvertTestOld()
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004381{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004382 QCBORDecodeContext DCtx;
4383 QCBORError nCBORError;
4384
Laurence Lundbladea826c502020-05-10 21:07:00 -07004385 /* exponent, mantissa
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004386 [
4387 4([-1, 3]),
4388 4([-20, 4759477275222530853136]),
4389 4([9223372036854775807, -4759477275222530853137]),
4390 5([300, 100]),
4391 5([-20, 4759477275222530853136]),
4392 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004393 5([ 9223372036854775806, -4759477275222530853137])
4394 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004395 ]
4396 */
4397
4398 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4399
4400 QCBORItem Item;
4401 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4402 if(nCBORError) {
4403 return -1;
4404 }
4405
4406 int64_t integer;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004407 // 4([-1, 3]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004408 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004409 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004410 return -2;
4411 }
4412 DCtx.uLastError = 0; // TODO: a method for this
4413
Laurence Lundbladea826c502020-05-10 21:07:00 -07004414 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004415 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004416 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004417 return -2;
4418 }
4419 DCtx.uLastError = 0; // TODO: a method for this
4420
Laurence Lundbladea826c502020-05-10 21:07:00 -07004421 // 4([9223372036854775807, -4759477275222530853137]),
4422 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004423 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004424 return -2;
4425 }
4426 DCtx.uLastError = 0; // TODO: a method for this
4427
4428 // 5([300, 100]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004429 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004430 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004431 return -2;
4432 }
4433 DCtx.uLastError = 0; // TODO: a method for this
4434
Laurence Lundbladea826c502020-05-10 21:07:00 -07004435 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004436 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004437 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004438 return -2;
4439 }
4440 DCtx.uLastError = 0; // TODO: a method for this
4441
Laurence Lundbladea826c502020-05-10 21:07:00 -07004442 // 5([-9223372036854775807, -4759477275222530853137])
4443 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004444 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004445 return -2;
4446 }
4447 DCtx.uLastError = 0; // TODO: a method for this
4448
4449 // 5([ 9223372036854775806, -4759477275222530853137])
4450 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004451 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004452 return -2;
4453 }
4454 DCtx.uLastError = 0; // TODO: a method for this
4455
4456 // 5([ 9223372036854775806, 9223372036854775806])]
4457 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004458 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004459 return -2;
4460 }
4461 DCtx.uLastError = 0; // TODO: a method for this
4462
4463
4464
4465 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4466
4467 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4468 if(nCBORError) {
4469 return -1;
4470 }
4471
4472 uint64_t uinteger;
4473 // 4([-1, 3]),
4474 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004475 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004476 return -2;
4477 }
4478 DCtx.uLastError = 0; // TODO: a method for this
4479
4480 // 4([-20, 4759477275222530853136]),
4481 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004482 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004483 return -2;
4484 }
4485 DCtx.uLastError = 0; // TODO: a method for this
4486
4487 // 4([9223372036854775807, -4759477275222530853137]),
4488 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004489 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004490 return -2;
4491 }
4492 DCtx.uLastError = 0; // TODO: a method for this
4493
4494 // 5([300, 100]),
4495 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004496 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004497 return -2;
4498 }
4499 DCtx.uLastError = 0; // TODO: a method for this
4500
4501 // 5([-20, 4759477275222530853136]),
4502 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004503 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004504 return -2;
4505 }
4506 DCtx.uLastError = 0; // TODO: a method for this
4507
4508 // 5([-9223372036854775807, -4759477275222530853137])
4509 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004510 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004511 return -2;
4512 }
4513 DCtx.uLastError = 0; // TODO: a method for this
4514
4515 // 5([ 9223372036854775806, -4759477275222530853137])
4516 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004517 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004518 return -2;
4519 }
4520 DCtx.uLastError = 0; // TODO: a method for this
4521
4522 // 5([ 9223372036854775806, 9223372036854775806])]
4523 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004524 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004525 return -2;
4526 }
4527 DCtx.uLastError = 0; // TODO: a method for this
4528
4529
4530
4531 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4532 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4533 if(nCBORError) {
4534 return -1;
4535 }
4536
4537 double dResult;
4538 // 4([-1, 3]),
4539 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004540 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004541 dResult != 0.3) {
4542 return -2;
4543 }
4544
4545 // 4([-20, 4759477275222530853136]),
4546 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004547 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004548 dResult != 47.408855671161923) {
4549 return -2;
4550 }
4551
4552 // 4([9223372036854775807, -4759477275222530853137]),
4553 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004554 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004555 dResult != -INFINITY) {
4556 return -2;
4557 }
4558
4559 // 5([300, 100]),
4560 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004561 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004562 dResult != -INFINITY) {
4563 return -2;
4564 }
4565
4566 // 5([-20, 4759477275222530853136]),
4567 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004568 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004569 dResult != 4521260802379792.0) {
4570 return -2;
4571 }
4572
4573 // 5([-9223372036854775807, -4759477275222530853137])
4574 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004575 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004576 dResult != -0.0) {
4577 return -2;
4578 }
4579
4580 // 5([9223372036854775806, 9223372036854775806])]
4581 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004582 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004583 dResult != INFINITY) {
4584 return -2;
4585 }
4586
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004587 return 0;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004588}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07004589
4590
Laurence Lundbladee3553422020-05-02 11:11:17 -07004591int32_t CBORSequenceDecodeTests(void)
4592{
4593 QCBORDecodeContext DCtx;
4594 QCBORItem Item;
4595 QCBORError uCBORError;
4596
4597 // --- Test a sequence with extra bytes ---
4598
4599 // The input for the date test happens to be a sequence so it
4600 // is reused. It is a sequence because it doesn't start as
4601 // an array or map.
4602 QCBORDecode_Init(&DCtx,
4603 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
4604 QCBOR_DECODE_MODE_NORMAL);
4605
4606 // Get the first item
4607 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4608 if(uCBORError != QCBOR_SUCCESS) {
4609 return 1;
4610 }
4611 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
4612 return 2;
4613 }
4614
4615 // Get a second item
4616 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4617 if(uCBORError != QCBOR_SUCCESS) {
4618 return 2;
4619 }
4620 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
4621 return 3;
4622 }
4623
4624 // A sequence can have stuff at the end that may
4625 // or may not be valid CBOR. The protocol decoder knows
4626 // when to stop by definition of the protocol, not
4627 // when the top-level map or array is ended.
4628 // Finish still has to be called to know that
4629 // maps and arrays (if there were any) were closed
4630 // off correctly. When called like this it
4631 // must return the error QCBOR_ERR_EXTRA_BYTES.
4632 uCBORError = QCBORDecode_Finish(&DCtx);
4633 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
4634 return 4;
4635 }
4636
4637
4638 // --- Test an empty input ----
4639 uint8_t empty[1];
4640 UsefulBufC Empty = {empty, 0};
4641 QCBORDecode_Init(&DCtx,
4642 Empty,
4643 QCBOR_DECODE_MODE_NORMAL);
4644
4645 uCBORError = QCBORDecode_Finish(&DCtx);
4646 if(uCBORError != QCBOR_SUCCESS) {
4647 return 5;
4648 }
4649
4650
4651 // --- Sequence with unclosed indefinite length array ---
4652 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
4653
4654 QCBORDecode_Init(&DCtx,
4655 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
4656 QCBOR_DECODE_MODE_NORMAL);
4657
4658 // Get the first item
4659 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4660 if(uCBORError != QCBOR_SUCCESS) {
4661 return 7;
4662 }
4663 if(Item.uDataType != QCBOR_TYPE_INT64) {
4664 return 8;
4665 }
4666
4667 // Get a second item
4668 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4669 if(uCBORError != QCBOR_SUCCESS) {
4670 return 9;
4671 }
4672 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4673 return 10;
4674 }
4675
4676 // Try to finish before consuming all bytes to confirm
4677 // that the still-open error is returned.
4678 uCBORError = QCBORDecode_Finish(&DCtx);
4679 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
4680 return 11;
4681 }
4682
4683
4684 // --- Sequence with a closed indefinite length array ---
4685 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
4686
4687 QCBORDecode_Init(&DCtx,
4688 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
4689 QCBOR_DECODE_MODE_NORMAL);
4690
4691 // Get the first item
4692 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4693 if(uCBORError != QCBOR_SUCCESS) {
4694 return 12;
4695 }
4696 if(Item.uDataType != QCBOR_TYPE_INT64) {
4697 return 13;
4698 }
4699
4700 // Get a second item
4701 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4702 if(uCBORError != QCBOR_SUCCESS) {
4703 return 14;
4704 }
4705 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4706 return 15;
4707 }
4708
4709 // Try to finish before consuming all bytes to confirm
4710 // that the still-open error is returned.
4711 uCBORError = QCBORDecode_Finish(&DCtx);
4712 if(uCBORError != QCBOR_SUCCESS) {
4713 return 16;
4714 }
4715
4716
4717 return 0;
4718}
4719
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004720
Laurence Lundblade70ecead2020-06-15 19:40:06 -07004721
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004722int32_t IntToTests()
4723{
4724 int nErrCode;
4725 int32_t n32;
4726 int16_t n16;
4727 int8_t n8;
4728 uint32_t u32;
4729 uint16_t u16;
4730 uint8_t u8;
4731 uint64_t u64;
4732
4733 nErrCode = QCBOR_Int64ToInt32(1, &n32);
4734 if(nErrCode == -1 || n32 != 1) {
4735 return 1;
4736 }
4737
4738 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
4739 if(nErrCode == -1 || n32 != INT32_MAX) {
4740 return 2;
4741 }
4742
4743 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
4744 if(nErrCode == -1 || n32 != INT32_MIN) {
4745 return 3;
4746 }
4747
4748 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
4749 if(nErrCode != -1) {
4750 return 4;
4751 }
4752
4753 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
4754 if(nErrCode != -1) {
4755 return 5;
4756 }
4757
4758
4759 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
4760 if(nErrCode == -1 || n16 != INT16_MAX) {
4761 return 6;
4762 }
4763
4764 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
4765 if(nErrCode == -1 || n16 != INT16_MIN) {
4766 return 7;
4767 }
4768
4769 nErrCode = QCBOR_Int64ToInt16(1, &n16);
4770 if(nErrCode == -1 || n16 != 1) {
4771 return 8;
4772 }
4773
4774 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
4775 if(nErrCode != -1) {
4776 return 9;
4777 }
4778
4779 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
4780 if(nErrCode != -1) {
4781 return 10;
4782 }
4783
4784
4785 nErrCode = QCBOR_Int64ToInt8(1, &n8);
4786 if(nErrCode == -1 || n8 != 1) {
4787 return 11;
4788 }
4789
4790 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
4791 if(nErrCode == -1 || n8 != INT8_MAX) {
4792 return 12;
4793 }
4794
4795 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
4796 if(nErrCode == -1 || n8 != INT8_MIN) {
4797 return 13;
4798 }
4799
4800 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
4801 if(nErrCode != -1) {
4802 return 14;
4803 }
4804
4805 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
4806 if(nErrCode != -1) {
4807 return 15;
4808 }
4809
4810
4811 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
4812 if(nErrCode == -1 || u32 != 1) {
4813 return 16;
4814 }
4815
4816 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
4817 if(nErrCode == -1 || u32 != UINT32_MAX) {
4818 return 17;
4819 }
4820
4821 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
4822 if(nErrCode == -1 || u32 != 0) {
4823 return 18;
4824 }
4825
4826 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
4827 if(nErrCode != -1) {
4828 return 19;
4829 }
4830
4831 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
4832 if(nErrCode != -1) {
4833 return 20;
4834 }
4835
4836
4837 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
4838 if(nErrCode == -1 || u16 != UINT16_MAX) {
4839 return 21;
4840 }
4841
4842 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
4843 if(nErrCode == -1 || u16 != 0) {
4844 return 22;
4845 }
4846
4847 nErrCode = QCBOR_Int64UToInt16(1, &u16);
4848 if(nErrCode == -1 || u16 != 1) {
4849 return 23;
4850 }
4851
4852 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
4853 if(nErrCode != -1) {
4854 return 24;
4855 }
4856
4857 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
4858 if(nErrCode != -1) {
4859 return 25;
4860 }
4861
4862
4863 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
4864 if(nErrCode == -1 || u8 != UINT8_MAX) {
4865 return 26;
4866 }
4867
4868 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
4869 if(nErrCode == -1 || u8 != 0) {
4870 return 27;
4871 }
4872
4873 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
4874 if(nErrCode == -1 || u8 != 1) {
4875 return 28;
4876 }
4877
4878 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
4879 if(nErrCode != -1) {
4880 return 29;
4881 }
4882
4883 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
4884 if(nErrCode != -1) {
4885 return 30;
4886 }
4887
4888
4889 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
4890 if(nErrCode == -1 || u64 != 1) {
4891 return 31;
4892 }
4893
4894 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
4895 if(nErrCode == -1 || u64 != INT64_MAX) {
4896 return 32;
4897 }
4898
4899 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
4900 if(nErrCode == -1 || u64 != 0) {
4901 return 33;
4902 }
4903
4904 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
4905 if(nErrCode != -1) {
4906 return 34;
4907 }
4908
4909 return 0;
4910}
4911
Laurence Lundblade0750fc42020-06-20 21:02:34 -07004912
4913/*
4914A sequence with
4915 A wrapping bstr
4916 containing a map
4917 1
4918 2
4919 A wrapping bstr
4920 containing an array
4921 3
4922 wrapping bstr
4923 4
4924 5
4925 6
4926 array
4927 7
4928 8
4929
4930 */
4931
4932static UsefulBufC foo(UsefulBuf ffo)
4933{
4934 UsefulBufC Encoded;
4935 QCBOREncodeContext EC;
4936 QCBORError uErr;
4937
4938 QCBOREncode_Init(&EC, ffo);
4939
4940 QCBOREncode_BstrWrap(&EC);
4941 QCBOREncode_OpenMap(&EC);
4942 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
4943 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
4944 QCBOREncode_CloseMap(&EC);
4945 QCBOREncode_BstrWrap(&EC);
4946 QCBOREncode_OpenArray(&EC);
4947 QCBOREncode_AddInt64(&EC, 3);
4948 QCBOREncode_BstrWrap(&EC);
4949 QCBOREncode_AddInt64(&EC, 4);
4950 QCBOREncode_CloseBstrWrap(&EC, NULL);
4951 QCBOREncode_AddInt64(&EC, 5);
4952 QCBOREncode_CloseArray(&EC);
4953 QCBOREncode_CloseBstrWrap(&EC, NULL);
4954 QCBOREncode_AddInt64(&EC, 6);
4955 QCBOREncode_CloseBstrWrap(&EC, NULL);
4956 QCBOREncode_OpenArray(&EC);
4957 QCBOREncode_AddInt64(&EC, 7);
4958 QCBOREncode_AddInt64(&EC, 8);
4959 QCBOREncode_CloseArray(&EC);
4960
4961 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07004962 if(uErr) {
4963 Encoded = NULLUsefulBufC;
4964 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07004965
4966 return Encoded;
4967}
4968
4969
4970int32_t EnterBstrTest()
4971{
4972 MakeUsefulBufOnStack(ffo, 100);
4973
4974 QCBORDecodeContext DC;
4975
4976 QCBORDecode_Init(&DC, foo(ffo), 0);
4977
4978 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
4979
4980
4981 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
4982 QCBORDecode_EnterMap(&DC);
4983 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
4984 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
4985 QCBORDecode_ExitMap(&DC);
4986 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
4987 QCBORDecode_EnterArray(&DC);
4988 QCBORDecode_GetInt64(&DC, &i3);
4989 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
4990 QCBORDecode_GetInt64(&DC, &i4);
4991 QCBORDecode_ExitBstrWrapped(&DC);
4992 QCBORDecode_GetInt64(&DC, &i5);
4993 QCBORDecode_ExitArray(&DC);
4994 QCBORDecode_ExitBstrWrapped(&DC);
4995 QCBORDecode_GetInt64(&DC, &i6);
4996 QCBORDecode_ExitBstrWrapped(&DC);
4997 QCBORDecode_EnterArray(&DC);
4998 QCBORDecode_GetInt64(&DC, &i7);
4999 QCBORDecode_GetInt64(&DC, &i8);
5000 QCBORDecode_ExitArray(&DC);
5001
5002 QCBORError uErr = QCBORDecode_Finish(&DC);
5003
5004 return (int32_t)uErr;
5005}