blob: 49cf76a1cb38b93fcfe654b39d86d29ff603450b [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070036#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080037#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080038#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070039#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080040
Laurence Lundblade9b334962020-08-27 10:55:53 -070041// Handy macro to compare a UsefulBuf to a C string
42#define UsefulBufCompareToSZ(x, y) \
43 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044
Laurence Lundbladea2e29072018-12-30 09:20:06 -080045#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080046#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080047
48static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080049{
50 if(szLabel) {
51 printf("%s ", szLabel);
52 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080053
Laurence Lundblade570fab52018-10-13 18:28:27 +080054 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080055 for(i = 0; i < Buf.len; i++) {
56 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 printf("%02x ", Z);
58 }
59 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080060
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 fflush(stdout);
62}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080063#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064
65
Laurence Lundblade9b334962020-08-27 10:55:53 -070066// TODO: error handling for well-formed CBOR that is invalid or hits an implementation limit
67
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070068static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080069 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
71 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
72 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
73 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
74 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
75 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
76 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
77 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
78 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
79 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
80 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
81 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
82 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
83 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
84 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
85 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
86 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
87 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
88 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
89 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
90 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
91 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
92 0xff, 0xff};
93
94
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080095// return CBOR error or -1 if type of value doesn't match
96
Laurence Lundbladec5fef682020-01-25 11:38:45 -080097static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080098{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070099 QCBORItem Item;
100 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800101
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700103 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800104 if(Item.uDataType != QCBOR_TYPE_ARRAY)
105 return -1;
106
107 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700108 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800109 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800110 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800111 return -1;
112
113 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 != -4294967297)
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 != -4294967296)
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 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800131 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700132 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800134 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800136
137
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 != -2147483648)
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 != -2147483647)
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 != -65538)
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 != -65537)
160 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800162 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700163 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164 if(Item.uDataType != QCBOR_TYPE_INT64 ||
165 Item.val.int64 != -65536)
166 return -1;
167
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700170 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171 if(Item.uDataType != QCBOR_TYPE_INT64 ||
172 Item.val.int64 != -65535)
173 return -1;
174
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800175
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700177 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800178 if(Item.uDataType != QCBOR_TYPE_INT64 ||
179 Item.val.int64 != -65534)
180 return -1;
181
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 != -257)
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 != -256)
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 != -255)
199 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700202 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800203 if(Item.uDataType != QCBOR_TYPE_INT64 ||
204 Item.val.int64 != -254)
205 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700209 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800210 if(Item.uDataType != QCBOR_TYPE_INT64 ||
211 Item.val.int64 != -25)
212 return -1;
213
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800214
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700216 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800217 if(Item.uDataType != QCBOR_TYPE_INT64 ||
218 Item.val.int64 != -24)
219 return -1;
220
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700223 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 if(Item.uDataType != QCBOR_TYPE_INT64 ||
225 Item.val.int64 != -23)
226 return -1;
227
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800228
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800229 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700230 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800231 if(Item.uDataType != QCBOR_TYPE_INT64 ||
232 Item.val.int64 != -1)
233 return -1;
234
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700237 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238 if(Item.uDataType != QCBOR_TYPE_INT64 ||
239 Item.val.int64 != 0)
240 return -1;
241
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 != 0)
247 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800248
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700250 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(Item.uDataType != QCBOR_TYPE_INT64 ||
252 Item.val.int64 != 1)
253 return -1;
254
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700257 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 if(Item.uDataType != QCBOR_TYPE_INT64 ||
259 Item.val.int64 != 22)
260 return -1;
261
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700264 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 if(Item.uDataType != QCBOR_TYPE_INT64 ||
266 Item.val.int64 != 23)
267 return -1;
268
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800269
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800270 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700271 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800272 if(Item.uDataType != QCBOR_TYPE_INT64 ||
273 Item.val.int64 != 24)
274 return -1;
275
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800276
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800277 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 != 25)
281 return -1;
282
283 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700284 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800285 if(Item.uDataType != QCBOR_TYPE_INT64 ||
286 Item.val.int64 != 26)
287 return -1;
288
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800289
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700291 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800292 if(Item.uDataType != QCBOR_TYPE_INT64 ||
293 Item.val.int64 != 254)
294 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800295
296
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700298 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800299 if(Item.uDataType != QCBOR_TYPE_INT64 ||
300 Item.val.int64 != 255)
301 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800302
303
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700305 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306 if(Item.uDataType != QCBOR_TYPE_INT64 ||
307 Item.val.int64 != 256)
308 return -1;
309
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 != 257)
315 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700318 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800319 if(Item.uDataType != QCBOR_TYPE_INT64 ||
320 Item.val.int64 != 65534)
321 return -1;
322
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800323
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700325 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800326 if(Item.uDataType != QCBOR_TYPE_INT64 ||
327 Item.val.int64 != 65535)
328 return -1;
329
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 != 65536)
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 != 65537)
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 != 65538)
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 != 2147483647)
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 != 2147483648)
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 != 2147483649)
371 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700374 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800375 if(Item.uDataType != QCBOR_TYPE_INT64 ||
376 Item.val.int64 != 4294967294)
377 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800378
379
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700381 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800382 if(Item.uDataType != QCBOR_TYPE_INT64 ||
383 Item.val.int64 != 4294967295)
384 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800385
386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700388 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800389 if(Item.uDataType != QCBOR_TYPE_INT64 ||
390 Item.val.int64 != 4294967296)
391 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800392
393
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800394 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700395 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800396 if(Item.uDataType != QCBOR_TYPE_INT64 ||
397 Item.val.int64 != 4294967297)
398 return -1;
399
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800400
401
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800402 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700403 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800404 if(Item.uDataType != QCBOR_TYPE_INT64 ||
405 Item.val.int64 != 9223372036854775807LL)
406 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800407
408
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700410 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
412 Item.val.uint64 != 18446744073709551615ULL)
413 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800414
415
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800416 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
417 return -1;
418 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800419
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800420 return 0;
421}
422
423
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800424// One less than the smallest negative integer allowed in C. Decoding
425// this should fail.
426static const uint8_t spTooSmallNegative[] = {
427 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000428};
429
430
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800431/*
432 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800433 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800435int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800436{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000437 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800438 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800439
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000440 QCBORDecode_Init(&DCtx,
441 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
442 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800443
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000444 // The really big test of all successes
445 nReturn = IntegerValuesParseTestInternal(&DCtx);
446 if(nReturn) {
447 return nReturn;
448 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800449
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000450 // The one large negative integer that can be parsed
451 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800452 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000453 QCBOR_DECODE_MODE_NORMAL);
454
455 QCBORItem item;
456 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
457 nReturn = -4000;
458 }
459
460 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800461}
462
463
464/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800465 Creates a simple CBOR array and returns it in *pEncoded. The array is
466 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800469
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 */
471
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800472static uint8_t spSimpleArrayBuffer[50];
473
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800474static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475{
476 QCBOREncodeContext ECtx;
477 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800478
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800479 *pEncoded = NULL;
480 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800483 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800484 // and last with the buffer to do the actual encoding
485 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700486 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487 QCBOREncode_OpenArray(&ECtx);
488 QCBOREncode_AddInt64(&ECtx, nInt1);
489 QCBOREncode_AddInt64(&ECtx, nInt2);
490 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
491 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
492 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800493
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800494 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800495 goto Done;
496
497 if(*pEncoded != NULL) {
498 nReturn = 0;
499 goto Done;
500 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800501
502 // Use static buffer to avoid dependency on malloc()
503 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800504 goto Done;
505 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800506 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800508 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800509
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800510Done:
511 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800512}
513
514
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800515/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800516 Some basic CBOR with map and array used in a lot of tests.
517 The map labels are all strings
518
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800519 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900520 "an array of two strings": [
521 "string1", "string2"
522 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523 "map in a map": {
524 "bytes 1": h'78787878',
525 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900526 "another int": 98,
527 "text 2": "lies, damn lies and statistics"
528 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529 }
530 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800531static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700532 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
533 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
534 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
535 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
536 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
537 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
538 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
539 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
540 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
541 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
542 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
543 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
544 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
545 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
546 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700547 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800548
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700549// Same as above, but with indefinite lengths.
550static const uint8_t pValidMapIndefEncoded[] = {
5510xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5520x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5530x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5540x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5550x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5560x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5570x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5580x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5590x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5600x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5610x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5620x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5630x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5640x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5650x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5660x73, 0xff, 0xff};
567
568
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800569static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700570 size_t nLen,
571 int64_t *pInt1,
572 int64_t *pInt2,
573 const uint8_t **pBuf3,
574 size_t *pBuf3Len,
575 const uint8_t **pBuf4,
576 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800577{
578 QCBORDecodeContext DCtx;
579 QCBORItem Item;
580 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800581
Laurence Lundbladeee851742020-01-08 08:37:05 -0800582 QCBORDecode_Init(&DCtx,
583 (UsefulBufC){pEncoded, nLen},
584 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800586 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700587 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
588 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800591
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800592 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700593 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
594 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700596 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800598
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700600 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
601 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800602 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700603 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700607 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
608 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700610 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800611 *pBuf3 = Item.val.string.ptr;
612 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800613
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800614 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700615 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
616 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700618 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800619 *pBuf4 = Item.val.string.ptr;
620 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800621
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800622 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800624Done:
625 return(nReturn);
626}
627
628
629
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800630
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800631int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800632{
633 uint8_t *pEncoded;
634 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800635
Laurence Lundblade5e390822019-01-06 12:35:01 -0800636 int64_t i1=0, i2=0;
637 size_t i3=0, i4=0;
638 const uint8_t *s3= (uint8_t *)"";
639 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800640
641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800642 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
643 return(-1);
644 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800646 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800648 if(i1 != 23 ||
649 i2 != 6000 ||
650 i3 != 8 ||
651 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530652 memcmp("galactic", s3, 8) !=0 ||
653 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654 return(-1);
655 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800656
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800657 return(0);
658}
659
660
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700661/*
662 [
663 0,
664 [],
665 [
666 [],
667 [
668 0
669 ],
670 {},
671 {
672 1: {},
673 2: {},
674 3: []
675 }
676 ]
677 ]
678 */
679static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
680 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
681
Laurence Lundblade02625d42020-06-25 14:41:41 -0700682/* Same as above, but with indefinte lengths */
683static uint8_t sEmptiesIndef[] = {
6840x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700685 0x00,
686 0x9F,
687 0xFF,
688 0x9F,
689 0x9F,
690 0xFF,
691 0x9F,
692 0x00,
693 0xFF,
694 0xBF,
695 0xFF,
696 0xBF,
697 0x01,
698 0xBF,
699 0xFF,
700 0x02,
701 0xBF,
702 0xFF,
703 0x03,
704 0x9F,
705 0xFF,
706 0xFF,
707 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700708 0xFF};
709
710
711
712static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700713{
714 QCBORDecodeContext DCtx;
715 QCBORItem Item;
716
Laurence Lundbladeee851742020-01-08 08:37:05 -0800717 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700718 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800719 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700720
721 // Array with 3 items
722 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
723 Item.uDataType != QCBOR_TYPE_ARRAY ||
724 Item.uNestingLevel != 0 ||
725 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700726 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700727 return -1;
728 }
729
730 // An integer 0
731 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
732 Item.uDataType != QCBOR_TYPE_INT64 ||
733 Item.uNestingLevel != 1 ||
734 Item.uNextNestLevel != 1 ||
735 Item.val.uint64 != 0) {
736 return -2;
737 }
738
739 // An empty array
740 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
741 Item.uDataType != QCBOR_TYPE_ARRAY ||
742 Item.uNestingLevel != 1 ||
743 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700744 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700745 return -3;
746 }
747
748 // An array with 4 items
749 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
750 Item.uDataType != QCBOR_TYPE_ARRAY ||
751 Item.uNestingLevel != 1 ||
752 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700753 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700754 return -4;
755 }
756
757 // An empty array
758 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
759 Item.uDataType != QCBOR_TYPE_ARRAY ||
760 Item.uNestingLevel != 2 ||
761 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700762 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700763 return -5;
764 }
765
766 // An array with 1 item
767 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
768 Item.uDataType != QCBOR_TYPE_ARRAY ||
769 Item.uNestingLevel != 2 ||
770 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700771 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700772 return -6;
773 }
774
775 // An integer 0
776 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
777 Item.uDataType != QCBOR_TYPE_INT64 ||
778 Item.uNestingLevel != 3 ||
779 Item.uNextNestLevel != 2 ||
780 Item.val.uint64 != 0) {
781 return -7;
782 }
783
784 // An empty map
785 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
786 Item.uDataType != QCBOR_TYPE_MAP ||
787 Item.uNestingLevel != 2 ||
788 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700789 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700790 return -8;
791 }
792
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700793 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700794 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
795 Item.uDataType != QCBOR_TYPE_MAP ||
796 Item.uNestingLevel != 2 ||
797 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700798 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700799 return -9;
800 }
801
802 // An empty map
803 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
804 Item.uDataType != QCBOR_TYPE_MAP ||
805 Item.uNestingLevel != 3 ||
806 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700807 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700808 return -10;
809 }
810
811 // An empty map
812 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
813 Item.uDataType != QCBOR_TYPE_MAP ||
814 Item.uNestingLevel != 3 ||
815 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700816 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700817 return -11;
818 }
819
820 // An empty array
821 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
822 Item.uDataType != QCBOR_TYPE_ARRAY ||
823 Item.uNestingLevel != 3 ||
824 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700825 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700826 return -12;
827 }
828
829 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
830 return -13;
831 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700832 return 0;
833}
834
835
836int32_t EmptyMapsAndArraysTest()
837{
838 int nResult;
839 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
840 true);
841 if(nResult) {
842 return nResult;
843 }
844
845 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
846 false);
847
848 if(nResult) {
849 return nResult -100;
850 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700851
852 return 0;
853}
854
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800855
Laurence Lundbladeee851742020-01-08 08:37:05 -0800856static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
857 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800858
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800859int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800860{
861 QCBORDecodeContext DCtx;
862 int nReturn = 0;
863 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800864
Laurence Lundbladeee851742020-01-08 08:37:05 -0800865 QCBORDecode_Init(&DCtx,
866 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
867 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800868
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800869 for(i = 0; i < 10; i++) {
870 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800871
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800872 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
873 Item.uDataType != QCBOR_TYPE_ARRAY ||
874 Item.uNestingLevel != i) {
875 nReturn = -1;
876 break;
877 }
878 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800879
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800880 return(nReturn);
881}
882
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700883// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800884static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
885 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
886 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
887 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800889int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800890{
891 QCBORDecodeContext DCtx;
892 int nReturn = 0;
893 int i;
894 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
896
Laurence Lundbladeee851742020-01-08 08:37:05 -0800897 QCBORDecode_Init(&DCtx,
898 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
899 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800900
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700901 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800902
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800903 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
904 Item.uDataType != QCBOR_TYPE_ARRAY ||
905 Item.uNestingLevel != i) {
906 nReturn = -1;
907 break;
908 }
909 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800910
Laurence Lundbladea9489f82020-09-12 13:50:56 -0700911 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800912 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800913
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800914 return(nReturn);
915}
916
917
918
919
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800920int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800921{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700922 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800923
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800924 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700925 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800926
Laurence Lundbladeee851742020-01-08 08:37:05 -0800927 QCBORDecode_Init(&DCtx,
928 (UsefulBufC){spExpectedEncodedInts, nNum},
929 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800931 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800932
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700933 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800934 nResult = -1;
935 goto Done;
936 }
937 }
938Done:
939 return nResult;
940}
941
942
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800943
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800944int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800945{
946 uint8_t *pEncoded;
947 int nReturn;
948 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800949
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800950 int64_t i1, i2;
951 size_t i3, i4;
952 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800953
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800955
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800956 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
957 return(-1);
958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800959
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -0700961 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
962 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800963 if(nResult == 0) {
964 nReturn = -1;
965 }
966 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800967
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800968 return(nReturn);
969}
970
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530971/*
972 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800973 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
974 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530975 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800976static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977{
978 QCBORDecodeContext DCtx;
979 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700980 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800981
Laurence Lundbladeee851742020-01-08 08:37:05 -0800982 QCBORDecode_Init(&DCtx,
983 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
984 nMode);
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.uDataType != QCBOR_TYPE_MAP ||
990 Item.val.uCount != 3)
991 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800992
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900993 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700994 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900995 }
Laurence Lundblade9b334962020-08-27 10:55:53 -0700996
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800997 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800998 Item.uDataType != QCBOR_TYPE_INT64 ||
999 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301000 Item.uDataAlloc ||
1001 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001002 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001003 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001005
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001007 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001008 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001009 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301010 Item.uDataAlloc ||
1011 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001012 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001013 Item.uDataType != QCBOR_TYPE_ARRAY ||
1014 Item.val.uCount != 2)
1015 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001016
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001017 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001018 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001019 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001020 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301021 Item.uDataAlloc ||
1022 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001023 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001024 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001025 }
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 Lundblade2ded3d92018-10-09 21:36:11 +08001030 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301031 Item.uDataAlloc ||
1032 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001033 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001034 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001036
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001037 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001038 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001039 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001040 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301041 Item.uDataAlloc ||
1042 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001043 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001045 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001046 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001047 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001048
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001049 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001050 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001051 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001052 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001053 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001054 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301055 Item.uDataAlloc ||
1056 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001057 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001058 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001060
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001061 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001062 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001063 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001064 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001065 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001066 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301067 Item.uDataAlloc ||
1068 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001069 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001071 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001072
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001073 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001074 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001075 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001076 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301077 Item.uDataAlloc ||
1078 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001079 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001080 Item.uDataType != QCBOR_TYPE_INT64 ||
1081 Item.val.int64 != 98)
1082 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001083
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001084 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001085 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001086 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001087 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001088 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301090 Item.uDataAlloc ||
1091 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001092 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001095
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001096 return 0;
1097}
1098
1099
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001100/*
1101 Decode and thoroughly check a moderately complex
1102 set of maps
1103 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001104int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001105{
1106 QCBORDecodeContext DCtx;
1107 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001108 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001109
Laurence Lundbladeee851742020-01-08 08:37:05 -08001110 QCBORDecode_Init(&DCtx,
1111 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1112 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001113
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001115 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001116 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001117 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1118 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 return -1;
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.uDataType != QCBOR_TYPE_TEXT_STRING ||
1126 Item.uDataAlloc ||
1127 Item.uLabelAlloc ||
1128 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001129 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 return -2;
1131 }
1132
1133 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_INT64 ||
1138 Item.val.int64 != 42 ||
1139 Item.uDataAlloc ||
1140 Item.uLabelAlloc) {
1141 return -3;
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 Item.uDataAlloc ||
1149 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001150 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001151 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1152 return -4;
1153 }
1154
1155 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.uDataAlloc ||
1160 Item.uLabelAlloc ||
1161 Item.uDataType != QCBOR_TYPE_ARRAY ||
1162 Item.val.uCount != 2) {
1163 return -5;
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.uDataType != QCBOR_TYPE_TEXT_STRING ||
1170 Item.val.string.len != 7 ||
1171 Item.uDataAlloc ||
1172 Item.uLabelAlloc ||
1173 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1174 return -6;
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.uDataType != QCBOR_TYPE_TEXT_STRING ||
1181 Item.uDataAlloc ||
1182 Item.uLabelAlloc ||
1183 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1184 return -7;
1185 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001186
1187
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 Item.uDataAlloc ||
1193 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001194 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 return -8;
1196 }
1197
1198 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001199 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 }
1201 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1202 Item.uDataAlloc ||
1203 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001204 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1205 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001206 return -9;
1207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001208
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001210 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001211 }
1212 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001213 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1215 Item.uDataAlloc ||
1216 Item.uLabelAlloc) {
1217 return -10;
1218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001219
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001221 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001222 }
1223 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1224 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1225 Item.uDataAlloc ||
1226 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001227 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001228 return -11;
1229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001230
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001232 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001233 }
1234 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001235 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001236 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1237 Item.uDataAlloc ||
1238 Item.uLabelAlloc) {
1239 return -12;
1240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001241
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001242 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001243 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001244 }
1245 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1246 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1247 Item.uDataAlloc ||
1248 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001249 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001250 return -13;
1251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001252
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001253 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001254 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001255 }
1256 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1257 Item.uDataAlloc ||
1258 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001259 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1261 return -14;
1262 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001263
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001264 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001265 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001266 }
1267 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1268 Item.uDataAlloc ||
1269 Item.uLabelAlloc ||
1270 Item.uDataType != QCBOR_TYPE_INT64 ||
1271 Item.val.int64 != 98) {
1272 return -15;
1273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001274
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001275 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001276 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001277 }
1278 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001279 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001280 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1281 Item.uDataAlloc ||
1282 Item.uLabelAlloc) {
1283 return -16;
1284 }
1285
1286 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001287 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001288 }
1289 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1290 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1291 Item.uDataAlloc ||
1292 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001293 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001294 return -17;
1295 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001296
1297
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001298 /*
1299 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1300 map that when interpreted as an array will be too many. Test
1301 data just has the start of the map, not all the items in the map.
1302 */
1303 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001304
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001305 QCBORDecode_Init(&DCtx,
1306 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1307 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001308
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001309 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001310 return -50;
1311 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001312
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001313 return 0;
1314}
1315
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001316
1317/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301318 Fully or partially decode pValidMapEncoded. When
1319 partially decoding check for the right error code.
1320 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301322 The partial decodes test error conditions of
1323 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001324
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301325 This could be combined with the above test
1326 and made prettier and maybe a little more
1327 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001329static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001330{
1331 QCBORDecodeContext DCtx;
1332 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001333 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001334
Laurence Lundbladeee851742020-01-08 08:37:05 -08001335 QCBORDecode_Init(&DCtx,
1336 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1337 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001338
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001339 if(nLevel < 1) {
1340 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1341 return -1;
1342 } else {
1343 return 0;
1344 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001345 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301346
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001347
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001348 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001349 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001350 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001351 if(Item.uDataType != QCBOR_TYPE_MAP ||
1352 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001353 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001354
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001355 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001356 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1357 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001358 } else {
1359 return 0;
1360 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001362
1363
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 Lundblade2ded3d92018-10-09 21:36:11 +08001368 Item.uDataType != QCBOR_TYPE_INT64 ||
1369 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001370 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001371 return -4;
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 < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001375 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1376 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001377 } 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 Lundblade9b334962020-08-27 10:55:53 -07001386 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001388 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001389 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
1392
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001393 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001394 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1395 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001396 } else {
1397 return 0;
1398 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001400
1401
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001402 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001403 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001406 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001407 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001409
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001410 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001411 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1412 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001413 } else {
1414 return 0;
1415 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001417
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001418 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001419 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001420 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001421 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001422 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001423 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001425
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001426 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001427 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1428 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001429 } else {
1430 return 0;
1431 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001432 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001433
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001434 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001435 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001436 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001438 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001439 Item.uDataType != QCBOR_TYPE_MAP ||
1440 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001441 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001442
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001443 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001444 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1445 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001446 } else {
1447 return 0;
1448 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001449 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001450
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001451 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001452 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001453 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001455 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001457 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001458 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001460
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001461 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001462 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1463 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001464 } else {
1465 return 0;
1466 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001469 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001470 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001471 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001472 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001473 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001474 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001475 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001476 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001479 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001480 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1481 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001482 } else {
1483 return 0;
1484 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001485 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001486
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001487 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001488 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001489 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001491 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492 Item.uDataType != QCBOR_TYPE_INT64 ||
1493 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001494 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001495
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001496 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001497 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1498 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001499 } else {
1500 return 0;
1501 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001502 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001503
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001504 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001505 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001506 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001507 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001508 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001509 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001510 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001511 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001512 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001513
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301514 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001515 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001517
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001518 return 0;
1519}
1520
1521
1522
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001523
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001524int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001525{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001526 // Parse a moderatly complex map structure very thoroughly
1527 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1528 if(nResult) {
1529 return nResult;
1530 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001531
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001532 // Again, but in strings-only mode. It should succeed since the input
1533 // map has only string labels.
1534 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1535 if(nResult) {
1536 return nResult;
1537 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001538
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001539 // Again, but try to finish the decoding before the end of the
1540 // input at 10 different place and see that the right error code
1541 // is returned.
1542 for(int i = 0; i < 10; i++) {
1543 nResult = ExtraBytesTest(i);
1544 if(nResult) {
1545 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001546 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001547 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001548
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001549 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001550}
1551
1552
Laurence Lundbladeee851742020-01-08 08:37:05 -08001553static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1554 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1555 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001556
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001557int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001558{
1559 QCBORDecodeContext DCtx;
1560 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001561 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001562
1563
Laurence Lundbladeee851742020-01-08 08:37:05 -08001564 QCBORDecode_Init(&DCtx,
1565 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1566 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001567
1568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001570 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001571 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1572 Item.val.uCount != 10)
1573 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001574
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001576 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001577 if(Item.uDataType != QCBOR_TYPE_FALSE)
1578 return -1;
1579
1580 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001581 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 if(Item.uDataType != QCBOR_TYPE_TRUE)
1583 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001586 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 if(Item.uDataType != QCBOR_TYPE_NULL)
1588 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001591 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001592 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1593 return -1;
1594
1595 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001596 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001597 return -1;
1598
1599 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001600 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001601 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1602 return -1;
1603
1604 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001605 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001606 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1607 return -1;
1608
Laurence Lundblade077475f2019-04-26 09:06:33 -07001609 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001610 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001611
Laurence Lundblade077475f2019-04-26 09:06:33 -07001612 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001613 return -1;
1614
Laurence Lundblade077475f2019-04-26 09:06:33 -07001615 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001616 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001617
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001618 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001619 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001620 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1621 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001622
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001623 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001624 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001625 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1626 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001627
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001628 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001629
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001630}
1631
1632
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001633int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001634{
1635 // Loop over all the not-well-formed instance of CBOR
1636 // that are test vectors in not_well_formed_cbor.h
1637 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1638 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1639 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1640 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1641
Laurence Lundbladeee851742020-01-08 08:37:05 -08001642 // Set up decoder context. String allocator needed for indefinite
1643 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001644 QCBORDecodeContext DCtx;
1645 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1646 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1647 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1648
1649 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001650 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001651 do {
1652 QCBORItem Item;
1653
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001654 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1655 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001656
1657 // Every test vector must fail with
1658 // a not-well-formed error. If not
1659 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001660 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001661 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001662 // Return index of failure in the error code
1663 return 2000 + nIterate;
1664 }
1665 }
1666 return 0;
1667}
1668
1669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001670struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001671 UsefulBufC Input;
1672 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001673};
1674
Laurence Lundblade59289e52019-12-30 13:44:37 -08001675
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001676static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001677{
1678 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1679 // Set up the decoding context including a memory pool so that
1680 // indefinite length items can be checked
1681 QCBORDecodeContext DCtx;
1682 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1683 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001684
Laurence Lundblade59289e52019-12-30 13:44:37 -08001685 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1686 if(nCBORError) {
1687 return -9;
1688 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001689
Laurence Lundblade59289e52019-12-30 13:44:37 -08001690 // Iterate until there is an error of some sort error
1691 QCBORItem Item;
1692 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001693 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001694 memset(&Item, 0x33, sizeof(Item));
1695
1696 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1697 } while(nCBORError == QCBOR_SUCCESS);
1698
1699 // Must get the expected error or the this test fails
1700 // The data and label type must also be QCBOR_TYPE_NONE
1701 if(nCBORError != pF->nError ||
1702 Item.uDataType != QCBOR_TYPE_NONE ||
1703 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001704 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001705 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001706 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001707 }
1708 }
1709
1710 return 0;
1711}
1712
1713
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001714struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001715 // Most of this is copied from not_well_formed.h. Here the error code
1716 // returned is also checked.
1717
1718 // Indefinite length strings must be closed off
1719 // An indefinite length byte string not closed off
1720 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1721 // An indefinite length text string not closed off
1722 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1723
1724
1725 // All the chunks in an indefinite length string must be of the type of indefinite length string
1726 // indefinite length byte string with text string chunk
1727 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1728 // indefinite length text string with a byte string chunk
1729 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1730 // indefinite length byte string with an positive integer chunk
1731 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1732 // indefinite length byte string with an negative integer chunk
1733 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1734 // indefinite length byte string with an array chunk
1735 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1736 // indefinite length byte string with an map chunk
1737 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1738 // indefinite length byte string with tagged integer chunk
1739 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1740 // indefinite length byte string with an simple type chunk
1741 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1742 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1743 // indefinite length text string with indefinite string inside
1744 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1745
1746
1747 // Definte length maps and arrays must be closed by having the right number of items
1748 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001749 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001750 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001751 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752 // A definte length array that is supposed to have 511 items, but has only 1
1753 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1754 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001755 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001756 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001757 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001758
1759
1760 // Indefinte length maps and arrays must be ended by a break
1761 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001762 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001764 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001766 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001767 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001768 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001769
1770
1771 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001772 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001773 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001774 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001775 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001776 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001777 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, // TODO: 23
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001778 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001779 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001780 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001781 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001782 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001783 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001784 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001785
1786
1787 // The "argument" for the data item is incomplete
1788 // Positive integer missing 1 byte argument
1789 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1790 // Positive integer missing 2 byte argument
1791 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1792 // Positive integer missing 4 byte argument
1793 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1794 // Positive integer missing 8 byte argument
1795 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1796 // Positive integer missing 1 byte of 2 byte argument
1797 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1798 // Positive integer missing 2 bytes of 4 byte argument
1799 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1800 // Positive integer missing 1 bytes of 7 byte argument
1801 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1802 // Negative integer missing 1 byte argument
1803 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1804 // Binary string missing 1 byte argument
1805 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1806 // Text string missing 1 byte argument
1807 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1808 // Array missing 1 byte argument
1809 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1810 // Map missing 1 byte argument
1811 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1812 // Tag missing 1 byte argument
1813 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1814 // Simple missing 1 byte argument
1815 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1816
1817
1818 // Breaks must not occur in definite length arrays and maps
1819 // Array of length 1 with sole member replaced by a break
1820 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1821 // Array of length 2 with 2nd member replaced by a break
1822 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1823 // Map of length 1 with sole member label replaced by a break
1824 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1825 // Map of length 1 with sole member label replaced by break
1826 // Alternate representation that some decoders handle difference
1827 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1828 // Array of length 1 with 2nd member value replaced by a break
1829 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1830 // Map of length 2 with 2nd member replaced by a break
1831 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1832
1833
1834 // Breaks must not occur on their own out of an indefinite length data item
1835 // A bare break is not well formed
1836 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1837 // A bare break after a zero length definite length array
1838 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1839 // A bare break after a zero length indefinite length map
1840 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1841
1842
1843 // Forbidden two byte encodings of simple types
1844 // Must use 0xe0 instead
1845 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1846 // Should use 0xe1 instead
1847 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1848 // Should use 0xe2 instead
1849 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1850 // Should use 0xe3 instead
1851 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1852 // Should use 0xe4 instead
1853 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1854 // Should use 0xe5 instead
1855 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1856 // Should use 0xe6 instead
1857 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1858 // Should use 0xe7 instead
1859 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1860 // Should use 0xe8 instead
1861 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1862 // Should use 0xe9 instead
1863 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1864 // Should use 0xea instead
1865 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1866 // Should use 0xeb instead
1867 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1868 // Should use 0xec instead
1869 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1870 // Should use 0xed instead
1871 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1872 // Should use 0xee instead
1873 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1874 // Should use 0xef instead
1875 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1876 // Should use 0xf0 instead
1877 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1878 // Should use 0xf1 instead
1879 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1880 // Should use 0xf2 instead
1881 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1882 // Must use 0xf3 instead
1883 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1884 // Must use 0xf4 instead
1885 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1886 // Must use 0xf5 instead
1887 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1888 // Must use 0xf6 instead
1889 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1890 // Must use 0xf7 instead
1891 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1892 // Must use 0xf8 instead
1893 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1894
1895
1896 // Integers with additional info indefinite length
1897 // Positive integer with additional info indefinite length
1898 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1899 // Negative integer with additional info indefinite length
1900 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1901 // CBOR tag with "argument" an indefinite length
1902 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1903 // CBOR tag with "argument" an indefinite length alternate vector
1904 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1905
1906
1907 // Missing bytes from a deterministic length string
1908 // A byte string is of length 1 without the 1 byte
1909 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1910 // A text string is of length 1 without the 1 byte
1911 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001912 // Byte string should have 2^32-15 bytes, but has one
1913 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1914 // Byte string should have 2^32-15 bytes, but has one
1915 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001916
1917
1918 // Use of unassigned additional information values
1919 // Major type positive integer with reserved value 28
1920 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1921 // Major type positive integer with reserved value 29
1922 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1923 // Major type positive integer with reserved value 30
1924 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1925 // Major type negative integer with reserved value 28
1926 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1927 // Major type negative integer with reserved value 29
1928 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1929 // Major type negative integer with reserved value 30
1930 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1931 // Major type byte string with reserved value 28 length
1932 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1933 // Major type byte string with reserved value 29 length
1934 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1935 // Major type byte string with reserved value 30 length
1936 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1937 // Major type text string with reserved value 28 length
1938 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1939 // Major type text string with reserved value 29 length
1940 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1941 // Major type text string with reserved value 30 length
1942 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1943 // Major type array with reserved value 28 length
1944 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1945 // Major type array with reserved value 29 length
1946 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1947 // Major type array with reserved value 30 length
1948 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1949 // Major type map with reserved value 28 length
1950 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1951 // Major type map with reserved value 29 length
1952 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1953 // Major type map with reserved value 30 length
1954 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1955 // Major type tag with reserved value 28 length
1956 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1957 // Major type tag with reserved value 29 length
1958 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1959 // Major type tag with reserved value 30 length
1960 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1961 // Major type simple with reserved value 28 length
1962 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1963 // Major type simple with reserved value 29 length
1964 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1965 // Major type simple with reserved value 30 length
1966 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1967
1968
1969 // Maps must have an even number of data items (key & value)
1970 // Map with 1 item when it should have 2
1971 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1972 // Map with 3 item when it should have 4
1973 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1974 // Map with 1 item when it should have 2
1975 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1976 // Map with 3 item when it should have 4
1977 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1978
1979
1980 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001981 // Text-based date, with an integer
1982 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1983 // Epoch date, with an byte string
1984 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1985 // tagged as both epoch and string dates
1986 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1987 // big num tagged an int, not a byte string
1988 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001989};
1990
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001991int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001993 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001994
Laurence Lundblade59289e52019-12-30 13:44:37 -08001995 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1996 if(nResult) {
1997 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001999
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002000 // Corrupt the UsefulInputBuf and see that
2001 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002002 QCBORDecodeContext DCtx;
2003 QCBORItem Item;
2004 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002005
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002006 QCBORDecode_Init(&DCtx,
2007 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2008 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002009
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002010 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2011 return (int32_t)uQCBORError;
2012 }
2013 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2014 // This wasn't supposed to happen
2015 return -1;
2016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002017
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002018 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002019
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002020 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2021 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2022 // Did not get back the error expected
2023 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002025
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002026
Laurence Lundblade98427e92020-09-28 21:33:23 -07002027 /*
2028 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2029 tests here can be performed to see that the max length
2030 error check works correctly. See DecodeBytes(). If the max
2031 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002032
Laurence Lundblade98427e92020-09-28 21:33:23 -07002033 This test will automatocally adapt the all CPU sizes
2034 through the use of SIZE_MAX.
2035 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002036
Laurence Lundblade98427e92020-09-28 21:33:23 -07002037 MakeUsefulBufOnStack( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
2038 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002039
Laurence Lundblade98427e92020-09-28 21:33:23 -07002040 // This makes a CBOR head with a text string that is very long
2041 // but doesn't fill in the bytes of the text string as that is
2042 // not needed to test this part of QCBOR.
2043 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2044
2045 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2046
2047 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2048 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002049 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002050
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002051 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002052}
2053
2054
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002055/* Try all 256 values of the byte at nLen including recursing for
2056 each of the values to try values at nLen+1 ... up to nLenMax
2057 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002058static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002059{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002060 if(nLen >= nLenMax) {
2061 return;
2062 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002063
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002064 for(int inputByte = 0; inputByte < 256; inputByte++) {
2065 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002066 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002067 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002068
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002069 // Get ready to parse
2070 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002071 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002072
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002073 // Parse by getting the next item until an error occurs
2074 // Just about every possible decoder error can occur here
2075 // The goal of this test is not to check for the correct
2076 // error since that is not really possible. It is to
2077 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002078 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002079 QCBORItem Item;
2080 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002081 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002082 break;
2083 }
2084 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002085
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002086 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002087 }
2088}
2089
2090
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002091int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002092{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002093 // Size 2 tests 64K inputs and runs quickly
2094 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002095
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002096 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002097
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002098 return 0;
2099}
2100
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002101
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002102int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002103{
2104 // size 3 tests 16 million inputs and runs OK
2105 // in seconds on fast machines. Size 4 takes
2106 // 10+ minutes and 5 half a day on fast
2107 // machines. This test is kept separate from
2108 // the others so as to no slow down the use
2109 // of them as a very frequent regression.
2110 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002111
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002112 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002113
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002114 return 0;
2115}
2116
2117
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002118static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002119 0xc0, // tag for string date
2120 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002121
Laurence Lundbladec7114722020-08-13 05:11:40 -07002122 0xc0, // tag for string date
2123 0x00, // Wrong type for a string date
2124
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002125 0xc1, // tag for epoch date
2126 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2127
Laurence Lundbladec7114722020-08-13 05:11:40 -07002128 0xc1,
2129 0x62, 'h', 'i', // wrong type tagged
2130
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002131 // CBOR_TAG_B64
Laurence Lundblade9b334962020-08-27 10:55:53 -07002132 0xcf, 0xd8, 0x22, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002133 0x1a, 0x53, 0x72, 0x4E, 0x01,
2134
2135 0xc1, // tag for epoch date
2136 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002137
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002138 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002139 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002140
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002141 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002142 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002143
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002144 0xc1, // tag for epoch date
2145 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2146 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2147
2148 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002149 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2150
2151 0xc1, // tag for epoch date
2152 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2153
2154 0xc1,
2155 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2156
2157 0xc1, // tag for epoch date
2158 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002159};
2160
2161
Laurence Lundbladec7114722020-08-13 05:11:40 -07002162
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002163// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002164#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002165static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002167 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002169 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002170
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002171 return diff > 0.0000001;
2172}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002173#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002174
2175
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002176int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002177{
2178 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002179 QCBORItem Item;
2180 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002181
Laurence Lundbladeee851742020-01-08 08:37:05 -08002182 QCBORDecode_Init(&DCtx,
2183 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2184 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002185
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002186 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002187 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002188 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002189 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002190 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002191 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002192 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002193 }
2194
Laurence Lundbladec7114722020-08-13 05:11:40 -07002195 // Wrong type for a string date
2196 uError = QCBORDecode_GetNext(&DCtx, &Item);
2197 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002198 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002199 }
2200
2201 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2202 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2203 return -4;
2204 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002205 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2206 Item.val.epochDate.nSeconds != 1400000000 ||
2207 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002208 return -5;
2209 }
2210
2211 // Wrong type for an epoch date
2212 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2213 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002215
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002216 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2217 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002218 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2219 return -7;
2220 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002221 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2222 Item.val.epochDate.nSeconds != 1400000001 ||
2223 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002224 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002225 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002226 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002227
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002228 // Epoch date that is too large for our representation
2229 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002230 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002232
Laurence Lundblade9682a532020-06-06 18:33:04 -07002233#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002234 // Epoch date in float format with fractional seconds
2235 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2236 return -10;
2237 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002238 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2239 Item.val.epochDate.nSeconds != 1 ||
2240 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002241 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002243
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002244 // Epoch date float that is too large for our representation
2245 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002246 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002247 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002248
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002249 // Epoch date double that is just slightly too large
2250 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002251 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002252 }
2253
2254 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002255 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2256 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2257 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2258 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002259 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002260 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002261
2262 // Nan
2263 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2264 return -15;
2265 }
2266
2267 // +Inifinity double-precision
2268 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2269 return -16;
2270 }
2271
2272#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2273 // -Inifinity half-precision
2274 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2275 return -17;
2276 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002277#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002278 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2279 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002280 }
2281#endif
2282
Laurence Lundbladec7114722020-08-13 05:11:40 -07002283#else
2284 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2285 return -19;
2286 }
2287 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2288 return -20;
2289 }
2290 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2291 return -21;
2292 }
2293 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2294 return -22;
2295 }
2296 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2297 return -23;
2298 }
2299 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2300 return -24;
2301 }
2302#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2303 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2304 return -25;
2305 }
2306#else
2307 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2308 return -26;
2309 }
2310#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002311
Laurence Lundbladec7114722020-08-13 05:11:40 -07002312#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002313
2314 return 0;
2315}
2316
Laurence Lundblade4b270642020-08-14 12:53:07 -07002317/*
2318 Test cases covered here. Some items cover more than one of these.
2319 positive integer (zero counts as a positive integer)
2320 negative integer
2321 half-precision float
2322 single-precision float
2323 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002324
Laurence Lundblade4b270642020-08-14 12:53:07 -07002325 float Overflow error
2326 Wrong type error for epoch
2327 Wrong type error for date string
2328 float disabled error
2329 half-precision disabled error
2330 -Infinity
2331 Slightly too large integer
2332 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002333
Laurence Lundblade4b270642020-08-14 12:53:07 -07002334 Get epoch by int
2335 Get string by int
2336 Get epoch by string
2337 Get string by string
2338 Fail to get epoch by wrong int label
2339 Fail to get string by wrong string label
2340 Fail to get epoch by string because it is invalid
2341 Fail to get epoch by int because it is invalid
2342
2343 Untagged values
2344 */
2345static uint8_t spSpiffyDateTestInput[] = {
2346 0x86,
2347
2348 0xc1,
2349 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2350
Laurence Lundbladec7114722020-08-13 05:11:40 -07002351 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002352 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2353
2354 0xc1, // tag for epoch date
2355 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2356
2357 0xc1, // tag for epoch date
2358 0x9f, 0xff, // Erroneous empty array as content for date
2359
2360 0xc0, // tag for string date
2361 0xbf, 0xff, // Erroneous empty map as content for date
2362
2363 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002364
2365 0x00,
2366 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002367 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002368
2369 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002370 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002371 0xc1, // tag for epoch date
2372 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2373
2374 // Untagged integer 0
2375 0x08,
2376 0x00,
2377
2378 // Utagged date string with string label y
2379 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002380 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002381
2382 // Untagged -1000 with label z
2383 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002384 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002385 0x39, 0x03, 0xe7,
2386
Laurence Lundbladec7114722020-08-13 05:11:40 -07002387 0x07,
2388 0xc1, // tag for epoch date
2389 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2390
Laurence Lundblade4b270642020-08-14 12:53:07 -07002391 0x05,
2392 0xc1,
2393 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2394
Laurence Lundbladec7114722020-08-13 05:11:40 -07002395 // Untagged single-precision float with value 3.14 with string label x
2396 0x61, 0x78,
2397 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2398
Laurence Lundbladec7114722020-08-13 05:11:40 -07002399 // Untagged half-precision float with value -2
2400 0x09,
2401 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002402
2403 0xff,
2404};
2405
2406int32_t SpiffyDateDecodeTest()
2407{
2408 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002409 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002410 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2411 nEpochDate4, nEpochDate6, nEpochDateFail,
2412 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002413 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002414 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002415
2416 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002417 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002418 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002419 QCBORDecode_EnterArray(&DC);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002420
Laurence Lundblade9b334962020-08-27 10:55:53 -07002421 // Too-negative float, -9.2233720368547748E+18
2422 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002423 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002424#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002425 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2426 return 1111;
2427 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002428#else
2429 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2430 return 1112;
2431 }
2432#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002433
2434 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002435 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002436 uError = QCBORDecode_GetAndResetError(&DC);
2437 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002438 return 1;
2439 }
2440
Laurence Lundblade4b270642020-08-14 12:53:07 -07002441 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002442 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002443 uError = QCBORDecode_GetAndResetError(&DC);
2444#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2445#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2446 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2447#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2448 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2449#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2450#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2451 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2452#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2453 if(uError != uExpectedforHalfMinusInfinity) {
2454 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002455 }
2456
Laurence Lundblade4b270642020-08-14 12:53:07 -07002457 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002458 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002459 uError = QCBORDecode_GetAndResetError(&DC);
2460 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2461 return 3;
2462 }
2463
2464 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002465 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002466 uError = QCBORDecode_GetAndResetError(&DC);
2467 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2468 return 4;
2469 }
2470
2471 QCBORDecode_EnterMap(&DC);
2472
2473 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002474 QCBORDecode_GetEpochDateInMapN(&DC,
2475 5,
2476 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2477 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2478 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002479#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2480 if(nEpochDate2 != -9223372036854773760LL) {
2481 return 101;
2482 }
2483#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2484 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002485 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002486 return 102;
2487 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002488#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002489
Laurence Lundblade4b270642020-08-14 12:53:07 -07002490 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002491 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2492 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002493#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2494 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002495 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002496 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002497#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2498 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002499 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002500 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002501 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002502#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2503
2504 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002505 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2506 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002507#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2508 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002509 return 103;
2510 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002511#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2512 uError = QCBORDecode_GetAndResetError(&DC);
2513 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2514 return 104;
2515 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002516#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2517
Laurence Lundblade9b334962020-08-27 10:55:53 -07002518 // A half-precision date with value -2 FFF
2519 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2520 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002521#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2522 if(nEpochDate4 != -2) {
2523 return 105;
2524 }
2525#else
2526 uError = QCBORDecode_GetAndResetError(&DC);
2527 if(uError == QCBOR_SUCCESS) {
2528 return 106;
2529 }
2530#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002531
Laurence Lundblade4b270642020-08-14 12:53:07 -07002532
2533 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002534 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2535 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2536 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002537 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002538 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002539 return 107;
2540 }
2541
2542 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002543 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2544 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002545 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002546 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002547 return 108;
2548 }
2549
2550 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002551 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2552 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2553 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002554 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002555 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002556 return 109;
2557 }
2558
2559 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002560 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2561 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002562 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002563 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002564 return 110;
2565 }
2566
2567 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002568
Laurence Lundblade4b270642020-08-14 12:53:07 -07002569 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002570 QCBORDecode_GetEpochDateInMapN(&DC,
2571 1,
2572 QCBOR_TAG_REQUIREMENT_TAG |
2573 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2574 &nEpochDate1400000000);
2575 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002576 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002577 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2578 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002579 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002580 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2581 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002582 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002583 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2584 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002585 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002586 QCBORDecode_GetEpochDateInMapSZ(&DC,
2587 "z",
2588 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2589 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2590 &nEpochDate6);
2591 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002592
2593 QCBORDecode_ExitMap(&DC);
2594 QCBORDecode_ExitArray(&DC);
2595 uError = QCBORDecode_Finish(&DC);
2596 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002597 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002598 }
2599
Laurence Lundblade9b334962020-08-27 10:55:53 -07002600 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002601 return 200;
2602 }
2603
Laurence Lundblade9b334962020-08-27 10:55:53 -07002604 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002605 return 201;
2606 }
2607
Laurence Lundblade9b334962020-08-27 10:55:53 -07002608 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002609 return 202;
2610 }
2611
Laurence Lundblade9b334962020-08-27 10:55:53 -07002612 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002613 return 203;
2614 }
2615
Laurence Lundblade9b334962020-08-27 10:55:53 -07002616 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002617 return 204;
2618 }
2619
Laurence Lundblade9b334962020-08-27 10:55:53 -07002620 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2621 return 205;
2622 }
2623
2624 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2625 return 206;
2626 }
2627
Laurence Lundbladec7114722020-08-13 05:11:40 -07002628 return 0;
2629}
2630
2631
2632
Laurence Lundblade9b334962020-08-27 10:55:53 -07002633// Input for one of the tagging tests
2634static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002635 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002636 0x81, // Array of one
2637 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2638 0x82, // Array of two that is the faction 1/3
2639 0x01,
2640 0x03,
2641
2642 /*
2643 More than 4 tags on an item 225(226(227(228(229([])))))
2644 */
2645 0xd8, 0xe1,
2646 0xd8, 0xe2,
2647 0xd8, 0xe3,
2648 0xd8, 0xe4,
2649 0xd8, 0xe5,
2650 0x80,
2651
2652 /* tag 10489608748473423768(
2653 2442302356(
2654 21590(
2655 240(
2656 []))))
2657 */
2658 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2659 0xda, 0x91, 0x92, 0x93, 0x94,
2660 0xd9, 0x54, 0x56,
2661 0xd8, 0xf0,
2662 0x80,
2663
2664 /* tag 21590(
2665 10489608748473423768(
2666 2442302357(
2667 65534(
2668 []))))
2669 */
2670 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2671 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2672 0xda, 0x91, 0x92, 0x93, 0x95,
2673 0xd9, 0xff, 0xfe,
2674 0x80,
2675
2676 /* Make sure to blow past the limit of tags that must be mapped.
2677 works in conjuntion with entries above.
2678 269488144(269488145(269488146(269488147([]))))
2679 */
2680 0xda, 0x10, 0x10, 0x10, 0x10,
2681 0xda, 0x10, 0x10, 0x10, 0x11,
2682 0xda, 0x10, 0x10, 0x10, 0x12,
2683 0xda, 0x10, 0x10, 0x10, 0x13,
2684 0x80,
2685
2686 /* An invalid decimal fraction with an additional tag */
2687 0xd9, 0xff, 0xfa,
2688 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2689 0x00, // the integer 0; should be a byte string
2690};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002691
Laurence Lundblade59289e52019-12-30 13:44:37 -08002692/*
2693 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002694 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002695 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002696static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2697 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002698
Laurence Lundblade59289e52019-12-30 13:44:37 -08002699/*
2700DB 9192939495969798 # tag(10489608748473423768)
2701 D8 88 # tag(136)
2702 C6 # tag(6)
2703 C7 # tag(7)
2704 80 # array(0)
2705*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002706static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2707 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002708
2709/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002710 55799(55799(55799({
2711 6(7(-23)): 5859837686836516696(7({
2712 7(-20): 11({
2713 17(-18): 17(17(17("Organization"))),
2714 9(-17): 773("SSG"),
2715 -15: 16(17(6(7("Confusion")))),
2716 17(-16): 17("San Diego"),
2717 17(-14): 17("US")
2718 }),
2719 23(-19): 19({
2720 -11: 9({
2721 -9: -7
2722 }),
2723 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2724 })
2725 })),
2726 16(-22): 23({
2727 11(8(7(-5))): 8(-3)
2728 })
2729 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002730 */
2731static uint8_t spCSRWithTags[] = {
2732 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2733 0xc6, 0xc7, 0x36,
2734 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2735 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2736 0xcb, 0xa5,
2737 0xd1, 0x31,
2738 0xd1, 0xd1, 0xd1, 0x6c,
2739 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2740 0xc9, 0x30,
2741 0xd9, 0x03, 0x05, 0x63,
2742 0x53, 0x53, 0x47,
2743 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002744 0xd0, 0xd1, 0xc6, 0xc7,
2745 0x69,
2746 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002747 0xd1, 0x2f,
2748 0xd1, 0x69,
2749 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2750 0xd1, 0x2d,
2751 0xd1, 0x62,
2752 0x55, 0x53,
2753 0xd7, 0x32,
2754 0xd3, 0xa2,
2755 0x2a,
2756 0xc9, 0xa1,
2757 0x28,
2758 0x26,
2759 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2760 0xcc, 0x4a,
2761 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2762 0xd0, 0x35,
2763 0xd7, 0xa1,
2764 0xcb, 0xc8, 0xc7, 0x24,
2765 0xc8, 0x22};
2766
Laurence Lundblade9b334962020-08-27 10:55:53 -07002767
2768static uint8_t spSpiffyTagInput[] = {
2769 0x9f, // Open indefinite array
2770
2771 0xc0, // tag for string date
2772 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2773
2774 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2775
2776 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2777
2778 0xd8, 0x23, // tag for regex
2779 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2780
2781 0xc0, // tag for string date
2782 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2783
2784 0xff
2785};
2786
2787
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002788static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002789
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002790
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002791int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002792{
2793 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002794 QCBORItem Item;
2795 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002796
Laurence Lundbladeee851742020-01-08 08:37:05 -08002797 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002798 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002799 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002800
Laurence Lundblade9b334962020-08-27 10:55:53 -07002801 /*
2802 This test matches the magic number tag and the fraction tag
2803 55799([...])
2804 */
2805 uError = QCBORDecode_GetNext(&DCtx, &Item);
2806 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002807 return -2;
2808 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002809 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002810 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2811 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002812 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002813
Laurence Lundblade9b334962020-08-27 10:55:53 -07002814 /*
2815 4([1,3])
2816 */
2817 uError = QCBORDecode_GetNext(&DCtx, &Item);
2818#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2819 if(uError != QCBOR_SUCCESS ||
2820 Item.uDataType != QCBOR_TYPE_ARRAY ||
2821 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2822 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2823 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2824 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2825 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2826 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2827 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002828 return -4;
2829 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002830 // consume the items in the array
2831 uError = QCBORDecode_GetNext(&DCtx, &Item);
2832 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002833
Laurence Lundblade59289e52019-12-30 13:44:37 -08002834#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002835 if(uError != QCBOR_SUCCESS ||
2836 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2837 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2838 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2839 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2840 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2841 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2842 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002843 }
2844#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002845
Laurence Lundblade9b334962020-08-27 10:55:53 -07002846 /*
2847 More than 4 tags on an item 225(226(227(228(229([])))))
2848 */
2849 uError = QCBORDecode_GetNext(&DCtx, &Item);
2850 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002851 return -6;
2852 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002853
2854 /* tag 10489608748473423768(
2855 2442302356(
2856 21590(
2857 240(
2858 []))))
2859 */
2860 uError = QCBORDecode_GetNext(&DCtx, &Item);
2861 if(uError != QCBOR_SUCCESS ||
2862 Item.uDataType != QCBOR_TYPE_ARRAY ||
2863 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2864 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2865 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2866 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002867 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002868 }
2869
2870 /* tag 21590(
2871 10489608748473423768(
2872 2442302357(
2873 21591(
2874 []))))
2875 */
2876 uError = QCBORDecode_GetNext(&DCtx, &Item);
2877 if(uError != QCBOR_SUCCESS ||
2878 Item.uDataType != QCBOR_TYPE_ARRAY ||
2879 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2880 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2881 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2882 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2883 return -8;
2884 }
2885
2886 /* Make sure to blow past the limit of tags that must be mapped.
2887 works in conjuntion with entries above.
2888 269488144(269488145(269488146(269488147([]))))
2889 */
2890 uError = QCBORDecode_GetNext(&DCtx, &Item);
2891 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2892 return -9;
2893 }
2894
2895 uError = QCBORDecode_GetNext(&DCtx, &Item);
2896 if(uError == QCBOR_SUCCESS) {
2897 return -10;
2898 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002899
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002900 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002901 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002902 // tage and then matches it. Caller-config lists are no longer
2903 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002904 QCBORDecode_Init(&DCtx,
2905 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2906 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002907 const uint64_t puList[] = {0x9192939495969798, 257};
2908 const QCBORTagListIn TL = {2, puList};
2909 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002910
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002911 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2912 return -8;
2913 }
2914 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2915 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2916 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2917 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2918 Item.val.uCount != 0) {
2919 return -9;
2920 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002921
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002922 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002923 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002924 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002925 const uint64_t puLongList[17] = {1,2,1};
2926 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002927 QCBORDecode_Init(&DCtx,
2928 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2929 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002930 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2931 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2932 return -11;
2933 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002934
Laurence Lundblade9b334962020-08-27 10:55:53 -07002935 uint64_t puTags[16];
2936 QCBORTagListOut Out = {0, 4, puTags};
2937
2938
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002939 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002940 QCBORDecode_Init(&DCtx,
2941 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2942 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002943 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2944 return -12;
2945 }
2946 if(puTags[0] != 0x9192939495969798 ||
2947 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002948 puTags[2] != 0x06 ||
2949 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002950 return -13;
2951 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002952
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002953 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002954 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002955 QCBORDecode_Init(&DCtx,
2956 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2957 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002958 QCBORTagListOut OutSmall = {0, 3, puTags};
2959 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2960 return -14;
2961 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002962
Laurence Lundblade9b334962020-08-27 10:55:53 -07002963
2964
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002965 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002966 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
2967 // It is a bit of a messy test and maybe could be improved, but
2968 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002969 QCBORDecode_Init(&DCtx,
2970 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2971 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002972 int n = CheckCSRMaps(&DCtx);
2973 if(n) {
2974 return n-2000;
2975 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002976
Laurence Lundblade59289e52019-12-30 13:44:37 -08002977 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002978 QCBORDecode_Init(&DCtx,
2979 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2980 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002981
Laurence Lundblade9b334962020-08-27 10:55:53 -07002982 /* With the spiffy decode revision, this tag list is not used.
2983 It doesn't matter if a tag is in this list or not so some
2984 tests that couldn't process a tag because it isn't in this list
2985 now can process these unlisted tags. The tests have been
2986 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002987 const uint64_t puTagList[] = {773, 1, 90599561};
2988 const QCBORTagListIn TagList = {3, puTagList};
2989 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002990
2991
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002992 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2993 return -100;
2994 }
2995 if(Item.uDataType != QCBOR_TYPE_MAP ||
2996 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2997 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2998 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2999 Item.val.uCount != 2 ||
3000 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3001 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3002 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3003 Out.uNumUsed != 3) {
3004 return -101;
3005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003007 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3008 return -102;
3009 }
3010 if(Item.uDataType != QCBOR_TYPE_MAP ||
3011 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3012 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003013 !QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003014 Item.val.uCount != 2 ||
3015 puTags[0] != 5859837686836516696 ||
3016 puTags[1] != 7 ||
3017 Out.uNumUsed != 2) {
3018 return -103;
3019 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003020
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003021 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3022 return -104;
3023 }
3024 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003025 //Item.uTagBits ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003026 Item.val.uCount != 5 ||
3027 puTags[0] != 0x0b ||
3028 Out.uNumUsed != 1) {
3029 return -105;
3030 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003031
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003032 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3033 return -106;
3034 }
3035 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3036 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3037 Item.val.string.len != 12 ||
3038 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3039 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3040 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3041 Out.uNumUsed != 3) {
3042 return -105;
3043 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003044
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003045 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3046 return -107;
3047 }
3048 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3049 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3050 Item.val.string.len != 3 ||
3051 puTags[0] != 773 ||
3052 Out.uNumUsed != 1) {
3053 return -108;
3054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003055
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003056 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3057 return -109;
3058 }
3059 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003060 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003061 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003062 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003063 puTags[3] != 7 ||
3064 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003065 return -110;
3066 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003067
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003068 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3069 return -111;
3070 }
3071 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3072 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3073 Item.val.string.len != 9 ||
3074 puTags[0] != 17 ||
3075 Out.uNumUsed != 1) {
3076 return -112;
3077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003078
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003079 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3080 return -111;
3081 }
3082 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3083 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3084 Item.val.string.len != 2 ||
3085 puTags[0] != 17 ||
3086 Out.uNumUsed != 1) {
3087 return -112;
3088 }
3089
3090 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3091 return -113;
3092 }
3093 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003094 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003095 Item.val.uCount != 2 ||
3096 puTags[0] != 19 ||
3097 Out.uNumUsed != 1) {
3098 return -114;
3099 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003100
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003101 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3102 return -115;
3103 }
3104 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003105 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003106 Item.val.uCount != 1 ||
3107 puTags[0] != 9 ||
3108 Out.uNumUsed != 1) {
3109 return -116;
3110 }
3111
3112 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3113 return -116;
3114 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003115 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003116 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003117 Out.uNumUsed != 0) {
3118 return -117;
3119 }
3120
3121 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3122 return -118;
3123 }
3124 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3125 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003126 puTags[0] != 12 ||
3127 Out.uNumUsed != 1) {
3128 return -119;
3129 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003130
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003131 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3132 return -120;
3133 }
3134 if(Item.uDataType != QCBOR_TYPE_MAP ||
3135 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3136 Item.val.uCount != 1 ||
3137 puTags[0] != 0x17 ||
3138 Out.uNumUsed != 1) {
3139 return -121;
3140 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003141
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003142 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3143 return -122;
3144 }
3145 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003146 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003147 Item.val.int64 != -3 ||
3148 puTags[0] != 8 ||
3149 Out.uNumUsed != 1) {
3150 return -123;
3151 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003152
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003153 if(QCBORDecode_Finish(&DCtx)) {
3154 return -124;
3155 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003156
3157 UsefulBufC DateString;
3158 QCBORDecode_Init(&DCtx,
3159 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3160 QCBOR_DECODE_MODE_NORMAL);
3161
3162 QCBORDecode_EnterArray(&DCtx);
3163 // tagged date string
3164 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3165 // untagged date string
3166 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3167 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3168 return 100;
3169 }
3170 // untagged byte string
3171 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3172 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3173 return 101;
3174 }
3175 // tagged regex
3176 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3177 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3178 return 102;
3179 }
3180 // tagged date string with a byte string
3181 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3182 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3183 return 103;
3184 }
3185 QCBORDecode_ExitArray(&DCtx);
3186 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3187 return 104;
3188 }
3189
3190
3191 QCBORDecode_Init(&DCtx,
3192 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3193 QCBOR_DECODE_MODE_NORMAL);
3194
3195 QCBORDecode_EnterArray(&DCtx);
3196 // tagged date string
3197 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3198 // untagged date string
3199 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3200 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3201 return 200;
3202 }
3203 // untagged byte string
3204 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3205 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3206 return 201;
3207 }
3208 // tagged regex
3209 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3210 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3211 return 202;
3212 }
3213 // tagged date string with a byte string
3214 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3215 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3216 return 203;
3217 }
3218 QCBORDecode_ExitArray(&DCtx);
3219 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3220 return 204;
3221 }
3222
3223 QCBORDecode_Init(&DCtx,
3224 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3225 QCBOR_DECODE_MODE_NORMAL);
3226
3227 QCBORDecode_EnterArray(&DCtx);
3228 // tagged date string
3229 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3230 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3231 return 300;
3232 }
3233 // untagged date string
3234 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3235 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3236 return 301;
3237 }
3238 // untagged byte string
3239 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3240 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3241 return 302;
3242 }
3243 // tagged regex
3244 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3245 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3246 return 303;
3247 }
3248 // tagged date string with a byte string
3249 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3250 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3251 return 304;
3252 }
3253 QCBORDecode_ExitArray(&DCtx);
3254 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3255 return 305;
3256 }
3257
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003258 return 0;
3259}
3260
3261
3262
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003263
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003264static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003265 0x83,
3266 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3267 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3268 0xA4,
3269 0x63, 0x42, 0x4E, 0x2B,
3270 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3271 0x18, 0x40,
3272 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3273 0x63, 0x42, 0x4E, 0x2D,
3274 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3275 0x38, 0x3F,
3276 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3277
3278
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003279static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003280
3281
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003282int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003283{
3284 QCBORDecodeContext DCtx;
3285 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003286 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003287
Laurence Lundbladeee851742020-01-08 08:37:05 -08003288 QCBORDecode_Init(&DCtx,
3289 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3290 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003291
3292
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003293 //
3294 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3295 return -1;
3296 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003297 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003299
3300 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003301 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003302 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003303 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003304 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003305 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003306 }
3307
3308 //
3309 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003310 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003311 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003312 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003313 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003315
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003316 //
3317 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003318 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003319 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003320 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003322
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003323 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003324 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003325 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3326 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003327 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003328 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003329 }
3330
3331 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003332 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003333 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3334 Item.uLabelType != QCBOR_TYPE_INT64 ||
3335 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003336 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003337 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003338 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003340 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003341 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003342 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3343 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003344 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003345 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003346 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003347
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003348 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003349 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003350 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3351 Item.uLabelType != QCBOR_TYPE_INT64 ||
3352 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003353 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003354 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003356
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003357 return 0;
3358}
3359
3360
3361
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003362static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003363 uint8_t uDataType,
3364 uint8_t uNestingLevel,
3365 uint8_t uNextNest,
3366 int64_t nLabel,
3367 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003368{
3369 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003370 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003371
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003372 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3373 if(Item.uDataType != uDataType) return -1;
3374 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003375 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3376 Item.uLabelType != QCBOR_TYPE_UINT64) {
3377 return -1;
3378 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003379 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3380 if(Item.label.int64 != nLabel) return -1;
3381 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003382 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003383 }
3384 }
3385 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303386 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003387
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003388 if(pItem) {
3389 *pItem = Item;
3390 }
3391 return 0;
3392}
3393
3394
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003395// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003396static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003397{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303398 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003399
Laurence Lundblade9b334962020-08-27 10:55:53 -07003400 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003401
Laurence Lundblade9b334962020-08-27 10:55:53 -07003402 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003403
Laurence Lundblade9b334962020-08-27 10:55:53 -07003404 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3405 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3406 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3407 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3408 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003409
Laurence Lundblade9b334962020-08-27 10:55:53 -07003410 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3411 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003412
Laurence Lundblade9b334962020-08-27 10:55:53 -07003413 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3414 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003415
Laurence Lundblade9b334962020-08-27 10:55:53 -07003416 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3417 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003418
Laurence Lundblade9b334962020-08-27 10:55:53 -07003419 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003420
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003421 return 0;
3422}
3423
3424
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003425/*
3426// cbor.me decoded output
3427{
3428 -23: {
3429 -20: {
3430 -18: "Organization",
3431 -17: "SSG",
3432 -15: "Confusion",
3433 -16: "San Diego",
3434 -14: "US"
3435 },
3436 -19: {
3437 -11: {
3438 -9: -7
3439 },
3440 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3441 }
3442 },
3443 -22: {
3444 -5: -3
3445 }
3446}
3447 */
3448
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003449
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003450static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003451 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3452 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3453 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3454 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3455 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3456 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3457 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3458 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3459 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3460
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003461int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003462{
3463 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003464
Laurence Lundbladeee851742020-01-08 08:37:05 -08003465 QCBORDecode_Init(&DCtx,
3466 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3467 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003468
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003469 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003470}
3471
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003472
3473
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003474int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003475{
3476 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003477
Laurence Lundbladeee851742020-01-08 08:37:05 -08003478 QCBORDecode_Init(&DCtx,
3479 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3480 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003481
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003482 QCBORItem Item;
3483 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003484
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003485 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3486 return -1;
3487 }
3488 if(Item.uDataType != QCBOR_TYPE_MAP) {
3489 return -2;
3490 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003491
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003492 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3493 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3494 return -3;
3495 }
3496
3497 return 0;
3498}
3499
3500
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003501// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003502static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003503 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3504 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3505 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3506 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3507 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3508 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003509 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3510 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3511 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3512 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003513
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003514int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003515{
3516 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003517
Laurence Lundbladeee851742020-01-08 08:37:05 -08003518 QCBORDecode_Init(&DCtx,
3519 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3520 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003521
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003522 return CheckCSRMaps(&DCtx);
3523}
3524
3525
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003526
Laurence Lundblade17ede402018-10-13 11:43:07 +08003527static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3528{
3529 UsefulOutBuf UOB;
3530 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003531
Laurence Lundblade17ede402018-10-13 11:43:07 +08003532 int i;
3533 for(i = 0; i < n; i++) {
3534 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3535 }
3536
3537 for(i = 0; i < n; i++) {
3538 UsefulOutBuf_AppendByte(&UOB, 0xff);
3539 }
3540 return UsefulOutBuf_OutUBuf(&UOB);
3541}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003542
3543
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003544static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003545{
3546 QCBORDecodeContext DC;
3547 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003548
Laurence Lundblade17ede402018-10-13 11:43:07 +08003549 int j;
3550 for(j = 0; j < nNestLevel; j++) {
3551 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003552 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003553 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3554 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003555 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003556 return -4;
3557 } else {
3558 return 0; // Decoding doesn't recover after an error
3559 }
3560 } else {
3561 // Should be no error
3562 if(nReturn) {
3563 return -9; // Should not have got an error
3564 }
3565 }
3566 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3567 return -7;
3568 }
3569 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003570 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003571 if(nReturn) {
3572 return -3;
3573 }
3574 return 0;
3575}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003576
3577
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003578int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003579{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303580 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003581 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003582 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003583 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003584 int nReturn = parse_indeflen_nested(Nested, i);
3585 if(nReturn) {
3586 return nReturn;
3587 }
3588 }
3589 return 0;
3590}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003591
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003592
Laurence Lundbladeee851742020-01-08 08:37:05 -08003593// [1, [2, 3]]
3594static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3595// No closing break
3596static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3597// Not enough closing breaks
3598static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3599// Too many closing breaks
3600static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3601// Unclosed indeflen inside def len
3602static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3603// confused tag
3604static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003605
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003606int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003607{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003608 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003609 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003610 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003611
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003612 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303613 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003614 QCBORDecodeContext DC;
3615 QCBORItem Item;
3616 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003617
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003618 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003619
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003620 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303621
3622 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3623 Item.uNestingLevel != 0 ||
3624 Item.uNextNestLevel != 1) {
3625 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003626 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003627
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003628 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303629 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3630 Item.uNestingLevel != 1 ||
3631 Item.uNextNestLevel != 1) {
3632 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003633 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003634
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003635 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303636 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3637 Item.uNestingLevel != 1 ||
3638 Item.uNextNestLevel != 2) {
3639 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003640 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003641
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003642 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003643 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303644 Item.uNestingLevel != 2 ||
3645 Item.uNextNestLevel != 2) {
3646 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003647 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003648
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003649 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003650 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303651 Item.uNestingLevel != 2 ||
3652 Item.uNextNestLevel != 0) {
3653 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003654 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003655
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003656 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303657 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003658 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003659
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003660 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003661 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003662
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003663 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003664
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003665 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003666
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003667 nResult = QCBORDecode_GetNext(&DC, &Item);
3668 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303669 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003670 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003671
Laurence Lundblade570fab52018-10-13 18:28:27 +08003672 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003673 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303674 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003675 }
3676
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003677
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003678 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003679 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003680
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003681 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003682
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003683 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003684
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003685 nResult = QCBORDecode_GetNext(&DC, &Item);
3686 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303687 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003688 }
3689
3690 nResult = QCBORDecode_GetNext(&DC, &Item);
3691 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303692 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003693 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003694
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003695 nResult = QCBORDecode_GetNext(&DC, &Item);
3696 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303697 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003698 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003699
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003700 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003701 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303702 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003703 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003704
3705
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003706 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003707 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003708
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003709 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003710
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003711 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003712
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003713 nResult = QCBORDecode_GetNext(&DC, &Item);
3714 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303715 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003716 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003717
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003718 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003719 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303720 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003721 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303722
Laurence Lundblade642282a2020-06-23 12:00:33 -07003723 nResult = QCBORDecode_GetNext(&DC, &Item);
3724 if(nResult != QCBOR_ERR_BAD_BREAK) {
3725 return -140;
3726 }
3727
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003728
Laurence Lundblade570fab52018-10-13 18:28:27 +08003729 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003730 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003731
Laurence Lundblade570fab52018-10-13 18:28:27 +08003732 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003733
Laurence Lundblade570fab52018-10-13 18:28:27 +08003734 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003735
Laurence Lundblade570fab52018-10-13 18:28:27 +08003736 nResult = QCBORDecode_GetNext(&DC, &Item);
3737 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303738 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003739 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003740
Laurence Lundblade570fab52018-10-13 18:28:27 +08003741 nResult = QCBORDecode_GetNext(&DC, &Item);
3742 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303743 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003744 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003745
Laurence Lundblade570fab52018-10-13 18:28:27 +08003746 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003747 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303748 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003749 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003750
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303751 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003752 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003753
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303754 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003755
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303756 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003757
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303758 nResult = QCBORDecode_GetNext(&DC, &Item);
3759 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303760 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303761 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003762
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303763 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303764 if(nResult != QCBOR_ERR_BAD_BREAK) {
3765 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303766 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003767
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003768 return 0;
3769}
3770
Laurence Lundblade17ede402018-10-13 11:43:07 +08003771
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003772static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003773 0x81, // Array of length one
3774 0x7f, // text string marked with indefinite length
3775 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3776 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3777 0xff // ending break
3778};
3779
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003780static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303781 0x81, // Array of length one
3782 0x7f, // text string marked with indefinite length
3783 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3784 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3785 0xff // ending break
3786};
3787
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003788static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303789 0x81, // Array of length one
3790 0x7f, // text string marked with indefinite length
3791 0x01, 0x02, // Not a string
3792 0xff // ending break
3793};
3794
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003795static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303796 0x81, // Array of length one
3797 0x7f, // text string marked with indefinite length
3798 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3799 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3800 // missing end of string
3801};
3802
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003803static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303804 0xa1, // Array of length one
3805 0x7f, // text string marked with indefinite length
3806 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3807 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3808 0xff, // ending break
3809 0x01 // integer being labeled.
3810};
3811
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003812/**
3813 Make an indefinite length string
3814
3815 @param Storage Storage for string, must be 144 bytes in size
3816 @return The indefinite length string
3817
3818 This makes an array with one indefinite length string that has 7 chunks
3819 from size of 1 byte up to 64 bytes.
3820 */
3821static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303822{
3823 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003824
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303825 UsefulOutBuf_Init(&UOB, Storage);
3826 UsefulOutBuf_AppendByte(&UOB, 0x81);
3827 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003828
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003829 uint8_t uStringByte = 0;
3830 // Use of type int is intentional
3831 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3832 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303833 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003834 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3835 for(int j = 0; j < uChunkSize; j++) {
3836 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3837 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303838 }
3839 }
3840 UsefulOutBuf_AppendByte(&UOB, 0xff);
3841
3842 return UsefulOutBuf_OutUBuf(&UOB);
3843}
3844
3845static int CheckBigString(UsefulBufC BigString)
3846{
3847 if(BigString.len != 255) {
3848 return 1;
3849 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003850
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303851 for(uint8_t i = 0; i < 255; i++){
3852 if(((const uint8_t *)BigString.ptr)[i] != i) {
3853 return 1;
3854 }
3855 }
3856 return 0;
3857}
3858
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303859
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003860int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303861{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303862 QCBORDecodeContext DC;
3863 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303864 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003865 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003866
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303867 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003868 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303869 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003870
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303871 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303872 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303873 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003874
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303875 if(QCBORDecode_GetNext(&DC, &Item)) {
3876 return -2;
3877 }
3878 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3879 return -3;
3880 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003881
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303882 if(QCBORDecode_GetNext(&DC, &Item)) {
3883 return -4;
3884 }
3885 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3886 return -5;
3887 }
3888 if(QCBORDecode_Finish(&DC)) {
3889 return -6;
3890 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303891
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303892 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003893 QCBORDecode_Init(&DC,
3894 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3895 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003896
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303897 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3898 return -7;
3899 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003900
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303901 if(QCBORDecode_GetNext(&DC, &Item)) {
3902 return -8;
3903 }
3904 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3905 return -9;
3906 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003907
Laurence Lundblade30816f22018-11-10 13:40:22 +07003908 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303909 return -10;
3910 }
3911
3912 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003913 QCBORDecode_Init(&DC,
3914 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3915 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003916
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303917 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3918 return -11;
3919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003920
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303921 if(QCBORDecode_GetNext(&DC, &Item)) {
3922 return -12;
3923 }
3924 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3925 return -13;
3926 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003927
Laurence Lundblade30816f22018-11-10 13:40:22 +07003928 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303929 return -14;
3930 }
3931
3932 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003933 QCBORDecode_Init(&DC,
3934 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3935 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003936
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303937 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3938 return -15;
3939 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003940
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303941 if(QCBORDecode_GetNext(&DC, &Item)) {
3942 return -16;
3943 }
3944 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3945 return -17;
3946 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003947
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303948 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3949 return -18;
3950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003951
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303952 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303953 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003954
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303955 QCBORDecode_GetNext(&DC, &Item);
3956 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303957 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003959
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303960 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303961 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003963
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303964 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003965 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303966
3967 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3968 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303969 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303970 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003971
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303972 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303973 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003974 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003975
Laurence Lundbladeee851742020-01-08 08:37:05 -08003976 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3977 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003978
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303979 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303980 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303981 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303982 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003983
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303984 QCBORDecode_GetNext(&DC, &Item);
3985 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303986 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303987 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003988 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303989 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003991
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303992 // ---- big bstr -----
3993 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003994
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303995 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3996 return -25;
3997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003998
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303999 if(QCBORDecode_GetNext(&DC, &Item)) {
4000 return -26;
4001 }
4002 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304003 return -26;
4004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004005
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304006 if(QCBORDecode_GetNext(&DC, &Item)) {
4007 return -27;
4008 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304009 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304010 return -28;
4011 }
4012 if(CheckBigString(Item.val.string)) {
4013 return -3;
4014 }
4015 if(QCBORDecode_Finish(&DC)) {
4016 return -29;
4017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004018
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304019 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004020 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004021
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304022 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4023 return -30;
4024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004025
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304026 QCBORDecode_GetNext(&DC, &Item);
4027 if(Item.uDataType != QCBOR_TYPE_MAP) {
4028 return -31;
4029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004030
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304031 if(QCBORDecode_GetNext(&DC, &Item)){
4032 return -32;
4033 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004034 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4035 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304036 Item.uDataAlloc || !Item.uLabelAlloc ||
4037 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4038 return -33;
4039 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004040
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304041 if(QCBORDecode_Finish(&DC)) {
4042 return -34;
4043 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004044
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004045 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004046}
4047
4048
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004049int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304050{
4051 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004052 QCBORError nCBORError;
4053
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004054
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304055 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004056 QCBORDecode_Init(&DC,
4057 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4058 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004059
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004060 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004061
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004062 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4063 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304064 return -1;
4065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004066
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004067 if(CheckCSRMaps(&DC)) {
4068 return -2;
4069 }
4070
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304071 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004072 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004073 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004074
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304075 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004076 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304077 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004078
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304079 QCBORItem Item1, Item2, Item3, Item4;
4080 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004081 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304082 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4083 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004084 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304085 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004086 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304087 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004088 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304089 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004090 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304091 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004092 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004093
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304094 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004095
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304096 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304097 Item1.uDataType != QCBOR_TYPE_INT64 ||
4098 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004099 Item1.uDataAlloc != 0 ||
4100 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004101 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004102 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004104
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304105
4106 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004107 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304108 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004109 Item2.uDataAlloc != 0 ||
4110 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304111 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004112 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004113
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304114 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004115 Item3.uDataAlloc == 0 ||
4116 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004117 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004118 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004119 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004120
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304121 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004122 Item4.uDataAlloc == 0 ||
4123 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004124 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004125 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004126 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004127
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304128 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004129 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004130 QCBORDecode_Init(&DC,
4131 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4132 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304133 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4134 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004135 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304136 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004137 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004138 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004139 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304140 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4141 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4142 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4143 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4144 }
4145 }
4146 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004147 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004148 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304149 }
4150
4151 return 0;
4152}
4153
Laurence Lundbladef6531662018-12-04 10:42:22 +09004154
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304155
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004156int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004157{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004158 // Set up the decoder with a tiny bit of CBOR to parse because
4159 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004160 QCBORDecodeContext DC;
4161 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4162 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004163
Laurence Lundbladef6531662018-12-04 10:42:22 +09004164 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004165 // Then fish into the internals of the decode context
4166 // to get the allocator function so it can be called directly.
4167 // Also figure out how much pool is available for use
4168 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004169 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004170 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4171 if(nError) {
4172 return -9;
4173 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004174 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4175 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4176 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004177
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004178 // First test -- ask for one more byte than available and see failure
4179 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004180 if(!UsefulBuf_IsNULL(Allocated)) {
4181 return -1;
4182 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004183
Laurence Lundbladef6531662018-12-04 10:42:22 +09004184 // Re do the set up for the next test that will do a successful alloc,
4185 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004186 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004187 pAlloc = DC.StringAllocator.pfAllocator;
4188 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4189 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004190
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004191 // Allocate one byte less than available and see success
4192 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004193 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4194 return -2;
4195 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004196 // Ask for some more and see failure
4197 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004198 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4199 return -3;
4200 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004201 // Free the first allocate, retry the second and see success
4202 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4203 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004204 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4205 return -4;
4206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004207
Laurence Lundbladef6531662018-12-04 10:42:22 +09004208 // Re do set up for next test that involves a successful alloc,
4209 // and a successful realloc and a failed realloc
4210 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004211 pAlloc = DC.StringAllocator.pfAllocator;
4212 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004213
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004214 // Allocate half the pool and see success
4215 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004216 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4217 return -5;
4218 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004219 // Reallocate to take up the whole pool and see success
4220 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004221 if(UsefulBuf_IsNULL(Allocated2)) {
4222 return -6;
4223 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004224 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004225 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4226 return -7;
4227 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004228 // Try to allocate more to be sure there is failure after a realloc
4229 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4230 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004231 return -8;
4232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004233
Laurence Lundbladef6531662018-12-04 10:42:22 +09004234 return 0;
4235}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004236
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004237
4238/* Just enough of an allocator to test configuration of one */
4239static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4240{
4241 (void)pOldMem; // unused variable
4242
4243 if(uNewSize) {
4244 // Assumes the context pointer is the buffer and
4245 // nothing too big will ever be asked for.
4246 // This is only good for this basic test!
4247 return (UsefulBuf) {pCtx, uNewSize};
4248 } else {
4249 return NULLUsefulBuf;
4250 }
4251}
4252
4253
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004254int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004255{
4256 // Set up the decoder with a tiny bit of CBOR to parse because
4257 // nothing can be done with it unless that is set up.
4258 QCBORDecodeContext DC;
4259 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4260 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4261
4262 uint8_t pAllocatorBuffer[50];
4263
4264 // This is really just to test that this call works.
4265 // The full functionality of string allocators is tested
4266 // elsewhere with the MemPool internal allocator.
4267 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4268
4269 QCBORItem Item;
4270 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4271 return -1;
4272 }
4273
4274 if(Item.uDataAlloc == 0 ||
4275 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4276 Item.val.string.ptr != pAllocatorBuffer) {
4277 return -2;
4278 }
4279
4280 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4281 return -3;
4282 }
4283
4284 return 0;
4285}
4286
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004287#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004288
Laurence Lundbladea826c502020-05-10 21:07:00 -07004289/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004290 [
4291 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004292 4([-20, 4759477275222530853136]),
4293 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004294 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004295 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004296 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004297 5([ 9223372036854775806, -4759477275222530853137])
4298 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004299 ]
4300 */
4301
4302static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004303 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004304 0xC4, 0x82, 0x20,
4305 0x03,
4306 0xC4, 0x82, 0x33,
4307 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4308 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4309 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4310 0xC5, 0x82, 0x19, 0x01, 0x2C,
4311 0x18, 0x64,
4312 0xC5, 0x82, 0x33,
4313 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4314 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4315 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004316 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4317 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004318 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4319 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4320};
4321
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004322
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004323int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004324{
4325 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004326 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004327 QCBORItem item;
4328
Laurence Lundblade17af4902020-01-07 19:11:55 -08004329 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4330 0x06, 0x07, 0x08, 0x09, 0x010};
4331 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004332
4333
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004334 QCBORDecode_Init(&DC,
4335 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4336 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004337
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004338 uErr = QCBORDecode_GetNext(&DC, &item);
4339 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004340 return 1;
4341 }
4342
4343 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4344 return 2;
4345 }
4346
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004347 uErr = QCBORDecode_GetNext(&DC, &item);
4348 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004349 return 3;
4350 }
4351
4352 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4353 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4354 item.val.expAndMantissa.nExponent != -1) {
4355 return 4;
4356 }
4357
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004358 uErr = QCBORDecode_GetNext(&DC, &item);
4359 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004360 return 5;
4361 }
4362
4363 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4364 item.val.expAndMantissa.nExponent != -20 ||
4365 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4366 return 6;
4367 }
4368
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004369 uErr = QCBORDecode_GetNext(&DC, &item);
4370 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004371 return 7;
4372 }
4373
4374 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4375 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4376 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4377 return 8;
4378 }
4379
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004380 uErr = QCBORDecode_GetNext(&DC, &item);
4381 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004382 return 9;
4383 }
4384
4385 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4386 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4387 item.val.expAndMantissa.nExponent != 300) {
4388 return 10;
4389 }
4390
Laurence Lundbladea826c502020-05-10 21:07:00 -07004391 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004392 uErr = QCBORDecode_GetNext(&DC, &item);
4393 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004394 return 11;
4395 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004396 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4397 item.val.expAndMantissa.nExponent != -20 ||
4398 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4399 return 12;
4400 }
4401
Laurence Lundbladea826c502020-05-10 21:07:00 -07004402 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004403 uErr = QCBORDecode_GetNext(&DC, &item);
4404 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004405 return 13;
4406 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004407 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4408 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4409 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4410 return 14;
4411 }
4412
Laurence Lundbladea826c502020-05-10 21:07:00 -07004413 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004414 uErr = QCBORDecode_GetNext(&DC, &item);
4415 if(uErr != QCBOR_SUCCESS) {
4416 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004417 }
4418 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4419 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4420 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004421 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004422 }
4423
Laurence Lundbladea826c502020-05-10 21:07:00 -07004424 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004425 uErr = QCBORDecode_GetNext(&DC, &item);
4426 if(uErr != QCBOR_SUCCESS) {
4427 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004428 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004429 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4430 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4431 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004432 return 18;
4433 }
4434
4435 uErr = QCBORDecode_Finish(&DC);
4436 if(uErr != QCBOR_SUCCESS) {
4437 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004438 }
4439
4440 /* Now encode some stuff and then decode it */
4441 uint8_t pBuf[40];
4442 QCBOREncodeContext EC;
4443 UsefulBufC Encoded;
4444
4445 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4446 QCBOREncode_OpenArray(&EC);
4447 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4448 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4449 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4450 QCBOREncode_CloseArray(&EC);
4451 QCBOREncode_Finish(&EC, &Encoded);
4452
4453
4454 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004455 uErr = QCBORDecode_GetNext(&DC, &item);
4456 if(uErr != QCBOR_SUCCESS) {
4457 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004458 }
4459
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004460 uErr = QCBORDecode_GetNext(&DC, &item);
4461 if(uErr != QCBOR_SUCCESS) {
4462 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004463 }
4464
4465 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4466 item.val.expAndMantissa.nExponent != 1000 ||
4467 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004468 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004469 }
4470
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004471 uErr = QCBORDecode_GetNext(&DC, &item);
4472 if(uErr != QCBOR_SUCCESS) {
4473 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004474 }
4475
4476 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4477 item.val.expAndMantissa.nExponent != INT32_MIN ||
4478 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004479 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004480 }
4481
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004482 uErr = QCBORDecode_GetNext(&DC, &item);
4483 if(uErr != QCBOR_SUCCESS) {
4484 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004485 }
4486
4487 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4488 item.val.expAndMantissa.nExponent != INT32_MAX ||
4489 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004490 return 106;
4491 }
4492
4493
4494 int64_t nExp, nMant;
4495 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4496 UsefulBufC Mant;
4497 bool bIsNeg;
4498
4499 QCBORDecode_Init(&DC,
4500 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4501 QCBOR_DECODE_MODE_NORMAL);
4502 QCBORDecode_EnterArray(&DC);
4503
4504 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004505 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004506
4507 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004508 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4509 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004510
4511 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004512 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4513 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004514
4515 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004516 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004517
4518 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004519 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4520 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004521
4522 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004523 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4524 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004525
4526 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004527 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4528 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004529
4530 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004531 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4532 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004533
4534 QCBORDecode_ExitArray(&DC);
4535
4536 uErr = QCBORDecode_Finish(&DC);
4537 if(uErr != QCBOR_SUCCESS) {
4538 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004539 }
4540
4541 return 0;
4542}
4543
4544
4545static struct FailInput ExponentAndMantissaFailures[] = {
4546 // Exponent > INT64_MAX
4547 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4548 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4549 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4550 // Mantissa > INT64_MAX
4551 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4552 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4553 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4554 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004555 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004556 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004557 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004558 // bad content for big num
4559 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4560 // bad content for big num
4561 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4562 // Bad integer for exponent
4563 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4564 // Bad integer for mantissa
4565 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4566 // 3 items in array
4567 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4568 // unterminated indefinite length array
4569 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4570 // Empty array
4571 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4572 // Second is not an integer
4573 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4574 // First is not an integer
4575 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4576 // Not an array
4577 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4578};
4579
4580
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004581int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004582{
4583 return ProcessFailures(ExponentAndMantissaFailures,
4584 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4585}
4586
4587#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004588
4589
4590
4591/*
4592 Some basic CBOR with map and array used in a lot of tests.
4593 The map labels are all strings
4594
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004595 {
4596 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004597 "an array of two strings": [
4598 "string1", "string2"
4599 ],
4600 "map in a map": {
4601 "bytes 1": h'78787878',
4602 "bytes 2": h'79797979',
4603 "another int": 98,
4604 "text 2": "lies, damn lies and statistics"
4605 }
4606 }
4607 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004608
Laurence Lundblade1341c592020-04-11 14:19:05 -07004609#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004610
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004611static char strbuf[10];
4612const char *PrintType(uint8_t type) {
4613 switch(type) {
4614 case QCBOR_TYPE_INT64: return "INT64";
4615 case QCBOR_TYPE_UINT64: return "UINT64";
4616 case QCBOR_TYPE_ARRAY: return "ARRAY";
4617 case QCBOR_TYPE_MAP: return "MAP";
4618 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
4619 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
4620 default:
4621 sprintf(strbuf, "%d", type);
4622 return strbuf;
4623 }
4624}
4625
4626
4627void PrintItem(QCBORItem Item)
4628{
Laurence Lundblade9b334962020-08-27 10:55:53 -07004629 printf("\nData: %s nest: %d,%d %s\n",
4630 PrintType(Item.uDataType),
4631 Item.uNestingLevel,
4632 Item.uNextNestLevel,
4633 Item.uDataAlloc ? "Allocated":"");
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004634 if(Item.uLabelType) {
4635 printf("Label: %s ", PrintType(Item.uLabelType));
4636 if(Item.uLabelType == QCBOR_TYPE_INT64) {
4637 printf("%lld\n", Item.label.int64);
4638 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07004639 // TODO: proper conversion to null-terminated string
4640 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004641 }
4642 }
4643}
4644
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004645
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004646int32_t EMap(UsefulBufC input)
4647{
4648 QCBORItem Item1, Item2, Item3;
4649 int64_t nDecodedInt1, nDecodedInt2;
4650 UsefulBufC B1, B2, S1, S2, S3;
4651
4652 QCBORDecodeContext DCtx;
4653 QCBORError nCBORError;
4654
4655 QCBORDecode_Init(&DCtx, input, 0);
4656
4657 QCBORDecode_EnterMap(&DCtx);
4658
4659 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4660
4661 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4662 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004663 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4664 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4665 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004666 QCBORDecode_ExitMap(&DCtx);
4667
4668 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4669 QCBORDecode_GetNext(&DCtx, &Item1);
4670 QCBORDecode_GetNext(&DCtx, &Item2);
4671 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4672 return -400;
4673 }
4674 QCBORDecode_ExitArray(&DCtx);
4675
4676 // Parse the same array again using GetText() instead of GetItem()
4677 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004678 QCBORDecode_GetTextString(&DCtx, &S2);
4679 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004680 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4681 return 5000;
4682 }
4683 /* QCBORDecode_GetText(&DCtx, &S3);
4684 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4685 return 5001;
4686 } */
4687
4688 QCBORDecode_ExitArray(&DCtx);
4689
4690 QCBORDecode_ExitMap(&DCtx);
4691
4692 nCBORError = QCBORDecode_Finish(&DCtx);
4693
4694 if(nCBORError) {
4695 return (int32_t)nCBORError;
4696 }
4697
4698 if(nDecodedInt1 != 42) {
4699 return 1001;
4700 }
4701
4702 if(nDecodedInt2 != 98) {
4703 return 1002;
4704 }
4705
4706 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004707 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004708 return 1003;
4709 }
4710
4711 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004712 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004713 return 1004;
4714 }
4715
Laurence Lundblade9b334962020-08-27 10:55:53 -07004716 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004717 return 1005;
4718 }
4719
4720 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4721 return 1006;
4722 }
4723
4724 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4725 return 1007;
4726 }
4727
4728 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4729 return 1008;
4730 }
4731
4732 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4733 return 1009;
4734 }
4735
4736 return 0;
4737}
4738
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004739
4740/*
4741 [23,
4742 6000,
4743 h'67616C6163746963',
4744 h'686176656E20746F6B656E'
4745 ]
4746 */
4747static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004748 0x84,
4749 0x17,
4750 0x19, 0x17, 0x70,
4751 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4752 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004753
4754
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004755static const uint8_t spEmptyMap[] = {0xa0};
4756
4757static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004758
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004759static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4760
Laurence Lundbladef0499502020-08-01 11:55:57 -07004761/*
4762 {
4763 0: [],
4764 9: [
4765 [],
4766 []
4767 ],
4768 8: {
4769 1: [],
4770 2: {},
4771 3: []
4772 },
4773 4: {},
4774 5: [],
4775 6: [
4776 [],
4777 []
4778 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004779 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004780 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004781
Laurence Lundbladef0499502020-08-01 11:55:57 -07004782static const uint8_t spMapOfEmpty[] = {
4783 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4784 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4785 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004786
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004787/*
4788 Too many tags
4789 Invalid tag content
4790 Duplicate label
4791 Integer overflow
4792 Date overflow
4793
4794 {1: 224(225(226(227(4(0))))),
4795 2: 1(h''),
4796 3: -18446744073709551616,
4797 4: 1(1.0e+300),
4798 5: 0, 8: 8}
4799 */
4800static const uint8_t spRecoverableMapErrors[] = {
4801 0xbf,
4802 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
4803 0x02, 0xc1, 0x40,
4804 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4805 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
4806 0x05, 0x00,
4807 0x05, 0x00,
4808 0x08, 0x08,
4809 0xff
4810};
4811
4812// Bad break
4813static const uint8_t spUnRecoverableMapError1[] = {
4814 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
4815};
4816
4817// No more items
4818static const uint8_t spUnRecoverableMapError2[] = {
4819 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
4820};
4821
4822// Hit end because string is too long
4823static const uint8_t spUnRecoverableMapError3[] = {
4824 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
4825};
4826
4827// Hit end because string is too long
4828static const uint8_t spUnRecoverableMapError4[] = {
4829 0xbf,
4830 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4831 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4832 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4833 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4834 0xff
4835};
4836
4837
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004838int32_t EnterMapTest()
4839{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004840 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004841 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004842 int32_t nReturn;
4843 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004844
4845
4846 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4847 QCBORDecode_EnterMap(&DCtx);
4848
Laurence Lundbladef0499502020-08-01 11:55:57 -07004849
4850 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004851 QCBORDecode_ExitArray(&DCtx);
4852
Laurence Lundbladef0499502020-08-01 11:55:57 -07004853 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004854 QCBORDecode_EnterArray(&DCtx);
4855 QCBORDecode_ExitArray(&DCtx);
4856 QCBORDecode_EnterArray(&DCtx);
4857 QCBORDecode_ExitArray(&DCtx);
4858 QCBORDecode_ExitArray(&DCtx);
4859
Laurence Lundbladef0499502020-08-01 11:55:57 -07004860 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004861 QCBORDecode_EnterArray(&DCtx);
4862 QCBORDecode_ExitArray(&DCtx);
4863 QCBORDecode_EnterMap(&DCtx);
4864 QCBORDecode_ExitMap(&DCtx);
4865 QCBORDecode_EnterArray(&DCtx);
4866 QCBORDecode_ExitArray(&DCtx);
4867 QCBORDecode_ExitMap(&DCtx);
4868
Laurence Lundbladef0499502020-08-01 11:55:57 -07004869 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004870 QCBORDecode_ExitMap(&DCtx);
4871
Laurence Lundbladef0499502020-08-01 11:55:57 -07004872 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004873 QCBORDecode_ExitArray(&DCtx);
4874
Laurence Lundbladef0499502020-08-01 11:55:57 -07004875 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004876 QCBORDecode_EnterArray(&DCtx);
4877 QCBORDecode_ExitArray(&DCtx);
4878 QCBORDecode_EnterArray(&DCtx);
4879 QCBORDecode_ExitArray(&DCtx);
4880 QCBORDecode_ExitArray(&DCtx);
4881
4882 QCBORDecode_ExitMap(&DCtx);
4883
4884 uErr = QCBORDecode_Finish(&DCtx);
4885 if(uErr != QCBOR_SUCCESS){
4886 return 3011;
4887 }
4888
4889
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004890 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004891 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004892 if(nReturn) {
4893 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004894 }
4895
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004896 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004897 if(nReturn) {
4898 return nReturn;
4899 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004900
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004901
Laurence Lundblade937ea812020-05-08 11:38:23 -07004902
4903 // These tests confirm the cursor is at the right place after entering a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004904 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004905
4906 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004907 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004908 QCBORDecode_EnterMap(&DCtx);
4909 QCBORDecode_GetNext(&DCtx, &Item1);
4910 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4911 return 2001;
4912 }
4913
4914
Laurence Lundblade9b334962020-08-27 10:55:53 -07004915 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004916 QCBORDecode_VGetNext(&DCtx, &Item1);
4917 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004918 QCBORDecode_EnterArray(&DCtx);
4919 QCBORDecode_GetNext(&DCtx, &Item1);
4920 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4921 return 2002;
4922 }
4923
Laurence Lundblade9b334962020-08-27 10:55:53 -07004924 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004925 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004926 QCBORDecode_GetNext(&DCtx, &Item1);
4927 QCBORDecode_GetNext(&DCtx, &Item1);
4928 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004929 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4930 QCBORDecode_GetNext(&DCtx, &Item1);
4931 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4932 return 2003;
4933 }
4934
Laurence Lundblade9b334962020-08-27 10:55:53 -07004935 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004936 QCBORDecode_EnterMap(&DCtx);
4937 QCBORDecode_GetNext(&DCtx, &Item1);
4938 QCBORDecode_GetNext(&DCtx, &Item1);
4939 QCBORDecode_GetNext(&DCtx, &Item1);
4940 QCBORDecode_GetNext(&DCtx, &Item1);
4941 QCBORDecode_GetNext(&DCtx, &Item1);
4942 QCBORDecode_GetNext(&DCtx, &Item1);
4943 QCBORDecode_GetNext(&DCtx, &Item1);
4944 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4945 QCBORDecode_GetNext(&DCtx, &Item1);
4946 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004947 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004948 }
4949
Laurence Lundblade9b334962020-08-27 10:55:53 -07004950 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004951 QCBORDecode_EnterMap(&DCtx);
4952 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4953 QCBORDecode_ExitArray(&DCtx);
4954 QCBORDecode_GetNext(&DCtx, &Item1);
4955 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4956 return 2006;
4957 }
4958 QCBORDecode_ExitMap(&DCtx);
4959 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4960 return 2007;
4961 }
4962
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004963 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4964 QCBORDecode_EnterArray(&DCtx);
4965 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004966 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4967 uErr = QCBORDecode_GetAndResetError(&DCtx);
4968 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004969 return 2008;
4970 }
4971 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004972 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004973 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004974 return 2009;
4975 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004976
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004977
4978 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4979 QCBORDecode_EnterMap(&DCtx);
4980 // This will fail because the map is empty.
4981 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4982 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004983 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004984 return 2010;
4985 }
4986 QCBORDecode_ExitMap(&DCtx);
4987 uErr = QCBORDecode_Finish(&DCtx);
4988 if(uErr != QCBOR_SUCCESS){
4989 return 2011;
4990 }
4991
4992
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004993 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4994 QCBORDecode_EnterMap(&DCtx);
4995 // This will fail because the map is empty.
4996 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4997 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004998 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004999 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005000 }
5001 QCBORDecode_ExitMap(&DCtx);
5002 uErr = QCBORDecode_Finish(&DCtx);
5003 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005004 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005005 }
5006
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005007
5008 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
5009 QCBORDecode_EnterArray(&DCtx);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005010 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005011 QCBORDecode_EnterMap(&DCtx);
5012 QCBORDecode_ExitMap(&DCtx);
5013 QCBORDecode_EnterArray(&DCtx);
5014 QCBORDecode_ExitArray(&DCtx);
5015 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5016 QCBORDecode_ExitArray(&DCtx);
5017 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005018 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005019 return 2014;
5020 }
5021
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005022 int64_t nInt;
5023 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
5024 QCBORDecode_EnterMap(&DCtx);
5025 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5026 uErr = QCBORDecode_GetAndResetError(&DCtx);
5027 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5028 return 2021;
5029 }
5030
5031 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5032 uErr = QCBORDecode_GetAndResetError(&DCtx);
5033 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5034 return 2022;
5035 }
5036
5037 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5038 uErr = QCBORDecode_GetAndResetError(&DCtx);
5039 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5040 return 2023;
5041 }
5042
5043 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5044 uErr = QCBORDecode_GetAndResetError(&DCtx);
5045#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5046 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5047 return 2024;
5048 }
5049#else
5050 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5051 return 2027;
5052 }
5053#endif
5054
5055 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5056 uErr = QCBORDecode_GetAndResetError(&DCtx);
5057 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5058 return 2025;
5059 }
5060
5061 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5062
5063 QCBORDecode_ExitMap(&DCtx);
5064 uErr = QCBORDecode_Finish(&DCtx);
5065 if(uErr != QCBOR_SUCCESS) {
5066 return 2026;
5067 }
5068
5069 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
5070 QCBORDecode_EnterMap(&DCtx);
5071 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5072 uErr = QCBORDecode_GetAndResetError(&DCtx);
5073 if(uErr != QCBOR_ERR_BAD_BREAK) {
5074 return 2030;
5075 }
5076
5077 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
5078 QCBORDecode_EnterMap(&DCtx);
5079 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5080 uErr = QCBORDecode_GetAndResetError(&DCtx);
5081 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5082 return 2031;
5083 }
5084
5085 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
5086 QCBORDecode_EnterMap(&DCtx);
5087 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5088 uErr = QCBORDecode_GetAndResetError(&DCtx);
5089 if(uErr != QCBOR_ERR_HIT_END) {
5090 return 2032;
5091 }
5092
5093 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
5094 QCBORDecode_EnterMap(&DCtx);
5095 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5096 uErr = QCBORDecode_GetAndResetError(&DCtx);
5097 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5098 return 2033;
5099 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005100
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005101 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005102}
5103
5104
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005105struct NumberConversion {
5106 char *szDescription;
5107 UsefulBufC CBOR;
5108 int64_t nConvertedToInt64;
5109 QCBORError uErrorInt64;
5110 uint64_t uConvertToUInt64;
5111 QCBORError uErrorUint64;
5112 double dConvertToDouble;
5113 QCBORError uErrorDouble;
5114};
5115
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005116static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005117 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005118 "too large to fit into int64_t",
5119 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5120 0,
5121 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5122 0,
5123 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5124 ((double)INT64_MIN) + 1 ,
5125 QCBOR_SUCCESS
5126 },
5127 {
5128 "largest negative int that fits in int64_t",
5129 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5130 INT64_MIN,
5131 QCBOR_SUCCESS,
5132 0,
5133 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5134 (double)INT64_MIN,
5135 QCBOR_SUCCESS
5136 },
5137 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005138 "negative bignum -1",
5139 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5140 -1,
5141 QCBOR_SUCCESS,
5142 0,
5143 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5144 -1.0,
5145 QCBOR_SUCCESS
5146 },
5147 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005148 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005149 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5150 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005151#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005152 257000,
5153 QCBOR_SUCCESS,
5154 257000,
5155 QCBOR_SUCCESS,
5156 257000.0,
5157 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005158#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5159 0,
5160 QCBOR_ERR_UNEXPECTED_TYPE,
5161 0,
5162 QCBOR_ERR_UNEXPECTED_TYPE,
5163 0.0,
5164 QCBOR_ERR_UNEXPECTED_TYPE
5165#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005166 },
5167 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005168 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005169 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5170 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005171#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005172 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005173 QCBOR_SUCCESS,
5174 0,
5175 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005176 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005177 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005178#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5179 0,
5180 QCBOR_ERR_UNEXPECTED_TYPE,
5181 0,
5182 QCBOR_ERR_UNEXPECTED_TYPE,
5183 0.0,
5184 QCBOR_ERR_UNEXPECTED_TYPE
5185#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005186 },
5187 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005188 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005189 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5190 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005191#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005192 2056,
5193 QCBOR_SUCCESS,
5194 2056,
5195 QCBOR_SUCCESS,
5196 2056.0,
5197 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005198#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5199 0,
5200 QCBOR_ERR_UNEXPECTED_TYPE,
5201 0,
5202 QCBOR_ERR_UNEXPECTED_TYPE,
5203 0.0,
5204 QCBOR_ERR_UNEXPECTED_TYPE
5205#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005206 },
5207 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005208 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005209 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5210 0,
5211 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5212 0,
5213 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5214 -18446744073709551617.0,
5215 QCBOR_SUCCESS
5216 },
5217 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005218 "Positive bignum 0x01020304 indefinite length string",
5219 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5220 0x01020304,
5221 QCBOR_SUCCESS,
5222 0x01020304,
5223 QCBOR_SUCCESS,
5224 16909060.0,
5225 QCBOR_SUCCESS
5226 },
5227 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005228 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005229 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5230 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005231#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005232 0,
5233 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5234 0,
5235 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5236 -INFINITY,
5237 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005238#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5239 0,
5240 QCBOR_ERR_UNEXPECTED_TYPE,
5241 0,
5242 QCBOR_ERR_UNEXPECTED_TYPE,
5243 0.0,
5244 QCBOR_ERR_UNEXPECTED_TYPE
5245#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005246 },
5247 {
5248 "big float [9223372036854775806, 9223372036854775806]",
5249 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5250 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005251#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005252 0,
5253 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5254 0,
5255 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5256 INFINITY,
5257 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005258#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5259 0,
5260 QCBOR_ERR_UNEXPECTED_TYPE,
5261 0,
5262 QCBOR_ERR_UNEXPECTED_TYPE,
5263 0.0,
5264 QCBOR_ERR_UNEXPECTED_TYPE
5265#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005266 },
5267 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005268 "Big float 3 * 2^^2",
5269 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005270#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005271 12,
5272 QCBOR_SUCCESS,
5273 12,
5274 QCBOR_SUCCESS,
5275 12.0,
5276 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005277#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5278 0,
5279 QCBOR_ERR_UNEXPECTED_TYPE,
5280 0,
5281 QCBOR_ERR_UNEXPECTED_TYPE,
5282 0.0,
5283 QCBOR_ERR_UNEXPECTED_TYPE
5284#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005285 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005286 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005287 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005288 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5289 0,
5290 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5291 18446744073709551615ULL,
5292 QCBOR_SUCCESS,
5293 18446744073709551615.0,
5294 QCBOR_SUCCESS
5295 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005296 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005297 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005298 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5299 65536-1,
5300 QCBOR_SUCCESS,
5301 0xffff,
5302 QCBOR_SUCCESS,
5303 65535.0,
5304 QCBOR_SUCCESS
5305 },
5306 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005307 "Postive integer 0",
5308 {(uint8_t[]){0x0}, 1},
5309 0LL,
5310 QCBOR_SUCCESS,
5311 0ULL,
5312 QCBOR_SUCCESS,
5313 0.0,
5314 QCBOR_SUCCESS
5315 },
5316 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005317 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005318 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5319 -9223372036854775807-1, // INT64_MIN
5320 QCBOR_SUCCESS,
5321 0ULL,
5322 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5323 -9223372036854775808.0,
5324 QCBOR_SUCCESS
5325 },
5326 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005327 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005328 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005329#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005330 100L,
5331 QCBOR_SUCCESS,
5332 100ULL,
5333 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005334#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5335 0,
5336 QCBOR_ERR_HW_FLOAT_DISABLED,
5337 0,
5338 QCBOR_ERR_HW_FLOAT_DISABLED,
5339#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005340 100.3,
5341 QCBOR_SUCCESS
5342 },
5343 {
5344 "Floating point value NaN 0xfa7fc00000",
5345 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005346#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005347 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005348 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005349 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005350 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005351#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5352 0,
5353 QCBOR_ERR_HW_FLOAT_DISABLED,
5354 0,
5355 QCBOR_ERR_HW_FLOAT_DISABLED,
5356#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005357 NAN,
5358 QCBOR_SUCCESS
5359 },
5360 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005361 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005362 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005363#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5364#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5365 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005366 -4,
5367 QCBOR_SUCCESS,
5368 0,
5369 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5370 -4.0,
5371 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005372#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5373 // Float HW disabled
5374 -4,
5375 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5376 0,
5377 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5378 -4.0,
5379 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5380#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5381#else
5382 // Half-precision disabled
5383 -4,
5384 QCBOR_ERR_HALF_PRECISION_DISABLED,
5385 0,
5386 QCBOR_ERR_HALF_PRECISION_DISABLED,
5387 -4.0,
5388 QCBOR_ERR_HALF_PRECISION_DISABLED
5389#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005390 },
5391 {
5392 "Decimal fraction 3/10",
5393 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005394#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005395 0,
5396 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5397 0,
5398 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5399 0.30000000000000004,
5400 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005401#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5402 0,
5403 QCBOR_ERR_UNEXPECTED_TYPE,
5404 0,
5405 QCBOR_ERR_UNEXPECTED_TYPE,
5406 0.0,
5407 QCBOR_ERR_UNEXPECTED_TYPE
5408#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005409 },
5410 {
5411 "+inifinity",
5412 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5413#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5414 0,
5415 QCBOR_ERR_FLOAT_EXCEPTION,
5416 0,
5417 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5418#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5419 0,
5420 QCBOR_ERR_HW_FLOAT_DISABLED,
5421 0,
5422 QCBOR_ERR_HW_FLOAT_DISABLED,
5423#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5424 INFINITY,
5425 QCBOR_SUCCESS
5426 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005427
5428 {
5429 "extreme pos bignum",
5430 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5431 // 50 rows of 8 is 400 digits.
5432 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5433 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5434 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5435 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5436 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5437 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5438 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5439 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5440 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5441 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5442 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5443 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5444 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5445 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5446 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5447 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5448 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5449 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5450 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5451 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5452 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5453 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5454 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5455 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5456 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5457 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5458 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5481 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5482 404},
5483 0,
5484 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5485 0,
5486 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5487#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5488 INFINITY,
5489 QCBOR_SUCCESS
5490#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5491 0,
5492 QCBOR_ERR_HW_FLOAT_DISABLED,
5493#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5494 },
5495
5496 {
5497 "extreme neg bignum",
5498 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5499 // 50 rows of 8 is 400 digits.
5500 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5501 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5502 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5503 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5504 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5505 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5506 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5507 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5508 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5509 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5510 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5511 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5512 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5513 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5514 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5515 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5516 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5517 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5518 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5519 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5520 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5521 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5522 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5523 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5524 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5525 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5526 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5527 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5528 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5529 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5530 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5531 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5532 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5533 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5534 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5535 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5536 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5537 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5538 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5539 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5540 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5541 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5542 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5543 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5544 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5545 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5546 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5547 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5548 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5549 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5550 404},
5551 0,
5552 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5553 0,
5554 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5555#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5556 -INFINITY,
5557 QCBOR_SUCCESS
5558#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5559 0,
5560 QCBOR_ERR_HW_FLOAT_DISABLED,
5561#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5562 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005563
5564 {
5565 "big float underflow [9223372036854775806, -9223372036854775806]",
5566 {(uint8_t[]){
5567 0xC5, 0x82,
5568 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5569 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5570#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5571 0,
5572 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5573 0,
5574 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5575 0,
5576 QCBOR_SUCCESS
5577#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5578 0,
5579 QCBOR_ERR_UNEXPECTED_TYPE,
5580 0,
5581 QCBOR_ERR_UNEXPECTED_TYPE,
5582 0.0,
5583 QCBOR_ERR_UNEXPECTED_TYPE
5584#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5585 },
5586
5587 {
5588 "bigfloat that evaluates to -INFINITY",
5589 {(uint8_t[]){
5590 0xC5, 0x82,
5591 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5592 0xC3, 0x42, 0x01, 0x01}, 15},
5593#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5594 0,
5595 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5596 0,
5597 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5598 -INFINITY,
5599 QCBOR_SUCCESS
5600#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5601 0,
5602 QCBOR_ERR_UNEXPECTED_TYPE,
5603 0,
5604 QCBOR_ERR_UNEXPECTED_TYPE,
5605 0.0,
5606 QCBOR_ERR_UNEXPECTED_TYPE
5607#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5608 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005609};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005610
5611
5612
Laurence Lundblade313b2862020-05-16 01:23:06 -07005613int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005614{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005615 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005616
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005617 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5618 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005619
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005620 // Set up the decoding context including a memory pool so that
5621 // indefinite length items can be checked
5622 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005623 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005624
5625 /* ----- test conversion to int64_t ------ */
5626 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005627 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5628 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005629 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005630 }
5631
5632 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005633 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005634 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005635 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005636 }
5637 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005638 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005639 }
5640
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005641 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005642 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5643 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5644 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005645 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005646 }
5647 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005648 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005649 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005650 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005651 }
5652 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005653 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005654 }
5655
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005656 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005657 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5658 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5659 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005660 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005661 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005662#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005663 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005664 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005665 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005666 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005667 }
5668 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5669 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005670 // NaN's can't be compared for equality. A NaN is
5671 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005672 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005673 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005674 }
5675 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005676 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005677 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005678 }
5679 }
5680 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005681#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005682 }
5683
5684 return 0;
5685}
5686
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005687
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005688
5689
Laurence Lundbladee3553422020-05-02 11:11:17 -07005690int32_t CBORSequenceDecodeTests(void)
5691{
5692 QCBORDecodeContext DCtx;
5693 QCBORItem Item;
5694 QCBORError uCBORError;
5695
5696 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005697
Laurence Lundbladee3553422020-05-02 11:11:17 -07005698 // The input for the date test happens to be a sequence so it
5699 // is reused. It is a sequence because it doesn't start as
5700 // an array or map.
5701 QCBORDecode_Init(&DCtx,
5702 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5703 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005704
Laurence Lundbladee3553422020-05-02 11:11:17 -07005705 // Get the first item
5706 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5707 if(uCBORError != QCBOR_SUCCESS) {
5708 return 1;
5709 }
5710 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5711 return 2;
5712 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005713
Laurence Lundbladee3553422020-05-02 11:11:17 -07005714 // Get a second item
5715 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005716 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5717 return 66;
5718 }
5719
5720 // Get a third item
5721 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005722 if(uCBORError != QCBOR_SUCCESS) {
5723 return 2;
5724 }
5725 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5726 return 3;
5727 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005728
Laurence Lundbladee3553422020-05-02 11:11:17 -07005729 // A sequence can have stuff at the end that may
5730 // or may not be valid CBOR. The protocol decoder knows
5731 // when to stop by definition of the protocol, not
5732 // when the top-level map or array is ended.
5733 // Finish still has to be called to know that
5734 // maps and arrays (if there were any) were closed
5735 // off correctly. When called like this it
5736 // must return the error QCBOR_ERR_EXTRA_BYTES.
5737 uCBORError = QCBORDecode_Finish(&DCtx);
5738 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5739 return 4;
5740 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005741
5742
Laurence Lundbladee3553422020-05-02 11:11:17 -07005743 // --- Test an empty input ----
5744 uint8_t empty[1];
5745 UsefulBufC Empty = {empty, 0};
5746 QCBORDecode_Init(&DCtx,
5747 Empty,
5748 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005749
Laurence Lundbladee3553422020-05-02 11:11:17 -07005750 uCBORError = QCBORDecode_Finish(&DCtx);
5751 if(uCBORError != QCBOR_SUCCESS) {
5752 return 5;
5753 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005754
5755
Laurence Lundbladee3553422020-05-02 11:11:17 -07005756 // --- Sequence with unclosed indefinite length array ---
5757 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005758
Laurence Lundbladee3553422020-05-02 11:11:17 -07005759 QCBORDecode_Init(&DCtx,
5760 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5761 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005762
Laurence Lundbladee3553422020-05-02 11:11:17 -07005763 // Get the first item
5764 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5765 if(uCBORError != QCBOR_SUCCESS) {
5766 return 7;
5767 }
5768 if(Item.uDataType != QCBOR_TYPE_INT64) {
5769 return 8;
5770 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005771
Laurence Lundbladee3553422020-05-02 11:11:17 -07005772 // Get a second item
5773 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5774 if(uCBORError != QCBOR_SUCCESS) {
5775 return 9;
5776 }
5777 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5778 return 10;
5779 }
5780
5781 // Try to finish before consuming all bytes to confirm
5782 // that the still-open error is returned.
5783 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005784 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005785 return 11;
5786 }
5787
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005788
Laurence Lundbladee3553422020-05-02 11:11:17 -07005789 // --- Sequence with a closed indefinite length array ---
5790 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005791
Laurence Lundbladee3553422020-05-02 11:11:17 -07005792 QCBORDecode_Init(&DCtx,
5793 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5794 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005795
Laurence Lundbladee3553422020-05-02 11:11:17 -07005796 // Get the first item
5797 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5798 if(uCBORError != QCBOR_SUCCESS) {
5799 return 12;
5800 }
5801 if(Item.uDataType != QCBOR_TYPE_INT64) {
5802 return 13;
5803 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005804
Laurence Lundbladee3553422020-05-02 11:11:17 -07005805 // Get a second item
5806 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5807 if(uCBORError != QCBOR_SUCCESS) {
5808 return 14;
5809 }
5810 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5811 return 15;
5812 }
5813
5814 // Try to finish before consuming all bytes to confirm
5815 // that the still-open error is returned.
5816 uCBORError = QCBORDecode_Finish(&DCtx);
5817 if(uCBORError != QCBOR_SUCCESS) {
5818 return 16;
5819 }
5820
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005821
Laurence Lundbladee3553422020-05-02 11:11:17 -07005822 return 0;
5823}
5824
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005825
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005826
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005827int32_t IntToTests()
5828{
5829 int nErrCode;
5830 int32_t n32;
5831 int16_t n16;
5832 int8_t n8;
5833 uint32_t u32;
5834 uint16_t u16;
5835 uint8_t u8;
5836 uint64_t u64;
5837
5838 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5839 if(nErrCode == -1 || n32 != 1) {
5840 return 1;
5841 }
5842
5843 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5844 if(nErrCode == -1 || n32 != INT32_MAX) {
5845 return 2;
5846 }
5847
5848 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5849 if(nErrCode == -1 || n32 != INT32_MIN) {
5850 return 3;
5851 }
5852
5853 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5854 if(nErrCode != -1) {
5855 return 4;
5856 }
5857
5858 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5859 if(nErrCode != -1) {
5860 return 5;
5861 }
5862
5863
5864 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5865 if(nErrCode == -1 || n16 != INT16_MAX) {
5866 return 6;
5867 }
5868
5869 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5870 if(nErrCode == -1 || n16 != INT16_MIN) {
5871 return 7;
5872 }
5873
5874 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5875 if(nErrCode == -1 || n16 != 1) {
5876 return 8;
5877 }
5878
5879 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5880 if(nErrCode != -1) {
5881 return 9;
5882 }
5883
5884 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5885 if(nErrCode != -1) {
5886 return 10;
5887 }
5888
5889
5890 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5891 if(nErrCode == -1 || n8 != 1) {
5892 return 11;
5893 }
5894
5895 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5896 if(nErrCode == -1 || n8 != INT8_MAX) {
5897 return 12;
5898 }
5899
5900 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5901 if(nErrCode == -1 || n8 != INT8_MIN) {
5902 return 13;
5903 }
5904
5905 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5906 if(nErrCode != -1) {
5907 return 14;
5908 }
5909
5910 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5911 if(nErrCode != -1) {
5912 return 15;
5913 }
5914
5915
5916 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5917 if(nErrCode == -1 || u32 != 1) {
5918 return 16;
5919 }
5920
5921 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5922 if(nErrCode == -1 || u32 != UINT32_MAX) {
5923 return 17;
5924 }
5925
5926 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5927 if(nErrCode == -1 || u32 != 0) {
5928 return 18;
5929 }
5930
5931 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5932 if(nErrCode != -1) {
5933 return 19;
5934 }
5935
5936 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5937 if(nErrCode != -1) {
5938 return 20;
5939 }
5940
5941
5942 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5943 if(nErrCode == -1 || u16 != UINT16_MAX) {
5944 return 21;
5945 }
5946
5947 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5948 if(nErrCode == -1 || u16 != 0) {
5949 return 22;
5950 }
5951
5952 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5953 if(nErrCode == -1 || u16 != 1) {
5954 return 23;
5955 }
5956
5957 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5958 if(nErrCode != -1) {
5959 return 24;
5960 }
5961
5962 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5963 if(nErrCode != -1) {
5964 return 25;
5965 }
5966
5967
5968 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5969 if(nErrCode == -1 || u8 != UINT8_MAX) {
5970 return 26;
5971 }
5972
5973 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5974 if(nErrCode == -1 || u8 != 0) {
5975 return 27;
5976 }
5977
5978 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5979 if(nErrCode == -1 || u8 != 1) {
5980 return 28;
5981 }
5982
5983 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5984 if(nErrCode != -1) {
5985 return 29;
5986 }
5987
5988 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5989 if(nErrCode != -1) {
5990 return 30;
5991 }
5992
5993
5994 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5995 if(nErrCode == -1 || u64 != 1) {
5996 return 31;
5997 }
5998
5999 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6000 if(nErrCode == -1 || u64 != INT64_MAX) {
6001 return 32;
6002 }
6003
6004 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6005 if(nErrCode == -1 || u64 != 0) {
6006 return 33;
6007 }
6008
6009 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6010 if(nErrCode != -1) {
6011 return 34;
6012 }
6013
6014 return 0;
6015}
6016
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006017
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006018
6019
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006020/*
6021A sequence with
6022 A wrapping bstr
6023 containing a map
6024 1
6025 2
6026 A wrapping bstr
6027 containing an array
6028 3
6029 wrapping bstr
6030 4
6031 5
6032 6
6033 array
6034 7
6035 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006036 */
6037
Laurence Lundblade55013642020-09-23 05:39:22 -07006038static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006039{
Laurence Lundblade55013642020-09-23 05:39:22 -07006040 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006041 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006042 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006043
Laurence Lundblade55013642020-09-23 05:39:22 -07006044 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006045
6046 QCBOREncode_BstrWrap(&EC);
6047 QCBOREncode_OpenMap(&EC);
6048 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6049 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6050 QCBOREncode_CloseMap(&EC);
6051 QCBOREncode_BstrWrap(&EC);
6052 QCBOREncode_OpenArray(&EC);
6053 QCBOREncode_AddInt64(&EC, 3);
6054 QCBOREncode_BstrWrap(&EC);
6055 QCBOREncode_AddInt64(&EC, 4);
6056 QCBOREncode_CloseBstrWrap(&EC, NULL);
6057 QCBOREncode_AddInt64(&EC, 5);
6058 QCBOREncode_CloseArray(&EC);
6059 QCBOREncode_CloseBstrWrap(&EC, NULL);
6060 QCBOREncode_AddInt64(&EC, 6);
6061 QCBOREncode_CloseBstrWrap(&EC, NULL);
6062 QCBOREncode_OpenArray(&EC);
6063 QCBOREncode_AddInt64(&EC, 7);
6064 QCBOREncode_AddInt64(&EC, 8);
6065 QCBOREncode_CloseArray(&EC);
6066
6067 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006068 if(uErr) {
6069 Encoded = NULLUsefulBufC;
6070 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006071
6072 return Encoded;
6073}
6074
6075
6076int32_t EnterBstrTest()
6077{
Laurence Lundblade55013642020-09-23 05:39:22 -07006078 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006079
6080 QCBORDecodeContext DC;
6081
Laurence Lundblade55013642020-09-23 05:39:22 -07006082 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006083
Laurence Lundblade55013642020-09-23 05:39:22 -07006084 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006085
6086
Laurence Lundblade9b334962020-08-27 10:55:53 -07006087 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006088 QCBORDecode_EnterMap(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006089 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6090 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006091 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006092 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006093 QCBORDecode_EnterArray(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006094 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006095 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006096 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006097 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006098 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006099 QCBORDecode_ExitArray(&DC);
6100 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006101 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006102 QCBORDecode_ExitBstrWrapped(&DC);
6103 QCBORDecode_EnterArray(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006104 QCBORDecode_GetInt64(&DC, &n7);
6105 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006106 QCBORDecode_ExitArray(&DC);
6107
6108 QCBORError uErr = QCBORDecode_Finish(&DC);
6109
6110 return (int32_t)uErr;
6111}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006112
6113
6114
6115
6116static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006117 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006118
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006119 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006120 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006121 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6122 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6123 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006124
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006125 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006126 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6127 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6128 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006129
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006130 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006131 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006132 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6133 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006134
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006135 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006136 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6137 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006138
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006139 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006140 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006141 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6142 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006143
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006144 21,
6145 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6146 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6147
6148 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006149 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006150 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6151 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006152
6153 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006154 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6155 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006156
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006157 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006158 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006159 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6160 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006161
6162 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006163 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6164 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006165
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006166 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006167 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006168 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6169 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006170
6171 0x18, 0x33,
6172 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6173
6174 // MIME
6175 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006176 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6177 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6178 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006179
6180 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006181 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6182 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006183
6184 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006185 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6186 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6187 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006188
6189 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006190 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6191 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006192
6193 // UUID
6194 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006195 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6196 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006197
6198 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006199 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6200 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006201};
6202
6203int32_t DecodeTaggedTypeTests()
6204{
6205 QCBORDecodeContext DC;
6206 QCBORError uErr;
6207
6208 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6209
6210 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006211 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006212
6213 QCBORDecode_EnterMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006214 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006215 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006216 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6217 return 1;
6218 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006219 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006220 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6221 return 2;
6222 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006223 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006224 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6225 return 3;
6226 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006227 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006228 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006229 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6230 return 4;
6231 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006232 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006233 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006234 return 5;
6235 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006236
Laurence Lundblade9b334962020-08-27 10:55:53 -07006237 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006238 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6239 bNeg != false) {
6240 return 10;
6241 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006242 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006243 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6244 bNeg != true) {
6245 return 11;
6246 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006247 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006248 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6249 return 12;
6250 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006251 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006252 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006253 return 13;
6254 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006255 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006256 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006257 return 14;
6258 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006259
Laurence Lundblade9b334962020-08-27 10:55:53 -07006260 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006261 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6262 return 20;
6263 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006264 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006265 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6266 return 21;
6267 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006268 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006269 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006270 return 22;
6271 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006272 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006273 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006274 return 23;
6275 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006276
Laurence Lundblade9b334962020-08-27 10:55:53 -07006277 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006278 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6279 return 30;
6280 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006281 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006282 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6283 return 31;
6284 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006285 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006286 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006287 return 32;
6288 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006289 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006290 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006291 return 33;
6292 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006293
Laurence Lundblade9b334962020-08-27 10:55:53 -07006294 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006295 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6296 return 40;
6297 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006298 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006299 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6300 return 41;
6301 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006302 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006303 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006304 return 42;
6305 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006306 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006307 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006308 return 43;
6309 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006310
Laurence Lundblade9b334962020-08-27 10:55:53 -07006311 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006312 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6313 return 50;
6314 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006315 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006316 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6317 return 51;
6318 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006319 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006320 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006321 return 52;
6322 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006323 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006324 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006325 return 53;
6326 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006327
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006328 // MIME
6329 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006330 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006331 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6332 bIsNot7Bit == true) {
6333 return 60;
6334 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006335 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006336 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6337 bIsNot7Bit == true) {
6338 return 61;
6339 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006340 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006341 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6342 bIsNot7Bit == false) {
6343 return 62;
6344 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006345 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006346 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6347 bIsNot7Bit == false) {
6348 return 63;
6349 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006350 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006351 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006352 return 64;
6353 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006354 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006355 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006356 return 65;
6357 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006358
Laurence Lundblade9b334962020-08-27 10:55:53 -07006359 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006360 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6361 return 70;
6362 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006363 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006364 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6365 return 71;
6366 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006367 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006368 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006369 return 72;
6370 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006371 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006372 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006373 return 73;
6374 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006375
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006376 // Improvement: add some more error test cases
6377
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006378 QCBORDecode_ExitMap(&DC);
6379
6380 uErr = QCBORDecode_Finish(&DC);
6381 if(uErr != QCBOR_SUCCESS) {
6382 return 100;
6383 }
6384
6385 return 0;
6386}