blob: 26ed0485224cc9ded381a626549667197c75abf3 [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 Lundbladee6f15112020-07-23 18:44:16 -07002027 TODO: fix this
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002028 This test is disabled until QCBOREncode_EncodeHead() is brought in so
2029 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
2030
2031 This relies on the largest string allowed being SIZE_MAX -4 rather than
2032 SIZE_MAX. That way the test can be performed.
2033 {
2034 QCBORDecodeContext DCtx;
2035 QCBORItem Item;
2036
2037 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
2038 0xff, 0xff, 0xff, 0xff};
2039
2040 QCBORDecode_Init(&DCtx,
2041 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
2042 QCBOR_DECODE_MODE_NORMAL);
2043
2044 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2045 return -4;
2046 }
2047 }
2048*/
2049
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002050 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002051}
2052
2053
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002054/* Try all 256 values of the byte at nLen including recursing for
2055 each of the values to try values at nLen+1 ... up to nLenMax
2056 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002057static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002058{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002059 if(nLen >= nLenMax) {
2060 return;
2061 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002062
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002063 for(int inputByte = 0; inputByte < 256; inputByte++) {
2064 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002065 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002066 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002067
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002068 // Get ready to parse
2069 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002070 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002071
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002072 // Parse by getting the next item until an error occurs
2073 // Just about every possible decoder error can occur here
2074 // The goal of this test is not to check for the correct
2075 // error since that is not really possible. It is to
2076 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002077 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002078 QCBORItem Item;
2079 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002081 break;
2082 }
2083 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002084
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002085 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002086 }
2087}
2088
2089
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002090int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002092 // Size 2 tests 64K inputs and runs quickly
2093 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002094
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002095 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002096
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002097 return 0;
2098}
2099
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002100
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002101int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002102{
2103 // size 3 tests 16 million inputs and runs OK
2104 // in seconds on fast machines. Size 4 takes
2105 // 10+ minutes and 5 half a day on fast
2106 // machines. This test is kept separate from
2107 // the others so as to no slow down the use
2108 // of them as a very frequent regression.
2109 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002110
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002111 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002112
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002113 return 0;
2114}
2115
2116
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002117static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002118 0xc0, // tag for string date
2119 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002120
Laurence Lundbladec7114722020-08-13 05:11:40 -07002121 0xc0, // tag for string date
2122 0x00, // Wrong type for a string date
2123
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002124 0xc1, // tag for epoch date
2125 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2126
Laurence Lundbladec7114722020-08-13 05:11:40 -07002127 0xc1,
2128 0x62, 'h', 'i', // wrong type tagged
2129
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002130 // CBOR_TAG_B64
Laurence Lundblade9b334962020-08-27 10:55:53 -07002131 0xcf, 0xd8, 0x22, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002132 0x1a, 0x53, 0x72, 0x4E, 0x01,
2133
2134 0xc1, // tag for epoch date
2135 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002136
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002137 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002138 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002139
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002140 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002141 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002142
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002143 0xc1, // tag for epoch date
2144 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2145 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2146
2147 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002148 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2149
2150 0xc1, // tag for epoch date
2151 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2152
2153 0xc1,
2154 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2155
2156 0xc1, // tag for epoch date
2157 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158};
2159
2160
Laurence Lundbladec7114722020-08-13 05:11:40 -07002161
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002162// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002163#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002164static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002165
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002166 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002167
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002168 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002169
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002170 return diff > 0.0000001;
2171}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002172#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002173
2174
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002175int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002176{
2177 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002178 QCBORItem Item;
2179 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002180
Laurence Lundbladeee851742020-01-08 08:37:05 -08002181 QCBORDecode_Init(&DCtx,
2182 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2183 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002184
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002185 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002186 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002187 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002188 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002189 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002190 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002191 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002192 }
2193
Laurence Lundbladec7114722020-08-13 05:11:40 -07002194 // Wrong type for a string date
2195 uError = QCBORDecode_GetNext(&DCtx, &Item);
2196 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002197 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002198 }
2199
2200 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2201 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2202 return -4;
2203 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002204 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2205 Item.val.epochDate.nSeconds != 1400000000 ||
2206 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002207 return -5;
2208 }
2209
2210 // Wrong type for an epoch date
2211 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2212 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002214
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002215 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2216 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002217 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2218 return -7;
2219 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002220 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2221 Item.val.epochDate.nSeconds != 1400000001 ||
2222 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002223 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002224 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227 // Epoch date that is too large for our representation
2228 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002229 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundblade9682a532020-06-06 18:33:04 -07002232#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002233 // Epoch date in float format with fractional seconds
2234 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2235 return -10;
2236 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002237 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2238 Item.val.epochDate.nSeconds != 1 ||
2239 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002240 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002243 // Epoch date float that is too large for our representation
2244 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002245 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002247
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002248 // Epoch date double that is just slightly too large
2249 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002250 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002251 }
2252
2253 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002254 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2255 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2256 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2257 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002258 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002259 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002260
2261 // Nan
2262 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2263 return -15;
2264 }
2265
2266 // +Inifinity double-precision
2267 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2268 return -16;
2269 }
2270
2271#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2272 // -Inifinity half-precision
2273 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2274 return -17;
2275 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002276#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002277 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2278 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002279 }
2280#endif
2281
Laurence Lundbladec7114722020-08-13 05:11:40 -07002282#else
2283 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2284 return -19;
2285 }
2286 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2287 return -20;
2288 }
2289 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2290 return -21;
2291 }
2292 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2293 return -22;
2294 }
2295 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2296 return -23;
2297 }
2298 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2299 return -24;
2300 }
2301#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2302 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2303 return -25;
2304 }
2305#else
2306 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2307 return -26;
2308 }
2309#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002310
Laurence Lundbladec7114722020-08-13 05:11:40 -07002311#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002312
2313 return 0;
2314}
2315
Laurence Lundblade4b270642020-08-14 12:53:07 -07002316/*
2317 Test cases covered here. Some items cover more than one of these.
2318 positive integer (zero counts as a positive integer)
2319 negative integer
2320 half-precision float
2321 single-precision float
2322 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002323
Laurence Lundblade4b270642020-08-14 12:53:07 -07002324 float Overflow error
2325 Wrong type error for epoch
2326 Wrong type error for date string
2327 float disabled error
2328 half-precision disabled error
2329 -Infinity
2330 Slightly too large integer
2331 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002332
Laurence Lundblade4b270642020-08-14 12:53:07 -07002333 Get epoch by int
2334 Get string by int
2335 Get epoch by string
2336 Get string by string
2337 Fail to get epoch by wrong int label
2338 Fail to get string by wrong string label
2339 Fail to get epoch by string because it is invalid
2340 Fail to get epoch by int because it is invalid
2341
2342 Untagged values
2343 */
2344static uint8_t spSpiffyDateTestInput[] = {
2345 0x86,
2346
2347 0xc1,
2348 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2349
Laurence Lundbladec7114722020-08-13 05:11:40 -07002350 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002351 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2352
2353 0xc1, // tag for epoch date
2354 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2355
2356 0xc1, // tag for epoch date
2357 0x9f, 0xff, // Erroneous empty array as content for date
2358
2359 0xc0, // tag for string date
2360 0xbf, 0xff, // Erroneous empty map as content for date
2361
2362 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002363
2364 0x00,
2365 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002366 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002367
2368 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002369 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002370 0xc1, // tag for epoch date
2371 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2372
2373 // Untagged integer 0
2374 0x08,
2375 0x00,
2376
2377 // Utagged date string with string label y
2378 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002379 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002380
2381 // Untagged -1000 with label z
2382 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002383 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002384 0x39, 0x03, 0xe7,
2385
Laurence Lundbladec7114722020-08-13 05:11:40 -07002386 0x07,
2387 0xc1, // tag for epoch date
2388 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2389
Laurence Lundblade4b270642020-08-14 12:53:07 -07002390 0x05,
2391 0xc1,
2392 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2393
Laurence Lundbladec7114722020-08-13 05:11:40 -07002394 // Untagged single-precision float with value 3.14 with string label x
2395 0x61, 0x78,
2396 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2397
Laurence Lundbladec7114722020-08-13 05:11:40 -07002398 // Untagged half-precision float with value -2
2399 0x09,
2400 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002401
2402 0xff,
2403};
2404
2405int32_t SpiffyDateDecodeTest()
2406{
2407 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002408 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002409 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2410 nEpochDate4, nEpochDate6, nEpochDateFail,
2411 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002412 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002413 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002414
2415 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002416 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002417 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002418 QCBORDecode_EnterArray(&DC);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002419
Laurence Lundblade9b334962020-08-27 10:55:53 -07002420 // Too-negative float, -9.2233720368547748E+18
2421 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002422 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002423#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002424 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2425 return 1111;
2426 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002427#else
2428 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2429 return 1112;
2430 }
2431#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002432
2433 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002434 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002435 uError = QCBORDecode_GetAndResetError(&DC);
2436 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002437 return 1;
2438 }
2439
Laurence Lundblade4b270642020-08-14 12:53:07 -07002440 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002441 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002442 uError = QCBORDecode_GetAndResetError(&DC);
2443#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2444#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2445 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2446#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2447 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2448#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2449#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2450 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2451#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2452 if(uError != uExpectedforHalfMinusInfinity) {
2453 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002454 }
2455
Laurence Lundblade4b270642020-08-14 12:53:07 -07002456 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002457 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002458 uError = QCBORDecode_GetAndResetError(&DC);
2459 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2460 return 3;
2461 }
2462
2463 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002464 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002465 uError = QCBORDecode_GetAndResetError(&DC);
2466 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2467 return 4;
2468 }
2469
2470 QCBORDecode_EnterMap(&DC);
2471
2472 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002473 QCBORDecode_GetEpochDateInMapN(&DC,
2474 5,
2475 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2476 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2477 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002478#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2479 if(nEpochDate2 != -9223372036854773760LL) {
2480 return 101;
2481 }
2482#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2483 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002484 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002485 return 102;
2486 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002487#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002488
Laurence Lundblade4b270642020-08-14 12:53:07 -07002489 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002490 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2491 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002492#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2493 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002494 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002495 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002496#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2497 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002498 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002499 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002500 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002501#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2502
2503 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002504 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2505 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002506#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2507 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002508 return 103;
2509 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002510#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2511 uError = QCBORDecode_GetAndResetError(&DC);
2512 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2513 return 104;
2514 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002515#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2516
Laurence Lundblade9b334962020-08-27 10:55:53 -07002517 // A half-precision date with value -2 FFF
2518 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2519 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002520#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2521 if(nEpochDate4 != -2) {
2522 return 105;
2523 }
2524#else
2525 uError = QCBORDecode_GetAndResetError(&DC);
2526 if(uError == QCBOR_SUCCESS) {
2527 return 106;
2528 }
2529#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002530
Laurence Lundblade4b270642020-08-14 12:53:07 -07002531
2532 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002533 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2534 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2535 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002536 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002537 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002538 return 107;
2539 }
2540
2541 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002542 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2543 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002544 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002545 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002546 return 108;
2547 }
2548
2549 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002550 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2551 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2552 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002553 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002554 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002555 return 109;
2556 }
2557
2558 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002559 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2560 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002561 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002562 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002563 return 110;
2564 }
2565
2566 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002567
Laurence Lundblade4b270642020-08-14 12:53:07 -07002568 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002569 QCBORDecode_GetEpochDateInMapN(&DC,
2570 1,
2571 QCBOR_TAG_REQUIREMENT_TAG |
2572 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2573 &nEpochDate1400000000);
2574 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002575 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002576 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2577 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002578 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002579 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2580 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002581 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002582 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2583 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002584 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002585 QCBORDecode_GetEpochDateInMapSZ(&DC,
2586 "z",
2587 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2588 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2589 &nEpochDate6);
2590 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002591
2592 QCBORDecode_ExitMap(&DC);
2593 QCBORDecode_ExitArray(&DC);
2594 uError = QCBORDecode_Finish(&DC);
2595 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002596 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002597 }
2598
Laurence Lundblade9b334962020-08-27 10:55:53 -07002599 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002600 return 200;
2601 }
2602
Laurence Lundblade9b334962020-08-27 10:55:53 -07002603 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002604 return 201;
2605 }
2606
Laurence Lundblade9b334962020-08-27 10:55:53 -07002607 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002608 return 202;
2609 }
2610
Laurence Lundblade9b334962020-08-27 10:55:53 -07002611 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002612 return 203;
2613 }
2614
Laurence Lundblade9b334962020-08-27 10:55:53 -07002615 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002616 return 204;
2617 }
2618
Laurence Lundblade9b334962020-08-27 10:55:53 -07002619 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2620 return 205;
2621 }
2622
2623 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2624 return 206;
2625 }
2626
Laurence Lundbladec7114722020-08-13 05:11:40 -07002627 return 0;
2628}
2629
2630
2631
Laurence Lundblade9b334962020-08-27 10:55:53 -07002632// Input for one of the tagging tests
2633static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002634 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002635 0x81, // Array of one
2636 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2637 0x82, // Array of two that is the faction 1/3
2638 0x01,
2639 0x03,
2640
2641 /*
2642 More than 4 tags on an item 225(226(227(228(229([])))))
2643 */
2644 0xd8, 0xe1,
2645 0xd8, 0xe2,
2646 0xd8, 0xe3,
2647 0xd8, 0xe4,
2648 0xd8, 0xe5,
2649 0x80,
2650
2651 /* tag 10489608748473423768(
2652 2442302356(
2653 21590(
2654 240(
2655 []))))
2656 */
2657 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2658 0xda, 0x91, 0x92, 0x93, 0x94,
2659 0xd9, 0x54, 0x56,
2660 0xd8, 0xf0,
2661 0x80,
2662
2663 /* tag 21590(
2664 10489608748473423768(
2665 2442302357(
2666 65534(
2667 []))))
2668 */
2669 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2670 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2671 0xda, 0x91, 0x92, 0x93, 0x95,
2672 0xd9, 0xff, 0xfe,
2673 0x80,
2674
2675 /* Make sure to blow past the limit of tags that must be mapped.
2676 works in conjuntion with entries above.
2677 269488144(269488145(269488146(269488147([]))))
2678 */
2679 0xda, 0x10, 0x10, 0x10, 0x10,
2680 0xda, 0x10, 0x10, 0x10, 0x11,
2681 0xda, 0x10, 0x10, 0x10, 0x12,
2682 0xda, 0x10, 0x10, 0x10, 0x13,
2683 0x80,
2684
2685 /* An invalid decimal fraction with an additional tag */
2686 0xd9, 0xff, 0xfa,
2687 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2688 0x00, // the integer 0; should be a byte string
2689};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002690
Laurence Lundblade59289e52019-12-30 13:44:37 -08002691/*
2692 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002693 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002694 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002695static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2696 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002697
Laurence Lundblade59289e52019-12-30 13:44:37 -08002698/*
2699DB 9192939495969798 # tag(10489608748473423768)
2700 D8 88 # tag(136)
2701 C6 # tag(6)
2702 C7 # tag(7)
2703 80 # array(0)
2704*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002705static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2706 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002707
2708/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002709 55799(55799(55799({
2710 6(7(-23)): 5859837686836516696(7({
2711 7(-20): 11({
2712 17(-18): 17(17(17("Organization"))),
2713 9(-17): 773("SSG"),
2714 -15: 16(17(6(7("Confusion")))),
2715 17(-16): 17("San Diego"),
2716 17(-14): 17("US")
2717 }),
2718 23(-19): 19({
2719 -11: 9({
2720 -9: -7
2721 }),
2722 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2723 })
2724 })),
2725 16(-22): 23({
2726 11(8(7(-5))): 8(-3)
2727 })
2728 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002729 */
2730static uint8_t spCSRWithTags[] = {
2731 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2732 0xc6, 0xc7, 0x36,
2733 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2734 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2735 0xcb, 0xa5,
2736 0xd1, 0x31,
2737 0xd1, 0xd1, 0xd1, 0x6c,
2738 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2739 0xc9, 0x30,
2740 0xd9, 0x03, 0x05, 0x63,
2741 0x53, 0x53, 0x47,
2742 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002743 0xd0, 0xd1, 0xc6, 0xc7,
2744 0x69,
2745 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002746 0xd1, 0x2f,
2747 0xd1, 0x69,
2748 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2749 0xd1, 0x2d,
2750 0xd1, 0x62,
2751 0x55, 0x53,
2752 0xd7, 0x32,
2753 0xd3, 0xa2,
2754 0x2a,
2755 0xc9, 0xa1,
2756 0x28,
2757 0x26,
2758 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2759 0xcc, 0x4a,
2760 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2761 0xd0, 0x35,
2762 0xd7, 0xa1,
2763 0xcb, 0xc8, 0xc7, 0x24,
2764 0xc8, 0x22};
2765
Laurence Lundblade9b334962020-08-27 10:55:53 -07002766
2767static uint8_t spSpiffyTagInput[] = {
2768 0x9f, // Open indefinite array
2769
2770 0xc0, // tag for string date
2771 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2772
2773 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2774
2775 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2776
2777 0xd8, 0x23, // tag for regex
2778 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2779
2780 0xc0, // tag for string date
2781 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2782
2783 0xff
2784};
2785
2786
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002787static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002788
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002789
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002790int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002791{
2792 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002793 QCBORItem Item;
2794 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002795
Laurence Lundbladeee851742020-01-08 08:37:05 -08002796 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002797 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002798 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002799
Laurence Lundblade9b334962020-08-27 10:55:53 -07002800 /*
2801 This test matches the magic number tag and the fraction tag
2802 55799([...])
2803 */
2804 uError = QCBORDecode_GetNext(&DCtx, &Item);
2805 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002806 return -2;
2807 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002808 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002809 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2810 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002811 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002812
Laurence Lundblade9b334962020-08-27 10:55:53 -07002813 /*
2814 4([1,3])
2815 */
2816 uError = QCBORDecode_GetNext(&DCtx, &Item);
2817#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2818 if(uError != QCBOR_SUCCESS ||
2819 Item.uDataType != QCBOR_TYPE_ARRAY ||
2820 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2821 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2822 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2823 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2824 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2825 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2826 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002827 return -4;
2828 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002829 // consume the items in the array
2830 uError = QCBORDecode_GetNext(&DCtx, &Item);
2831 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002832
Laurence Lundblade59289e52019-12-30 13:44:37 -08002833#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002834 if(uError != QCBOR_SUCCESS ||
2835 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2836 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2837 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2838 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2839 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2840 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2841 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002842 }
2843#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002844
Laurence Lundblade9b334962020-08-27 10:55:53 -07002845 /*
2846 More than 4 tags on an item 225(226(227(228(229([])))))
2847 */
2848 uError = QCBORDecode_GetNext(&DCtx, &Item);
2849 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002850 return -6;
2851 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002852
2853 /* tag 10489608748473423768(
2854 2442302356(
2855 21590(
2856 240(
2857 []))))
2858 */
2859 uError = QCBORDecode_GetNext(&DCtx, &Item);
2860 if(uError != QCBOR_SUCCESS ||
2861 Item.uDataType != QCBOR_TYPE_ARRAY ||
2862 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2863 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2864 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2865 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002866 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002867 }
2868
2869 /* tag 21590(
2870 10489608748473423768(
2871 2442302357(
2872 21591(
2873 []))))
2874 */
2875 uError = QCBORDecode_GetNext(&DCtx, &Item);
2876 if(uError != QCBOR_SUCCESS ||
2877 Item.uDataType != QCBOR_TYPE_ARRAY ||
2878 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2879 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2880 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2881 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2882 return -8;
2883 }
2884
2885 /* Make sure to blow past the limit of tags that must be mapped.
2886 works in conjuntion with entries above.
2887 269488144(269488145(269488146(269488147([]))))
2888 */
2889 uError = QCBORDecode_GetNext(&DCtx, &Item);
2890 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2891 return -9;
2892 }
2893
2894 uError = QCBORDecode_GetNext(&DCtx, &Item);
2895 if(uError == QCBOR_SUCCESS) {
2896 return -10;
2897 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002898
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002899 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002900 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002901 // tage and then matches it. Caller-config lists are no longer
2902 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002903 QCBORDecode_Init(&DCtx,
2904 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2905 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002906 const uint64_t puList[] = {0x9192939495969798, 257};
2907 const QCBORTagListIn TL = {2, puList};
2908 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002909
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002910 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2911 return -8;
2912 }
2913 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2914 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2915 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2916 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2917 Item.val.uCount != 0) {
2918 return -9;
2919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002920
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002921 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002922 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002923 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002924 const uint64_t puLongList[17] = {1,2,1};
2925 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002926 QCBORDecode_Init(&DCtx,
2927 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2928 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002929 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2930 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2931 return -11;
2932 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002933
Laurence Lundblade9b334962020-08-27 10:55:53 -07002934 uint64_t puTags[16];
2935 QCBORTagListOut Out = {0, 4, puTags};
2936
2937
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002938 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002939 QCBORDecode_Init(&DCtx,
2940 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2941 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002942 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2943 return -12;
2944 }
2945 if(puTags[0] != 0x9192939495969798 ||
2946 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002947 puTags[2] != 0x06 ||
2948 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002949 return -13;
2950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002951
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002952 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002953 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002954 QCBORDecode_Init(&DCtx,
2955 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2956 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002957 QCBORTagListOut OutSmall = {0, 3, puTags};
2958 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2959 return -14;
2960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002961
Laurence Lundblade9b334962020-08-27 10:55:53 -07002962
2963
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002964 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002965 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
2966 // It is a bit of a messy test and maybe could be improved, but
2967 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002968 QCBORDecode_Init(&DCtx,
2969 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2970 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002971 int n = CheckCSRMaps(&DCtx);
2972 if(n) {
2973 return n-2000;
2974 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002975
Laurence Lundblade59289e52019-12-30 13:44:37 -08002976 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002977 QCBORDecode_Init(&DCtx,
2978 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2979 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002980
Laurence Lundblade9b334962020-08-27 10:55:53 -07002981 /* With the spiffy decode revision, this tag list is not used.
2982 It doesn't matter if a tag is in this list or not so some
2983 tests that couldn't process a tag because it isn't in this list
2984 now can process these unlisted tags. The tests have been
2985 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002986 const uint64_t puTagList[] = {773, 1, 90599561};
2987 const QCBORTagListIn TagList = {3, puTagList};
2988 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002989
2990
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002991 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2992 return -100;
2993 }
2994 if(Item.uDataType != QCBOR_TYPE_MAP ||
2995 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2996 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2997 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2998 Item.val.uCount != 2 ||
2999 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3000 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3001 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3002 Out.uNumUsed != 3) {
3003 return -101;
3004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003005
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003006 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3007 return -102;
3008 }
3009 if(Item.uDataType != QCBOR_TYPE_MAP ||
3010 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3011 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003012 !QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003013 Item.val.uCount != 2 ||
3014 puTags[0] != 5859837686836516696 ||
3015 puTags[1] != 7 ||
3016 Out.uNumUsed != 2) {
3017 return -103;
3018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003019
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003020 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3021 return -104;
3022 }
3023 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003024 //Item.uTagBits ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003025 Item.val.uCount != 5 ||
3026 puTags[0] != 0x0b ||
3027 Out.uNumUsed != 1) {
3028 return -105;
3029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003030
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003031 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3032 return -106;
3033 }
3034 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3035 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3036 Item.val.string.len != 12 ||
3037 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3038 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3039 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3040 Out.uNumUsed != 3) {
3041 return -105;
3042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003043
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003044 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3045 return -107;
3046 }
3047 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3048 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3049 Item.val.string.len != 3 ||
3050 puTags[0] != 773 ||
3051 Out.uNumUsed != 1) {
3052 return -108;
3053 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003054
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003055 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3056 return -109;
3057 }
3058 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003059 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003060 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003061 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003062 puTags[3] != 7 ||
3063 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003064 return -110;
3065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003066
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003067 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3068 return -111;
3069 }
3070 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3071 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3072 Item.val.string.len != 9 ||
3073 puTags[0] != 17 ||
3074 Out.uNumUsed != 1) {
3075 return -112;
3076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003077
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003078 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3079 return -111;
3080 }
3081 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3082 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3083 Item.val.string.len != 2 ||
3084 puTags[0] != 17 ||
3085 Out.uNumUsed != 1) {
3086 return -112;
3087 }
3088
3089 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3090 return -113;
3091 }
3092 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003093 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003094 Item.val.uCount != 2 ||
3095 puTags[0] != 19 ||
3096 Out.uNumUsed != 1) {
3097 return -114;
3098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003099
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003100 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3101 return -115;
3102 }
3103 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003104 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003105 Item.val.uCount != 1 ||
3106 puTags[0] != 9 ||
3107 Out.uNumUsed != 1) {
3108 return -116;
3109 }
3110
3111 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3112 return -116;
3113 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003114 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003115 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003116 Out.uNumUsed != 0) {
3117 return -117;
3118 }
3119
3120 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3121 return -118;
3122 }
3123 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3124 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003125 puTags[0] != 12 ||
3126 Out.uNumUsed != 1) {
3127 return -119;
3128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003129
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003130 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3131 return -120;
3132 }
3133 if(Item.uDataType != QCBOR_TYPE_MAP ||
3134 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3135 Item.val.uCount != 1 ||
3136 puTags[0] != 0x17 ||
3137 Out.uNumUsed != 1) {
3138 return -121;
3139 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003140
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003141 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3142 return -122;
3143 }
3144 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003145 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003146 Item.val.int64 != -3 ||
3147 puTags[0] != 8 ||
3148 Out.uNumUsed != 1) {
3149 return -123;
3150 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003151
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003152 if(QCBORDecode_Finish(&DCtx)) {
3153 return -124;
3154 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003155
3156 UsefulBufC DateString;
3157 QCBORDecode_Init(&DCtx,
3158 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3159 QCBOR_DECODE_MODE_NORMAL);
3160
3161 QCBORDecode_EnterArray(&DCtx);
3162 // tagged date string
3163 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3164 // untagged date string
3165 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3166 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3167 return 100;
3168 }
3169 // untagged byte string
3170 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3171 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3172 return 101;
3173 }
3174 // tagged regex
3175 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3176 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3177 return 102;
3178 }
3179 // tagged date string with a byte string
3180 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3181 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3182 return 103;
3183 }
3184 QCBORDecode_ExitArray(&DCtx);
3185 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3186 return 104;
3187 }
3188
3189
3190 QCBORDecode_Init(&DCtx,
3191 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3192 QCBOR_DECODE_MODE_NORMAL);
3193
3194 QCBORDecode_EnterArray(&DCtx);
3195 // tagged date string
3196 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3197 // untagged date string
3198 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3199 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3200 return 200;
3201 }
3202 // untagged byte string
3203 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3204 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3205 return 201;
3206 }
3207 // tagged regex
3208 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3209 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3210 return 202;
3211 }
3212 // tagged date string with a byte string
3213 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3214 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3215 return 203;
3216 }
3217 QCBORDecode_ExitArray(&DCtx);
3218 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3219 return 204;
3220 }
3221
3222 QCBORDecode_Init(&DCtx,
3223 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3224 QCBOR_DECODE_MODE_NORMAL);
3225
3226 QCBORDecode_EnterArray(&DCtx);
3227 // tagged date string
3228 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3229 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3230 return 300;
3231 }
3232 // untagged date string
3233 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3234 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3235 return 301;
3236 }
3237 // untagged byte string
3238 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3239 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3240 return 302;
3241 }
3242 // tagged regex
3243 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3244 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3245 return 303;
3246 }
3247 // tagged date string with a byte string
3248 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3249 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3250 return 304;
3251 }
3252 QCBORDecode_ExitArray(&DCtx);
3253 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3254 return 305;
3255 }
3256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003257 return 0;
3258}
3259
3260
3261
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003262
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003263static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003264 0x83,
3265 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3266 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3267 0xA4,
3268 0x63, 0x42, 0x4E, 0x2B,
3269 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3270 0x18, 0x40,
3271 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3272 0x63, 0x42, 0x4E, 0x2D,
3273 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3274 0x38, 0x3F,
3275 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3276
3277
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003278static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003279
3280
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003281int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003282{
3283 QCBORDecodeContext DCtx;
3284 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003285 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003286
Laurence Lundbladeee851742020-01-08 08:37:05 -08003287 QCBORDecode_Init(&DCtx,
3288 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3289 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003290
3291
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003292 //
3293 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3294 return -1;
3295 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003296 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003298
3299 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003300 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003301 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003302 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003303 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003304 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003305 }
3306
3307 //
3308 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003309 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003310 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003311 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003312 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003315 //
3316 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003317 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003318 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003319 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003322 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003323 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003324 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3325 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003326 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003327 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003328 }
3329
3330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003331 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003332 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3333 Item.uLabelType != QCBOR_TYPE_INT64 ||
3334 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003335 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003336 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003338
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003339 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003340 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003341 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3342 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003343 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003344 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003348 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003349 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3350 Item.uLabelType != QCBOR_TYPE_INT64 ||
3351 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003352 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003353 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003356 return 0;
3357}
3358
3359
3360
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003361static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003362 uint8_t uDataType,
3363 uint8_t uNestingLevel,
3364 uint8_t uNextNest,
3365 int64_t nLabel,
3366 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003367{
3368 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003369 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003370
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003371 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3372 if(Item.uDataType != uDataType) return -1;
3373 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003374 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3375 Item.uLabelType != QCBOR_TYPE_UINT64) {
3376 return -1;
3377 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003378 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3379 if(Item.label.int64 != nLabel) return -1;
3380 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003381 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003382 }
3383 }
3384 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303385 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003387 if(pItem) {
3388 *pItem = Item;
3389 }
3390 return 0;
3391}
3392
3393
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003394// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003395static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003396{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303397 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003398
Laurence Lundblade9b334962020-08-27 10:55:53 -07003399 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003400
Laurence Lundblade9b334962020-08-27 10:55:53 -07003401 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003402
Laurence Lundblade9b334962020-08-27 10:55:53 -07003403 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3404 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3405 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3406 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3407 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003408
Laurence Lundblade9b334962020-08-27 10:55:53 -07003409 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3410 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003411
Laurence Lundblade9b334962020-08-27 10:55:53 -07003412 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3413 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003414
Laurence Lundblade9b334962020-08-27 10:55:53 -07003415 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3416 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003417
Laurence Lundblade9b334962020-08-27 10:55:53 -07003418 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003419
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003420 return 0;
3421}
3422
3423
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003424/*
3425// cbor.me decoded output
3426{
3427 -23: {
3428 -20: {
3429 -18: "Organization",
3430 -17: "SSG",
3431 -15: "Confusion",
3432 -16: "San Diego",
3433 -14: "US"
3434 },
3435 -19: {
3436 -11: {
3437 -9: -7
3438 },
3439 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3440 }
3441 },
3442 -22: {
3443 -5: -3
3444 }
3445}
3446 */
3447
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003448
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003449static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003450 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3451 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3452 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3453 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3454 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3455 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3456 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3457 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3458 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3459
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003460int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003461{
3462 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003463
Laurence Lundbladeee851742020-01-08 08:37:05 -08003464 QCBORDecode_Init(&DCtx,
3465 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3466 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003467
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003468 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003469}
3470
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003471
3472
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003473int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003474{
3475 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003476
Laurence Lundbladeee851742020-01-08 08:37:05 -08003477 QCBORDecode_Init(&DCtx,
3478 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3479 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003480
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003481 QCBORItem Item;
3482 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003483
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003484 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3485 return -1;
3486 }
3487 if(Item.uDataType != QCBOR_TYPE_MAP) {
3488 return -2;
3489 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003490
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003491 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3492 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3493 return -3;
3494 }
3495
3496 return 0;
3497}
3498
3499
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003500// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003501static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003502 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3503 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3504 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3505 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3506 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3507 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003508 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3509 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3510 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3511 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003512
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003513int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003514{
3515 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003516
Laurence Lundbladeee851742020-01-08 08:37:05 -08003517 QCBORDecode_Init(&DCtx,
3518 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3519 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003520
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003521 return CheckCSRMaps(&DCtx);
3522}
3523
3524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003525
Laurence Lundblade17ede402018-10-13 11:43:07 +08003526static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3527{
3528 UsefulOutBuf UOB;
3529 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003530
Laurence Lundblade17ede402018-10-13 11:43:07 +08003531 int i;
3532 for(i = 0; i < n; i++) {
3533 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3534 }
3535
3536 for(i = 0; i < n; i++) {
3537 UsefulOutBuf_AppendByte(&UOB, 0xff);
3538 }
3539 return UsefulOutBuf_OutUBuf(&UOB);
3540}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003541
3542
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003543static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003544{
3545 QCBORDecodeContext DC;
3546 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003547
Laurence Lundblade17ede402018-10-13 11:43:07 +08003548 int j;
3549 for(j = 0; j < nNestLevel; j++) {
3550 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003551 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003552 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3553 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003554 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003555 return -4;
3556 } else {
3557 return 0; // Decoding doesn't recover after an error
3558 }
3559 } else {
3560 // Should be no error
3561 if(nReturn) {
3562 return -9; // Should not have got an error
3563 }
3564 }
3565 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3566 return -7;
3567 }
3568 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003569 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003570 if(nReturn) {
3571 return -3;
3572 }
3573 return 0;
3574}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003575
3576
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003577int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003578{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303579 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003580 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003581 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003582 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003583 int nReturn = parse_indeflen_nested(Nested, i);
3584 if(nReturn) {
3585 return nReturn;
3586 }
3587 }
3588 return 0;
3589}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003590
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003591
Laurence Lundbladeee851742020-01-08 08:37:05 -08003592// [1, [2, 3]]
3593static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3594// No closing break
3595static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3596// Not enough closing breaks
3597static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3598// Too many closing breaks
3599static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3600// Unclosed indeflen inside def len
3601static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3602// confused tag
3603static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003604
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003605int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003606{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003607 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003608 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003609 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003610
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003611 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303612 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003613 QCBORDecodeContext DC;
3614 QCBORItem Item;
3615 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003616
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003617 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003618
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003619 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303620
3621 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3622 Item.uNestingLevel != 0 ||
3623 Item.uNextNestLevel != 1) {
3624 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003625 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003626
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003627 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303628 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3629 Item.uNestingLevel != 1 ||
3630 Item.uNextNestLevel != 1) {
3631 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003632 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003633
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003634 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303635 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3636 Item.uNestingLevel != 1 ||
3637 Item.uNextNestLevel != 2) {
3638 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003639 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003640
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003641 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003642 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303643 Item.uNestingLevel != 2 ||
3644 Item.uNextNestLevel != 2) {
3645 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003646 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003647
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003648 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003649 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303650 Item.uNestingLevel != 2 ||
3651 Item.uNextNestLevel != 0) {
3652 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003654
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003655 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303656 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003657 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003658
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003659 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003660 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003661
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003662 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003663
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003664 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003665
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003666 nResult = QCBORDecode_GetNext(&DC, &Item);
3667 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303668 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003670
Laurence Lundblade570fab52018-10-13 18:28:27 +08003671 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003672 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303673 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003674 }
3675
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003676
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003677 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003678 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003679
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003680 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003681
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003682 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003683
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003684 nResult = QCBORDecode_GetNext(&DC, &Item);
3685 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303686 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003687 }
3688
3689 nResult = QCBORDecode_GetNext(&DC, &Item);
3690 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303691 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003692 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003693
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003694 nResult = QCBORDecode_GetNext(&DC, &Item);
3695 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303696 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003698
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003699 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003700 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303701 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003702 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003703
3704
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003705 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003706 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003707
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003708 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003709
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003710 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003711
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003712 nResult = QCBORDecode_GetNext(&DC, &Item);
3713 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303714 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003715 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003716
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003717 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003718 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303719 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003720 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303721
Laurence Lundblade642282a2020-06-23 12:00:33 -07003722 nResult = QCBORDecode_GetNext(&DC, &Item);
3723 if(nResult != QCBOR_ERR_BAD_BREAK) {
3724 return -140;
3725 }
3726
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003727
Laurence Lundblade570fab52018-10-13 18:28:27 +08003728 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003729 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003730
Laurence Lundblade570fab52018-10-13 18:28:27 +08003731 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003732
Laurence Lundblade570fab52018-10-13 18:28:27 +08003733 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003734
Laurence Lundblade570fab52018-10-13 18:28:27 +08003735 nResult = QCBORDecode_GetNext(&DC, &Item);
3736 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303737 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003738 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003739
Laurence Lundblade570fab52018-10-13 18:28:27 +08003740 nResult = QCBORDecode_GetNext(&DC, &Item);
3741 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303742 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003743 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003744
Laurence Lundblade570fab52018-10-13 18:28:27 +08003745 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003746 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303747 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003748 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003749
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303750 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003751 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003752
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303753 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003754
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303755 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003756
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303757 nResult = QCBORDecode_GetNext(&DC, &Item);
3758 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303759 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303760 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003761
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303762 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303763 if(nResult != QCBOR_ERR_BAD_BREAK) {
3764 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303765 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003766
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003767 return 0;
3768}
3769
Laurence Lundblade17ede402018-10-13 11:43:07 +08003770
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003771static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003772 0x81, // Array of length one
3773 0x7f, // text string marked with indefinite length
3774 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3775 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3776 0xff // ending break
3777};
3778
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003779static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303780 0x81, // Array of length one
3781 0x7f, // text string marked with indefinite length
3782 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3783 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3784 0xff // ending break
3785};
3786
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003787static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303788 0x81, // Array of length one
3789 0x7f, // text string marked with indefinite length
3790 0x01, 0x02, // Not a string
3791 0xff // ending break
3792};
3793
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003794static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303795 0x81, // Array of length one
3796 0x7f, // text string marked with indefinite length
3797 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3798 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3799 // missing end of string
3800};
3801
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003802static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303803 0xa1, // Array of length one
3804 0x7f, // text string marked with indefinite length
3805 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3806 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3807 0xff, // ending break
3808 0x01 // integer being labeled.
3809};
3810
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003811/**
3812 Make an indefinite length string
3813
3814 @param Storage Storage for string, must be 144 bytes in size
3815 @return The indefinite length string
3816
3817 This makes an array with one indefinite length string that has 7 chunks
3818 from size of 1 byte up to 64 bytes.
3819 */
3820static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303821{
3822 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003823
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303824 UsefulOutBuf_Init(&UOB, Storage);
3825 UsefulOutBuf_AppendByte(&UOB, 0x81);
3826 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003827
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003828 uint8_t uStringByte = 0;
3829 // Use of type int is intentional
3830 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3831 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303832 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003833 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3834 for(int j = 0; j < uChunkSize; j++) {
3835 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3836 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303837 }
3838 }
3839 UsefulOutBuf_AppendByte(&UOB, 0xff);
3840
3841 return UsefulOutBuf_OutUBuf(&UOB);
3842}
3843
3844static int CheckBigString(UsefulBufC BigString)
3845{
3846 if(BigString.len != 255) {
3847 return 1;
3848 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003849
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303850 for(uint8_t i = 0; i < 255; i++){
3851 if(((const uint8_t *)BigString.ptr)[i] != i) {
3852 return 1;
3853 }
3854 }
3855 return 0;
3856}
3857
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303858
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003859int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303860{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303861 QCBORDecodeContext DC;
3862 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303863 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003864 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003865
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303866 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003867 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303868 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003869
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303870 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303871 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303872 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003873
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303874 if(QCBORDecode_GetNext(&DC, &Item)) {
3875 return -2;
3876 }
3877 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3878 return -3;
3879 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003880
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303881 if(QCBORDecode_GetNext(&DC, &Item)) {
3882 return -4;
3883 }
3884 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3885 return -5;
3886 }
3887 if(QCBORDecode_Finish(&DC)) {
3888 return -6;
3889 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303890
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303891 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003892 QCBORDecode_Init(&DC,
3893 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3894 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003895
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303896 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3897 return -7;
3898 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003899
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303900 if(QCBORDecode_GetNext(&DC, &Item)) {
3901 return -8;
3902 }
3903 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3904 return -9;
3905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003906
Laurence Lundblade30816f22018-11-10 13:40:22 +07003907 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303908 return -10;
3909 }
3910
3911 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003912 QCBORDecode_Init(&DC,
3913 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3914 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003915
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303916 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3917 return -11;
3918 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003919
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303920 if(QCBORDecode_GetNext(&DC, &Item)) {
3921 return -12;
3922 }
3923 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3924 return -13;
3925 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003926
Laurence Lundblade30816f22018-11-10 13:40:22 +07003927 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303928 return -14;
3929 }
3930
3931 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003932 QCBORDecode_Init(&DC,
3933 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3934 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003935
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303936 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3937 return -15;
3938 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003939
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303940 if(QCBORDecode_GetNext(&DC, &Item)) {
3941 return -16;
3942 }
3943 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3944 return -17;
3945 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003946
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303947 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3948 return -18;
3949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003950
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303951 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303952 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003953
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303954 QCBORDecode_GetNext(&DC, &Item);
3955 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303956 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303957 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303959 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303960 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303961 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003962
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303963 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003964 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303965
3966 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3967 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303968 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003970
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303971 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303972 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003973 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003974
Laurence Lundbladeee851742020-01-08 08:37:05 -08003975 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3976 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003977
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303978 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303979 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303980 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003982
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303983 QCBORDecode_GetNext(&DC, &Item);
3984 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303985 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303986 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003987 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303988 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303989 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303991 // ---- big bstr -----
3992 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003993
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303994 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3995 return -25;
3996 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003997
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303998 if(QCBORDecode_GetNext(&DC, &Item)) {
3999 return -26;
4000 }
4001 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304002 return -26;
4003 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004004
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304005 if(QCBORDecode_GetNext(&DC, &Item)) {
4006 return -27;
4007 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304008 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304009 return -28;
4010 }
4011 if(CheckBigString(Item.val.string)) {
4012 return -3;
4013 }
4014 if(QCBORDecode_Finish(&DC)) {
4015 return -29;
4016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004017
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304018 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004019 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004020
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304021 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4022 return -30;
4023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004024
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304025 QCBORDecode_GetNext(&DC, &Item);
4026 if(Item.uDataType != QCBOR_TYPE_MAP) {
4027 return -31;
4028 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004029
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304030 if(QCBORDecode_GetNext(&DC, &Item)){
4031 return -32;
4032 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004033 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4034 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304035 Item.uDataAlloc || !Item.uLabelAlloc ||
4036 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4037 return -33;
4038 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004039
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304040 if(QCBORDecode_Finish(&DC)) {
4041 return -34;
4042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004043
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004044 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004045}
4046
4047
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004048int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304049{
4050 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004051 QCBORError nCBORError;
4052
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004053
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304054 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004055 QCBORDecode_Init(&DC,
4056 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4057 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004058
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004059 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004060
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004061 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4062 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304063 return -1;
4064 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004065
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004066 if(CheckCSRMaps(&DC)) {
4067 return -2;
4068 }
4069
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304070 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004071 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004072 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004073
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304074 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004075 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304076 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004077
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304078 QCBORItem Item1, Item2, Item3, Item4;
4079 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004080 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304081 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4082 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004083 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304084 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004085 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304086 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004087 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304088 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004089 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304090 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004091 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004092
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304093 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004094
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304095 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304096 Item1.uDataType != QCBOR_TYPE_INT64 ||
4097 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004098 Item1.uDataAlloc != 0 ||
4099 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004100 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004101 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004103
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304104
4105 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004106 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304107 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004108 Item2.uDataAlloc != 0 ||
4109 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304110 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004111 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004112
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304113 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004114 Item3.uDataAlloc == 0 ||
4115 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004116 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004117 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004119
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304120 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004121 Item4.uDataAlloc == 0 ||
4122 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004123 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004124 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004126
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304127 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004128 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004129 QCBORDecode_Init(&DC,
4130 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4131 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304132 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4133 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004134 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304135 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004136 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004137 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004138 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304139 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4140 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4141 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4142 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4143 }
4144 }
4145 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004146 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004147 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304148 }
4149
4150 return 0;
4151}
4152
Laurence Lundbladef6531662018-12-04 10:42:22 +09004153
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304154
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004155int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004156{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004157 // Set up the decoder with a tiny bit of CBOR to parse because
4158 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004159 QCBORDecodeContext DC;
4160 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4161 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004162
Laurence Lundbladef6531662018-12-04 10:42:22 +09004163 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004164 // Then fish into the internals of the decode context
4165 // to get the allocator function so it can be called directly.
4166 // Also figure out how much pool is available for use
4167 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004168 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004169 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4170 if(nError) {
4171 return -9;
4172 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004173 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4174 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4175 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004176
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004177 // First test -- ask for one more byte than available and see failure
4178 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004179 if(!UsefulBuf_IsNULL(Allocated)) {
4180 return -1;
4181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004182
Laurence Lundbladef6531662018-12-04 10:42:22 +09004183 // Re do the set up for the next test that will do a successful alloc,
4184 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004185 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004186 pAlloc = DC.StringAllocator.pfAllocator;
4187 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4188 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004189
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004190 // Allocate one byte less than available and see success
4191 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004192 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4193 return -2;
4194 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004195 // Ask for some more and see failure
4196 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004197 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4198 return -3;
4199 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004200 // Free the first allocate, retry the second and see success
4201 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4202 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004203 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4204 return -4;
4205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004206
Laurence Lundbladef6531662018-12-04 10:42:22 +09004207 // Re do set up for next test that involves a successful alloc,
4208 // and a successful realloc and a failed realloc
4209 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004210 pAlloc = DC.StringAllocator.pfAllocator;
4211 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004212
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004213 // Allocate half the pool and see success
4214 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004215 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4216 return -5;
4217 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004218 // Reallocate to take up the whole pool and see success
4219 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004220 if(UsefulBuf_IsNULL(Allocated2)) {
4221 return -6;
4222 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004223 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004224 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4225 return -7;
4226 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004227 // Try to allocate more to be sure there is failure after a realloc
4228 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4229 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004230 return -8;
4231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004232
Laurence Lundbladef6531662018-12-04 10:42:22 +09004233 return 0;
4234}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004235
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004236
4237/* Just enough of an allocator to test configuration of one */
4238static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4239{
4240 (void)pOldMem; // unused variable
4241
4242 if(uNewSize) {
4243 // Assumes the context pointer is the buffer and
4244 // nothing too big will ever be asked for.
4245 // This is only good for this basic test!
4246 return (UsefulBuf) {pCtx, uNewSize};
4247 } else {
4248 return NULLUsefulBuf;
4249 }
4250}
4251
4252
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004253int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004254{
4255 // Set up the decoder with a tiny bit of CBOR to parse because
4256 // nothing can be done with it unless that is set up.
4257 QCBORDecodeContext DC;
4258 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4259 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4260
4261 uint8_t pAllocatorBuffer[50];
4262
4263 // This is really just to test that this call works.
4264 // The full functionality of string allocators is tested
4265 // elsewhere with the MemPool internal allocator.
4266 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4267
4268 QCBORItem Item;
4269 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4270 return -1;
4271 }
4272
4273 if(Item.uDataAlloc == 0 ||
4274 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4275 Item.val.string.ptr != pAllocatorBuffer) {
4276 return -2;
4277 }
4278
4279 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4280 return -3;
4281 }
4282
4283 return 0;
4284}
4285
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004286#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004287
Laurence Lundbladea826c502020-05-10 21:07:00 -07004288/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004289 [
4290 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004291 4([-20, 4759477275222530853136]),
4292 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004293 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004294 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004295 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004296 5([ 9223372036854775806, -4759477275222530853137])
4297 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004298 ]
4299 */
4300
4301static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004302 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004303 0xC4, 0x82, 0x20,
4304 0x03,
4305 0xC4, 0x82, 0x33,
4306 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4307 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4308 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4309 0xC5, 0x82, 0x19, 0x01, 0x2C,
4310 0x18, 0x64,
4311 0xC5, 0x82, 0x33,
4312 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4313 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4314 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004315 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4316 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004317 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4318 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4319};
4320
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004321
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004322int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004323{
4324 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004325 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004326 QCBORItem item;
4327
Laurence Lundblade17af4902020-01-07 19:11:55 -08004328 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4329 0x06, 0x07, 0x08, 0x09, 0x010};
4330 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004331
4332
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004333 QCBORDecode_Init(&DC,
4334 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4335 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004336
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004337 uErr = QCBORDecode_GetNext(&DC, &item);
4338 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004339 return 1;
4340 }
4341
4342 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4343 return 2;
4344 }
4345
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004346 uErr = QCBORDecode_GetNext(&DC, &item);
4347 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004348 return 3;
4349 }
4350
4351 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4352 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4353 item.val.expAndMantissa.nExponent != -1) {
4354 return 4;
4355 }
4356
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004357 uErr = QCBORDecode_GetNext(&DC, &item);
4358 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004359 return 5;
4360 }
4361
4362 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4363 item.val.expAndMantissa.nExponent != -20 ||
4364 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4365 return 6;
4366 }
4367
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004368 uErr = QCBORDecode_GetNext(&DC, &item);
4369 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004370 return 7;
4371 }
4372
4373 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4374 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4375 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4376 return 8;
4377 }
4378
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004379 uErr = QCBORDecode_GetNext(&DC, &item);
4380 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004381 return 9;
4382 }
4383
4384 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4385 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4386 item.val.expAndMantissa.nExponent != 300) {
4387 return 10;
4388 }
4389
Laurence Lundbladea826c502020-05-10 21:07:00 -07004390 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004391 uErr = QCBORDecode_GetNext(&DC, &item);
4392 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004393 return 11;
4394 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004395 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4396 item.val.expAndMantissa.nExponent != -20 ||
4397 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4398 return 12;
4399 }
4400
Laurence Lundbladea826c502020-05-10 21:07:00 -07004401 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004402 uErr = QCBORDecode_GetNext(&DC, &item);
4403 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004404 return 13;
4405 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004406 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4407 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4408 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4409 return 14;
4410 }
4411
Laurence Lundbladea826c502020-05-10 21:07:00 -07004412 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004413 uErr = QCBORDecode_GetNext(&DC, &item);
4414 if(uErr != QCBOR_SUCCESS) {
4415 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004416 }
4417 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4418 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4419 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004420 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004421 }
4422
Laurence Lundbladea826c502020-05-10 21:07:00 -07004423 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004424 uErr = QCBORDecode_GetNext(&DC, &item);
4425 if(uErr != QCBOR_SUCCESS) {
4426 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004427 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004428 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4429 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4430 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004431 return 18;
4432 }
4433
4434 uErr = QCBORDecode_Finish(&DC);
4435 if(uErr != QCBOR_SUCCESS) {
4436 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004437 }
4438
4439 /* Now encode some stuff and then decode it */
4440 uint8_t pBuf[40];
4441 QCBOREncodeContext EC;
4442 UsefulBufC Encoded;
4443
4444 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4445 QCBOREncode_OpenArray(&EC);
4446 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4447 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4448 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4449 QCBOREncode_CloseArray(&EC);
4450 QCBOREncode_Finish(&EC, &Encoded);
4451
4452
4453 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004454 uErr = QCBORDecode_GetNext(&DC, &item);
4455 if(uErr != QCBOR_SUCCESS) {
4456 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004457 }
4458
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004459 uErr = QCBORDecode_GetNext(&DC, &item);
4460 if(uErr != QCBOR_SUCCESS) {
4461 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004462 }
4463
4464 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4465 item.val.expAndMantissa.nExponent != 1000 ||
4466 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004467 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004468 }
4469
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004470 uErr = QCBORDecode_GetNext(&DC, &item);
4471 if(uErr != QCBOR_SUCCESS) {
4472 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004473 }
4474
4475 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4476 item.val.expAndMantissa.nExponent != INT32_MIN ||
4477 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004478 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004479 }
4480
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004481 uErr = QCBORDecode_GetNext(&DC, &item);
4482 if(uErr != QCBOR_SUCCESS) {
4483 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004484 }
4485
4486 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4487 item.val.expAndMantissa.nExponent != INT32_MAX ||
4488 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004489 return 106;
4490 }
4491
4492
4493 int64_t nExp, nMant;
4494 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4495 UsefulBufC Mant;
4496 bool bIsNeg;
4497
4498 QCBORDecode_Init(&DC,
4499 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4500 QCBOR_DECODE_MODE_NORMAL);
4501 QCBORDecode_EnterArray(&DC);
4502
4503 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004504 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004505
4506 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004507 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4508 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004509
4510 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004511 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4512 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004513
4514 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004515 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004516
4517 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004518 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4519 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004520
4521 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004522 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4523 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004524
4525 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004526 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4527 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004528
4529 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004530 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4531 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004532
4533 QCBORDecode_ExitArray(&DC);
4534
4535 uErr = QCBORDecode_Finish(&DC);
4536 if(uErr != QCBOR_SUCCESS) {
4537 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004538 }
4539
4540 return 0;
4541}
4542
4543
4544static struct FailInput ExponentAndMantissaFailures[] = {
4545 // Exponent > INT64_MAX
4546 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4547 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4548 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4549 // Mantissa > INT64_MAX
4550 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4551 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4552 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4553 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004554 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004555 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004556 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004557 // bad content for big num
4558 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4559 // bad content for big num
4560 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4561 // Bad integer for exponent
4562 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4563 // Bad integer for mantissa
4564 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4565 // 3 items in array
4566 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4567 // unterminated indefinite length array
4568 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4569 // Empty array
4570 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4571 // Second is not an integer
4572 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4573 // First is not an integer
4574 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4575 // Not an array
4576 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4577};
4578
4579
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004580int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004581{
4582 return ProcessFailures(ExponentAndMantissaFailures,
4583 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4584}
4585
4586#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004587
4588
4589
4590/*
4591 Some basic CBOR with map and array used in a lot of tests.
4592 The map labels are all strings
4593
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004594 {
4595 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004596 "an array of two strings": [
4597 "string1", "string2"
4598 ],
4599 "map in a map": {
4600 "bytes 1": h'78787878',
4601 "bytes 2": h'79797979',
4602 "another int": 98,
4603 "text 2": "lies, damn lies and statistics"
4604 }
4605 }
4606 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004607
Laurence Lundblade1341c592020-04-11 14:19:05 -07004608#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004609
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004610static char strbuf[10];
4611const char *PrintType(uint8_t type) {
4612 switch(type) {
4613 case QCBOR_TYPE_INT64: return "INT64";
4614 case QCBOR_TYPE_UINT64: return "UINT64";
4615 case QCBOR_TYPE_ARRAY: return "ARRAY";
4616 case QCBOR_TYPE_MAP: return "MAP";
4617 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
4618 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
4619 default:
4620 sprintf(strbuf, "%d", type);
4621 return strbuf;
4622 }
4623}
4624
4625
4626void PrintItem(QCBORItem Item)
4627{
Laurence Lundblade9b334962020-08-27 10:55:53 -07004628 printf("\nData: %s nest: %d,%d %s\n",
4629 PrintType(Item.uDataType),
4630 Item.uNestingLevel,
4631 Item.uNextNestLevel,
4632 Item.uDataAlloc ? "Allocated":"");
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004633 if(Item.uLabelType) {
4634 printf("Label: %s ", PrintType(Item.uLabelType));
4635 if(Item.uLabelType == QCBOR_TYPE_INT64) {
4636 printf("%lld\n", Item.label.int64);
4637 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07004638 // TODO: proper conversion to null-terminated string
4639 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004640 }
4641 }
4642}
4643
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004644
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004645int32_t EMap(UsefulBufC input)
4646{
4647 QCBORItem Item1, Item2, Item3;
4648 int64_t nDecodedInt1, nDecodedInt2;
4649 UsefulBufC B1, B2, S1, S2, S3;
4650
4651 QCBORDecodeContext DCtx;
4652 QCBORError nCBORError;
4653
4654 QCBORDecode_Init(&DCtx, input, 0);
4655
4656 QCBORDecode_EnterMap(&DCtx);
4657
4658 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4659
4660 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4661 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004662 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4663 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4664 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004665 QCBORDecode_ExitMap(&DCtx);
4666
4667 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4668 QCBORDecode_GetNext(&DCtx, &Item1);
4669 QCBORDecode_GetNext(&DCtx, &Item2);
4670 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4671 return -400;
4672 }
4673 QCBORDecode_ExitArray(&DCtx);
4674
4675 // Parse the same array again using GetText() instead of GetItem()
4676 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004677 QCBORDecode_GetTextString(&DCtx, &S2);
4678 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004679 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4680 return 5000;
4681 }
4682 /* QCBORDecode_GetText(&DCtx, &S3);
4683 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4684 return 5001;
4685 } */
4686
4687 QCBORDecode_ExitArray(&DCtx);
4688
4689 QCBORDecode_ExitMap(&DCtx);
4690
4691 nCBORError = QCBORDecode_Finish(&DCtx);
4692
4693 if(nCBORError) {
4694 return (int32_t)nCBORError;
4695 }
4696
4697 if(nDecodedInt1 != 42) {
4698 return 1001;
4699 }
4700
4701 if(nDecodedInt2 != 98) {
4702 return 1002;
4703 }
4704
4705 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004706 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004707 return 1003;
4708 }
4709
4710 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004711 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004712 return 1004;
4713 }
4714
Laurence Lundblade9b334962020-08-27 10:55:53 -07004715 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004716 return 1005;
4717 }
4718
4719 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4720 return 1006;
4721 }
4722
4723 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4724 return 1007;
4725 }
4726
4727 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4728 return 1008;
4729 }
4730
4731 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4732 return 1009;
4733 }
4734
4735 return 0;
4736}
4737
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004738
4739/*
4740 [23,
4741 6000,
4742 h'67616C6163746963',
4743 h'686176656E20746F6B656E'
4744 ]
4745 */
4746static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004747 0x84,
4748 0x17,
4749 0x19, 0x17, 0x70,
4750 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4751 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004752
4753
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004754static const uint8_t spEmptyMap[] = {0xa0};
4755
4756static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004757
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004758static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4759
Laurence Lundbladef0499502020-08-01 11:55:57 -07004760/*
4761 {
4762 0: [],
4763 9: [
4764 [],
4765 []
4766 ],
4767 8: {
4768 1: [],
4769 2: {},
4770 3: []
4771 },
4772 4: {},
4773 5: [],
4774 6: [
4775 [],
4776 []
4777 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004778 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004779 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004780
4781
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 Lundbladebb87be22020-04-09 19:15:32 -07004787int32_t EnterMapTest()
4788{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004789 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004790 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004791 int32_t nReturn;
4792 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004793
4794
4795 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4796 QCBORDecode_EnterMap(&DCtx);
4797
Laurence Lundbladef0499502020-08-01 11:55:57 -07004798
4799 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004800 QCBORDecode_ExitArray(&DCtx);
4801
Laurence Lundbladef0499502020-08-01 11:55:57 -07004802 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004803 QCBORDecode_EnterArray(&DCtx);
4804 QCBORDecode_ExitArray(&DCtx);
4805 QCBORDecode_EnterArray(&DCtx);
4806 QCBORDecode_ExitArray(&DCtx);
4807 QCBORDecode_ExitArray(&DCtx);
4808
Laurence Lundbladef0499502020-08-01 11:55:57 -07004809 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004810 QCBORDecode_EnterArray(&DCtx);
4811 QCBORDecode_ExitArray(&DCtx);
4812 QCBORDecode_EnterMap(&DCtx);
4813 QCBORDecode_ExitMap(&DCtx);
4814 QCBORDecode_EnterArray(&DCtx);
4815 QCBORDecode_ExitArray(&DCtx);
4816 QCBORDecode_ExitMap(&DCtx);
4817
Laurence Lundbladef0499502020-08-01 11:55:57 -07004818 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004819 QCBORDecode_ExitMap(&DCtx);
4820
Laurence Lundbladef0499502020-08-01 11:55:57 -07004821 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004822 QCBORDecode_ExitArray(&DCtx);
4823
Laurence Lundbladef0499502020-08-01 11:55:57 -07004824 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004825 QCBORDecode_EnterArray(&DCtx);
4826 QCBORDecode_ExitArray(&DCtx);
4827 QCBORDecode_EnterArray(&DCtx);
4828 QCBORDecode_ExitArray(&DCtx);
4829 QCBORDecode_ExitArray(&DCtx);
4830
4831 QCBORDecode_ExitMap(&DCtx);
4832
4833 uErr = QCBORDecode_Finish(&DCtx);
4834 if(uErr != QCBOR_SUCCESS){
4835 return 3011;
4836 }
4837
4838
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004839 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004840 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004841 if(nReturn) {
4842 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004843 }
4844
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004845 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004846 if(nReturn) {
4847 return nReturn;
4848 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004849
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004850
Laurence Lundblade937ea812020-05-08 11:38:23 -07004851
4852 // These tests confirm the cursor is at the right place after entering a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004853 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004854
4855 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004856 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004857 QCBORDecode_EnterMap(&DCtx);
4858 QCBORDecode_GetNext(&DCtx, &Item1);
4859 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4860 return 2001;
4861 }
4862
4863
Laurence Lundblade9b334962020-08-27 10:55:53 -07004864 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004865 QCBORDecode_VGetNext(&DCtx, &Item1);
4866 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004867 QCBORDecode_EnterArray(&DCtx);
4868 QCBORDecode_GetNext(&DCtx, &Item1);
4869 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4870 return 2002;
4871 }
4872
Laurence Lundblade9b334962020-08-27 10:55:53 -07004873 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004874 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004875 QCBORDecode_GetNext(&DCtx, &Item1);
4876 QCBORDecode_GetNext(&DCtx, &Item1);
4877 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004878 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4879 QCBORDecode_GetNext(&DCtx, &Item1);
4880 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4881 return 2003;
4882 }
4883
Laurence Lundblade9b334962020-08-27 10:55:53 -07004884 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004885 QCBORDecode_EnterMap(&DCtx);
4886 QCBORDecode_GetNext(&DCtx, &Item1);
4887 QCBORDecode_GetNext(&DCtx, &Item1);
4888 QCBORDecode_GetNext(&DCtx, &Item1);
4889 QCBORDecode_GetNext(&DCtx, &Item1);
4890 QCBORDecode_GetNext(&DCtx, &Item1);
4891 QCBORDecode_GetNext(&DCtx, &Item1);
4892 QCBORDecode_GetNext(&DCtx, &Item1);
4893 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4894 QCBORDecode_GetNext(&DCtx, &Item1);
4895 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004896 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004897 }
4898
Laurence Lundblade9b334962020-08-27 10:55:53 -07004899 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004900 QCBORDecode_EnterMap(&DCtx);
4901 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4902 QCBORDecode_ExitArray(&DCtx);
4903 QCBORDecode_GetNext(&DCtx, &Item1);
4904 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4905 return 2006;
4906 }
4907 QCBORDecode_ExitMap(&DCtx);
4908 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4909 return 2007;
4910 }
4911
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004912 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4913 QCBORDecode_EnterArray(&DCtx);
4914 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004915 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4916 uErr = QCBORDecode_GetAndResetError(&DCtx);
4917 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004918 return 2008;
4919 }
4920 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004921 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004922 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004923 return 2009;
4924 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004925
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004926
4927 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4928 QCBORDecode_EnterMap(&DCtx);
4929 // This will fail because the map is empty.
4930 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4931 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004932 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004933 return 2010;
4934 }
4935 QCBORDecode_ExitMap(&DCtx);
4936 uErr = QCBORDecode_Finish(&DCtx);
4937 if(uErr != QCBOR_SUCCESS){
4938 return 2011;
4939 }
4940
4941
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004942 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4943 QCBORDecode_EnterMap(&DCtx);
4944 // This will fail because the map is empty.
4945 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4946 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004947 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004948 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004949 }
4950 QCBORDecode_ExitMap(&DCtx);
4951 uErr = QCBORDecode_Finish(&DCtx);
4952 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004953 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004954 }
4955
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004956
4957 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
4958 QCBORDecode_EnterArray(&DCtx);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004959 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004960 QCBORDecode_EnterMap(&DCtx);
4961 QCBORDecode_ExitMap(&DCtx);
4962 QCBORDecode_EnterArray(&DCtx);
4963 QCBORDecode_ExitArray(&DCtx);
4964 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
4965 QCBORDecode_ExitArray(&DCtx);
4966 uErr = QCBORDecode_Finish(&DCtx);
4967 if(uErr != QCBOR_SUCCESS){
4968 return 2014;
4969 }
4970
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07004971 // TODO: more testing of entered mapps and arrays with problems
4972 // TODO: document error handling better (maybe improve error handling)
4973
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004974 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004975}
4976
4977
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004978struct NumberConversion {
4979 char *szDescription;
4980 UsefulBufC CBOR;
4981 int64_t nConvertedToInt64;
4982 QCBORError uErrorInt64;
4983 uint64_t uConvertToUInt64;
4984 QCBORError uErrorUint64;
4985 double dConvertToDouble;
4986 QCBORError uErrorDouble;
4987};
4988
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004989static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004990 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07004991 "too large to fit into int64_t",
4992 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
4993 0,
4994 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4995 0,
4996 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4997 ((double)INT64_MIN) + 1 ,
4998 QCBOR_SUCCESS
4999 },
5000 {
5001 "largest negative int that fits in int64_t",
5002 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5003 INT64_MIN,
5004 QCBOR_SUCCESS,
5005 0,
5006 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5007 (double)INT64_MIN,
5008 QCBOR_SUCCESS
5009 },
5010 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005011 "negative bignum -1",
5012 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5013 -1,
5014 QCBOR_SUCCESS,
5015 0,
5016 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5017 -1.0,
5018 QCBOR_SUCCESS
5019 },
5020 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005021 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005022 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5023 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005024#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005025 257000,
5026 QCBOR_SUCCESS,
5027 257000,
5028 QCBOR_SUCCESS,
5029 257000.0,
5030 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005031#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5032 0,
5033 QCBOR_ERR_UNEXPECTED_TYPE,
5034 0,
5035 QCBOR_ERR_UNEXPECTED_TYPE,
5036 0.0,
5037 QCBOR_ERR_UNEXPECTED_TYPE
5038#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005039 },
5040 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005041 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005042 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5043 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005044#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005045 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005046 QCBOR_SUCCESS,
5047 0,
5048 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005049 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005050 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005051#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5052 0,
5053 QCBOR_ERR_UNEXPECTED_TYPE,
5054 0,
5055 QCBOR_ERR_UNEXPECTED_TYPE,
5056 0.0,
5057 QCBOR_ERR_UNEXPECTED_TYPE
5058#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005059 },
5060 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005061 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005062 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5063 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005064#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005065 2056,
5066 QCBOR_SUCCESS,
5067 2056,
5068 QCBOR_SUCCESS,
5069 2056.0,
5070 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005071#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5072 0,
5073 QCBOR_ERR_UNEXPECTED_TYPE,
5074 0,
5075 QCBOR_ERR_UNEXPECTED_TYPE,
5076 0.0,
5077 QCBOR_ERR_UNEXPECTED_TYPE
5078#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005079 },
5080 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005081 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005082 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5083 0,
5084 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5085 0,
5086 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5087 -18446744073709551617.0,
5088 QCBOR_SUCCESS
5089 },
5090 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005091 "Positive bignum 0x01020304 indefinite length string",
5092 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5093 0x01020304,
5094 QCBOR_SUCCESS,
5095 0x01020304,
5096 QCBOR_SUCCESS,
5097 16909060.0,
5098 QCBOR_SUCCESS
5099 },
5100 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005101 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005102 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5103 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005104#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005105 0,
5106 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5107 0,
5108 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5109 -INFINITY,
5110 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005111#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5112 0,
5113 QCBOR_ERR_UNEXPECTED_TYPE,
5114 0,
5115 QCBOR_ERR_UNEXPECTED_TYPE,
5116 0.0,
5117 QCBOR_ERR_UNEXPECTED_TYPE
5118#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005119 },
5120 {
5121 "big float [9223372036854775806, 9223372036854775806]",
5122 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5123 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005124#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005125 0,
5126 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5127 0,
5128 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5129 INFINITY,
5130 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005131#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5132 0,
5133 QCBOR_ERR_UNEXPECTED_TYPE,
5134 0,
5135 QCBOR_ERR_UNEXPECTED_TYPE,
5136 0.0,
5137 QCBOR_ERR_UNEXPECTED_TYPE
5138#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005139 },
5140 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005141 "Big float 3 * 2^^2",
5142 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005143#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005144 12,
5145 QCBOR_SUCCESS,
5146 12,
5147 QCBOR_SUCCESS,
5148 12.0,
5149 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005150#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5151 0,
5152 QCBOR_ERR_UNEXPECTED_TYPE,
5153 0,
5154 QCBOR_ERR_UNEXPECTED_TYPE,
5155 0.0,
5156 QCBOR_ERR_UNEXPECTED_TYPE
5157#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005158 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005159 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005160 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005161 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5162 0,
5163 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5164 18446744073709551615ULL,
5165 QCBOR_SUCCESS,
5166 18446744073709551615.0,
5167 QCBOR_SUCCESS
5168 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005169 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005170 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005171 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5172 65536-1,
5173 QCBOR_SUCCESS,
5174 0xffff,
5175 QCBOR_SUCCESS,
5176 65535.0,
5177 QCBOR_SUCCESS
5178 },
5179 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005180 "Postive integer 0",
5181 {(uint8_t[]){0x0}, 1},
5182 0LL,
5183 QCBOR_SUCCESS,
5184 0ULL,
5185 QCBOR_SUCCESS,
5186 0.0,
5187 QCBOR_SUCCESS
5188 },
5189 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005190 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005191 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5192 -9223372036854775807-1, // INT64_MIN
5193 QCBOR_SUCCESS,
5194 0ULL,
5195 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5196 -9223372036854775808.0,
5197 QCBOR_SUCCESS
5198 },
5199 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005200 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005201 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005202#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005203 100L,
5204 QCBOR_SUCCESS,
5205 100ULL,
5206 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005207#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5208 0,
5209 QCBOR_ERR_HW_FLOAT_DISABLED,
5210 0,
5211 QCBOR_ERR_HW_FLOAT_DISABLED,
5212#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005213 100.3,
5214 QCBOR_SUCCESS
5215 },
5216 {
5217 "Floating point value NaN 0xfa7fc00000",
5218 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005219#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005220 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005221 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005222 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005223 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005224#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5225 0,
5226 QCBOR_ERR_HW_FLOAT_DISABLED,
5227 0,
5228 QCBOR_ERR_HW_FLOAT_DISABLED,
5229#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005230 NAN,
5231 QCBOR_SUCCESS
5232 },
5233 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005234 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005235 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005236#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5237#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5238 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005239 -4,
5240 QCBOR_SUCCESS,
5241 0,
5242 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5243 -4.0,
5244 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005245#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5246 // Float HW disabled
5247 -4,
5248 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5249 0,
5250 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5251 -4.0,
5252 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5253#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5254#else
5255 // Half-precision disabled
5256 -4,
5257 QCBOR_ERR_HALF_PRECISION_DISABLED,
5258 0,
5259 QCBOR_ERR_HALF_PRECISION_DISABLED,
5260 -4.0,
5261 QCBOR_ERR_HALF_PRECISION_DISABLED
5262#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005263 },
5264 {
5265 "Decimal fraction 3/10",
5266 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005267#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005268 0,
5269 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5270 0,
5271 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5272 0.30000000000000004,
5273 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005274#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5275 0,
5276 QCBOR_ERR_UNEXPECTED_TYPE,
5277 0,
5278 QCBOR_ERR_UNEXPECTED_TYPE,
5279 0.0,
5280 QCBOR_ERR_UNEXPECTED_TYPE
5281#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005282 },
5283 {
5284 "+inifinity",
5285 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5286#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5287 0,
5288 QCBOR_ERR_FLOAT_EXCEPTION,
5289 0,
5290 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5291#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5292 0,
5293 QCBOR_ERR_HW_FLOAT_DISABLED,
5294 0,
5295 QCBOR_ERR_HW_FLOAT_DISABLED,
5296#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5297 INFINITY,
5298 QCBOR_SUCCESS
5299 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005300
5301 {
5302 "extreme pos bignum",
5303 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5304 // 50 rows of 8 is 400 digits.
5305 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5306 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5307 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5308 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5309 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5310 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5311 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5312 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5313 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5314 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5315 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5316 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5317 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5318 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5319 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5320 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5321 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5322 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5323 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5324 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5325 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5326 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5327 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5328 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5329 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5330 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5331 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5332 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5333 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5334 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5335 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5336 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5337 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5338 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5339 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5340 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5341 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5342 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5343 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5344 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5345 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5346 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5347 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5348 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5349 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5350 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5351 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5352 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5353 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5354 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5355 404},
5356 0,
5357 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5358 0,
5359 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5360#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5361 INFINITY,
5362 QCBOR_SUCCESS
5363#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5364 0,
5365 QCBOR_ERR_HW_FLOAT_DISABLED,
5366#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5367 },
5368
5369 {
5370 "extreme neg bignum",
5371 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5372 // 50 rows of 8 is 400 digits.
5373 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5374 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5375 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5376 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5377 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5378 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5379 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5380 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5381 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5382 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5383 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5384 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5385 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5386 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5387 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5388 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5389 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5390 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5391 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5392 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5393 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5394 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5395 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5396 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5397 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5398 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5399 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5400 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5401 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5402 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5403 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5404 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5405 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5406 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5407 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5408 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5409 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5410 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5411 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5412 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5413 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5414 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5415 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5416 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5417 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5418 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5419 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5420 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5421 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5422 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5423 404},
5424 0,
5425 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5426 0,
5427 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5428#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5429 -INFINITY,
5430 QCBOR_SUCCESS
5431#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5432 0,
5433 QCBOR_ERR_HW_FLOAT_DISABLED,
5434#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5435 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005436
5437 {
5438 "big float underflow [9223372036854775806, -9223372036854775806]",
5439 {(uint8_t[]){
5440 0xC5, 0x82,
5441 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5442 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5443#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5444 0,
5445 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5446 0,
5447 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5448 0,
5449 QCBOR_SUCCESS
5450#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5451 0,
5452 QCBOR_ERR_UNEXPECTED_TYPE,
5453 0,
5454 QCBOR_ERR_UNEXPECTED_TYPE,
5455 0.0,
5456 QCBOR_ERR_UNEXPECTED_TYPE
5457#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5458 },
5459
5460 {
5461 "bigfloat that evaluates to -INFINITY",
5462 {(uint8_t[]){
5463 0xC5, 0x82,
5464 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5465 0xC3, 0x42, 0x01, 0x01}, 15},
5466#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5467 0,
5468 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5469 0,
5470 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5471 -INFINITY,
5472 QCBOR_SUCCESS
5473#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5474 0,
5475 QCBOR_ERR_UNEXPECTED_TYPE,
5476 0,
5477 QCBOR_ERR_UNEXPECTED_TYPE,
5478 0.0,
5479 QCBOR_ERR_UNEXPECTED_TYPE
5480#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5481 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005482};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005483
5484
5485
Laurence Lundblade313b2862020-05-16 01:23:06 -07005486int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005487{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005488 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005489
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005490 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5491 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005492
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005493 // Set up the decoding context including a memory pool so that
5494 // indefinite length items can be checked
5495 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005496 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005497
5498 /* ----- test conversion to int64_t ------ */
5499 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005500 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5501 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005502 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005503 }
5504
5505 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005506 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005507 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005508 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005509 }
5510 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005511 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005512 }
5513
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005514 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005515 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5516 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5517 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005518 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005519 }
5520 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005521 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005522 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005523 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005524 }
5525 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005526 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005527 }
5528
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005529 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005530 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5531 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5532 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005533 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005534 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005535#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005536 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005537 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005538 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005539 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005540 }
5541 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5542 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005543 // NaN's can't be compared for equality. A NaN is
5544 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005545 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005546 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005547 }
5548 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005549 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005550 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005551 }
5552 }
5553 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005554#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005555 }
5556
5557 return 0;
5558}
5559
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005560
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005561
5562
Laurence Lundbladee3553422020-05-02 11:11:17 -07005563int32_t CBORSequenceDecodeTests(void)
5564{
5565 QCBORDecodeContext DCtx;
5566 QCBORItem Item;
5567 QCBORError uCBORError;
5568
5569 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005570
Laurence Lundbladee3553422020-05-02 11:11:17 -07005571 // The input for the date test happens to be a sequence so it
5572 // is reused. It is a sequence because it doesn't start as
5573 // an array or map.
5574 QCBORDecode_Init(&DCtx,
5575 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5576 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005577
Laurence Lundbladee3553422020-05-02 11:11:17 -07005578 // Get the first item
5579 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5580 if(uCBORError != QCBOR_SUCCESS) {
5581 return 1;
5582 }
5583 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5584 return 2;
5585 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005586
Laurence Lundbladee3553422020-05-02 11:11:17 -07005587 // Get a second item
5588 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005589 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5590 return 66;
5591 }
5592
5593 // Get a third item
5594 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005595 if(uCBORError != QCBOR_SUCCESS) {
5596 return 2;
5597 }
5598 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5599 return 3;
5600 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005601
Laurence Lundbladee3553422020-05-02 11:11:17 -07005602 // A sequence can have stuff at the end that may
5603 // or may not be valid CBOR. The protocol decoder knows
5604 // when to stop by definition of the protocol, not
5605 // when the top-level map or array is ended.
5606 // Finish still has to be called to know that
5607 // maps and arrays (if there were any) were closed
5608 // off correctly. When called like this it
5609 // must return the error QCBOR_ERR_EXTRA_BYTES.
5610 uCBORError = QCBORDecode_Finish(&DCtx);
5611 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5612 return 4;
5613 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005614
5615
Laurence Lundbladee3553422020-05-02 11:11:17 -07005616 // --- Test an empty input ----
5617 uint8_t empty[1];
5618 UsefulBufC Empty = {empty, 0};
5619 QCBORDecode_Init(&DCtx,
5620 Empty,
5621 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005622
Laurence Lundbladee3553422020-05-02 11:11:17 -07005623 uCBORError = QCBORDecode_Finish(&DCtx);
5624 if(uCBORError != QCBOR_SUCCESS) {
5625 return 5;
5626 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005627
5628
Laurence Lundbladee3553422020-05-02 11:11:17 -07005629 // --- Sequence with unclosed indefinite length array ---
5630 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005631
Laurence Lundbladee3553422020-05-02 11:11:17 -07005632 QCBORDecode_Init(&DCtx,
5633 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5634 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005635
Laurence Lundbladee3553422020-05-02 11:11:17 -07005636 // Get the first item
5637 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5638 if(uCBORError != QCBOR_SUCCESS) {
5639 return 7;
5640 }
5641 if(Item.uDataType != QCBOR_TYPE_INT64) {
5642 return 8;
5643 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005644
Laurence Lundbladee3553422020-05-02 11:11:17 -07005645 // Get a second item
5646 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5647 if(uCBORError != QCBOR_SUCCESS) {
5648 return 9;
5649 }
5650 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5651 return 10;
5652 }
5653
5654 // Try to finish before consuming all bytes to confirm
5655 // that the still-open error is returned.
5656 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005657 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005658 return 11;
5659 }
5660
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005661
Laurence Lundbladee3553422020-05-02 11:11:17 -07005662 // --- Sequence with a closed indefinite length array ---
5663 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005664
Laurence Lundbladee3553422020-05-02 11:11:17 -07005665 QCBORDecode_Init(&DCtx,
5666 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5667 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005668
Laurence Lundbladee3553422020-05-02 11:11:17 -07005669 // Get the first item
5670 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5671 if(uCBORError != QCBOR_SUCCESS) {
5672 return 12;
5673 }
5674 if(Item.uDataType != QCBOR_TYPE_INT64) {
5675 return 13;
5676 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005677
Laurence Lundbladee3553422020-05-02 11:11:17 -07005678 // Get a second item
5679 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5680 if(uCBORError != QCBOR_SUCCESS) {
5681 return 14;
5682 }
5683 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5684 return 15;
5685 }
5686
5687 // Try to finish before consuming all bytes to confirm
5688 // that the still-open error is returned.
5689 uCBORError = QCBORDecode_Finish(&DCtx);
5690 if(uCBORError != QCBOR_SUCCESS) {
5691 return 16;
5692 }
5693
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005694
Laurence Lundbladee3553422020-05-02 11:11:17 -07005695 return 0;
5696}
5697
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005698
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005699
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005700int32_t IntToTests()
5701{
5702 int nErrCode;
5703 int32_t n32;
5704 int16_t n16;
5705 int8_t n8;
5706 uint32_t u32;
5707 uint16_t u16;
5708 uint8_t u8;
5709 uint64_t u64;
5710
5711 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5712 if(nErrCode == -1 || n32 != 1) {
5713 return 1;
5714 }
5715
5716 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5717 if(nErrCode == -1 || n32 != INT32_MAX) {
5718 return 2;
5719 }
5720
5721 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5722 if(nErrCode == -1 || n32 != INT32_MIN) {
5723 return 3;
5724 }
5725
5726 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5727 if(nErrCode != -1) {
5728 return 4;
5729 }
5730
5731 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5732 if(nErrCode != -1) {
5733 return 5;
5734 }
5735
5736
5737 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5738 if(nErrCode == -1 || n16 != INT16_MAX) {
5739 return 6;
5740 }
5741
5742 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5743 if(nErrCode == -1 || n16 != INT16_MIN) {
5744 return 7;
5745 }
5746
5747 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5748 if(nErrCode == -1 || n16 != 1) {
5749 return 8;
5750 }
5751
5752 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5753 if(nErrCode != -1) {
5754 return 9;
5755 }
5756
5757 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5758 if(nErrCode != -1) {
5759 return 10;
5760 }
5761
5762
5763 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5764 if(nErrCode == -1 || n8 != 1) {
5765 return 11;
5766 }
5767
5768 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5769 if(nErrCode == -1 || n8 != INT8_MAX) {
5770 return 12;
5771 }
5772
5773 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5774 if(nErrCode == -1 || n8 != INT8_MIN) {
5775 return 13;
5776 }
5777
5778 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5779 if(nErrCode != -1) {
5780 return 14;
5781 }
5782
5783 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5784 if(nErrCode != -1) {
5785 return 15;
5786 }
5787
5788
5789 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5790 if(nErrCode == -1 || u32 != 1) {
5791 return 16;
5792 }
5793
5794 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5795 if(nErrCode == -1 || u32 != UINT32_MAX) {
5796 return 17;
5797 }
5798
5799 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5800 if(nErrCode == -1 || u32 != 0) {
5801 return 18;
5802 }
5803
5804 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5805 if(nErrCode != -1) {
5806 return 19;
5807 }
5808
5809 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5810 if(nErrCode != -1) {
5811 return 20;
5812 }
5813
5814
5815 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5816 if(nErrCode == -1 || u16 != UINT16_MAX) {
5817 return 21;
5818 }
5819
5820 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5821 if(nErrCode == -1 || u16 != 0) {
5822 return 22;
5823 }
5824
5825 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5826 if(nErrCode == -1 || u16 != 1) {
5827 return 23;
5828 }
5829
5830 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5831 if(nErrCode != -1) {
5832 return 24;
5833 }
5834
5835 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5836 if(nErrCode != -1) {
5837 return 25;
5838 }
5839
5840
5841 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5842 if(nErrCode == -1 || u8 != UINT8_MAX) {
5843 return 26;
5844 }
5845
5846 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5847 if(nErrCode == -1 || u8 != 0) {
5848 return 27;
5849 }
5850
5851 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5852 if(nErrCode == -1 || u8 != 1) {
5853 return 28;
5854 }
5855
5856 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5857 if(nErrCode != -1) {
5858 return 29;
5859 }
5860
5861 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5862 if(nErrCode != -1) {
5863 return 30;
5864 }
5865
5866
5867 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5868 if(nErrCode == -1 || u64 != 1) {
5869 return 31;
5870 }
5871
5872 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
5873 if(nErrCode == -1 || u64 != INT64_MAX) {
5874 return 32;
5875 }
5876
5877 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
5878 if(nErrCode == -1 || u64 != 0) {
5879 return 33;
5880 }
5881
5882 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
5883 if(nErrCode != -1) {
5884 return 34;
5885 }
5886
5887 return 0;
5888}
5889
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005890
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005891
5892
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005893/*
5894A sequence with
5895 A wrapping bstr
5896 containing a map
5897 1
5898 2
5899 A wrapping bstr
5900 containing an array
5901 3
5902 wrapping bstr
5903 4
5904 5
5905 6
5906 array
5907 7
5908 8
5909
5910 */
5911
5912static UsefulBufC foo(UsefulBuf ffo)
5913{
5914 UsefulBufC Encoded;
5915 QCBOREncodeContext EC;
5916 QCBORError uErr;
5917
5918 QCBOREncode_Init(&EC, ffo);
5919
5920 QCBOREncode_BstrWrap(&EC);
5921 QCBOREncode_OpenMap(&EC);
5922 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
5923 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
5924 QCBOREncode_CloseMap(&EC);
5925 QCBOREncode_BstrWrap(&EC);
5926 QCBOREncode_OpenArray(&EC);
5927 QCBOREncode_AddInt64(&EC, 3);
5928 QCBOREncode_BstrWrap(&EC);
5929 QCBOREncode_AddInt64(&EC, 4);
5930 QCBOREncode_CloseBstrWrap(&EC, NULL);
5931 QCBOREncode_AddInt64(&EC, 5);
5932 QCBOREncode_CloseArray(&EC);
5933 QCBOREncode_CloseBstrWrap(&EC, NULL);
5934 QCBOREncode_AddInt64(&EC, 6);
5935 QCBOREncode_CloseBstrWrap(&EC, NULL);
5936 QCBOREncode_OpenArray(&EC);
5937 QCBOREncode_AddInt64(&EC, 7);
5938 QCBOREncode_AddInt64(&EC, 8);
5939 QCBOREncode_CloseArray(&EC);
5940
5941 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07005942 if(uErr) {
5943 Encoded = NULLUsefulBufC;
5944 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005945
5946 return Encoded;
5947}
5948
5949
5950int32_t EnterBstrTest()
5951{
5952 MakeUsefulBufOnStack(ffo, 100);
5953
5954 QCBORDecodeContext DC;
5955
5956 QCBORDecode_Init(&DC, foo(ffo), 0);
5957
5958 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
5959
5960
Laurence Lundblade9b334962020-08-27 10:55:53 -07005961 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005962 QCBORDecode_EnterMap(&DC);
5963 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
5964 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
5965 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005966 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005967 QCBORDecode_EnterArray(&DC);
5968 QCBORDecode_GetInt64(&DC, &i3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005969 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005970 QCBORDecode_GetInt64(&DC, &i4);
5971 QCBORDecode_ExitBstrWrapped(&DC);
5972 QCBORDecode_GetInt64(&DC, &i5);
5973 QCBORDecode_ExitArray(&DC);
5974 QCBORDecode_ExitBstrWrapped(&DC);
5975 QCBORDecode_GetInt64(&DC, &i6);
5976 QCBORDecode_ExitBstrWrapped(&DC);
5977 QCBORDecode_EnterArray(&DC);
5978 QCBORDecode_GetInt64(&DC, &i7);
5979 QCBORDecode_GetInt64(&DC, &i8);
5980 QCBORDecode_ExitArray(&DC);
5981
5982 QCBORError uErr = QCBORDecode_Finish(&DC);
5983
5984 return (int32_t)uErr;
5985}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005986
5987
5988
5989
5990static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005991 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005992
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005993 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005994 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005995 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
5996 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
5997 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005998
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005999 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006000 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6001 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6002 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006003
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006004 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006005 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006006 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6007 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006008
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006009 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006010 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6011 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006012
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006013 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006014 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006015 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6016 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006017
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006018 21,
6019 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6020 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6021
6022 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006023 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006024 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6025 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006026
6027 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006028 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6029 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006030
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006031 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006032 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006033 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6034 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006035
6036 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006037 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6038 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006039
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006040 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006041 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006042 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6043 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006044
6045 0x18, 0x33,
6046 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6047
6048 // MIME
6049 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006050 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6051 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6052 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006053
6054 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006055 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6056 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006057
6058 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006059 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6060 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6061 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006062
6063 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006064 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6065 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006066
6067 // UUID
6068 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006069 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6070 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006071
6072 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006073 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6074 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006075};
6076
6077int32_t DecodeTaggedTypeTests()
6078{
6079 QCBORDecodeContext DC;
6080 QCBORError uErr;
6081
6082 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6083
6084 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006085 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006086
6087 QCBORDecode_EnterMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006088 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006089 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006090 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6091 return 1;
6092 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006093 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006094 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6095 return 2;
6096 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006097 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006098 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6099 return 3;
6100 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006101 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006102 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006103 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6104 return 4;
6105 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006106 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006107 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006108 return 5;
6109 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006110
Laurence Lundblade9b334962020-08-27 10:55:53 -07006111 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006112 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6113 bNeg != false) {
6114 return 10;
6115 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006116 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006117 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6118 bNeg != true) {
6119 return 11;
6120 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006121 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006122 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6123 return 12;
6124 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006125 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006126 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006127 return 13;
6128 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006129 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006130 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006131 return 14;
6132 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006133
Laurence Lundblade9b334962020-08-27 10:55:53 -07006134 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006135 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6136 return 20;
6137 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006138 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006139 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6140 return 21;
6141 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006142 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006143 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006144 return 22;
6145 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006146 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006147 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006148 return 23;
6149 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006150
Laurence Lundblade9b334962020-08-27 10:55:53 -07006151 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006152 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6153 return 30;
6154 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006155 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006156 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6157 return 31;
6158 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006159 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006160 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006161 return 32;
6162 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006163 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006164 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006165 return 33;
6166 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006167
Laurence Lundblade9b334962020-08-27 10:55:53 -07006168 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006169 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6170 return 40;
6171 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006172 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006173 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6174 return 41;
6175 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006176 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006177 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006178 return 42;
6179 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006180 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006181 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006182 return 43;
6183 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006184
Laurence Lundblade9b334962020-08-27 10:55:53 -07006185 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006186 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6187 return 50;
6188 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006189 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006190 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6191 return 51;
6192 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006193 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006194 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006195 return 52;
6196 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006197 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006198 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006199 return 53;
6200 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006201
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006202 // MIME
6203 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006204 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006205 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6206 bIsNot7Bit == true) {
6207 return 60;
6208 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006209 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006210 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6211 bIsNot7Bit == true) {
6212 return 61;
6213 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006214 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006215 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6216 bIsNot7Bit == false) {
6217 return 62;
6218 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006219 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006220 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6221 bIsNot7Bit == false) {
6222 return 63;
6223 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006224 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006225 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006226 return 64;
6227 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006228 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006229 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006230 return 65;
6231 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006232
Laurence Lundblade9b334962020-08-27 10:55:53 -07006233 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006234 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6235 return 70;
6236 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006237 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006238 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6239 return 71;
6240 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006241 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006242 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006243 return 72;
6244 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006245 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006246 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006247 return 73;
6248 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006249
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006250 // Improvement: add some more error test cases
6251
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006252 QCBORDecode_ExitMap(&DC);
6253
6254 uErr = QCBORDecode_Finish(&DC);
6255 if(uErr != QCBOR_SUCCESS) {
6256 return 100;
6257 }
6258
6259 return 0;
6260}