blob: c87f9e153187885c38bbea0d65f8022491cf98cb [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 Lundbladee6f15112020-07-23 18:44:16 -07001615static bool IsNotWellFormedError(QCBORError nErr)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001616{
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:
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001626 case QCBOR_ERR_NO_MORE_ITEMS: // TODO: really keep this?
1627 return true;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001628 default:
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001629 return false;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001630 }
1631}
1632
1633
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001634int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001635{
1636 // Loop over all the not-well-formed instance of CBOR
1637 // that are test vectors in not_well_formed_cbor.h
1638 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1639 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1640 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1641 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1642
Laurence Lundbladeee851742020-01-08 08:37:05 -08001643 // Set up decoder context. String allocator needed for indefinite
1644 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001645 QCBORDecodeContext DCtx;
1646 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1647 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1648 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1649
1650 // Loop getting items until no more to get
1651 QCBORError nCBORError;
1652 do {
1653 QCBORItem Item;
1654
1655 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1656 } while(nCBORError == QCBOR_SUCCESS);
1657
1658 // Every test vector must fail with
1659 // a not-well-formed error. If not
1660 // this test fails.
1661 if(!IsNotWellFormedError(nCBORError)) {
1662 // Return index of failure in the error code
1663 return 2000 + nIterate;
1664 }
1665 }
1666 return 0;
1667}
1668
1669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001670struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001671 UsefulBufC Input;
1672 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001673};
1674
Laurence Lundblade59289e52019-12-30 13:44:37 -08001675
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001676static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001677{
1678 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1679 // Set up the decoding context including a memory pool so that
1680 // indefinite length items can be checked
1681 QCBORDecodeContext DCtx;
1682 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1683 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001684
Laurence Lundblade59289e52019-12-30 13:44:37 -08001685 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1686 if(nCBORError) {
1687 return -9;
1688 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001689
Laurence Lundblade59289e52019-12-30 13:44:37 -08001690 // Iterate until there is an error of some sort error
1691 QCBORItem Item;
1692 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001693 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001694 memset(&Item, 0x33, sizeof(Item));
1695
1696 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1697 } while(nCBORError == QCBOR_SUCCESS);
1698
1699 // Must get the expected error or the this test fails
1700 // The data and label type must also be QCBOR_TYPE_NONE
1701 if(nCBORError != pF->nError ||
1702 Item.uDataType != QCBOR_TYPE_NONE ||
1703 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001704 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001705 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001706 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001707 }
1708 }
1709
1710 return 0;
1711}
1712
1713
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001714struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001715 // Most of this is copied from not_well_formed.h. Here the error code
1716 // returned is also checked.
1717
1718 // Indefinite length strings must be closed off
1719 // An indefinite length byte string not closed off
1720 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1721 // An indefinite length text string not closed off
1722 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1723
1724
1725 // All the chunks in an indefinite length string must be of the type of indefinite length string
1726 // indefinite length byte string with text string chunk
1727 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1728 // indefinite length text string with a byte string chunk
1729 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1730 // indefinite length byte string with an positive integer chunk
1731 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1732 // indefinite length byte string with an negative integer chunk
1733 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1734 // indefinite length byte string with an array chunk
1735 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1736 // indefinite length byte string with an map chunk
1737 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1738 // indefinite length byte string with tagged integer chunk
1739 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1740 // indefinite length byte string with an simple type chunk
1741 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1742 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1743 // indefinite length text string with indefinite string inside
1744 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1745
1746
1747 // Definte length maps and arrays must be closed by having the right number of items
1748 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001749 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001750 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001751 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752 // A definte length array that is supposed to have 511 items, but has only 1
1753 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1754 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001755 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001756 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001757 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001758
1759
1760 // Indefinte length maps and arrays must be ended by a break
1761 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001762 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001764 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001766 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001767 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001768 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001769
1770
1771 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001772 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001773 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001774 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001775 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001776 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001777 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, // TODO: 23
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001778 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001779 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001780 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001781 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001782 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001783 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001784 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001785
1786
1787 // The "argument" for the data item is incomplete
1788 // Positive integer missing 1 byte argument
1789 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1790 // Positive integer missing 2 byte argument
1791 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1792 // Positive integer missing 4 byte argument
1793 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1794 // Positive integer missing 8 byte argument
1795 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1796 // Positive integer missing 1 byte of 2 byte argument
1797 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1798 // Positive integer missing 2 bytes of 4 byte argument
1799 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1800 // Positive integer missing 1 bytes of 7 byte argument
1801 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1802 // Negative integer missing 1 byte argument
1803 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1804 // Binary string missing 1 byte argument
1805 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1806 // Text string missing 1 byte argument
1807 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1808 // Array missing 1 byte argument
1809 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1810 // Map missing 1 byte argument
1811 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1812 // Tag missing 1 byte argument
1813 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1814 // Simple missing 1 byte argument
1815 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1816
1817
1818 // Breaks must not occur in definite length arrays and maps
1819 // Array of length 1 with sole member replaced by a break
1820 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1821 // Array of length 2 with 2nd member replaced by a break
1822 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1823 // Map of length 1 with sole member label replaced by a break
1824 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1825 // Map of length 1 with sole member label replaced by break
1826 // Alternate representation that some decoders handle difference
1827 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1828 // Array of length 1 with 2nd member value replaced by a break
1829 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1830 // Map of length 2 with 2nd member replaced by a break
1831 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1832
1833
1834 // Breaks must not occur on their own out of an indefinite length data item
1835 // A bare break is not well formed
1836 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1837 // A bare break after a zero length definite length array
1838 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1839 // A bare break after a zero length indefinite length map
1840 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1841
1842
1843 // Forbidden two byte encodings of simple types
1844 // Must use 0xe0 instead
1845 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1846 // Should use 0xe1 instead
1847 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1848 // Should use 0xe2 instead
1849 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1850 // Should use 0xe3 instead
1851 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1852 // Should use 0xe4 instead
1853 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1854 // Should use 0xe5 instead
1855 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1856 // Should use 0xe6 instead
1857 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1858 // Should use 0xe7 instead
1859 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1860 // Should use 0xe8 instead
1861 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1862 // Should use 0xe9 instead
1863 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1864 // Should use 0xea instead
1865 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1866 // Should use 0xeb instead
1867 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1868 // Should use 0xec instead
1869 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1870 // Should use 0xed instead
1871 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1872 // Should use 0xee instead
1873 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1874 // Should use 0xef instead
1875 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1876 // Should use 0xf0 instead
1877 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1878 // Should use 0xf1 instead
1879 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1880 // Should use 0xf2 instead
1881 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1882 // Must use 0xf3 instead
1883 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1884 // Must use 0xf4 instead
1885 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1886 // Must use 0xf5 instead
1887 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1888 // Must use 0xf6 instead
1889 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1890 // Must use 0xf7 instead
1891 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1892 // Must use 0xf8 instead
1893 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1894
1895
1896 // Integers with additional info indefinite length
1897 // Positive integer with additional info indefinite length
1898 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1899 // Negative integer with additional info indefinite length
1900 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1901 // CBOR tag with "argument" an indefinite length
1902 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1903 // CBOR tag with "argument" an indefinite length alternate vector
1904 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1905
1906
1907 // Missing bytes from a deterministic length string
1908 // A byte string is of length 1 without the 1 byte
1909 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1910 // A text string is of length 1 without the 1 byte
1911 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001912 // Byte string should have 2^32-15 bytes, but has one
1913 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1914 // Byte string should have 2^32-15 bytes, but has one
1915 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001916
1917
1918 // Use of unassigned additional information values
1919 // Major type positive integer with reserved value 28
1920 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1921 // Major type positive integer with reserved value 29
1922 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1923 // Major type positive integer with reserved value 30
1924 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1925 // Major type negative integer with reserved value 28
1926 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1927 // Major type negative integer with reserved value 29
1928 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1929 // Major type negative integer with reserved value 30
1930 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1931 // Major type byte string with reserved value 28 length
1932 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1933 // Major type byte string with reserved value 29 length
1934 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1935 // Major type byte string with reserved value 30 length
1936 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1937 // Major type text string with reserved value 28 length
1938 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1939 // Major type text string with reserved value 29 length
1940 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1941 // Major type text string with reserved value 30 length
1942 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1943 // Major type array with reserved value 28 length
1944 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1945 // Major type array with reserved value 29 length
1946 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1947 // Major type array with reserved value 30 length
1948 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1949 // Major type map with reserved value 28 length
1950 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1951 // Major type map with reserved value 29 length
1952 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1953 // Major type map with reserved value 30 length
1954 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1955 // Major type tag with reserved value 28 length
1956 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1957 // Major type tag with reserved value 29 length
1958 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1959 // Major type tag with reserved value 30 length
1960 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1961 // Major type simple with reserved value 28 length
1962 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1963 // Major type simple with reserved value 29 length
1964 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1965 // Major type simple with reserved value 30 length
1966 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1967
1968
1969 // Maps must have an even number of data items (key & value)
1970 // Map with 1 item when it should have 2
1971 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1972 // Map with 3 item when it should have 4
1973 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1974 // Map with 1 item when it should have 2
1975 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1976 // Map with 3 item when it should have 4
1977 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1978
1979
1980 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001981 // Text-based date, with an integer
1982 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1983 // Epoch date, with an byte string
1984 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1985 // tagged as both epoch and string dates
1986 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1987 // big num tagged an int, not a byte string
1988 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001989};
1990
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001991int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001993 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001994
Laurence Lundblade59289e52019-12-30 13:44:37 -08001995 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1996 if(nResult) {
1997 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001999
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002000 // Corrupt the UsefulInputBuf and see that
2001 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002002 QCBORDecodeContext DCtx;
2003 QCBORItem Item;
2004 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002005
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002006 QCBORDecode_Init(&DCtx,
2007 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2008 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002009
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002010 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2011 return (int32_t)uQCBORError;
2012 }
2013 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2014 // This wasn't supposed to happen
2015 return -1;
2016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002017
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002018 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002019
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002020 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2021 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2022 // Did not get back the error expected
2023 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002025
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002026/*
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002027 TODO: fix this
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002028 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
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003851 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08003852 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003853 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08003854 // 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 Lundbladeabf5c572020-06-29 21:21:29 -07004032
4033/*
4034 [23,
4035 6000,
4036 h'67616C6163746963',
4037 h'686176656E20746F6B656E'
4038 ]
4039 */
4040static const uint8_t spSimpleArray[] = {
40410x84, 0x17, 0x19, 0x17, 0x70, 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63, 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
4042
4043
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004044static const uint8_t spEmptyMap[] = {0xa0};
4045
4046static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004047
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004048int32_t EnterMapTest()
4049{
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004050 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004051 QCBORDecodeContext DCtx;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004052
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004053 int32_t nReturn;
Laurence Lundblade1341c592020-04-11 14:19:05 -07004054
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004055 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004056 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004057 if(nReturn) {
4058 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004059 }
4060
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004061 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004062 if(nReturn) {
4063 return nReturn;
4064 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004065
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004066
Laurence Lundblade937ea812020-05-08 11:38:23 -07004067
4068 // These tests confirm the cursor is at the right place after entering a map or array
4069
4070 // Confirm cursor is at right place
4071 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4072 QCBORDecode_EnterMap(&DCtx);
4073 QCBORDecode_GetNext(&DCtx, &Item1);
4074 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4075 return 2001;
4076 }
4077
4078
4079 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4080 QCBORDecode_GetNext(&DCtx, &Item1);
4081 QCBORDecode_GetNext(&DCtx, &Item1);
4082 QCBORDecode_EnterArray(&DCtx);
4083 QCBORDecode_GetNext(&DCtx, &Item1);
4084 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4085 return 2002;
4086 }
4087
4088 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4089 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004090 QCBORDecode_GetNext(&DCtx, &Item1);
4091 QCBORDecode_GetNext(&DCtx, &Item1);
4092 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004093 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4094 QCBORDecode_GetNext(&DCtx, &Item1);
4095 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4096 return 2003;
4097 }
4098
4099 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4100 QCBORDecode_EnterMap(&DCtx);
4101 QCBORDecode_GetNext(&DCtx, &Item1);
4102 QCBORDecode_GetNext(&DCtx, &Item1);
4103 QCBORDecode_GetNext(&DCtx, &Item1);
4104 QCBORDecode_GetNext(&DCtx, &Item1);
4105 QCBORDecode_GetNext(&DCtx, &Item1);
4106 QCBORDecode_GetNext(&DCtx, &Item1);
4107 QCBORDecode_GetNext(&DCtx, &Item1);
4108 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4109 QCBORDecode_GetNext(&DCtx, &Item1);
4110 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004111 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004112 }
4113
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004114 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4115 QCBORDecode_EnterMap(&DCtx);
4116 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4117 QCBORDecode_ExitArray(&DCtx);
4118 QCBORDecode_GetNext(&DCtx, &Item1);
4119 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4120 return 2006;
4121 }
4122 QCBORDecode_ExitMap(&DCtx);
4123 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4124 return 2007;
4125 }
4126
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004127 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4128 QCBORDecode_EnterArray(&DCtx);
4129 int64_t nDecodedInt2;
4130 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4131 QCBORError uErr = QCBORDecode_GetAndResetError(&DCtx);
4132 if(uErr != QCBOR_ERR_NOT_A_MAP){
4133 return 2008;
4134 }
4135 UsefulBufC String;
4136 QCBORDecode_GetTextInMapN(&DCtx, 88, &String);
4137 if(uErr != QCBOR_ERR_NOT_A_MAP){
4138 return 2009;
4139 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004140
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004141
4142 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4143 QCBORDecode_EnterMap(&DCtx);
4144 // This will fail because the map is empty.
4145 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4146 uErr = QCBORDecode_GetAndResetError(&DCtx);
4147 if(uErr != QCBOR_ERR_NOT_FOUND){
4148 return 2010;
4149 }
4150 QCBORDecode_ExitMap(&DCtx);
4151 uErr = QCBORDecode_Finish(&DCtx);
4152 if(uErr != QCBOR_SUCCESS){
4153 return 2011;
4154 }
4155
4156
4157 // TODO: more testing of nested zero length maps; also test zero-length arrays
4158 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4159 QCBORDecode_EnterMap(&DCtx);
4160 // This will fail because the map is empty.
4161 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4162 uErr = QCBORDecode_GetAndResetError(&DCtx);
4163 if(uErr != QCBOR_ERR_NOT_FOUND){
4164 return 2010;
4165 }
4166 QCBORDecode_ExitMap(&DCtx);
4167 uErr = QCBORDecode_Finish(&DCtx);
4168 if(uErr != QCBOR_SUCCESS){
4169 return 2011;
4170 }
4171
4172 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004173}
4174
4175
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004176struct NumberConversion {
4177 char *szDescription;
4178 UsefulBufC CBOR;
4179 int64_t nConvertedToInt64;
4180 QCBORError uErrorInt64;
4181 uint64_t uConvertToUInt64;
4182 QCBORError uErrorUint64;
4183 double dConvertToDouble;
4184 QCBORError uErrorDouble;
4185};
4186
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004187static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004188 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004189 "negative bignum -1",
4190 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
4191 -1,
4192 QCBOR_SUCCESS,
4193 0,
4194 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4195 -1.0,
4196 QCBOR_SUCCESS
4197 },
4198 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004199 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004200 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4201 0xC2, 0x42, 0x01, 0x01}, 15},
4202 257000,
4203 QCBOR_SUCCESS,
4204 257000,
4205 QCBOR_SUCCESS,
4206 257000.0,
4207 QCBOR_SUCCESS
4208 },
4209 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004210 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004211 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4212 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07004213 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07004214 QCBOR_SUCCESS,
4215 0,
4216 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07004217 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07004218 QCBOR_SUCCESS
4219 },
4220 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004221 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004222 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4223 0xC2, 0x42, 0x01, 0x01}, 15},
4224 2056,
4225 QCBOR_SUCCESS,
4226 2056,
4227 QCBOR_SUCCESS,
4228 2056.0,
4229 QCBOR_SUCCESS
4230 },
4231 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004232 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07004233 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4234 0,
4235 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4236 0,
4237 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4238 -18446744073709551617.0,
4239 QCBOR_SUCCESS
4240 },
4241 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004242 "Positive bignum 0x01020304 indefinite length string",
4243 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
4244 0x01020304,
4245 QCBOR_SUCCESS,
4246 0x01020304,
4247 QCBOR_SUCCESS,
4248 16909060.0,
4249 QCBOR_SUCCESS
4250 },
4251 {
Laurence Lundblade887add82020-05-17 05:50:34 -07004252 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07004253 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4254 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
4255 0,
4256 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4257 0,
4258 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4259 -INFINITY,
4260 QCBOR_SUCCESS
4261 },
4262 {
4263 "big float [9223372036854775806, 9223372036854775806]",
4264 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4265 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
4266 0,
4267 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4268 0,
4269 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4270 INFINITY,
4271 QCBOR_SUCCESS
4272 },
4273 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004274 "Big float 3 * 2^^2",
4275 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
4276 12,
4277 QCBOR_SUCCESS,
4278 12,
4279 QCBOR_SUCCESS,
4280 12.0,
4281 QCBOR_SUCCESS
4282 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004283 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004284 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004285 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4286 0,
4287 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4288 18446744073709551615ULL,
4289 QCBOR_SUCCESS,
4290 18446744073709551615.0,
4291 QCBOR_SUCCESS
4292 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004293 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004294 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004295 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4296 65536-1,
4297 QCBOR_SUCCESS,
4298 0xffff,
4299 QCBOR_SUCCESS,
4300 65535.0,
4301 QCBOR_SUCCESS
4302 },
4303 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004304 "Postive integer 0",
4305 {(uint8_t[]){0x0}, 1},
4306 0LL,
4307 QCBOR_SUCCESS,
4308 0ULL,
4309 QCBOR_SUCCESS,
4310 0.0,
4311 QCBOR_SUCCESS
4312 },
4313 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004314 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004315 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4316 -9223372036854775807-1, // INT64_MIN
4317 QCBOR_SUCCESS,
4318 0ULL,
4319 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4320 -9223372036854775808.0,
4321 QCBOR_SUCCESS
4322 },
4323 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004324 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004325 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
4326 100L,
4327 QCBOR_SUCCESS,
4328 100ULL,
4329 QCBOR_SUCCESS,
4330 100.3,
4331 QCBOR_SUCCESS
4332 },
4333 {
4334 "Floating point value NaN 0xfa7fc00000",
4335 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
4336 0,
4337 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4338 0,
4339 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4340 NAN,
4341 QCBOR_SUCCESS
4342 },
4343 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004344 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004345 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
4346 -4,
4347 QCBOR_SUCCESS,
4348 0,
4349 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4350 -4.0,
4351 QCBOR_SUCCESS
4352 },
4353 {
4354 "Decimal fraction 3/10",
4355 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
4356 0,
4357 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4358 0,
4359 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4360 0.30000000000000004,
4361 QCBOR_SUCCESS
4362 }
4363};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004364
4365
4366
Laurence Lundblade313b2862020-05-16 01:23:06 -07004367int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004368{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004369 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004370
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004371 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
4372 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004373
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004374 // Set up the decoding context including a memory pool so that
4375 // indefinite length items can be checked
4376 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004377 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004378
4379 /* ----- test conversion to int64_t ------ */
4380 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004381 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4382 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004383 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004384 }
4385
4386 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004387 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004388 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004389 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004390 }
4391 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004392 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004393 }
4394
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004395 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004396 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4397 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4398 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004399 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004400 }
4401 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004402 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004403 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004404 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004405 }
4406 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004407 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004408 }
4409
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004410 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004411 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4412 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4413 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004414 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004415 }
4416 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004417 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004418 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004419 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004420 }
4421 if(pF->uErrorDouble == QCBOR_SUCCESS) {
4422 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004423 // NaN's can't be compared for equality. A NaN is
4424 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004425 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004426 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004427 }
4428 } else {
4429 // TODO: this comparison may need a margin of error
4430 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004431 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004432 }
4433 }
4434 }
4435 }
4436
4437 return 0;
4438}
4439
Laurence Lundblade313b2862020-05-16 01:23:06 -07004440int32_t IntegerConvertTestOld()
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004441{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004442 QCBORDecodeContext DCtx;
4443 QCBORError nCBORError;
4444
Laurence Lundbladea826c502020-05-10 21:07:00 -07004445 /* exponent, mantissa
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004446 [
4447 4([-1, 3]),
4448 4([-20, 4759477275222530853136]),
4449 4([9223372036854775807, -4759477275222530853137]),
4450 5([300, 100]),
4451 5([-20, 4759477275222530853136]),
4452 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004453 5([ 9223372036854775806, -4759477275222530853137])
4454 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004455 ]
4456 */
4457
4458 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4459
4460 QCBORItem Item;
4461 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4462 if(nCBORError) {
4463 return -1;
4464 }
4465
4466 int64_t integer;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004467 // 4([-1, 3]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004468 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004469 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004470 return -2;
4471 }
4472 DCtx.uLastError = 0; // TODO: a method for this
4473
Laurence Lundbladea826c502020-05-10 21:07:00 -07004474 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004475 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004476 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004477 return -2;
4478 }
4479 DCtx.uLastError = 0; // TODO: a method for this
4480
Laurence Lundbladea826c502020-05-10 21:07:00 -07004481 // 4([9223372036854775807, -4759477275222530853137]),
4482 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004483 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004484 return -2;
4485 }
4486 DCtx.uLastError = 0; // TODO: a method for this
4487
4488 // 5([300, 100]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004489 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004490 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004491 return -2;
4492 }
4493 DCtx.uLastError = 0; // TODO: a method for this
4494
Laurence Lundbladea826c502020-05-10 21:07:00 -07004495 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004496 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004497 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004498 return -2;
4499 }
4500 DCtx.uLastError = 0; // TODO: a method for this
4501
Laurence Lundbladea826c502020-05-10 21:07:00 -07004502 // 5([-9223372036854775807, -4759477275222530853137])
4503 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004504 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004505 return -2;
4506 }
4507 DCtx.uLastError = 0; // TODO: a method for this
4508
4509 // 5([ 9223372036854775806, -4759477275222530853137])
4510 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004511 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004512 return -2;
4513 }
4514 DCtx.uLastError = 0; // TODO: a method for this
4515
4516 // 5([ 9223372036854775806, 9223372036854775806])]
4517 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004518 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004519 return -2;
4520 }
4521 DCtx.uLastError = 0; // TODO: a method for this
4522
4523
4524
4525 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4526
4527 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4528 if(nCBORError) {
4529 return -1;
4530 }
4531
4532 uint64_t uinteger;
4533 // 4([-1, 3]),
4534 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004535 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004536 return -2;
4537 }
4538 DCtx.uLastError = 0; // TODO: a method for this
4539
4540 // 4([-20, 4759477275222530853136]),
4541 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004542 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004543 return -2;
4544 }
4545 DCtx.uLastError = 0; // TODO: a method for this
4546
4547 // 4([9223372036854775807, -4759477275222530853137]),
4548 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004549 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004550 return -2;
4551 }
4552 DCtx.uLastError = 0; // TODO: a method for this
4553
4554 // 5([300, 100]),
4555 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004556 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004557 return -2;
4558 }
4559 DCtx.uLastError = 0; // TODO: a method for this
4560
4561 // 5([-20, 4759477275222530853136]),
4562 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004563 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004564 return -2;
4565 }
4566 DCtx.uLastError = 0; // TODO: a method for this
4567
4568 // 5([-9223372036854775807, -4759477275222530853137])
4569 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004570 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004571 return -2;
4572 }
4573 DCtx.uLastError = 0; // TODO: a method for this
4574
4575 // 5([ 9223372036854775806, -4759477275222530853137])
4576 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004577 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004578 return -2;
4579 }
4580 DCtx.uLastError = 0; // TODO: a method for this
4581
4582 // 5([ 9223372036854775806, 9223372036854775806])]
4583 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004584 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004585 return -2;
4586 }
4587 DCtx.uLastError = 0; // TODO: a method for this
4588
4589
4590
4591 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4592 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4593 if(nCBORError) {
4594 return -1;
4595 }
4596
4597 double dResult;
4598 // 4([-1, 3]),
4599 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004600 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004601 dResult != 0.3) {
4602 return -2;
4603 }
4604
4605 // 4([-20, 4759477275222530853136]),
4606 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004607 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004608 dResult != 47.408855671161923) {
4609 return -2;
4610 }
4611
4612 // 4([9223372036854775807, -4759477275222530853137]),
4613 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004614 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004615 dResult != -INFINITY) {
4616 return -2;
4617 }
4618
4619 // 5([300, 100]),
4620 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004621 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004622 dResult != -INFINITY) {
4623 return -2;
4624 }
4625
4626 // 5([-20, 4759477275222530853136]),
4627 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004628 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004629 dResult != 4521260802379792.0) {
4630 return -2;
4631 }
4632
4633 // 5([-9223372036854775807, -4759477275222530853137])
4634 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004635 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004636 dResult != -0.0) {
4637 return -2;
4638 }
4639
4640 // 5([9223372036854775806, 9223372036854775806])]
4641 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004642 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004643 dResult != INFINITY) {
4644 return -2;
4645 }
4646
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004647 return 0;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004648}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07004649
4650
Laurence Lundbladee3553422020-05-02 11:11:17 -07004651int32_t CBORSequenceDecodeTests(void)
4652{
4653 QCBORDecodeContext DCtx;
4654 QCBORItem Item;
4655 QCBORError uCBORError;
4656
4657 // --- Test a sequence with extra bytes ---
4658
4659 // The input for the date test happens to be a sequence so it
4660 // is reused. It is a sequence because it doesn't start as
4661 // an array or map.
4662 QCBORDecode_Init(&DCtx,
4663 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
4664 QCBOR_DECODE_MODE_NORMAL);
4665
4666 // Get the first item
4667 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4668 if(uCBORError != QCBOR_SUCCESS) {
4669 return 1;
4670 }
4671 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
4672 return 2;
4673 }
4674
4675 // Get a second item
4676 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4677 if(uCBORError != QCBOR_SUCCESS) {
4678 return 2;
4679 }
4680 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
4681 return 3;
4682 }
4683
4684 // A sequence can have stuff at the end that may
4685 // or may not be valid CBOR. The protocol decoder knows
4686 // when to stop by definition of the protocol, not
4687 // when the top-level map or array is ended.
4688 // Finish still has to be called to know that
4689 // maps and arrays (if there were any) were closed
4690 // off correctly. When called like this it
4691 // must return the error QCBOR_ERR_EXTRA_BYTES.
4692 uCBORError = QCBORDecode_Finish(&DCtx);
4693 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
4694 return 4;
4695 }
4696
4697
4698 // --- Test an empty input ----
4699 uint8_t empty[1];
4700 UsefulBufC Empty = {empty, 0};
4701 QCBORDecode_Init(&DCtx,
4702 Empty,
4703 QCBOR_DECODE_MODE_NORMAL);
4704
4705 uCBORError = QCBORDecode_Finish(&DCtx);
4706 if(uCBORError != QCBOR_SUCCESS) {
4707 return 5;
4708 }
4709
4710
4711 // --- Sequence with unclosed indefinite length array ---
4712 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
4713
4714 QCBORDecode_Init(&DCtx,
4715 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
4716 QCBOR_DECODE_MODE_NORMAL);
4717
4718 // Get the first item
4719 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4720 if(uCBORError != QCBOR_SUCCESS) {
4721 return 7;
4722 }
4723 if(Item.uDataType != QCBOR_TYPE_INT64) {
4724 return 8;
4725 }
4726
4727 // Get a second item
4728 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4729 if(uCBORError != QCBOR_SUCCESS) {
4730 return 9;
4731 }
4732 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4733 return 10;
4734 }
4735
4736 // Try to finish before consuming all bytes to confirm
4737 // that the still-open error is returned.
4738 uCBORError = QCBORDecode_Finish(&DCtx);
4739 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
4740 return 11;
4741 }
4742
4743
4744 // --- Sequence with a closed indefinite length array ---
4745 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
4746
4747 QCBORDecode_Init(&DCtx,
4748 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
4749 QCBOR_DECODE_MODE_NORMAL);
4750
4751 // Get the first item
4752 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4753 if(uCBORError != QCBOR_SUCCESS) {
4754 return 12;
4755 }
4756 if(Item.uDataType != QCBOR_TYPE_INT64) {
4757 return 13;
4758 }
4759
4760 // Get a second item
4761 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4762 if(uCBORError != QCBOR_SUCCESS) {
4763 return 14;
4764 }
4765 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4766 return 15;
4767 }
4768
4769 // Try to finish before consuming all bytes to confirm
4770 // that the still-open error is returned.
4771 uCBORError = QCBORDecode_Finish(&DCtx);
4772 if(uCBORError != QCBOR_SUCCESS) {
4773 return 16;
4774 }
4775
4776
4777 return 0;
4778}
4779
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004780
Laurence Lundblade70ecead2020-06-15 19:40:06 -07004781
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004782int32_t IntToTests()
4783{
4784 int nErrCode;
4785 int32_t n32;
4786 int16_t n16;
4787 int8_t n8;
4788 uint32_t u32;
4789 uint16_t u16;
4790 uint8_t u8;
4791 uint64_t u64;
4792
4793 nErrCode = QCBOR_Int64ToInt32(1, &n32);
4794 if(nErrCode == -1 || n32 != 1) {
4795 return 1;
4796 }
4797
4798 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
4799 if(nErrCode == -1 || n32 != INT32_MAX) {
4800 return 2;
4801 }
4802
4803 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
4804 if(nErrCode == -1 || n32 != INT32_MIN) {
4805 return 3;
4806 }
4807
4808 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
4809 if(nErrCode != -1) {
4810 return 4;
4811 }
4812
4813 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
4814 if(nErrCode != -1) {
4815 return 5;
4816 }
4817
4818
4819 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
4820 if(nErrCode == -1 || n16 != INT16_MAX) {
4821 return 6;
4822 }
4823
4824 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
4825 if(nErrCode == -1 || n16 != INT16_MIN) {
4826 return 7;
4827 }
4828
4829 nErrCode = QCBOR_Int64ToInt16(1, &n16);
4830 if(nErrCode == -1 || n16 != 1) {
4831 return 8;
4832 }
4833
4834 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
4835 if(nErrCode != -1) {
4836 return 9;
4837 }
4838
4839 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
4840 if(nErrCode != -1) {
4841 return 10;
4842 }
4843
4844
4845 nErrCode = QCBOR_Int64ToInt8(1, &n8);
4846 if(nErrCode == -1 || n8 != 1) {
4847 return 11;
4848 }
4849
4850 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
4851 if(nErrCode == -1 || n8 != INT8_MAX) {
4852 return 12;
4853 }
4854
4855 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
4856 if(nErrCode == -1 || n8 != INT8_MIN) {
4857 return 13;
4858 }
4859
4860 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
4861 if(nErrCode != -1) {
4862 return 14;
4863 }
4864
4865 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
4866 if(nErrCode != -1) {
4867 return 15;
4868 }
4869
4870
4871 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
4872 if(nErrCode == -1 || u32 != 1) {
4873 return 16;
4874 }
4875
4876 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
4877 if(nErrCode == -1 || u32 != UINT32_MAX) {
4878 return 17;
4879 }
4880
4881 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
4882 if(nErrCode == -1 || u32 != 0) {
4883 return 18;
4884 }
4885
4886 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
4887 if(nErrCode != -1) {
4888 return 19;
4889 }
4890
4891 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
4892 if(nErrCode != -1) {
4893 return 20;
4894 }
4895
4896
4897 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
4898 if(nErrCode == -1 || u16 != UINT16_MAX) {
4899 return 21;
4900 }
4901
4902 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
4903 if(nErrCode == -1 || u16 != 0) {
4904 return 22;
4905 }
4906
4907 nErrCode = QCBOR_Int64UToInt16(1, &u16);
4908 if(nErrCode == -1 || u16 != 1) {
4909 return 23;
4910 }
4911
4912 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
4913 if(nErrCode != -1) {
4914 return 24;
4915 }
4916
4917 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
4918 if(nErrCode != -1) {
4919 return 25;
4920 }
4921
4922
4923 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
4924 if(nErrCode == -1 || u8 != UINT8_MAX) {
4925 return 26;
4926 }
4927
4928 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
4929 if(nErrCode == -1 || u8 != 0) {
4930 return 27;
4931 }
4932
4933 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
4934 if(nErrCode == -1 || u8 != 1) {
4935 return 28;
4936 }
4937
4938 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
4939 if(nErrCode != -1) {
4940 return 29;
4941 }
4942
4943 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
4944 if(nErrCode != -1) {
4945 return 30;
4946 }
4947
4948
4949 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
4950 if(nErrCode == -1 || u64 != 1) {
4951 return 31;
4952 }
4953
4954 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
4955 if(nErrCode == -1 || u64 != INT64_MAX) {
4956 return 32;
4957 }
4958
4959 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
4960 if(nErrCode == -1 || u64 != 0) {
4961 return 33;
4962 }
4963
4964 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
4965 if(nErrCode != -1) {
4966 return 34;
4967 }
4968
4969 return 0;
4970}
4971
Laurence Lundblade0750fc42020-06-20 21:02:34 -07004972
4973/*
4974A sequence with
4975 A wrapping bstr
4976 containing a map
4977 1
4978 2
4979 A wrapping bstr
4980 containing an array
4981 3
4982 wrapping bstr
4983 4
4984 5
4985 6
4986 array
4987 7
4988 8
4989
4990 */
4991
4992static UsefulBufC foo(UsefulBuf ffo)
4993{
4994 UsefulBufC Encoded;
4995 QCBOREncodeContext EC;
4996 QCBORError uErr;
4997
4998 QCBOREncode_Init(&EC, ffo);
4999
5000 QCBOREncode_BstrWrap(&EC);
5001 QCBOREncode_OpenMap(&EC);
5002 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
5003 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
5004 QCBOREncode_CloseMap(&EC);
5005 QCBOREncode_BstrWrap(&EC);
5006 QCBOREncode_OpenArray(&EC);
5007 QCBOREncode_AddInt64(&EC, 3);
5008 QCBOREncode_BstrWrap(&EC);
5009 QCBOREncode_AddInt64(&EC, 4);
5010 QCBOREncode_CloseBstrWrap(&EC, NULL);
5011 QCBOREncode_AddInt64(&EC, 5);
5012 QCBOREncode_CloseArray(&EC);
5013 QCBOREncode_CloseBstrWrap(&EC, NULL);
5014 QCBOREncode_AddInt64(&EC, 6);
5015 QCBOREncode_CloseBstrWrap(&EC, NULL);
5016 QCBOREncode_OpenArray(&EC);
5017 QCBOREncode_AddInt64(&EC, 7);
5018 QCBOREncode_AddInt64(&EC, 8);
5019 QCBOREncode_CloseArray(&EC);
5020
5021 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07005022 if(uErr) {
5023 Encoded = NULLUsefulBufC;
5024 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005025
5026 return Encoded;
5027}
5028
5029
5030int32_t EnterBstrTest()
5031{
5032 MakeUsefulBufOnStack(ffo, 100);
5033
5034 QCBORDecodeContext DC;
5035
5036 QCBORDecode_Init(&DC, foo(ffo), 0);
5037
5038 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
5039
5040
5041 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
5042 QCBORDecode_EnterMap(&DC);
5043 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
5044 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
5045 QCBORDecode_ExitMap(&DC);
5046 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
5047 QCBORDecode_EnterArray(&DC);
5048 QCBORDecode_GetInt64(&DC, &i3);
5049 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
5050 QCBORDecode_GetInt64(&DC, &i4);
5051 QCBORDecode_ExitBstrWrapped(&DC);
5052 QCBORDecode_GetInt64(&DC, &i5);
5053 QCBORDecode_ExitArray(&DC);
5054 QCBORDecode_ExitBstrWrapped(&DC);
5055 QCBORDecode_GetInt64(&DC, &i6);
5056 QCBORDecode_ExitBstrWrapped(&DC);
5057 QCBORDecode_EnterArray(&DC);
5058 QCBORDecode_GetInt64(&DC, &i7);
5059 QCBORDecode_GetInt64(&DC, &i8);
5060 QCBORDecode_ExitArray(&DC);
5061
5062 QCBORError uErr = QCBORDecode_Finish(&DC);
5063
5064 return (int32_t)uErr;
5065}