blob: fca76e91954e79cc46e53f054e3a3f731ff9ec2c [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080036#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080037#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070038#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080039
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080040
Laurence Lundbladea2e29072018-12-30 09:20:06 -080041#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080042#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080043
44static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045{
46 if(szLabel) {
47 printf("%s ", szLabel);
48 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080049
Laurence Lundblade570fab52018-10-13 18:28:27 +080050 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080051 for(i = 0; i < Buf.len; i++) {
52 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080053 printf("%02x ", Z);
54 }
55 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080056
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 fflush(stdout);
58}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080059#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080060
61
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070062static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080063 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
65 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
66 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
67 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
68 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
69 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
70 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
71 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
72 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
73 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
74 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
75 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
76 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
77 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
78 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
79 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
80 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
81 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
82 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
83 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
84 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0xff};
87
88
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080089// return CBOR error or -1 if type of value doesn't match
90
Laurence Lundbladec5fef682020-01-25 11:38:45 -080091static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070093 QCBORItem Item;
94 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080095
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080096 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070097 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080098 if(Item.uDataType != QCBOR_TYPE_ARRAY)
99 return -1;
100
101 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700102 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800103 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800104 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800105 return -1;
106
107 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700108 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800110 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800111 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800112
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700114 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800116 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800118
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700120 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800122 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800124
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700126 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800128 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800130
131
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800132 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700133 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800134 if(Item.uDataType != QCBOR_TYPE_INT64 ||
135 Item.val.int64 != -2147483648)
136 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800137
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700139 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800140 if(Item.uDataType != QCBOR_TYPE_INT64 ||
141 Item.val.int64 != -2147483647)
142 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800143
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700145 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800146 if(Item.uDataType != QCBOR_TYPE_INT64 ||
147 Item.val.int64 != -65538)
148 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800149
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700151 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800152 if(Item.uDataType != QCBOR_TYPE_INT64 ||
153 Item.val.int64 != -65537)
154 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800155
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700157 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800158 if(Item.uDataType != QCBOR_TYPE_INT64 ||
159 Item.val.int64 != -65536)
160 return -1;
161
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800163 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700164 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800165 if(Item.uDataType != QCBOR_TYPE_INT64 ||
166 Item.val.int64 != -65535)
167 return -1;
168
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800169
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700171 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800172 if(Item.uDataType != QCBOR_TYPE_INT64 ||
173 Item.val.int64 != -65534)
174 return -1;
175
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800177 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700178 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800179 if(Item.uDataType != QCBOR_TYPE_INT64 ||
180 Item.val.int64 != -257)
181 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700184 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 if(Item.uDataType != QCBOR_TYPE_INT64 ||
186 Item.val.int64 != -256)
187 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800188
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700190 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 if(Item.uDataType != QCBOR_TYPE_INT64 ||
192 Item.val.int64 != -255)
193 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700196 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if(Item.uDataType != QCBOR_TYPE_INT64 ||
198 Item.val.int64 != -254)
199 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
201
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800202 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700203 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800204 if(Item.uDataType != QCBOR_TYPE_INT64 ||
205 Item.val.int64 != -25)
206 return -1;
207
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800208
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800209 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700210 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800211 if(Item.uDataType != QCBOR_TYPE_INT64 ||
212 Item.val.int64 != -24)
213 return -1;
214
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800215
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800216 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700217 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218 if(Item.uDataType != QCBOR_TYPE_INT64 ||
219 Item.val.int64 != -23)
220 return -1;
221
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800222
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800223 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700224 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800225 if(Item.uDataType != QCBOR_TYPE_INT64 ||
226 Item.val.int64 != -1)
227 return -1;
228
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800230 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700231 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800232 if(Item.uDataType != QCBOR_TYPE_INT64 ||
233 Item.val.int64 != 0)
234 return -1;
235
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800236
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800237 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700238 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800239 if(Item.uDataType != QCBOR_TYPE_INT64 ||
240 Item.val.int64 != 0)
241 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700244 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800245 if(Item.uDataType != QCBOR_TYPE_INT64 ||
246 Item.val.int64 != 1)
247 return -1;
248
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800250 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700251 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800252 if(Item.uDataType != QCBOR_TYPE_INT64 ||
253 Item.val.int64 != 22)
254 return -1;
255
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700258 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800259 if(Item.uDataType != QCBOR_TYPE_INT64 ||
260 Item.val.int64 != 23)
261 return -1;
262
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800263
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800264 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700265 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800266 if(Item.uDataType != QCBOR_TYPE_INT64 ||
267 Item.val.int64 != 24)
268 return -1;
269
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800270
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800271 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700272 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800273 if(Item.uDataType != QCBOR_TYPE_INT64 ||
274 Item.val.int64 != 25)
275 return -1;
276
277 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700278 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800279 if(Item.uDataType != QCBOR_TYPE_INT64 ||
280 Item.val.int64 != 26)
281 return -1;
282
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800283
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800284 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700285 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800286 if(Item.uDataType != QCBOR_TYPE_INT64 ||
287 Item.val.int64 != 254)
288 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800289
290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800291 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700292 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800293 if(Item.uDataType != QCBOR_TYPE_INT64 ||
294 Item.val.int64 != 255)
295 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800296
297
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800298 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700299 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800300 if(Item.uDataType != QCBOR_TYPE_INT64 ||
301 Item.val.int64 != 256)
302 return -1;
303
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800304
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800305 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700306 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800307 if(Item.uDataType != QCBOR_TYPE_INT64 ||
308 Item.val.int64 != 257)
309 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800310
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700312 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800313 if(Item.uDataType != QCBOR_TYPE_INT64 ||
314 Item.val.int64 != 65534)
315 return -1;
316
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800317
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800318 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700319 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800320 if(Item.uDataType != QCBOR_TYPE_INT64 ||
321 Item.val.int64 != 65535)
322 return -1;
323
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800324
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800325 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700326 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800327 if(Item.uDataType != QCBOR_TYPE_INT64 ||
328 Item.val.int64 != 65536)
329 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800330
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700332 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800333 if(Item.uDataType != QCBOR_TYPE_INT64 ||
334 Item.val.int64 != 65537)
335 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700338 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800339 if(Item.uDataType != QCBOR_TYPE_INT64 ||
340 Item.val.int64 != 65538)
341 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700344 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800345 if(Item.uDataType != QCBOR_TYPE_INT64 ||
346 Item.val.int64 != 2147483647)
347 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800348
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700350 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
352 Item.val.int64 != 2147483647)
353 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700356 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800357 if(Item.uDataType != QCBOR_TYPE_INT64 ||
358 Item.val.int64 != 2147483648)
359 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700362 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800363 if(Item.uDataType != QCBOR_TYPE_INT64 ||
364 Item.val.int64 != 2147483649)
365 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700368 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800369 if(Item.uDataType != QCBOR_TYPE_INT64 ||
370 Item.val.int64 != 4294967294)
371 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
373
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700374 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
375 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800376 if(Item.uDataType != QCBOR_TYPE_INT64 ||
377 Item.val.int64 != 4294967295)
378 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800379
380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800381 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700382 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800383 if(Item.uDataType != QCBOR_TYPE_INT64 ||
384 Item.val.int64 != 4294967296)
385 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800386
387
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800388 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700389 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800390 if(Item.uDataType != QCBOR_TYPE_INT64 ||
391 Item.val.int64 != 4294967297)
392 return -1;
393
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800394
395
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800396 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700397 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800398 if(Item.uDataType != QCBOR_TYPE_INT64 ||
399 Item.val.int64 != 9223372036854775807LL)
400 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800401
402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800403 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700404 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800405 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
406 Item.val.uint64 != 18446744073709551615ULL)
407 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800408
409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800410 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
411 return -1;
412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 return 0;
415}
416
417
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800418// One less than the smallest negative integer allowed in C. Decoding
419// this should fail.
420static const uint8_t spTooSmallNegative[] = {
421 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000422};
423
424
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800425/*
426 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800427 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800428 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800429int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800430{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000431 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800433
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000434 QCBORDecode_Init(&DCtx,
435 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
436 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000438 // The really big test of all successes
439 nReturn = IntegerValuesParseTestInternal(&DCtx);
440 if(nReturn) {
441 return nReturn;
442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800443
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000444 // The one large negative integer that can be parsed
445 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800446 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000447 QCBOR_DECODE_MODE_NORMAL);
448
449 QCBORItem item;
450 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
451 nReturn = -4000;
452 }
453
454 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455}
456
457
458/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800459 Creates a simple CBOR array and returns it in *pEncoded. The array is
460 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800461
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800463
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800464 */
465
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800466static uint8_t spSimpleArrayBuffer[50];
467
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800468static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800469{
470 QCBOREncodeContext ECtx;
471 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473 *pEncoded = NULL;
474 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800475
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800476 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800477 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800478 // and last with the buffer to do the actual encoding
479 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700480 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800481 QCBOREncode_OpenArray(&ECtx);
482 QCBOREncode_AddInt64(&ECtx, nInt1);
483 QCBOREncode_AddInt64(&ECtx, nInt2);
484 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
485 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
486 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800487
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800488 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489 goto Done;
490
491 if(*pEncoded != NULL) {
492 nReturn = 0;
493 goto Done;
494 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800495
496 // Use static buffer to avoid dependency on malloc()
497 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800498 goto Done;
499 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800500 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800501
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800503
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800504Done:
505 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506}
507
508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800509/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800510 Some basic CBOR with map and array used in a lot of tests.
511 The map labels are all strings
512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900514 "an array of two strings": [
515 "string1", "string2"
516 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 "map in a map": {
518 "bytes 1": h'78787878',
519 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900520 "another int": 98,
521 "text 2": "lies, damn lies and statistics"
522 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523 }
524 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800525static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700526 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
527 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
528 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
529 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
530 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
531 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
532 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
533 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
534 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
535 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
536 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
537 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
538 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
539 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
540 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
541 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800542
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800543static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800544 size_t nLen,
545 int64_t *pInt1,
546 int64_t *pInt2,
547 const uint8_t **pBuf3,
548 size_t *pBuf3Len,
549 const uint8_t **pBuf4,
550 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551{
552 QCBORDecodeContext DCtx;
553 QCBORItem Item;
554 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800555
Laurence Lundbladeee851742020-01-08 08:37:05 -0800556 QCBORDecode_Init(&DCtx,
557 (UsefulBufC){pEncoded, nLen},
558 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800559
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800560 // Make sure the first thing is a map
561 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
562 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800564 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800565 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566 goto Done;
567 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800569 // Second integer
570 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
571 goto Done;
572 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800573
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800574 // First string
575 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
576 goto Done;
577 *pBuf3 = Item.val.string.ptr;
578 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 // Second string
581 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
582 goto Done;
583 *pBuf4 = Item.val.string.ptr;
584 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800586 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800588Done:
589 return(nReturn);
590}
591
592
593
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800594
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800595int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800596{
597 uint8_t *pEncoded;
598 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800599
Laurence Lundblade5e390822019-01-06 12:35:01 -0800600 int64_t i1=0, i2=0;
601 size_t i3=0, i4=0;
602 const uint8_t *s3= (uint8_t *)"";
603 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800604
605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
607 return(-1);
608 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800609
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800610 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 if(i1 != 23 ||
613 i2 != 6000 ||
614 i3 != 8 ||
615 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530616 memcmp("galactic", s3, 8) !=0 ||
617 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 return(-1);
619 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800620
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800621 return(0);
622}
623
624
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700625/*
626 [
627 0,
628 [],
629 [
630 [],
631 [
632 0
633 ],
634 {},
635 {
636 1: {},
637 2: {},
638 3: []
639 }
640 ]
641 ]
642 */
643static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
644 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
645
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800646int32_t EmptyMapsAndArraysTest()
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700647{
648 QCBORDecodeContext DCtx;
649 QCBORItem Item;
650
Laurence Lundbladeee851742020-01-08 08:37:05 -0800651 QCBORDecode_Init(&DCtx,
652 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
653 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700654
655 // Array with 3 items
656 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
657 Item.uDataType != QCBOR_TYPE_ARRAY ||
658 Item.uNestingLevel != 0 ||
659 Item.uNextNestLevel != 1 ||
660 Item.val.uCount != 3) {
661 return -1;
662 }
663
664 // An integer 0
665 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
666 Item.uDataType != QCBOR_TYPE_INT64 ||
667 Item.uNestingLevel != 1 ||
668 Item.uNextNestLevel != 1 ||
669 Item.val.uint64 != 0) {
670 return -2;
671 }
672
673 // An empty array
674 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
675 Item.uDataType != QCBOR_TYPE_ARRAY ||
676 Item.uNestingLevel != 1 ||
677 Item.uNextNestLevel != 1 ||
678 Item.val.uCount != 0) {
679 return -3;
680 }
681
682 // An array with 4 items
683 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
684 Item.uDataType != QCBOR_TYPE_ARRAY ||
685 Item.uNestingLevel != 1 ||
686 Item.uNextNestLevel != 2 ||
687 Item.val.uCount != 4) {
688 return -4;
689 }
690
691 // An empty array
692 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
693 Item.uDataType != QCBOR_TYPE_ARRAY ||
694 Item.uNestingLevel != 2 ||
695 Item.uNextNestLevel != 2 ||
696 Item.val.uCount != 0) {
697 return -5;
698 }
699
700 // An array with 1 item
701 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
702 Item.uDataType != QCBOR_TYPE_ARRAY ||
703 Item.uNestingLevel != 2 ||
704 Item.uNextNestLevel != 3 ||
705 Item.val.uCount != 1) {
706 return -6;
707 }
708
709 // An integer 0
710 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
711 Item.uDataType != QCBOR_TYPE_INT64 ||
712 Item.uNestingLevel != 3 ||
713 Item.uNextNestLevel != 2 ||
714 Item.val.uint64 != 0) {
715 return -7;
716 }
717
718 // An empty map
719 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
720 Item.uDataType != QCBOR_TYPE_MAP ||
721 Item.uNestingLevel != 2 ||
722 Item.uNextNestLevel != 2 ||
723 Item.val.uCount != 0) {
724 return -8;
725 }
726
727 // An map with 3 items
728 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
729 Item.uDataType != QCBOR_TYPE_MAP ||
730 Item.uNestingLevel != 2 ||
731 Item.uNextNestLevel != 3 ||
732 Item.val.uCount != 3) {
733 return -9;
734 }
735
736 // An empty map
737 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
738 Item.uDataType != QCBOR_TYPE_MAP ||
739 Item.uNestingLevel != 3 ||
740 Item.uNextNestLevel != 3 ||
741 Item.val.uCount != 0) {
742 return -10;
743 }
744
745 // An empty map
746 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
747 Item.uDataType != QCBOR_TYPE_MAP ||
748 Item.uNestingLevel != 3 ||
749 Item.uNextNestLevel != 3 ||
750 Item.val.uCount != 0) {
751 return -11;
752 }
753
754 // An empty array
755 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
756 Item.uDataType != QCBOR_TYPE_ARRAY ||
757 Item.uNestingLevel != 3 ||
758 Item.uNextNestLevel != 0 ||
759 Item.val.uCount != 0) {
760 return -12;
761 }
762
763 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
764 return -13;
765 }
766
767 return 0;
768}
769
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800770
Laurence Lundbladeee851742020-01-08 08:37:05 -0800771static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
772 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800773
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800774int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800775{
776 QCBORDecodeContext DCtx;
777 int nReturn = 0;
778 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800779
Laurence Lundbladeee851742020-01-08 08:37:05 -0800780 QCBORDecode_Init(&DCtx,
781 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
782 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800783
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784 for(i = 0; i < 10; i++) {
785 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800786
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800787 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
788 Item.uDataType != QCBOR_TYPE_ARRAY ||
789 Item.uNestingLevel != i) {
790 nReturn = -1;
791 break;
792 }
793 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800794
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800795 return(nReturn);
796}
797
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700798// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800799static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
800 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
801 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
802 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800803
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800804int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800805{
806 QCBORDecodeContext DCtx;
807 int nReturn = 0;
808 int i;
809 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800810
811
Laurence Lundbladeee851742020-01-08 08:37:05 -0800812 QCBORDecode_Init(&DCtx,
813 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
814 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800815
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700816 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800817
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800818 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
819 Item.uDataType != QCBOR_TYPE_ARRAY ||
820 Item.uNestingLevel != i) {
821 nReturn = -1;
822 break;
823 }
824 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800825
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800826 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
827 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800828
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800829 return(nReturn);
830}
831
832
833
834
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800835int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800836{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700837 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800838
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800839 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700840 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800841
Laurence Lundbladeee851742020-01-08 08:37:05 -0800842 QCBORDecode_Init(&DCtx,
843 (UsefulBufC){spExpectedEncodedInts, nNum},
844 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800845
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800846 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800847
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700848 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800849 nResult = -1;
850 goto Done;
851 }
852 }
853Done:
854 return nResult;
855}
856
857
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800858
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800859int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800860{
861 uint8_t *pEncoded;
862 int nReturn;
863 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800864
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800865 int64_t i1, i2;
866 size_t i3, i4;
867 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800868
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800869 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800870
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800871 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
872 return(-1);
873 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800874
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
876 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
877 if(nResult == 0) {
878 nReturn = -1;
879 }
880 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800881
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800882 return(nReturn);
883}
884
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530885/*
886 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800887 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
888 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530889 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800890static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800891{
892 QCBORDecodeContext DCtx;
893 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700894 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundbladeee851742020-01-08 08:37:05 -0800896 QCBORDecode_Init(&DCtx,
897 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
898 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800899
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900900 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700901 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900902 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800903 if(Item.uDataType != QCBOR_TYPE_MAP ||
904 Item.val.uCount != 3)
905 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900907 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700908 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900909 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800911 Item.uDataType != QCBOR_TYPE_INT64 ||
912 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530913 Item.uDataAlloc ||
914 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900915 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800916 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900917 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800918
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900919 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700920 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900921 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800922 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530923 Item.uDataAlloc ||
924 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900925 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800926 Item.uDataType != QCBOR_TYPE_ARRAY ||
927 Item.val.uCount != 2)
928 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800929
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900930 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700931 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900932 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800933 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530934 Item.uDataAlloc ||
935 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900936 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800937 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900938 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800939
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900940 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700941 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900942 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800943 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530944 Item.uDataAlloc ||
945 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900946 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800947 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800949
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900950 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700951 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900952 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800953 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530954 Item.uDataAlloc ||
955 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900956 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800957 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900958 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800959 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800961
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900962 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700963 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900964 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800965 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900966 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800967 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530968 Item.uDataAlloc ||
969 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900970 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800971 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800973
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900974 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700975 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900976 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900978 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800979 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530980 Item.uDataAlloc ||
981 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900982 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800983 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800985
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700987 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900988 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800989 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530990 Item.uDataAlloc ||
991 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900992 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800993 Item.uDataType != QCBOR_TYPE_INT64 ||
994 Item.val.int64 != 98)
995 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800996
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900997 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700998 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900999 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001000 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001001 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301003 Item.uDataAlloc ||
1004 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001005 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001006 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001008
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001009 return 0;
1010}
1011
1012
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001013/*
1014 Decode and thoroughly check a moderately complex
1015 set of maps
1016 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001017int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001018{
1019 QCBORDecodeContext DCtx;
1020 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001021 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001022
Laurence Lundbladeee851742020-01-08 08:37:05 -08001023 QCBORDecode_Init(&DCtx,
1024 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1025 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001026
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001028 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001029 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001030 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1031 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001032 return -1;
1033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001034
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001036 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001037 }
1038 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1039 Item.uDataAlloc ||
1040 Item.uLabelAlloc ||
1041 Item.uLabelType != QCBOR_TYPE_NONE ||
1042 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1043 return -2;
1044 }
1045
1046 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001047 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001048 }
1049 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1050 Item.uDataType != QCBOR_TYPE_INT64 ||
1051 Item.val.int64 != 42 ||
1052 Item.uDataAlloc ||
1053 Item.uLabelAlloc) {
1054 return -3;
1055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001056
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001058 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 }
1060 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1061 Item.uDataAlloc ||
1062 Item.uLabelAlloc ||
1063 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1064 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1065 return -4;
1066 }
1067
1068 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001069 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001070 }
1071 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1072 Item.uDataAlloc ||
1073 Item.uLabelAlloc ||
1074 Item.uDataType != QCBOR_TYPE_ARRAY ||
1075 Item.val.uCount != 2) {
1076 return -5;
1077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001079 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001080 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001081 }
1082 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1083 Item.val.string.len != 7 ||
1084 Item.uDataAlloc ||
1085 Item.uLabelAlloc ||
1086 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1087 return -6;
1088 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001089
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001090 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001091 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 }
1093 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1094 Item.uDataAlloc ||
1095 Item.uLabelAlloc ||
1096 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1097 return -7;
1098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001099
1100
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001102 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001103 }
1104 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1105 Item.uDataAlloc ||
1106 Item.uLabelAlloc ||
1107 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1108 return -8;
1109 }
1110
1111 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001112 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001113 }
1114 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1115 Item.uDataAlloc ||
1116 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001117 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1118 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 return -9;
1120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001121
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001123 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001124 }
1125 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1126 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1127 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1128 Item.uDataAlloc ||
1129 Item.uLabelAlloc) {
1130 return -10;
1131 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001132
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001134 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001135 }
1136 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1137 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1138 Item.uDataAlloc ||
1139 Item.uLabelAlloc ||
1140 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1141 return -11;
1142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001143
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001145 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001146 }
1147 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1148 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1149 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1150 Item.uDataAlloc ||
1151 Item.uLabelAlloc) {
1152 return -12;
1153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001154
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001156 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 }
1158 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1159 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1160 Item.uDataAlloc ||
1161 Item.uLabelAlloc ||
1162 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1163 return -13;
1164 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001165
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001167 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001168 }
1169 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1170 Item.uDataAlloc ||
1171 Item.uLabelAlloc ||
1172 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1173 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1174 return -14;
1175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001176
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001178 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 }
1180 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1181 Item.uDataAlloc ||
1182 Item.uLabelAlloc ||
1183 Item.uDataType != QCBOR_TYPE_INT64 ||
1184 Item.val.int64 != 98) {
1185 return -15;
1186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001187
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001189 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001190 }
1191 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1192 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1193 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1194 Item.uDataAlloc ||
1195 Item.uLabelAlloc) {
1196 return -16;
1197 }
1198
1199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001200 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001201 }
1202 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1203 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1204 Item.uDataAlloc ||
1205 Item.uLabelAlloc ||
1206 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1207 return -17;
1208 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001209
1210
1211 /*
1212 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1213 map that when interpreted as an array will be too many. Test
1214 data just has the start of the map, not all the items in the map.
1215 */
1216 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
1217
1218 QCBORDecode_Init(&DCtx,
1219 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1220 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1221
1222 if((QCBOR_ERR_ARRAY_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
1223 return -50;
1224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001225
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 return 0;
1227}
1228
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001229
1230/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301231 Fully or partially decode pValidMapEncoded. When
1232 partially decoding check for the right error code.
1233 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001234
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301235 The partial decodes test error conditions of
1236 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001237
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301238 This could be combined with the above test
1239 and made prettier and maybe a little more
1240 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001242static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001243{
1244 QCBORDecodeContext DCtx;
1245 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001246 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001247
Laurence Lundbladeee851742020-01-08 08:37:05 -08001248 QCBORDecode_Init(&DCtx,
1249 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1250 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001251
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001252 if(nLevel < 1) {
1253 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1254 return -1;
1255 } else {
1256 return 0;
1257 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001258 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301259
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001260
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001261 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001262 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001263 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001264 if(Item.uDataType != QCBOR_TYPE_MAP ||
1265 Item.val.uCount != 3)
1266 return -1;
1267
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001268 if(nLevel < 2) {
1269 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1270 return -1;
1271 } else {
1272 return 0;
1273 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001274 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001275
1276
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001277 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001278 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001279 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001280 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 Item.uDataType != QCBOR_TYPE_INT64 ||
1282 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001283 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001284 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001286
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001287 if(nLevel < 3) {
1288 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1289 return -1;
1290 } else {
1291 return 0;
1292 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001294
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001295 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001296 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001297 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001298 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001299 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001300 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001301 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001302 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
1305
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001306 if(nLevel < 4) {
1307 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1308 return -1;
1309 } else {
1310 return 0;
1311 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001313
1314
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001315 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001316 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001317 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001318 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001319 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001320 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001323 if(nLevel < 5) {
1324 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1325 return -1;
1326 } else {
1327 return 0;
1328 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001330
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001331 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001332 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001333 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001335 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001336 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001338
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001339 if(nLevel < 6) {
1340 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1341 return -1;
1342 } else {
1343 return 0;
1344 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001346
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001348 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 Item.uDataType != QCBOR_TYPE_MAP ||
1353 Item.val.uCount != 4)
1354 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001355
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001356 if(nLevel < 7) {
1357 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1358 return -1;
1359 } else {
1360 return 0;
1361 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001363
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001364 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001365 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001366 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001368 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001370 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001371 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001373
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001374 if(nLevel < 8) {
1375 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1376 return -1;
1377 } else {
1378 return 0;
1379 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001381
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001382 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001383 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001388 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001389 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001392 if(nLevel < 9) {
1393 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1394 return -1;
1395 } else {
1396 return 0;
1397 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001399
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001401 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001402 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 Item.uDataType != QCBOR_TYPE_INT64 ||
1406 Item.val.int64 != 98)
1407 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001408
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001409 if(nLevel < 10) {
1410 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1411 return -1;
1412 } else {
1413 return 0;
1414 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001416
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001417 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001418 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001420 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001421 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001423 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001424 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001426
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301427 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001428 return -1;
1429 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001431 return 0;
1432}
1433
1434
1435
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001436
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001437int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001439 // Parse a moderatly complex map structure very thoroughly
1440 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1441 if(nResult) {
1442 return nResult;
1443 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001444
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001445 // Again, but in strings-only mode. It should succeed since the input
1446 // map has only string labels.
1447 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1448 if(nResult) {
1449 return nResult;
1450 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001451
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001452 // Again, but try to finish the decoding before the end of the
1453 // input at 10 different place and see that the right error code
1454 // is returned.
1455 for(int i = 0; i < 10; i++) {
1456 nResult = ExtraBytesTest(i);
1457 if(nResult) {
1458 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001459 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001461
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001462 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463}
1464
1465
Laurence Lundbladeee851742020-01-08 08:37:05 -08001466static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1467 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1468 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001469
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001470int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001471{
1472 QCBORDecodeContext DCtx;
1473 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001474 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001475
1476
Laurence Lundbladeee851742020-01-08 08:37:05 -08001477 QCBORDecode_Init(&DCtx,
1478 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1479 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001480
1481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001482 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001483 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001484 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1485 Item.val.uCount != 10)
1486 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001489 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 if(Item.uDataType != QCBOR_TYPE_FALSE)
1491 return -1;
1492
1493 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001494 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001495 if(Item.uDataType != QCBOR_TYPE_TRUE)
1496 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001497
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001499 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001500 if(Item.uDataType != QCBOR_TYPE_NULL)
1501 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001502
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001504 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001505 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1506 return -1;
1507
1508 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001509 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 return -1;
1511
1512 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001513 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1515 return -1;
1516
1517 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001518 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001519 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1520 return -1;
1521
Laurence Lundblade077475f2019-04-26 09:06:33 -07001522 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
Laurence Lundblade077475f2019-04-26 09:06:33 -07001525 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526 return -1;
1527
Laurence Lundblade077475f2019-04-26 09:06:33 -07001528 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001529 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001531 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001532 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001533 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1534 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001535
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001536 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001537 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001538 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1539 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001540
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001541 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001542
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001543}
1544
1545
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001546static int IsNotWellFormedError(QCBORError nErr)
1547{
1548 switch(nErr){
1549 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1550 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1551 case QCBOR_ERR_UNSUPPORTED:
1552 case QCBOR_ERR_HIT_END:
1553 case QCBOR_ERR_BAD_TYPE_7:
1554 case QCBOR_ERR_BAD_BREAK:
1555 case QCBOR_ERR_EXTRA_BYTES:
1556 case QCBOR_ERR_BAD_INT:
1557 return 1;
1558 default:
1559 return 0;
1560 }
1561}
1562
1563
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001564int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001565{
1566 // Loop over all the not-well-formed instance of CBOR
1567 // that are test vectors in not_well_formed_cbor.h
1568 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1569 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1570 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1571 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1572
Laurence Lundbladeee851742020-01-08 08:37:05 -08001573 // Set up decoder context. String allocator needed for indefinite
1574 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001575 QCBORDecodeContext DCtx;
1576 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1577 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1578 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1579
1580 // Loop getting items until no more to get
1581 QCBORError nCBORError;
1582 do {
1583 QCBORItem Item;
1584
1585 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1586 } while(nCBORError == QCBOR_SUCCESS);
1587
1588 // Every test vector must fail with
1589 // a not-well-formed error. If not
1590 // this test fails.
1591 if(!IsNotWellFormedError(nCBORError)) {
1592 // Return index of failure in the error code
1593 return 2000 + nIterate;
1594 }
1595 }
1596 return 0;
1597}
1598
1599
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001600struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001601 UsefulBufC Input;
1602 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001603};
1604
Laurence Lundblade59289e52019-12-30 13:44:37 -08001605
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001606static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001607{
1608 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1609 // Set up the decoding context including a memory pool so that
1610 // indefinite length items can be checked
1611 QCBORDecodeContext DCtx;
1612 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1613 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001614
Laurence Lundblade59289e52019-12-30 13:44:37 -08001615 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1616 if(nCBORError) {
1617 return -9;
1618 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001619 if((pF - pFailInputs) == 113) {
1620 nCBORError = 0;
1621 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08001622 // Iterate until there is an error of some sort error
1623 QCBORItem Item;
1624 do {
1625 // Set to something none-zero other than QCBOR_TYPE_NONE
1626 memset(&Item, 0x33, sizeof(Item));
1627
1628 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1629 } while(nCBORError == QCBOR_SUCCESS);
1630
1631 // Must get the expected error or the this test fails
1632 // The data and label type must also be QCBOR_TYPE_NONE
1633 if(nCBORError != pF->nError ||
1634 Item.uDataType != QCBOR_TYPE_NONE ||
1635 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001636 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001637 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001638 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001639 }
1640 }
1641
1642 return 0;
1643}
1644
1645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001646struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001647 // Most of this is copied from not_well_formed.h. Here the error code
1648 // returned is also checked.
1649
1650 // Indefinite length strings must be closed off
1651 // An indefinite length byte string not closed off
1652 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1653 // An indefinite length text string not closed off
1654 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1655
1656
1657 // All the chunks in an indefinite length string must be of the type of indefinite length string
1658 // indefinite length byte string with text string chunk
1659 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1660 // indefinite length text string with a byte string chunk
1661 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1662 // indefinite length byte string with an positive integer chunk
1663 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1664 // indefinite length byte string with an negative integer chunk
1665 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1666 // indefinite length byte string with an array chunk
1667 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1668 // indefinite length byte string with an map chunk
1669 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1670 // indefinite length byte string with tagged integer chunk
1671 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1672 // indefinite length byte string with an simple type chunk
1673 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1674 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1675 // indefinite length text string with indefinite string inside
1676 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1677
1678
1679 // Definte length maps and arrays must be closed by having the right number of items
1680 // A definte length array that is supposed to have 1 item, but has none
1681 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1682 // A definte length array that is supposed to have 2 items, but has only 1
1683 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1684 // A definte length array that is supposed to have 511 items, but has only 1
1685 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1686 // A definte length map that is supposed to have 1 item, but has none
1687 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1688 // A definte length map that is supposed to have s item, but has only 1
1689 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1690
1691
1692 // Indefinte length maps and arrays must be ended by a break
1693 // Indefinite length array with zero items and no break
1694 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1695 // Indefinite length array with two items and no break
1696 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1697 // Indefinite length map with zero items and no break
1698 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1699 // Indefinite length map with two items and no break
1700 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1701
1702
1703 // Nested maps and arrays must be closed off (some extra nested test vectors)
1704 // Unclosed indefinite array containing a close definite array
1705 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
1706 // Definite length array containing an unclosed indefinite array
1707 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1708 // Deeply nested definite length arrays with deepest one unclosed
1709 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1710 // Deeply nested indefinite length arrays with deepest one unclosed
1711 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1712 // Mixed nesting with indefinite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001713 // TODO: think through this one
1714 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001715 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001716 // TODO: think through this one
1717 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001718
1719
1720 // The "argument" for the data item is incomplete
1721 // Positive integer missing 1 byte argument
1722 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1723 // Positive integer missing 2 byte argument
1724 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1725 // Positive integer missing 4 byte argument
1726 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1727 // Positive integer missing 8 byte argument
1728 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1729 // Positive integer missing 1 byte of 2 byte argument
1730 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1731 // Positive integer missing 2 bytes of 4 byte argument
1732 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1733 // Positive integer missing 1 bytes of 7 byte argument
1734 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1735 // Negative integer missing 1 byte argument
1736 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1737 // Binary string missing 1 byte argument
1738 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1739 // Text string missing 1 byte argument
1740 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1741 // Array missing 1 byte argument
1742 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1743 // Map missing 1 byte argument
1744 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1745 // Tag missing 1 byte argument
1746 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1747 // Simple missing 1 byte argument
1748 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1749
1750
1751 // Breaks must not occur in definite length arrays and maps
1752 // Array of length 1 with sole member replaced by a break
1753 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1754 // Array of length 2 with 2nd member replaced by a break
1755 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1756 // Map of length 1 with sole member label replaced by a break
1757 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1758 // Map of length 1 with sole member label replaced by break
1759 // Alternate representation that some decoders handle difference
1760 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1761 // Array of length 1 with 2nd member value replaced by a break
1762 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1763 // Map of length 2 with 2nd member replaced by a break
1764 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1765
1766
1767 // Breaks must not occur on their own out of an indefinite length data item
1768 // A bare break is not well formed
1769 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1770 // A bare break after a zero length definite length array
1771 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1772 // A bare break after a zero length indefinite length map
1773 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1774
1775
1776 // Forbidden two byte encodings of simple types
1777 // Must use 0xe0 instead
1778 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1779 // Should use 0xe1 instead
1780 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1781 // Should use 0xe2 instead
1782 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1783 // Should use 0xe3 instead
1784 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1785 // Should use 0xe4 instead
1786 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1787 // Should use 0xe5 instead
1788 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1789 // Should use 0xe6 instead
1790 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1791 // Should use 0xe7 instead
1792 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1793 // Should use 0xe8 instead
1794 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1795 // Should use 0xe9 instead
1796 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1797 // Should use 0xea instead
1798 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1799 // Should use 0xeb instead
1800 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1801 // Should use 0xec instead
1802 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1803 // Should use 0xed instead
1804 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1805 // Should use 0xee instead
1806 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1807 // Should use 0xef instead
1808 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1809 // Should use 0xf0 instead
1810 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1811 // Should use 0xf1 instead
1812 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1813 // Should use 0xf2 instead
1814 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1815 // Must use 0xf3 instead
1816 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1817 // Must use 0xf4 instead
1818 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1819 // Must use 0xf5 instead
1820 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1821 // Must use 0xf6 instead
1822 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1823 // Must use 0xf7 instead
1824 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1825 // Must use 0xf8 instead
1826 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1827
1828
1829 // Integers with additional info indefinite length
1830 // Positive integer with additional info indefinite length
1831 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1832 // Negative integer with additional info indefinite length
1833 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1834 // CBOR tag with "argument" an indefinite length
1835 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1836 // CBOR tag with "argument" an indefinite length alternate vector
1837 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1838
1839
1840 // Missing bytes from a deterministic length string
1841 // A byte string is of length 1 without the 1 byte
1842 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1843 // A text string is of length 1 without the 1 byte
1844 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001845 // Byte string should have 2^32-15 bytes, but has one
1846 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1847 // Byte string should have 2^32-15 bytes, but has one
1848 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001849
1850
1851 // Use of unassigned additional information values
1852 // Major type positive integer with reserved value 28
1853 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1854 // Major type positive integer with reserved value 29
1855 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1856 // Major type positive integer with reserved value 30
1857 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1858 // Major type negative integer with reserved value 28
1859 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1860 // Major type negative integer with reserved value 29
1861 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1862 // Major type negative integer with reserved value 30
1863 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1864 // Major type byte string with reserved value 28 length
1865 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1866 // Major type byte string with reserved value 29 length
1867 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1868 // Major type byte string with reserved value 30 length
1869 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1870 // Major type text string with reserved value 28 length
1871 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1872 // Major type text string with reserved value 29 length
1873 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1874 // Major type text string with reserved value 30 length
1875 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1876 // Major type array with reserved value 28 length
1877 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1878 // Major type array with reserved value 29 length
1879 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1880 // Major type array with reserved value 30 length
1881 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1882 // Major type map with reserved value 28 length
1883 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1884 // Major type map with reserved value 29 length
1885 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1886 // Major type map with reserved value 30 length
1887 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1888 // Major type tag with reserved value 28 length
1889 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1890 // Major type tag with reserved value 29 length
1891 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1892 // Major type tag with reserved value 30 length
1893 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1894 // Major type simple with reserved value 28 length
1895 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1896 // Major type simple with reserved value 29 length
1897 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1898 // Major type simple with reserved value 30 length
1899 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1900
1901
1902 // Maps must have an even number of data items (key & value)
1903 // Map with 1 item when it should have 2
1904 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1905 // Map with 3 item when it should have 4
1906 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1907 // Map with 1 item when it should have 2
1908 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1909 // Map with 3 item when it should have 4
1910 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1911
1912
1913 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001914 // Text-based date, with an integer
1915 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1916 // Epoch date, with an byte string
1917 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1918 // tagged as both epoch and string dates
1919 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1920 // big num tagged an int, not a byte string
1921 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001922};
1923
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001924int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001925{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001926 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001927
Laurence Lundblade59289e52019-12-30 13:44:37 -08001928 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1929 if(nResult) {
1930 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001931 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001932
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001933 // Corrupt the UsefulInputBuf and see that
1934 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001935 {
1936 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001937 QCBORItem Item;
1938 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001939
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001940 QCBORDecode_Init(&DCtx,
1941 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1942 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001943
1944 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001945 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001946 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001947 Item.val.uCount != 10) {
1948 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001949 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001951
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001952 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001953
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001954 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001955 if(nCBORError != QCBOR_ERR_HIT_END) {
1956 // Did not get back the error expected
1957 return -2;
1958 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001960
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001961/*
1962 This test is disabled until QCBOREncode_EncodeHead() is brought in so
1963 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
1964
1965 This relies on the largest string allowed being SIZE_MAX -4 rather than
1966 SIZE_MAX. That way the test can be performed.
1967 {
1968 QCBORDecodeContext DCtx;
1969 QCBORItem Item;
1970
1971 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
1972 0xff, 0xff, 0xff, 0xff};
1973
1974 QCBORDecode_Init(&DCtx,
1975 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
1976 QCBOR_DECODE_MODE_NORMAL);
1977
1978 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
1979 return -4;
1980 }
1981 }
1982*/
1983
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001984 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001985}
1986
1987
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001988/* Try all 256 values of the byte at nLen including recursing for
1989 each of the values to try values at nLen+1 ... up to nLenMax
1990 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001991static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001993 if(nLen >= nLenMax) {
1994 return;
1995 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001996
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001997 for(int inputByte = 0; inputByte < 256; inputByte++) {
1998 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001999 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002000 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002001
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002002 // Get ready to parse
2003 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002004 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002005
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002006 // Parse by getting the next item until an error occurs
2007 // Just about every possible decoder error can occur here
2008 // The goal of this test is not to check for the correct
2009 // error since that is not really possible. It is to
2010 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002011 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002012 QCBORItem Item;
2013 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002014 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002015 break;
2016 }
2017 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002018
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002019 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002020 }
2021}
2022
2023
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002024int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002025{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002026 // Size 2 tests 64K inputs and runs quickly
2027 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002028
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002029 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002030
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002031 return 0;
2032}
2033
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002034
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002035int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002036{
2037 // size 3 tests 16 million inputs and runs OK
2038 // in seconds on fast machines. Size 4 takes
2039 // 10+ minutes and 5 half a day on fast
2040 // machines. This test is kept separate from
2041 // the others so as to no slow down the use
2042 // of them as a very frequent regression.
2043 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002044
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002045 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002046
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002047 return 0;
2048}
2049
2050
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002051static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002052 0xc0, // tag for string date
2053 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002055 0xc1, // tag for epoch date
2056 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2057
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002058 // CBOR_TAG_B64
2059 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002060 0x1a, 0x53, 0x72, 0x4E, 0x01,
2061
2062 0xc1, // tag for epoch date
2063 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002064
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002065 0xc1, // tag for epoch date
2066 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002067
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002068 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002069 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002070
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002071 0xc1, // tag for epoch date
2072 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2073 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2074
2075 0xc1, // tag for epoch date
2076 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002077};
2078
2079
2080// have to check float expected only to within an epsilon
2081int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002082
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002083 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002084
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002085 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002086
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002087 return diff > 0.0000001;
2088}
2089
2090
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002091int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002092{
2093 QCBORDecodeContext DCtx;
2094 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002095 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002096
Laurence Lundbladeee851742020-01-08 08:37:05 -08002097 QCBORDecode_Init(&DCtx,
2098 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2099 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002100
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002101 const uint64_t uTags[] = {15};
2102 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002103
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002104 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002105
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002106 // String date
2107 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2108 return -1;
2109 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002110 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002111 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002112 }
2113
2114 // Epoch date
2115 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002116 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002117 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2118 Item.val.epochDate.nSeconds != 1400000000 ||
2119 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002120 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002121 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002122
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002123 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2124 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002125 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002126 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002127 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2128 Item.val.epochDate.nSeconds != 1400000001 ||
2129 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002130 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002131 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002132 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002133
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002134 // Epoch date that is too large for our representation
2135 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002136 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002138
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002139 // Epoch date in float format with fractional seconds
2140 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002141 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002142 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2143 Item.val.epochDate.nSeconds != 1 ||
2144 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002145 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002146 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002147
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002148 // Epoch date float that is too large for our representation
2149 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002150 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002151 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002152
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002153 // Epoch date double that is just slightly too large
2154 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2155 return -11;
2156 }
2157
2158 // Largest double epoch date supported
2159 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2160 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2161 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2162 Item.val.epochDate.nSeconds == 0) {
2163 return -12;
2164 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002165 // TODO: could use a few more tests with float, double, and half precsion
2166 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002167
2168 return 0;
2169}
2170
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002171// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002172static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002173 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002174 0x81, // Array of one
2175 0xd8, 0x04, // non-preferred serialization of tag 4
2176 0x82, 0x01, 0x03}; // fraction 1/3
2177
Laurence Lundblade59289e52019-12-30 13:44:37 -08002178/*
2179 DB 9192939495969798 # tag(10489608748473423768)
2180 80 # array(0)
2181 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002182static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2183 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002184
Laurence Lundblade59289e52019-12-30 13:44:37 -08002185/*
2186DB 9192939495969798 # tag(10489608748473423768)
2187 D8 88 # tag(136)
2188 C6 # tag(6)
2189 C7 # tag(7)
2190 80 # array(0)
2191*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002192static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2193 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002194
2195/*
2196 The cbor.me parse of this.
2197 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002198 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 +07002199 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2200 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2201 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2202 */
2203static uint8_t spCSRWithTags[] = {
2204 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2205 0xc6, 0xc7, 0x36,
2206 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2207 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2208 0xcb, 0xa5,
2209 0xd1, 0x31,
2210 0xd1, 0xd1, 0xd1, 0x6c,
2211 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2212 0xc9, 0x30,
2213 0xd9, 0x03, 0x05, 0x63,
2214 0x53, 0x53, 0x47,
2215 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002216 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002217 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2218 0xd1, 0x2f,
2219 0xd1, 0x69,
2220 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2221 0xd1, 0x2d,
2222 0xd1, 0x62,
2223 0x55, 0x53,
2224 0xd7, 0x32,
2225 0xd3, 0xa2,
2226 0x2a,
2227 0xc9, 0xa1,
2228 0x28,
2229 0x26,
2230 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2231 0xcc, 0x4a,
2232 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2233 0xd0, 0x35,
2234 0xd7, 0xa1,
2235 0xcb, 0xc8, 0xc7, 0x24,
2236 0xc8, 0x22};
2237
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002238static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002239
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002240
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002241int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002242{
2243 QCBORDecodeContext DCtx;
2244 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002245
Laurence Lundbladeee851742020-01-08 08:37:05 -08002246 QCBORDecode_Init(&DCtx,
2247 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2248 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002249
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002250 //-------------------------
2251 // This text matches the magic number tag and the fraction tag
2252 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2253 return -2;
2254 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002255 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002256 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2257 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002259
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002260 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2261 return -4;
2262 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002263
2264#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002265 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002266 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002267 Item.val.uCount != 2) {
2268 return -5;
2269 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002270#else
2271 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2272 return -6;
2273 }
2274#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002275
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002276 // --------------------------------
2277 // This test decodes the very large tag, but it is not in
2278 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002279 QCBORDecode_Init(&DCtx,
2280 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2281 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002282 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2283 return -6;
2284 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002285 /*
2286 if(Item.uTagBits) { // TODO: make sure it is OK to remove this
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002287 return -7;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002288 }*/
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002289
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002290 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002291 // This test sets up a caller-config list that includes the very large
2292 // tage and then matches it.
2293 QCBORDecode_Init(&DCtx,
2294 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2295 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002296 const uint64_t puList[] = {0x9192939495969798, 257};
2297 const QCBORTagListIn TL = {2, puList};
2298 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002299
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002300 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2301 return -8;
2302 }
2303 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2304 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2305 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2306 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2307 Item.val.uCount != 0) {
2308 return -9;
2309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002310
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002311 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002312 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002313 const uint64_t puLongList[17] = {1,2,1};
2314 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002315 QCBORDecode_Init(&DCtx,
2316 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2317 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002318 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2319 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2320 return -11;
2321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002322
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002323 // -----------------------
2324 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002325 QCBORDecode_Init(&DCtx,
2326 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2327 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002328 uint64_t puTags[16];
2329 QCBORTagListOut Out = {0, 4, puTags};
2330 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2331 return -12;
2332 }
2333 if(puTags[0] != 0x9192939495969798 ||
2334 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002335 puTags[2] != 0x06 ||
2336 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002337 return -13;
2338 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002339
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002340 // ----------------------
2341 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002342 QCBORDecode_Init(&DCtx,
2343 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2344 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002345 QCBORTagListOut OutSmall = {0, 3, puTags};
2346 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2347 return -14;
2348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002349
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002350#if 0
2351 // TODO: this test needs to be re evaluated
2352
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002353 // ---------------
2354 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002355 QCBORDecode_Init(&DCtx,
2356 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2357 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002358 int n = CheckCSRMaps(&DCtx);
2359 if(n) {
2360 return n-2000;
2361 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade59289e52019-12-30 13:44:37 -08002363 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002364 QCBORDecode_Init(&DCtx,
2365 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2366 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002367
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002368 const uint64_t puTagList[] = {773, 1, 90599561};
2369 const QCBORTagListIn TagList = {3, puTagList};
2370 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002371
2372
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002373 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2374 return -100;
2375 }
2376 if(Item.uDataType != QCBOR_TYPE_MAP ||
2377 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2378 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2379 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2380 Item.val.uCount != 2 ||
2381 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2382 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2383 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2384 Out.uNumUsed != 3) {
2385 return -101;
2386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002387
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002388 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2389 return -102;
2390 }
2391 if(Item.uDataType != QCBOR_TYPE_MAP ||
2392 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2393 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2394 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2395 Item.val.uCount != 2 ||
2396 puTags[0] != 5859837686836516696 ||
2397 puTags[1] != 7 ||
2398 Out.uNumUsed != 2) {
2399 return -103;
2400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002401
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002402 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2403 return -104;
2404 }
2405 if(Item.uDataType != QCBOR_TYPE_MAP ||
2406 Item.uTagBits ||
2407 Item.val.uCount != 5 ||
2408 puTags[0] != 0x0b ||
2409 Out.uNumUsed != 1) {
2410 return -105;
2411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002412
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002413 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2414 return -106;
2415 }
2416 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2417 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2418 Item.val.string.len != 12 ||
2419 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2420 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2421 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2422 Out.uNumUsed != 3) {
2423 return -105;
2424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002425
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002426 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2427 return -107;
2428 }
2429 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2430 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2431 Item.val.string.len != 3 ||
2432 puTags[0] != 773 ||
2433 Out.uNumUsed != 1) {
2434 return -108;
2435 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002436
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002437 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2438 return -109;
2439 }
2440 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002441 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002442 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002443 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002444 puTags[11] != 0x0f ||
2445 Out.uNumUsed != 12) {
2446 return -110;
2447 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002448
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002449 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2450 return -111;
2451 }
2452 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2453 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2454 Item.val.string.len != 9 ||
2455 puTags[0] != 17 ||
2456 Out.uNumUsed != 1) {
2457 return -112;
2458 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002459
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002460 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2461 return -111;
2462 }
2463 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2464 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2465 Item.val.string.len != 2 ||
2466 puTags[0] != 17 ||
2467 Out.uNumUsed != 1) {
2468 return -112;
2469 }
2470
2471 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2472 return -113;
2473 }
2474 if(Item.uDataType != QCBOR_TYPE_MAP ||
2475 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2476 Item.val.uCount != 2 ||
2477 puTags[0] != 19 ||
2478 Out.uNumUsed != 1) {
2479 return -114;
2480 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002481
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002482 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2483 return -115;
2484 }
2485 if(Item.uDataType != QCBOR_TYPE_MAP ||
2486 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2487 Item.uTagBits ||
2488 Item.val.uCount != 1 ||
2489 puTags[0] != 9 ||
2490 Out.uNumUsed != 1) {
2491 return -116;
2492 }
2493
2494 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2495 return -116;
2496 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002497 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002498 Item.val.int64 != -7 ||
2499 Item.uTagBits ||
2500 Out.uNumUsed != 0) {
2501 return -117;
2502 }
2503
2504 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2505 return -118;
2506 }
2507 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2508 Item.val.string.len != 10 ||
2509 Item.uTagBits ||
2510 puTags[0] != 12 ||
2511 Out.uNumUsed != 1) {
2512 return -119;
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 -120;
2517 }
2518 if(Item.uDataType != QCBOR_TYPE_MAP ||
2519 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2520 Item.val.uCount != 1 ||
2521 puTags[0] != 0x17 ||
2522 Out.uNumUsed != 1) {
2523 return -121;
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 -122;
2528 }
2529 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2530 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2531 Item.val.int64 != -3 ||
2532 puTags[0] != 8 ||
2533 Out.uNumUsed != 1) {
2534 return -123;
2535 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002536
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002537 if(QCBORDecode_Finish(&DCtx)) {
2538 return -124;
2539 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002540#else
2541 (void)spCSRWithTags;
2542#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002543 return 0;
2544}
2545
2546
2547
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002548
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002549static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002550 0x83,
2551 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2552 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2553 0xA4,
2554 0x63, 0x42, 0x4E, 0x2B,
2555 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2556 0x18, 0x40,
2557 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2558 0x63, 0x42, 0x4E, 0x2D,
2559 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2560 0x38, 0x3F,
2561 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2562
2563
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002564static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002565
2566
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002567int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002568{
2569 QCBORDecodeContext DCtx;
2570 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002571 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002572
Laurence Lundbladeee851742020-01-08 08:37:05 -08002573 QCBORDecode_Init(&DCtx,
2574 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2575 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002576
2577
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002578 //
2579 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2580 return -1;
2581 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002582 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002583 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002584
2585 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002586 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002587 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002588 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002589 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002590 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002591 }
2592
2593 //
2594 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002595 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002596 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002597 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002598 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002599 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002601 //
2602 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002603 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002604 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002605 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002606 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002607
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002608 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002609 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002610 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2611 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002612 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002613 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002614 }
2615
2616 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002617 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002618 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2619 Item.uLabelType != QCBOR_TYPE_INT64 ||
2620 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002621 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002622 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002623 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002624
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002625 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002626 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002627 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2628 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002629 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002630 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002631 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002632
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002633 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002634 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002635 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2636 Item.uLabelType != QCBOR_TYPE_INT64 ||
2637 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002638 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002639 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002640 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002642 return 0;
2643}
2644
2645
2646
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002647static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002648 uint8_t uDataType,
2649 uint8_t uNestingLevel,
2650 uint8_t uNextNest,
2651 int64_t nLabel,
2652 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002653{
2654 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002655 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002656
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002657 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2658 if(Item.uDataType != uDataType) return -1;
2659 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002660 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2661 Item.uLabelType != QCBOR_TYPE_UINT64) {
2662 return -1;
2663 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002664 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2665 if(Item.label.int64 != nLabel) return -1;
2666 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002667 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002668 }
2669 }
2670 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302671 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002672
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002673 if(pItem) {
2674 *pItem = Item;
2675 }
2676 return 0;
2677}
2678
2679
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002680// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002681static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002682{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302683 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002684
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302685 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002686
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302687 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002688
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302689 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2690 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2691 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2692 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2693 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002694
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302695 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2696 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002697
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302698 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2699 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002700
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302701 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2702 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002703
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002704 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002705
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002706 return 0;
2707}
2708
2709
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002710/*
2711// cbor.me decoded output
2712{
2713 -23: {
2714 -20: {
2715 -18: "Organization",
2716 -17: "SSG",
2717 -15: "Confusion",
2718 -16: "San Diego",
2719 -14: "US"
2720 },
2721 -19: {
2722 -11: {
2723 -9: -7
2724 },
2725 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2726 }
2727 },
2728 -22: {
2729 -5: -3
2730 }
2731}
2732 */
2733
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002734
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002735static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002736 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2737 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2738 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2739 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2740 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2741 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2742 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2743 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2744 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2745
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002746int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002747{
2748 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002749
Laurence Lundbladeee851742020-01-08 08:37:05 -08002750 QCBORDecode_Init(&DCtx,
2751 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2752 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002753
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002754 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002755}
2756
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002757
2758
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002759int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002760{
2761 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002762
Laurence Lundbladeee851742020-01-08 08:37:05 -08002763 QCBORDecode_Init(&DCtx,
2764 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2765 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002766
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002767 QCBORItem Item;
2768 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002769
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002770 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2771 return -1;
2772 }
2773 if(Item.uDataType != QCBOR_TYPE_MAP) {
2774 return -2;
2775 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002776
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002777 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2778 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2779 return -3;
2780 }
2781
2782 return 0;
2783}
2784
2785
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002786// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002787static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002788 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2789 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2790 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2791 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2792 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2793 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002794 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2795 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2796 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2797 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002798
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002799int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002800{
2801 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002802
Laurence Lundbladeee851742020-01-08 08:37:05 -08002803 QCBORDecode_Init(&DCtx,
2804 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2805 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002806
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002807 return CheckCSRMaps(&DCtx);
2808}
2809
2810
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002811
Laurence Lundblade17ede402018-10-13 11:43:07 +08002812static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2813{
2814 UsefulOutBuf UOB;
2815 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002816
Laurence Lundblade17ede402018-10-13 11:43:07 +08002817 int i;
2818 for(i = 0; i < n; i++) {
2819 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2820 }
2821
2822 for(i = 0; i < n; i++) {
2823 UsefulOutBuf_AppendByte(&UOB, 0xff);
2824 }
2825 return UsefulOutBuf_OutUBuf(&UOB);
2826}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002827
2828
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002829static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002830{
2831 QCBORDecodeContext DC;
2832 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002833
Laurence Lundblade17ede402018-10-13 11:43:07 +08002834 int j;
2835 for(j = 0; j < nNestLevel; j++) {
2836 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002837 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002838 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2839 // Should be in error
2840 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2841 return -4;
2842 } else {
2843 return 0; // Decoding doesn't recover after an error
2844 }
2845 } else {
2846 // Should be no error
2847 if(nReturn) {
2848 return -9; // Should not have got an error
2849 }
2850 }
2851 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2852 return -7;
2853 }
2854 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002855 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002856 if(nReturn) {
2857 return -3;
2858 }
2859 return 0;
2860}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002861
2862
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002863int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002864{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302865 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002866 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002867 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002868 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002869 int nReturn = parse_indeflen_nested(Nested, i);
2870 if(nReturn) {
2871 return nReturn;
2872 }
2873 }
2874 return 0;
2875}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002876
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002877
Laurence Lundbladeee851742020-01-08 08:37:05 -08002878// [1, [2, 3]]
2879static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2880// No closing break
2881static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2882// Not enough closing breaks
2883static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2884// Too many closing breaks
2885static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2886// Unclosed indeflen inside def len
2887static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2888// confused tag
2889static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002890
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002891int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002892{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002893 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002894 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002895 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002896
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002897 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302898 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002899 QCBORDecodeContext DC;
2900 QCBORItem Item;
2901 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002902
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002903 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002904
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002905 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302906
2907 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2908 Item.uNestingLevel != 0 ||
2909 Item.uNextNestLevel != 1) {
2910 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002911 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002912
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002913 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302914 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2915 Item.uNestingLevel != 1 ||
2916 Item.uNextNestLevel != 1) {
2917 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002918 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002919
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002920 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302921 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2922 Item.uNestingLevel != 1 ||
2923 Item.uNextNestLevel != 2) {
2924 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002925 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002926
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002927 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002928 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302929 Item.uNestingLevel != 2 ||
2930 Item.uNextNestLevel != 2) {
2931 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002932 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002933
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002934 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002935 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302936 Item.uNestingLevel != 2 ||
2937 Item.uNextNestLevel != 0) {
2938 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002939 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002940
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002941 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302942 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002943 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002944
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002945 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002946 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002947
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002948 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002949
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002950 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002951
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002952 nResult = QCBORDecode_GetNext(&DC, &Item);
2953 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302954 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002955 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002956
Laurence Lundblade570fab52018-10-13 18:28:27 +08002957 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302958 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2959 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002960 }
2961
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002962
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002963 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002964 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002965
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002966 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002967
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002968 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002969
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002970 nResult = QCBORDecode_GetNext(&DC, &Item);
2971 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302972 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002973 }
2974
2975 nResult = QCBORDecode_GetNext(&DC, &Item);
2976 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302977 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002978 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002979
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002980 nResult = QCBORDecode_GetNext(&DC, &Item);
2981 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302982 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002984
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002985 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302986 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2987 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002988 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002989
2990
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002991 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002992 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002993
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002994 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002995
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002996 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002997
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002998 nResult = QCBORDecode_GetNext(&DC, &Item);
2999 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303000 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003002
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003003 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303004 if(nResult != QCBOR_ERR_BAD_BREAK) {
3005 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003006 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303007
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003008
Laurence Lundblade570fab52018-10-13 18:28:27 +08003009 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003010 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003011
Laurence Lundblade570fab52018-10-13 18:28:27 +08003012 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003013
Laurence Lundblade570fab52018-10-13 18:28:27 +08003014 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003015
Laurence Lundblade570fab52018-10-13 18:28:27 +08003016 nResult = QCBORDecode_GetNext(&DC, &Item);
3017 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303018 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003019 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003020
Laurence Lundblade570fab52018-10-13 18:28:27 +08003021 nResult = QCBORDecode_GetNext(&DC, &Item);
3022 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303023 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003025
Laurence Lundblade570fab52018-10-13 18:28:27 +08003026 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303027 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3028 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003030
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303031 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003032 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003033
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303034 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003035
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303036 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003037
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303038 nResult = QCBORDecode_GetNext(&DC, &Item);
3039 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303040 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303041 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003042
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303043 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303044 if(nResult != QCBOR_ERR_BAD_BREAK) {
3045 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303046 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003047
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003048 return 0;
3049}
3050
Laurence Lundblade17ede402018-10-13 11:43:07 +08003051
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003052static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003053 0x81, // Array of length one
3054 0x7f, // text string marked with indefinite length
3055 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3056 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3057 0xff // ending break
3058};
3059
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003060static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303061 0x81, // Array of length one
3062 0x7f, // text string marked with indefinite length
3063 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3064 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3065 0xff // ending break
3066};
3067
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003068static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303069 0x81, // Array of length one
3070 0x7f, // text string marked with indefinite length
3071 0x01, 0x02, // Not a string
3072 0xff // ending break
3073};
3074
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003075static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303076 0x81, // Array of length one
3077 0x7f, // text string marked with indefinite length
3078 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3079 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3080 // missing end of string
3081};
3082
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003083static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303084 0xa1, // Array of length one
3085 0x7f, // text string marked with indefinite length
3086 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3087 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3088 0xff, // ending break
3089 0x01 // integer being labeled.
3090};
3091
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003092/**
3093 Make an indefinite length string
3094
3095 @param Storage Storage for string, must be 144 bytes in size
3096 @return The indefinite length string
3097
3098 This makes an array with one indefinite length string that has 7 chunks
3099 from size of 1 byte up to 64 bytes.
3100 */
3101static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303102{
3103 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003104
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303105 UsefulOutBuf_Init(&UOB, Storage);
3106 UsefulOutBuf_AppendByte(&UOB, 0x81);
3107 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003108
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003109 uint8_t uStringByte = 0;
3110 // Use of type int is intentional
3111 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3112 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303113 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003114 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3115 for(int j = 0; j < uChunkSize; j++) {
3116 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3117 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303118 }
3119 }
3120 UsefulOutBuf_AppendByte(&UOB, 0xff);
3121
3122 return UsefulOutBuf_OutUBuf(&UOB);
3123}
3124
3125static int CheckBigString(UsefulBufC BigString)
3126{
3127 if(BigString.len != 255) {
3128 return 1;
3129 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003130
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303131 for(uint8_t i = 0; i < 255; i++){
3132 if(((const uint8_t *)BigString.ptr)[i] != i) {
3133 return 1;
3134 }
3135 }
3136 return 0;
3137}
3138
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303139
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003140int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303141{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303142 QCBORDecodeContext DC;
3143 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303144 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003145 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003146
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303147 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003148 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303149 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003150
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303151 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303152 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003154
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303155 if(QCBORDecode_GetNext(&DC, &Item)) {
3156 return -2;
3157 }
3158 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3159 return -3;
3160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003161
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303162 if(QCBORDecode_GetNext(&DC, &Item)) {
3163 return -4;
3164 }
3165 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3166 return -5;
3167 }
3168 if(QCBORDecode_Finish(&DC)) {
3169 return -6;
3170 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303171
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303172 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003173 QCBORDecode_Init(&DC,
3174 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3175 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003176
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303177 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3178 return -7;
3179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003180
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303181 if(QCBORDecode_GetNext(&DC, &Item)) {
3182 return -8;
3183 }
3184 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3185 return -9;
3186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003187
Laurence Lundblade30816f22018-11-10 13:40:22 +07003188 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303189 return -10;
3190 }
3191
3192 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003193 QCBORDecode_Init(&DC,
3194 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3195 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003196
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303197 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3198 return -11;
3199 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003200
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303201 if(QCBORDecode_GetNext(&DC, &Item)) {
3202 return -12;
3203 }
3204 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3205 return -13;
3206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003207
Laurence Lundblade30816f22018-11-10 13:40:22 +07003208 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303209 return -14;
3210 }
3211
3212 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003213 QCBORDecode_Init(&DC,
3214 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3215 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003216
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303217 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3218 return -15;
3219 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003220
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303221 if(QCBORDecode_GetNext(&DC, &Item)) {
3222 return -16;
3223 }
3224 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3225 return -17;
3226 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003227
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303228 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3229 return -18;
3230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003231
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303232 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303233 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003234
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303235 QCBORDecode_GetNext(&DC, &Item);
3236 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303237 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003239
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303240 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303241 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003243
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303244 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003245 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303246
3247 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3248 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303249 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003251
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303252 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303253 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003254 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003255
Laurence Lundbladeee851742020-01-08 08:37:05 -08003256 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3257 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003258
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303259 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303260 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303261 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303262 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003263
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303264 QCBORDecode_GetNext(&DC, &Item);
3265 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303266 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303267 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003268 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303269 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003271
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303272 // ---- big bstr -----
3273 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003274
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303275 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3276 return -25;
3277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003278
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303279 if(QCBORDecode_GetNext(&DC, &Item)) {
3280 return -26;
3281 }
3282 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303283 return -26;
3284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003285
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303286 if(QCBORDecode_GetNext(&DC, &Item)) {
3287 return -27;
3288 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303289 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303290 return -28;
3291 }
3292 if(CheckBigString(Item.val.string)) {
3293 return -3;
3294 }
3295 if(QCBORDecode_Finish(&DC)) {
3296 return -29;
3297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003298
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303299 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003300 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003301
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303302 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3303 return -30;
3304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003305
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303306 QCBORDecode_GetNext(&DC, &Item);
3307 if(Item.uDataType != QCBOR_TYPE_MAP) {
3308 return -31;
3309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003310
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303311 if(QCBORDecode_GetNext(&DC, &Item)){
3312 return -32;
3313 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003314 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3315 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303316 Item.uDataAlloc || !Item.uLabelAlloc ||
3317 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3318 return -33;
3319 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003320
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303321 if(QCBORDecode_Finish(&DC)) {
3322 return -34;
3323 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003324
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003325 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003326}
3327
3328
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003329int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303330{
3331 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003332 QCBORError nCBORError;
3333
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003334
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303335 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003336 QCBORDecode_Init(&DC,
3337 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3338 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003339
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003340 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003341
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003342 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3343 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303344 return -1;
3345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003346
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003347 if(CheckCSRMaps(&DC)) {
3348 return -2;
3349 }
3350
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303351 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003352 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003353 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003354
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303355 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003356 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303357 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003358
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303359 QCBORItem Item1, Item2, Item3, Item4;
3360 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003361 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303362 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3363 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003364 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303365 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003366 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303367 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003368 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303369 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003370 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303371 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003372 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003373
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303374 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303376 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303377 Item1.uDataType != QCBOR_TYPE_INT64 ||
3378 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003379 Item1.uDataAlloc != 0 ||
3380 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003381 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003382 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003383 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003384
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303385
3386 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003387 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303388 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003389 Item2.uDataAlloc != 0 ||
3390 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303391 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003392 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003393
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303394 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003395 Item3.uDataAlloc == 0 ||
3396 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003397 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003398 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003400
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303401 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003402 Item4.uDataAlloc == 0 ||
3403 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003404 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003405 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003406 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003407
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303408 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003409 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003410 QCBORDecode_Init(&DC,
3411 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3412 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303413 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3414 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003415 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303416 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003417 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003418 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003419 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303420 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3421 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3422 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3423 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3424 }
3425 }
3426 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003427 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003428 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303429 }
3430
3431 return 0;
3432}
3433
Laurence Lundbladef6531662018-12-04 10:42:22 +09003434
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303435
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003436int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003437{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003438 // Set up the decoder with a tiny bit of CBOR to parse because
3439 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003440 QCBORDecodeContext DC;
3441 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3442 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003443
Laurence Lundbladef6531662018-12-04 10:42:22 +09003444 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003445 // Then fish into the internals of the decode context
3446 // to get the allocator function so it can be called directly.
3447 // Also figure out how much pool is available for use
3448 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003449 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003450 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3451 if(nError) {
3452 return -9;
3453 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003454 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3455 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3456 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003457
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003458 // First test -- ask for one more byte than available and see failure
3459 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003460 if(!UsefulBuf_IsNULL(Allocated)) {
3461 return -1;
3462 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003463
Laurence Lundbladef6531662018-12-04 10:42:22 +09003464 // Re do the set up for the next test that will do a successful alloc,
3465 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003466 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003467 pAlloc = DC.StringAllocator.pfAllocator;
3468 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3469 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003470
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003471 // Allocate one byte less than available and see success
3472 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003473 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3474 return -2;
3475 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003476 // Ask for some more and see failure
3477 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003478 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3479 return -3;
3480 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003481 // Free the first allocate, retry the second and see success
3482 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3483 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003484 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3485 return -4;
3486 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003487
Laurence Lundbladef6531662018-12-04 10:42:22 +09003488 // Re do set up for next test that involves a successful alloc,
3489 // and a successful realloc and a failed realloc
3490 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003491 pAlloc = DC.StringAllocator.pfAllocator;
3492 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003493
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003494 // Allocate half the pool and see success
3495 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003496 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3497 return -5;
3498 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003499 // Reallocate to take up the whole pool and see success
3500 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003501 if(UsefulBuf_IsNULL(Allocated2)) {
3502 return -6;
3503 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003504 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003505 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3506 return -7;
3507 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003508 // Try to allocate more to be sure there is failure after a realloc
3509 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3510 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003511 return -8;
3512 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003513
Laurence Lundbladef6531662018-12-04 10:42:22 +09003514 return 0;
3515}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003516
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003517
3518/* Just enough of an allocator to test configuration of one */
3519static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3520{
3521 (void)pOldMem; // unused variable
3522
3523 if(uNewSize) {
3524 // Assumes the context pointer is the buffer and
3525 // nothing too big will ever be asked for.
3526 // This is only good for this basic test!
3527 return (UsefulBuf) {pCtx, uNewSize};
3528 } else {
3529 return NULLUsefulBuf;
3530 }
3531}
3532
3533
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003534int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003535{
3536 // Set up the decoder with a tiny bit of CBOR to parse because
3537 // nothing can be done with it unless that is set up.
3538 QCBORDecodeContext DC;
3539 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3540 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3541
3542 uint8_t pAllocatorBuffer[50];
3543
3544 // This is really just to test that this call works.
3545 // The full functionality of string allocators is tested
3546 // elsewhere with the MemPool internal allocator.
3547 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3548
3549 QCBORItem Item;
3550 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3551 return -1;
3552 }
3553
3554 if(Item.uDataAlloc == 0 ||
3555 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3556 Item.val.string.ptr != pAllocatorBuffer) {
3557 return -2;
3558 }
3559
3560 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3561 return -3;
3562 }
3563
3564 return 0;
3565}
3566
Laurence Lundblade59289e52019-12-30 13:44:37 -08003567
3568#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladea826c502020-05-10 21:07:00 -07003569/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08003570 [
3571 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003572 4([-20, 4759477275222530853136]),
3573 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003574 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003575 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003576 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07003577 5([ 9223372036854775806, -4759477275222530853137])
3578 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003579 ]
3580 */
3581
3582static const uint8_t spExpectedExponentsAndMantissas[] = {
3583 0x87,
3584 0xC4, 0x82, 0x20,
3585 0x03,
3586 0xC4, 0x82, 0x33,
3587 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3588 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3589 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3590 0xC5, 0x82, 0x19, 0x01, 0x2C,
3591 0x18, 0x64,
3592 0xC5, 0x82, 0x33,
3593 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3594 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3595 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07003596 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3597 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003598 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3599 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3600};
3601
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003602int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003603{
3604 QCBORDecodeContext DC;
3605 QCBORError nCBORError;
3606 QCBORItem item;
3607
Laurence Lundblade17af4902020-01-07 19:11:55 -08003608 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3609 0x06, 0x07, 0x08, 0x09, 0x010};
3610 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003611
3612
3613 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3614
3615 nCBORError = QCBORDecode_GetNext(&DC, &item);
3616 if(nCBORError != QCBOR_SUCCESS) {
3617 return 1;
3618 }
3619
3620 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3621 return 2;
3622 }
3623
3624 nCBORError = QCBORDecode_GetNext(&DC, &item);
3625 if(nCBORError != QCBOR_SUCCESS) {
3626 return 3;
3627 }
3628
3629 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3630 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3631 item.val.expAndMantissa.nExponent != -1) {
3632 return 4;
3633 }
3634
3635 nCBORError = QCBORDecode_GetNext(&DC, &item);
3636 if(nCBORError != QCBOR_SUCCESS) {
3637 return 5;
3638 }
3639
3640 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3641 item.val.expAndMantissa.nExponent != -20 ||
3642 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3643 return 6;
3644 }
3645
3646 nCBORError = QCBORDecode_GetNext(&DC, &item);
3647 if(nCBORError != QCBOR_SUCCESS) {
3648 return 7;
3649 }
3650
3651 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3652 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3653 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3654 return 8;
3655 }
3656
3657 nCBORError = QCBORDecode_GetNext(&DC, &item);
3658 if(nCBORError != QCBOR_SUCCESS) {
3659 return 9;
3660 }
3661
3662 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3663 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3664 item.val.expAndMantissa.nExponent != 300) {
3665 return 10;
3666 }
3667
Laurence Lundbladea826c502020-05-10 21:07:00 -07003668 // 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003669 nCBORError = QCBORDecode_GetNext(&DC, &item);
3670 if(nCBORError != QCBOR_SUCCESS) {
3671 return 11;
3672 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003673 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3674 item.val.expAndMantissa.nExponent != -20 ||
3675 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3676 return 12;
3677 }
3678
Laurence Lundbladea826c502020-05-10 21:07:00 -07003679 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade59289e52019-12-30 13:44:37 -08003680 nCBORError = QCBORDecode_GetNext(&DC, &item);
3681 if(nCBORError != QCBOR_SUCCESS) {
3682 return 13;
3683 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003684 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3685 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3686 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3687 return 14;
3688 }
3689
Laurence Lundbladea826c502020-05-10 21:07:00 -07003690 // 5([ 9223372036854775806, -4759477275222530853137])
3691 nCBORError = QCBORDecode_GetNext(&DC, &item);
3692 if(nCBORError != QCBOR_SUCCESS) {
3693 return 13;
3694 }
3695 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3696 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3697 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3698 return 14;
3699 }
3700
3701
3702 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003703 nCBORError = QCBORDecode_GetNext(&DC, &item);
3704 if(nCBORError != QCBOR_SUCCESS) {
3705 return 15;
3706 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003707 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3708 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3709 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3710 return 16;
3711 }
3712
3713 /* Now encode some stuff and then decode it */
3714 uint8_t pBuf[40];
3715 QCBOREncodeContext EC;
3716 UsefulBufC Encoded;
3717
3718 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3719 QCBOREncode_OpenArray(&EC);
3720 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3721 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3722 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3723 QCBOREncode_CloseArray(&EC);
3724 QCBOREncode_Finish(&EC, &Encoded);
3725
3726
3727 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3728 nCBORError = QCBORDecode_GetNext(&DC, &item);
3729 if(nCBORError != QCBOR_SUCCESS) {
3730 return 13;
3731 }
3732
3733 nCBORError = QCBORDecode_GetNext(&DC, &item);
3734 if(nCBORError != QCBOR_SUCCESS) {
3735 return 13;
3736 }
3737
3738 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3739 item.val.expAndMantissa.nExponent != 1000 ||
3740 item.val.expAndMantissa.Mantissa.nInt != 999) {
3741 return 15;
3742 }
3743
3744 nCBORError = QCBORDecode_GetNext(&DC, &item);
3745 if(nCBORError != QCBOR_SUCCESS) {
3746 return 13;
3747 }
3748
3749 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3750 item.val.expAndMantissa.nExponent != INT32_MIN ||
3751 item.val.expAndMantissa.Mantissa.nInt != 100) {
3752 return 15;
3753 }
3754
3755 nCBORError = QCBORDecode_GetNext(&DC, &item);
3756 if(nCBORError != QCBOR_SUCCESS) {
3757 return 13;
3758 }
3759
3760 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3761 item.val.expAndMantissa.nExponent != INT32_MAX ||
3762 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3763 return 12;
3764 }
3765
3766 return 0;
3767}
3768
3769
3770static struct FailInput ExponentAndMantissaFailures[] = {
3771 // Exponent > INT64_MAX
3772 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3773 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3774 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3775 // Mantissa > INT64_MAX
3776 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3777 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3778 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3779 // End of input
3780 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END},
3781 // End of input
3782 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END},
3783 // bad content for big num
3784 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3785 // bad content for big num
3786 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3787 // Bad integer for exponent
3788 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3789 // Bad integer for mantissa
3790 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3791 // 3 items in array
3792 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3793 // unterminated indefinite length array
3794 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3795 // Empty array
3796 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3797 // Second is not an integer
3798 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3799 // First is not an integer
3800 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3801 // Not an array
3802 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3803};
3804
3805
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003806int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003807{
3808 return ProcessFailures(ExponentAndMantissaFailures,
3809 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3810}
3811
3812#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003813
3814
3815
3816/*
3817 Some basic CBOR with map and array used in a lot of tests.
3818 The map labels are all strings
3819
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003820 {
3821 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003822 "an array of two strings": [
3823 "string1", "string2"
3824 ],
3825 "map in a map": {
3826 "bytes 1": h'78787878',
3827 "bytes 2": h'79797979',
3828 "another int": 98,
3829 "text 2": "lies, damn lies and statistics"
3830 }
3831 }
3832 */
3833
Laurence Lundbladeeb0b53d2020-04-14 18:34:25 -07003834#include "qcbor/qcbor_decode_map.h"
Laurence Lundblade1341c592020-04-11 14:19:05 -07003835#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003836
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003837static char strbuf[10];
3838const char *PrintType(uint8_t type) {
3839 switch(type) {
3840 case QCBOR_TYPE_INT64: return "INT64";
3841 case QCBOR_TYPE_UINT64: return "UINT64";
3842 case QCBOR_TYPE_ARRAY: return "ARRAY";
3843 case QCBOR_TYPE_MAP: return "MAP";
3844 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
3845 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
3846 default:
3847 sprintf(strbuf, "%d", type);
3848 return strbuf;
3849 }
3850}
3851
3852
3853void PrintItem(QCBORItem Item)
3854{
3855 printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":"");
3856 if(Item.uLabelType) {
3857 printf("Label: %s ", PrintType(Item.uLabelType));
3858 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3859 printf("%lld\n", Item.label.int64);
3860 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
3861 printf("\"%4.4s\"\n", Item.label.string.ptr);
3862 }
3863 }
3864}
3865
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003866
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003867int32_t EnterMapTest()
3868{
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003869 QCBORItem Item1, Item2, Item3;
3870 int64_t nDecodedInt1, nDecodedInt2;
3871 UsefulBufC B1, B2, S1, S2, S3;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003872
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003873 QCBORDecodeContext DCtx;
3874 QCBORError nCBORError;
3875
3876 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003877
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003878 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003879
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003880 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003881
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003882 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003883 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
3884 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1);
3885 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2);
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003886 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
3887 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003888
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003889 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3890 QCBORDecode_GetNext(&DCtx, &Item1);
3891 QCBORDecode_GetNext(&DCtx, &Item2);
3892 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
3893 return -400;
3894 }
3895 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade3f9ef042020-04-14 13:15:51 -07003896
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003897 // Parse the same array again using GetText() instead of GetItem()
3898 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3899 QCBORDecode_GetText(&DCtx, &S2);
3900 QCBORDecode_GetText(&DCtx, &S3);
3901 // TODO, check for end of array?
3902 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003903
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003904 QCBORDecode_ExitMap(&DCtx);
3905
3906 nCBORError = QCBORDecode_Finish(&DCtx);
3907
3908 if(nCBORError) {
3909 return (int32_t)nCBORError;
3910 }
3911
3912 if(nDecodedInt1 != 42) {
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003913 return 1001;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003914 }
3915
Laurence Lundblade1341c592020-04-11 14:19:05 -07003916 if(nDecodedInt2 != 98) {
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003917 return 1002;
3918 }
3919
3920 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
3921 UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){
3922 return 1003;
3923 }
3924
3925 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
3926 UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){
3927 return 1004;
3928 }
3929
3930 if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){
3931 return 1005;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003932 }
3933
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003934 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
3935 return 1006;
3936 }
3937
3938 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
3939 return 1007;
3940 }
3941
3942 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
3943 return 1008;
3944 }
3945
3946 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
3947 return 1009;
3948 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07003949
3950 // These tests confirm the cursor is at the right place after entering a map or array
3951
3952 // Confirm cursor is at right place
3953 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
3954 QCBORDecode_EnterMap(&DCtx);
3955 QCBORDecode_GetNext(&DCtx, &Item1);
3956 if(Item1.uDataType != QCBOR_TYPE_INT64) {
3957 return 2001;
3958 }
3959
3960
3961 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
3962 QCBORDecode_GetNext(&DCtx, &Item1);
3963 QCBORDecode_GetNext(&DCtx, &Item1);
3964 QCBORDecode_EnterArray(&DCtx);
3965 QCBORDecode_GetNext(&DCtx, &Item1);
3966 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
3967 return 2002;
3968 }
3969
3970 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
3971 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07003972 QCBORDecode_GetNext(&DCtx, &Item1);
3973 QCBORDecode_GetNext(&DCtx, &Item1);
3974 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07003975 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
3976 QCBORDecode_GetNext(&DCtx, &Item1);
3977 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
3978 return 2003;
3979 }
3980
3981 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
3982 QCBORDecode_EnterMap(&DCtx);
3983 QCBORDecode_GetNext(&DCtx, &Item1);
3984 QCBORDecode_GetNext(&DCtx, &Item1);
3985 QCBORDecode_GetNext(&DCtx, &Item1);
3986 QCBORDecode_GetNext(&DCtx, &Item1);
3987 QCBORDecode_GetNext(&DCtx, &Item1);
3988 QCBORDecode_GetNext(&DCtx, &Item1);
3989 QCBORDecode_GetNext(&DCtx, &Item1);
3990 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3991 QCBORDecode_GetNext(&DCtx, &Item1);
3992 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07003993 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07003994 }
3995
3996
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003997 return 0;
3998}
3999
4000
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004001struct NumberConversion {
4002 char *szDescription;
4003 UsefulBufC CBOR;
4004 int64_t nConvertedToInt64;
4005 QCBORError uErrorInt64;
4006 uint64_t uConvertToUInt64;
4007 QCBORError uErrorUint64;
4008 double dConvertToDouble;
4009 QCBORError uErrorDouble;
4010};
4011
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004012static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004013 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004014 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004015 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4016 0xC2, 0x42, 0x01, 0x01}, 15},
4017 257000,
4018 QCBOR_SUCCESS,
4019 257000,
4020 QCBOR_SUCCESS,
4021 257000.0,
4022 QCBOR_SUCCESS
4023 },
4024 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004025 "bigfloat with negative bignum -257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004026 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4027 0xC3, 0x42, 0x01, 0x01}, 15},
4028 -2056,
4029 QCBOR_SUCCESS,
4030 0,
4031 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4032 -2056.0,
4033 QCBOR_SUCCESS
4034 },
4035 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004036 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004037 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4038 0xC2, 0x42, 0x01, 0x01}, 15},
4039 2056,
4040 QCBOR_SUCCESS,
4041 2056,
4042 QCBOR_SUCCESS,
4043 2056.0,
4044 QCBOR_SUCCESS
4045 },
4046 {
4047 "negative bignum 0xc349010000000000000000",
4048 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4049 0,
4050 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4051 0,
4052 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4053 -18446744073709551617.0,
4054 QCBOR_SUCCESS
4055 },
4056 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004057 "Positive bignum 0x01020304 indefinite length string",
4058 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
4059 0x01020304,
4060 QCBOR_SUCCESS,
4061 0x01020304,
4062 QCBOR_SUCCESS,
4063 16909060.0,
4064 QCBOR_SUCCESS
4065 },
4066 {
Laurence Lundblade887add82020-05-17 05:50:34 -07004067 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07004068 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4069 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
4070 0,
4071 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4072 0,
4073 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4074 -INFINITY,
4075 QCBOR_SUCCESS
4076 },
4077 {
4078 "big float [9223372036854775806, 9223372036854775806]",
4079 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4080 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
4081 0,
4082 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4083 0,
4084 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4085 INFINITY,
4086 QCBOR_SUCCESS
4087 },
4088 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004089 "Big float 3 * 2^^2",
4090 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
4091 12,
4092 QCBOR_SUCCESS,
4093 12,
4094 QCBOR_SUCCESS,
4095 12.0,
4096 QCBOR_SUCCESS
4097 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004098 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004099 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004100 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4101 0,
4102 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4103 18446744073709551615ULL,
4104 QCBOR_SUCCESS,
4105 18446744073709551615.0,
4106 QCBOR_SUCCESS
4107 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004108 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004109 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004110 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4111 65536-1,
4112 QCBOR_SUCCESS,
4113 0xffff,
4114 QCBOR_SUCCESS,
4115 65535.0,
4116 QCBOR_SUCCESS
4117 },
4118 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004119 "Postive integer 0",
4120 {(uint8_t[]){0x0}, 1},
4121 0LL,
4122 QCBOR_SUCCESS,
4123 0ULL,
4124 QCBOR_SUCCESS,
4125 0.0,
4126 QCBOR_SUCCESS
4127 },
4128 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004129 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004130 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4131 -9223372036854775807-1, // INT64_MIN
4132 QCBOR_SUCCESS,
4133 0ULL,
4134 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4135 -9223372036854775808.0,
4136 QCBOR_SUCCESS
4137 },
4138 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004139 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004140 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
4141 100L,
4142 QCBOR_SUCCESS,
4143 100ULL,
4144 QCBOR_SUCCESS,
4145 100.3,
4146 QCBOR_SUCCESS
4147 },
4148 {
4149 "Floating point value NaN 0xfa7fc00000",
4150 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
4151 0,
4152 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4153 0,
4154 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4155 NAN,
4156 QCBOR_SUCCESS
4157 },
4158 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004159 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004160 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
4161 -4,
4162 QCBOR_SUCCESS,
4163 0,
4164 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4165 -4.0,
4166 QCBOR_SUCCESS
4167 },
4168 {
4169 "Decimal fraction 3/10",
4170 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
4171 0,
4172 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4173 0,
4174 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4175 0.30000000000000004,
4176 QCBOR_SUCCESS
4177 }
4178};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004179
4180
4181
Laurence Lundblade313b2862020-05-16 01:23:06 -07004182int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004183{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004184 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004185
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004186 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
4187 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004188
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004189 // Set up the decoding context including a memory pool so that
4190 // indefinite length items can be checked
4191 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004192 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004193
4194 /* ----- test conversion to int64_t ------ */
4195 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004196 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4197 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004198 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004199 }
4200
4201 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004202 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004203 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004204 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004205 }
4206 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004207 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004208 }
4209
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004210 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004211 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4212 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4213 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004214 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004215 }
4216 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004217 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004218 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004219 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004220 }
4221 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004222 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004223 }
4224
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004225 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004226 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4227 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4228 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004229 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004230 }
4231 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004232 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004233 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004234 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004235 }
4236 if(pF->uErrorDouble == QCBOR_SUCCESS) {
4237 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004238 // NaN's can't be compared for equality. A NaN is
4239 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004240 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004241 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004242 }
4243 } else {
4244 // TODO: this comparison may need a margin of error
4245 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004246 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004247 }
4248 }
4249 }
4250 }
4251
4252 return 0;
4253}
4254
Laurence Lundblade313b2862020-05-16 01:23:06 -07004255int32_t IntegerConvertTestOld()
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004256{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004257 QCBORDecodeContext DCtx;
4258 QCBORError nCBORError;
4259
Laurence Lundbladea826c502020-05-10 21:07:00 -07004260 /* exponent, mantissa
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004261 [
4262 4([-1, 3]),
4263 4([-20, 4759477275222530853136]),
4264 4([9223372036854775807, -4759477275222530853137]),
4265 5([300, 100]),
4266 5([-20, 4759477275222530853136]),
4267 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004268 5([ 9223372036854775806, -4759477275222530853137])
4269 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004270 ]
4271 */
4272
4273 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4274
4275 QCBORItem Item;
4276 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4277 if(nCBORError) {
4278 return -1;
4279 }
4280
4281 int64_t integer;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004282 // 4([-1, 3]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004283 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004284 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004285 return -2;
4286 }
4287 DCtx.uLastError = 0; // TODO: a method for this
4288
Laurence Lundbladea826c502020-05-10 21:07:00 -07004289 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004290 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004291 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004292 return -2;
4293 }
4294 DCtx.uLastError = 0; // TODO: a method for this
4295
Laurence Lundbladea826c502020-05-10 21:07:00 -07004296 // 4([9223372036854775807, -4759477275222530853137]),
4297 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004298 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004299 return -2;
4300 }
4301 DCtx.uLastError = 0; // TODO: a method for this
4302
4303 // 5([300, 100]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004304 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004305 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004306 return -2;
4307 }
4308 DCtx.uLastError = 0; // TODO: a method for this
4309
Laurence Lundbladea826c502020-05-10 21:07:00 -07004310 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004311 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004312 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004313 return -2;
4314 }
4315 DCtx.uLastError = 0; // TODO: a method for this
4316
Laurence Lundbladea826c502020-05-10 21:07:00 -07004317 // 5([-9223372036854775807, -4759477275222530853137])
4318 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004319 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004320 return -2;
4321 }
4322 DCtx.uLastError = 0; // TODO: a method for this
4323
4324 // 5([ 9223372036854775806, -4759477275222530853137])
4325 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004326 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004327 return -2;
4328 }
4329 DCtx.uLastError = 0; // TODO: a method for this
4330
4331 // 5([ 9223372036854775806, 9223372036854775806])]
4332 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004333 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004334 return -2;
4335 }
4336 DCtx.uLastError = 0; // TODO: a method for this
4337
4338
4339
4340 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4341
4342 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4343 if(nCBORError) {
4344 return -1;
4345 }
4346
4347 uint64_t uinteger;
4348 // 4([-1, 3]),
4349 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004350 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004351 return -2;
4352 }
4353 DCtx.uLastError = 0; // TODO: a method for this
4354
4355 // 4([-20, 4759477275222530853136]),
4356 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004357 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004358 return -2;
4359 }
4360 DCtx.uLastError = 0; // TODO: a method for this
4361
4362 // 4([9223372036854775807, -4759477275222530853137]),
4363 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004364 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004365 return -2;
4366 }
4367 DCtx.uLastError = 0; // TODO: a method for this
4368
4369 // 5([300, 100]),
4370 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004371 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004372 return -2;
4373 }
4374 DCtx.uLastError = 0; // TODO: a method for this
4375
4376 // 5([-20, 4759477275222530853136]),
4377 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004378 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004379 return -2;
4380 }
4381 DCtx.uLastError = 0; // TODO: a method for this
4382
4383 // 5([-9223372036854775807, -4759477275222530853137])
4384 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004385 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004386 return -2;
4387 }
4388 DCtx.uLastError = 0; // TODO: a method for this
4389
4390 // 5([ 9223372036854775806, -4759477275222530853137])
4391 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004392 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004393 return -2;
4394 }
4395 DCtx.uLastError = 0; // TODO: a method for this
4396
4397 // 5([ 9223372036854775806, 9223372036854775806])]
4398 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004399 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004400 return -2;
4401 }
4402 DCtx.uLastError = 0; // TODO: a method for this
4403
4404
4405
4406 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4407 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4408 if(nCBORError) {
4409 return -1;
4410 }
4411
4412 double dResult;
4413 // 4([-1, 3]),
4414 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004415 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004416 dResult != 0.3) {
4417 return -2;
4418 }
4419
4420 // 4([-20, 4759477275222530853136]),
4421 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004422 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004423 dResult != 47.408855671161923) {
4424 return -2;
4425 }
4426
4427 // 4([9223372036854775807, -4759477275222530853137]),
4428 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004429 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004430 dResult != -INFINITY) {
4431 return -2;
4432 }
4433
4434 // 5([300, 100]),
4435 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004436 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004437 dResult != -INFINITY) {
4438 return -2;
4439 }
4440
4441 // 5([-20, 4759477275222530853136]),
4442 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004443 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004444 dResult != 4521260802379792.0) {
4445 return -2;
4446 }
4447
4448 // 5([-9223372036854775807, -4759477275222530853137])
4449 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004450 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004451 dResult != -0.0) {
4452 return -2;
4453 }
4454
4455 // 5([9223372036854775806, 9223372036854775806])]
4456 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004457 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004458 dResult != INFINITY) {
4459 return -2;
4460 }
4461
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004462 return 0;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004463}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07004464
4465
Laurence Lundbladee3553422020-05-02 11:11:17 -07004466int32_t CBORSequenceDecodeTests(void)
4467{
4468 QCBORDecodeContext DCtx;
4469 QCBORItem Item;
4470 QCBORError uCBORError;
4471
4472 // --- Test a sequence with extra bytes ---
4473
4474 // The input for the date test happens to be a sequence so it
4475 // is reused. It is a sequence because it doesn't start as
4476 // an array or map.
4477 QCBORDecode_Init(&DCtx,
4478 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
4479 QCBOR_DECODE_MODE_NORMAL);
4480
4481 // Get the first item
4482 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4483 if(uCBORError != QCBOR_SUCCESS) {
4484 return 1;
4485 }
4486 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
4487 return 2;
4488 }
4489
4490 // Get a second item
4491 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4492 if(uCBORError != QCBOR_SUCCESS) {
4493 return 2;
4494 }
4495 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
4496 return 3;
4497 }
4498
4499 // A sequence can have stuff at the end that may
4500 // or may not be valid CBOR. The protocol decoder knows
4501 // when to stop by definition of the protocol, not
4502 // when the top-level map or array is ended.
4503 // Finish still has to be called to know that
4504 // maps and arrays (if there were any) were closed
4505 // off correctly. When called like this it
4506 // must return the error QCBOR_ERR_EXTRA_BYTES.
4507 uCBORError = QCBORDecode_Finish(&DCtx);
4508 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
4509 return 4;
4510 }
4511
4512
4513 // --- Test an empty input ----
4514 uint8_t empty[1];
4515 UsefulBufC Empty = {empty, 0};
4516 QCBORDecode_Init(&DCtx,
4517 Empty,
4518 QCBOR_DECODE_MODE_NORMAL);
4519
4520 uCBORError = QCBORDecode_Finish(&DCtx);
4521 if(uCBORError != QCBOR_SUCCESS) {
4522 return 5;
4523 }
4524
4525
4526 // --- Sequence with unclosed indefinite length array ---
4527 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
4528
4529 QCBORDecode_Init(&DCtx,
4530 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
4531 QCBOR_DECODE_MODE_NORMAL);
4532
4533 // Get the first item
4534 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4535 if(uCBORError != QCBOR_SUCCESS) {
4536 return 7;
4537 }
4538 if(Item.uDataType != QCBOR_TYPE_INT64) {
4539 return 8;
4540 }
4541
4542 // Get a second item
4543 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4544 if(uCBORError != QCBOR_SUCCESS) {
4545 return 9;
4546 }
4547 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4548 return 10;
4549 }
4550
4551 // Try to finish before consuming all bytes to confirm
4552 // that the still-open error is returned.
4553 uCBORError = QCBORDecode_Finish(&DCtx);
4554 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
4555 return 11;
4556 }
4557
4558
4559 // --- Sequence with a closed indefinite length array ---
4560 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
4561
4562 QCBORDecode_Init(&DCtx,
4563 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
4564 QCBOR_DECODE_MODE_NORMAL);
4565
4566 // Get the first item
4567 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4568 if(uCBORError != QCBOR_SUCCESS) {
4569 return 12;
4570 }
4571 if(Item.uDataType != QCBOR_TYPE_INT64) {
4572 return 13;
4573 }
4574
4575 // Get a second item
4576 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4577 if(uCBORError != QCBOR_SUCCESS) {
4578 return 14;
4579 }
4580 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4581 return 15;
4582 }
4583
4584 // Try to finish before consuming all bytes to confirm
4585 // that the still-open error is returned.
4586 uCBORError = QCBORDecode_Finish(&DCtx);
4587 if(uCBORError != QCBOR_SUCCESS) {
4588 return 16;
4589 }
4590
4591
4592 return 0;
4593}