blob: 329956763c60869b62292144cb86aa04d5d3c9ef [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 Lundblade2ded3d92018-10-09 21:36:11 +0800911 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
912 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 Lundbladee6bcef12020-04-01 10:56:27 -07001309 if((QCBOR_ERR_ARRAY_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
1310 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)
1353 return -1;
1354
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001355 if(nLevel < 2) {
1356 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1357 return -1;
1358 } 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 Lundblade2ded3d92018-10-09 21:36:11 +08001371 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001373
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001374 if(nLevel < 3) {
1375 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1376 return -1;
1377 } else {
1378 return 0;
1379 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001381
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001382 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001383 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence 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 Lundblade2ded3d92018-10-09 21:36:11 +08001389 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
1392
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001393 if(nLevel < 4) {
1394 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1395 return -1;
1396 } 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 Lundblade2ded3d92018-10-09 21:36:11 +08001407 return -1;
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) {
1411 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1412 return -1;
1413 } 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 Lundblade2ded3d92018-10-09 21:36:11 +08001423 return -1;
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) {
1427 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1428 return -1;
1429 } 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)
1441 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001442
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001443 if(nLevel < 7) {
1444 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1445 return -1;
1446 } else {
1447 return 0;
1448 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001449 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001450
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001451 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001452 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001453 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence 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 Lundblade2ded3d92018-10-09 21:36:11 +08001458 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001460
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001461 if(nLevel < 8) {
1462 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1463 return -1;
1464 } else {
1465 return 0;
1466 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001469 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001470 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001471 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001472 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence 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 Lundblade2ded3d92018-10-09 21:36:11 +08001476 return -1;
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) {
1480 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1481 return -1;
1482 } 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)
1494 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001495
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001496 if(nLevel < 10) {
1497 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1498 return -1;
1499 } 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 Lundblade2ded3d92018-10-09 21:36:11 +08001511 return -1;
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 Lundblade2ded3d92018-10-09 21:36:11 +08001515 return -1;
1516 }
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 Lundbladef71e1622020-08-06 18:52:13 -07001660 if(!QCBORDecode_IsNotWellFormed(uCBORError) &&
1661 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);
2484 if(uError != QCBOR_ERR_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);
2498 if(uError != QCBOR_ERR_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#if 1
2518 // A half-precision date with value -2 FFF
2519 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2520 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002521#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2522 if(nEpochDate4 != -2) {
2523 return 105;
2524 }
2525#else
2526 uError = QCBORDecode_GetAndResetError(&DC);
2527 if(uError == QCBOR_SUCCESS) {
2528 return 106;
2529 }
2530#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade9b334962020-08-27 10:55:53 -07002531#else
2532 (void)nEpochDate4;
2533#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002534
2535 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002536 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2537 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2538 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002539 uError = QCBORDecode_GetAndResetError(&DC);
2540 if(uError != QCBOR_ERR_NOT_FOUND) {
2541 return 107;
2542 }
2543
2544 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002545 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2546 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002547 uError = QCBORDecode_GetAndResetError(&DC);
2548 if(uError != QCBOR_ERR_NOT_FOUND) {
2549 return 108;
2550 }
2551
2552 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002553 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2554 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2555 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002556 uError = QCBORDecode_GetAndResetError(&DC);
2557 if(uError != QCBOR_ERR_NOT_FOUND) {
2558 return 109;
2559 }
2560
2561 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002562 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2563 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002564 uError = QCBORDecode_GetAndResetError(&DC);
2565 if(uError != QCBOR_ERR_NOT_FOUND) {
2566 return 110;
2567 }
2568
2569 // The rest of these succeed even if float features are disabled
2570 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002571 QCBORDecode_GetEpochDateInMapN(&DC,
2572 1,
2573 QCBOR_TAG_REQUIREMENT_TAG |
2574 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2575 &nEpochDate1400000000);
2576 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002577 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002578 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2579 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002580 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002581 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2582 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002583 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002584 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2585 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002586 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002587 QCBORDecode_GetEpochDateInMapSZ(&DC,
2588 "z",
2589 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2590 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2591 &nEpochDate6);
2592 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002593
2594 QCBORDecode_ExitMap(&DC);
2595 QCBORDecode_ExitArray(&DC);
2596 uError = QCBORDecode_Finish(&DC);
2597 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002598 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002599 }
2600
Laurence Lundblade9b334962020-08-27 10:55:53 -07002601 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002602 return 200;
2603 }
2604
Laurence Lundblade9b334962020-08-27 10:55:53 -07002605 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002606 return 201;
2607 }
2608
Laurence Lundblade9b334962020-08-27 10:55:53 -07002609 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002610 return 202;
2611 }
2612
Laurence Lundblade9b334962020-08-27 10:55:53 -07002613 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002614 return 203;
2615 }
2616
Laurence Lundblade9b334962020-08-27 10:55:53 -07002617 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002618 return 204;
2619 }
2620
Laurence Lundblade9b334962020-08-27 10:55:53 -07002621 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2622 return 205;
2623 }
2624
2625 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2626 return 206;
2627 }
2628
Laurence Lundbladec7114722020-08-13 05:11:40 -07002629 return 0;
2630}
2631
2632
2633
Laurence Lundblade9b334962020-08-27 10:55:53 -07002634// Input for one of the tagging tests
2635static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002636 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002637 0x81, // Array of one
2638 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2639 0x82, // Array of two that is the faction 1/3
2640 0x01,
2641 0x03,
2642
2643 /*
2644 More than 4 tags on an item 225(226(227(228(229([])))))
2645 */
2646 0xd8, 0xe1,
2647 0xd8, 0xe2,
2648 0xd8, 0xe3,
2649 0xd8, 0xe4,
2650 0xd8, 0xe5,
2651 0x80,
2652
2653 /* tag 10489608748473423768(
2654 2442302356(
2655 21590(
2656 240(
2657 []))))
2658 */
2659 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2660 0xda, 0x91, 0x92, 0x93, 0x94,
2661 0xd9, 0x54, 0x56,
2662 0xd8, 0xf0,
2663 0x80,
2664
2665 /* tag 21590(
2666 10489608748473423768(
2667 2442302357(
2668 65534(
2669 []))))
2670 */
2671 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2672 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2673 0xda, 0x91, 0x92, 0x93, 0x95,
2674 0xd9, 0xff, 0xfe,
2675 0x80,
2676
2677 /* Make sure to blow past the limit of tags that must be mapped.
2678 works in conjuntion with entries above.
2679 269488144(269488145(269488146(269488147([]))))
2680 */
2681 0xda, 0x10, 0x10, 0x10, 0x10,
2682 0xda, 0x10, 0x10, 0x10, 0x11,
2683 0xda, 0x10, 0x10, 0x10, 0x12,
2684 0xda, 0x10, 0x10, 0x10, 0x13,
2685 0x80,
2686
2687 /* An invalid decimal fraction with an additional tag */
2688 0xd9, 0xff, 0xfa,
2689 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2690 0x00, // the integer 0; should be a byte string
2691};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002692
Laurence Lundblade59289e52019-12-30 13:44:37 -08002693/*
2694 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002695 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002696 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002697static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2698 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002699
Laurence Lundblade59289e52019-12-30 13:44:37 -08002700/*
2701DB 9192939495969798 # tag(10489608748473423768)
2702 D8 88 # tag(136)
2703 C6 # tag(6)
2704 C7 # tag(7)
2705 80 # array(0)
2706*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002707static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2708 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002709
2710/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002711 55799(55799(55799({
2712 6(7(-23)): 5859837686836516696(7({
2713 7(-20): 11({
2714 17(-18): 17(17(17("Organization"))),
2715 9(-17): 773("SSG"),
2716 -15: 16(17(6(7("Confusion")))),
2717 17(-16): 17("San Diego"),
2718 17(-14): 17("US")
2719 }),
2720 23(-19): 19({
2721 -11: 9({
2722 -9: -7
2723 }),
2724 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2725 })
2726 })),
2727 16(-22): 23({
2728 11(8(7(-5))): 8(-3)
2729 })
2730 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002731 */
2732static uint8_t spCSRWithTags[] = {
2733 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2734 0xc6, 0xc7, 0x36,
2735 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2736 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2737 0xcb, 0xa5,
2738 0xd1, 0x31,
2739 0xd1, 0xd1, 0xd1, 0x6c,
2740 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2741 0xc9, 0x30,
2742 0xd9, 0x03, 0x05, 0x63,
2743 0x53, 0x53, 0x47,
2744 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002745 0xd0, 0xd1, 0xc6, 0xc7,
2746 0x69,
2747 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002748 0xd1, 0x2f,
2749 0xd1, 0x69,
2750 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2751 0xd1, 0x2d,
2752 0xd1, 0x62,
2753 0x55, 0x53,
2754 0xd7, 0x32,
2755 0xd3, 0xa2,
2756 0x2a,
2757 0xc9, 0xa1,
2758 0x28,
2759 0x26,
2760 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2761 0xcc, 0x4a,
2762 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2763 0xd0, 0x35,
2764 0xd7, 0xa1,
2765 0xcb, 0xc8, 0xc7, 0x24,
2766 0xc8, 0x22};
2767
Laurence Lundblade9b334962020-08-27 10:55:53 -07002768
2769static uint8_t spSpiffyTagInput[] = {
2770 0x9f, // Open indefinite array
2771
2772 0xc0, // tag for string date
2773 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2774
2775 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2776
2777 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2778
2779 0xd8, 0x23, // tag for regex
2780 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2781
2782 0xc0, // tag for string date
2783 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2784
2785 0xff
2786};
2787
2788
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002789static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002790
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002791
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002792int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002793{
2794 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002795 QCBORItem Item;
2796 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002797
Laurence Lundbladeee851742020-01-08 08:37:05 -08002798 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002799 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002800 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002801
Laurence Lundblade9b334962020-08-27 10:55:53 -07002802 /*
2803 This test matches the magic number tag and the fraction tag
2804 55799([...])
2805 */
2806 uError = QCBORDecode_GetNext(&DCtx, &Item);
2807 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002808 return -2;
2809 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002810 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002811 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2812 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002813 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002814
Laurence Lundblade9b334962020-08-27 10:55:53 -07002815 /*
2816 4([1,3])
2817 */
2818 uError = QCBORDecode_GetNext(&DCtx, &Item);
2819#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2820 if(uError != QCBOR_SUCCESS ||
2821 Item.uDataType != QCBOR_TYPE_ARRAY ||
2822 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2823 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2824 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2825 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2826 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2827 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2828 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002829 return -4;
2830 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002831 // consume the items in the array
2832 uError = QCBORDecode_GetNext(&DCtx, &Item);
2833 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002834
Laurence Lundblade59289e52019-12-30 13:44:37 -08002835#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002836 if(uError != QCBOR_SUCCESS ||
2837 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2838 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2839 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2840 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2841 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2842 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2843 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002844 }
2845#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002846
Laurence Lundblade9b334962020-08-27 10:55:53 -07002847 /*
2848 More than 4 tags on an item 225(226(227(228(229([])))))
2849 */
2850 uError = QCBORDecode_GetNext(&DCtx, &Item);
2851 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002852 return -6;
2853 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002854
2855 /* tag 10489608748473423768(
2856 2442302356(
2857 21590(
2858 240(
2859 []))))
2860 */
2861 uError = QCBORDecode_GetNext(&DCtx, &Item);
2862 if(uError != QCBOR_SUCCESS ||
2863 Item.uDataType != QCBOR_TYPE_ARRAY ||
2864 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2865 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2866 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2867 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002868 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002869 }
2870
2871 /* tag 21590(
2872 10489608748473423768(
2873 2442302357(
2874 21591(
2875 []))))
2876 */
2877 uError = QCBORDecode_GetNext(&DCtx, &Item);
2878 if(uError != QCBOR_SUCCESS ||
2879 Item.uDataType != QCBOR_TYPE_ARRAY ||
2880 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2881 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2882 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2883 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2884 return -8;
2885 }
2886
2887 /* Make sure to blow past the limit of tags that must be mapped.
2888 works in conjuntion with entries above.
2889 269488144(269488145(269488146(269488147([]))))
2890 */
2891 uError = QCBORDecode_GetNext(&DCtx, &Item);
2892 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2893 return -9;
2894 }
2895
2896 uError = QCBORDecode_GetNext(&DCtx, &Item);
2897 if(uError == QCBOR_SUCCESS) {
2898 return -10;
2899 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002900
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002901 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002902 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002903 // tage and then matches it. Caller-config lists are no longer
2904 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002905 QCBORDecode_Init(&DCtx,
2906 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2907 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002908 const uint64_t puList[] = {0x9192939495969798, 257};
2909 const QCBORTagListIn TL = {2, puList};
2910 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002911
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002912 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2913 return -8;
2914 }
2915 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2916 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2917 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2918 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2919 Item.val.uCount != 0) {
2920 return -9;
2921 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002922
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002923 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002924 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002925 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002926 const uint64_t puLongList[17] = {1,2,1};
2927 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002928 QCBORDecode_Init(&DCtx,
2929 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2930 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002931 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2932 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2933 return -11;
2934 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002935
Laurence Lundblade9b334962020-08-27 10:55:53 -07002936 uint64_t puTags[16];
2937 QCBORTagListOut Out = {0, 4, puTags};
2938
2939
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002940 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002941 QCBORDecode_Init(&DCtx,
2942 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2943 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002944 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2945 return -12;
2946 }
2947 if(puTags[0] != 0x9192939495969798 ||
2948 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002949 puTags[2] != 0x06 ||
2950 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002951 return -13;
2952 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002953
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002954 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002955 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002956 QCBORDecode_Init(&DCtx,
2957 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2958 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002959 QCBORTagListOut OutSmall = {0, 3, puTags};
2960 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2961 return -14;
2962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002963
Laurence Lundblade9b334962020-08-27 10:55:53 -07002964
2965
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002966 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002967 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
2968 // It is a bit of a messy test and maybe could be improved, but
2969 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002970 QCBORDecode_Init(&DCtx,
2971 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2972 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002973 int n = CheckCSRMaps(&DCtx);
2974 if(n) {
2975 return n-2000;
2976 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002977
Laurence Lundblade59289e52019-12-30 13:44:37 -08002978 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002979 QCBORDecode_Init(&DCtx,
2980 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2981 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002982
Laurence Lundblade9b334962020-08-27 10:55:53 -07002983 /* With the spiffy decode revision, this tag list is not used.
2984 It doesn't matter if a tag is in this list or not so some
2985 tests that couldn't process a tag because it isn't in this list
2986 now can process these unlisted tags. The tests have been
2987 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002988 const uint64_t puTagList[] = {773, 1, 90599561};
2989 const QCBORTagListIn TagList = {3, puTagList};
2990 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002991
2992
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002993 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2994 return -100;
2995 }
2996 if(Item.uDataType != QCBOR_TYPE_MAP ||
2997 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2998 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2999 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3000 Item.val.uCount != 2 ||
3001 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3002 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3003 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3004 Out.uNumUsed != 3) {
3005 return -101;
3006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003007
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003008 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3009 return -102;
3010 }
3011 if(Item.uDataType != QCBOR_TYPE_MAP ||
3012 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3013 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003014 !QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003015 Item.val.uCount != 2 ||
3016 puTags[0] != 5859837686836516696 ||
3017 puTags[1] != 7 ||
3018 Out.uNumUsed != 2) {
3019 return -103;
3020 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003021
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003022 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3023 return -104;
3024 }
3025 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003026 //Item.uTagBits ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003027 Item.val.uCount != 5 ||
3028 puTags[0] != 0x0b ||
3029 Out.uNumUsed != 1) {
3030 return -105;
3031 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003032
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003033 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3034 return -106;
3035 }
3036 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3037 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3038 Item.val.string.len != 12 ||
3039 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3040 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3041 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3042 Out.uNumUsed != 3) {
3043 return -105;
3044 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003045
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003046 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3047 return -107;
3048 }
3049 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3050 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3051 Item.val.string.len != 3 ||
3052 puTags[0] != 773 ||
3053 Out.uNumUsed != 1) {
3054 return -108;
3055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003056
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003057 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3058 return -109;
3059 }
3060 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003061 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003062 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003063 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003064 puTags[3] != 7 ||
3065 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003066 return -110;
3067 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003068
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003069 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3070 return -111;
3071 }
3072 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3073 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3074 Item.val.string.len != 9 ||
3075 puTags[0] != 17 ||
3076 Out.uNumUsed != 1) {
3077 return -112;
3078 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003079
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003080 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3081 return -111;
3082 }
3083 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3084 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3085 Item.val.string.len != 2 ||
3086 puTags[0] != 17 ||
3087 Out.uNumUsed != 1) {
3088 return -112;
3089 }
3090
3091 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3092 return -113;
3093 }
3094 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003095 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003096 Item.val.uCount != 2 ||
3097 puTags[0] != 19 ||
3098 Out.uNumUsed != 1) {
3099 return -114;
3100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003101
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003102 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3103 return -115;
3104 }
3105 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003106 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003107 Item.val.uCount != 1 ||
3108 puTags[0] != 9 ||
3109 Out.uNumUsed != 1) {
3110 return -116;
3111 }
3112
3113 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3114 return -116;
3115 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003116 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003117 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003118 Out.uNumUsed != 0) {
3119 return -117;
3120 }
3121
3122 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3123 return -118;
3124 }
3125 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3126 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003127 puTags[0] != 12 ||
3128 Out.uNumUsed != 1) {
3129 return -119;
3130 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003131
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003132 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3133 return -120;
3134 }
3135 if(Item.uDataType != QCBOR_TYPE_MAP ||
3136 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3137 Item.val.uCount != 1 ||
3138 puTags[0] != 0x17 ||
3139 Out.uNumUsed != 1) {
3140 return -121;
3141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003142
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003143 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3144 return -122;
3145 }
3146 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003147 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003148 Item.val.int64 != -3 ||
3149 puTags[0] != 8 ||
3150 Out.uNumUsed != 1) {
3151 return -123;
3152 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003153
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003154 if(QCBORDecode_Finish(&DCtx)) {
3155 return -124;
3156 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003157
3158 UsefulBufC DateString;
3159 QCBORDecode_Init(&DCtx,
3160 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3161 QCBOR_DECODE_MODE_NORMAL);
3162
3163 QCBORDecode_EnterArray(&DCtx);
3164 // tagged date string
3165 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3166 // untagged date string
3167 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3168 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3169 return 100;
3170 }
3171 // untagged byte string
3172 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3173 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3174 return 101;
3175 }
3176 // tagged regex
3177 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3178 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3179 return 102;
3180 }
3181 // tagged date string with a byte string
3182 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3183 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3184 return 103;
3185 }
3186 QCBORDecode_ExitArray(&DCtx);
3187 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3188 return 104;
3189 }
3190
3191
3192 QCBORDecode_Init(&DCtx,
3193 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3194 QCBOR_DECODE_MODE_NORMAL);
3195
3196 QCBORDecode_EnterArray(&DCtx);
3197 // tagged date string
3198 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3199 // untagged date string
3200 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3201 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3202 return 200;
3203 }
3204 // untagged byte string
3205 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3206 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3207 return 201;
3208 }
3209 // tagged regex
3210 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3211 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3212 return 202;
3213 }
3214 // tagged date string with a byte string
3215 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3216 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3217 return 203;
3218 }
3219 QCBORDecode_ExitArray(&DCtx);
3220 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3221 return 204;
3222 }
3223
3224 QCBORDecode_Init(&DCtx,
3225 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3226 QCBOR_DECODE_MODE_NORMAL);
3227
3228 QCBORDecode_EnterArray(&DCtx);
3229 // tagged date string
3230 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3231 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3232 return 300;
3233 }
3234 // untagged date string
3235 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3236 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3237 return 301;
3238 }
3239 // untagged byte string
3240 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3241 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3242 return 302;
3243 }
3244 // tagged regex
3245 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3246 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3247 return 303;
3248 }
3249 // tagged date string with a byte string
3250 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3251 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3252 return 304;
3253 }
3254 QCBORDecode_ExitArray(&DCtx);
3255 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3256 return 305;
3257 }
3258
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003259 return 0;
3260}
3261
3262
3263
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003264
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003265static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003266 0x83,
3267 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3268 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3269 0xA4,
3270 0x63, 0x42, 0x4E, 0x2B,
3271 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3272 0x18, 0x40,
3273 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3274 0x63, 0x42, 0x4E, 0x2D,
3275 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3276 0x38, 0x3F,
3277 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3278
3279
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003280static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003281
3282
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003283int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003284{
3285 QCBORDecodeContext DCtx;
3286 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003287 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003288
Laurence Lundbladeee851742020-01-08 08:37:05 -08003289 QCBORDecode_Init(&DCtx,
3290 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3291 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003292
3293
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003294 //
3295 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3296 return -1;
3297 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003298 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003299 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003300
3301 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003302 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003303 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003304 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003305 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003306 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003307 }
3308
3309 //
3310 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003311 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003312 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003313 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003314 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003315 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003317 //
3318 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003319 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003320 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003321 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003322 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003323
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003324 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003325 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003326 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3327 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003328 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003329 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003330 }
3331
3332 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003333 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003334 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3335 Item.uLabelType != QCBOR_TYPE_INT64 ||
3336 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003337 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003338 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003341 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003342 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003343 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3344 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003345 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003346 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003347 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003348
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003349 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003350 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003351 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3352 Item.uLabelType != QCBOR_TYPE_INT64 ||
3353 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003354 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003355 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003357
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003358 return 0;
3359}
3360
3361
3362
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003363static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003364 uint8_t uDataType,
3365 uint8_t uNestingLevel,
3366 uint8_t uNextNest,
3367 int64_t nLabel,
3368 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003369{
3370 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003371 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003372
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003373 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3374 if(Item.uDataType != uDataType) return -1;
3375 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003376 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3377 Item.uLabelType != QCBOR_TYPE_UINT64) {
3378 return -1;
3379 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003380 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3381 if(Item.label.int64 != nLabel) return -1;
3382 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003383 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003384 }
3385 }
3386 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303387 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003388
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003389 if(pItem) {
3390 *pItem = Item;
3391 }
3392 return 0;
3393}
3394
3395
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003396// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003397static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003398{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303399 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003400
Laurence Lundblade9b334962020-08-27 10:55:53 -07003401 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003402
Laurence Lundblade9b334962020-08-27 10:55:53 -07003403 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003404
Laurence Lundblade9b334962020-08-27 10:55:53 -07003405 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3406 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3407 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3408 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3409 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003410
Laurence Lundblade9b334962020-08-27 10:55:53 -07003411 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3412 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003413
Laurence Lundblade9b334962020-08-27 10:55:53 -07003414 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3415 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003416
Laurence Lundblade9b334962020-08-27 10:55:53 -07003417 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3418 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003419
Laurence Lundblade9b334962020-08-27 10:55:53 -07003420 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003421
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003422 return 0;
3423}
3424
3425
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003426/*
3427// cbor.me decoded output
3428{
3429 -23: {
3430 -20: {
3431 -18: "Organization",
3432 -17: "SSG",
3433 -15: "Confusion",
3434 -16: "San Diego",
3435 -14: "US"
3436 },
3437 -19: {
3438 -11: {
3439 -9: -7
3440 },
3441 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3442 }
3443 },
3444 -22: {
3445 -5: -3
3446 }
3447}
3448 */
3449
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003450
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003451static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003452 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3453 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3454 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3455 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3456 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3457 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3458 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3459 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3460 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3461
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003462int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003463{
3464 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003465
Laurence Lundbladeee851742020-01-08 08:37:05 -08003466 QCBORDecode_Init(&DCtx,
3467 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3468 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003469
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003470 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003471}
3472
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003473
3474
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003475int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003476{
3477 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003478
Laurence Lundbladeee851742020-01-08 08:37:05 -08003479 QCBORDecode_Init(&DCtx,
3480 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3481 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003482
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003483 QCBORItem Item;
3484 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003485
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003486 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3487 return -1;
3488 }
3489 if(Item.uDataType != QCBOR_TYPE_MAP) {
3490 return -2;
3491 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003492
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003493 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3494 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3495 return -3;
3496 }
3497
3498 return 0;
3499}
3500
3501
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003502// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003503static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003504 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3505 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3506 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3507 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3508 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3509 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003510 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3511 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3512 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3513 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003514
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003515int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003516{
3517 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003518
Laurence Lundbladeee851742020-01-08 08:37:05 -08003519 QCBORDecode_Init(&DCtx,
3520 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3521 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003522
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003523 return CheckCSRMaps(&DCtx);
3524}
3525
3526
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003527
Laurence Lundblade17ede402018-10-13 11:43:07 +08003528static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3529{
3530 UsefulOutBuf UOB;
3531 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003532
Laurence Lundblade17ede402018-10-13 11:43:07 +08003533 int i;
3534 for(i = 0; i < n; i++) {
3535 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3536 }
3537
3538 for(i = 0; i < n; i++) {
3539 UsefulOutBuf_AppendByte(&UOB, 0xff);
3540 }
3541 return UsefulOutBuf_OutUBuf(&UOB);
3542}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003543
3544
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003545static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003546{
3547 QCBORDecodeContext DC;
3548 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003549
Laurence Lundblade17ede402018-10-13 11:43:07 +08003550 int j;
3551 for(j = 0; j < nNestLevel; j++) {
3552 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003553 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003554 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3555 // Should be in error
3556 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
3557 return -4;
3558 } else {
3559 return 0; // Decoding doesn't recover after an error
3560 }
3561 } else {
3562 // Should be no error
3563 if(nReturn) {
3564 return -9; // Should not have got an error
3565 }
3566 }
3567 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3568 return -7;
3569 }
3570 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003571 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003572 if(nReturn) {
3573 return -3;
3574 }
3575 return 0;
3576}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003577
3578
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003579int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003580{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303581 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003582 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003583 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003584 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003585 int nReturn = parse_indeflen_nested(Nested, i);
3586 if(nReturn) {
3587 return nReturn;
3588 }
3589 }
3590 return 0;
3591}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003592
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003593
Laurence Lundbladeee851742020-01-08 08:37:05 -08003594// [1, [2, 3]]
3595static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3596// No closing break
3597static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3598// Not enough closing breaks
3599static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3600// Too many closing breaks
3601static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3602// Unclosed indeflen inside def len
3603static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3604// confused tag
3605static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003606
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003607int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003608{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003609 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003610 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003611 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003612
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003613 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303614 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003615 QCBORDecodeContext DC;
3616 QCBORItem Item;
3617 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003618
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003619 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003620
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003621 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303622
3623 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3624 Item.uNestingLevel != 0 ||
3625 Item.uNextNestLevel != 1) {
3626 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003627 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003628
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003629 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303630 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3631 Item.uNestingLevel != 1 ||
3632 Item.uNextNestLevel != 1) {
3633 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003634 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003635
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003636 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303637 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3638 Item.uNestingLevel != 1 ||
3639 Item.uNextNestLevel != 2) {
3640 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003641 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003642
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003643 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003644 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303645 Item.uNestingLevel != 2 ||
3646 Item.uNextNestLevel != 2) {
3647 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003648 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003649
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003650 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003651 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303652 Item.uNestingLevel != 2 ||
3653 Item.uNextNestLevel != 0) {
3654 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003655 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003656
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003657 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303658 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003659 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003660
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003661 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003662 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003663
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003664 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003665
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003666 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003667
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003668 nResult = QCBORDecode_GetNext(&DC, &Item);
3669 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303670 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003671 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003672
Laurence Lundblade570fab52018-10-13 18:28:27 +08003673 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303674 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3675 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003676 }
3677
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003678
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003679 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003680 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003681
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003682 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003683
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003684 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003685
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003686 nResult = QCBORDecode_GetNext(&DC, &Item);
3687 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303688 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003689 }
3690
3691 nResult = QCBORDecode_GetNext(&DC, &Item);
3692 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303693 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003694 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003695
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003696 nResult = QCBORDecode_GetNext(&DC, &Item);
3697 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303698 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003699 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003700
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003701 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303702 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3703 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003704 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003705
3706
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003707 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003708 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003709
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003710 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003711
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003712 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003713
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003714 nResult = QCBORDecode_GetNext(&DC, &Item);
3715 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303716 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003717 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003718
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003719 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003720 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303721 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003722 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303723
Laurence Lundblade642282a2020-06-23 12:00:33 -07003724 nResult = QCBORDecode_GetNext(&DC, &Item);
3725 if(nResult != QCBOR_ERR_BAD_BREAK) {
3726 return -140;
3727 }
3728
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003729
Laurence Lundblade570fab52018-10-13 18:28:27 +08003730 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003731 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003732
Laurence Lundblade570fab52018-10-13 18:28:27 +08003733 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003734
Laurence Lundblade570fab52018-10-13 18:28:27 +08003735 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003736
Laurence Lundblade570fab52018-10-13 18:28:27 +08003737 nResult = QCBORDecode_GetNext(&DC, &Item);
3738 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303739 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003740 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003741
Laurence Lundblade570fab52018-10-13 18:28:27 +08003742 nResult = QCBORDecode_GetNext(&DC, &Item);
3743 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303744 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003745 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003746
Laurence Lundblade570fab52018-10-13 18:28:27 +08003747 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303748 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3749 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003750 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003751
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303752 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003753 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003754
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303755 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003756
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303757 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003758
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303759 nResult = QCBORDecode_GetNext(&DC, &Item);
3760 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303761 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303762 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003763
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303764 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303765 if(nResult != QCBOR_ERR_BAD_BREAK) {
3766 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303767 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003768
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003769 return 0;
3770}
3771
Laurence Lundblade17ede402018-10-13 11:43:07 +08003772
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003773static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003774 0x81, // Array of length one
3775 0x7f, // text string marked with indefinite length
3776 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3777 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3778 0xff // ending break
3779};
3780
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003781static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303782 0x81, // Array of length one
3783 0x7f, // text string marked with indefinite length
3784 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3785 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3786 0xff // ending break
3787};
3788
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003789static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303790 0x81, // Array of length one
3791 0x7f, // text string marked with indefinite length
3792 0x01, 0x02, // Not a string
3793 0xff // ending break
3794};
3795
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003796static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303797 0x81, // Array of length one
3798 0x7f, // text string marked with indefinite length
3799 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3800 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3801 // missing end of string
3802};
3803
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003804static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303805 0xa1, // Array of length one
3806 0x7f, // text string marked with indefinite length
3807 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3808 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3809 0xff, // ending break
3810 0x01 // integer being labeled.
3811};
3812
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003813/**
3814 Make an indefinite length string
3815
3816 @param Storage Storage for string, must be 144 bytes in size
3817 @return The indefinite length string
3818
3819 This makes an array with one indefinite length string that has 7 chunks
3820 from size of 1 byte up to 64 bytes.
3821 */
3822static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303823{
3824 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003825
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303826 UsefulOutBuf_Init(&UOB, Storage);
3827 UsefulOutBuf_AppendByte(&UOB, 0x81);
3828 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003829
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003830 uint8_t uStringByte = 0;
3831 // Use of type int is intentional
3832 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3833 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303834 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003835 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3836 for(int j = 0; j < uChunkSize; j++) {
3837 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3838 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303839 }
3840 }
3841 UsefulOutBuf_AppendByte(&UOB, 0xff);
3842
3843 return UsefulOutBuf_OutUBuf(&UOB);
3844}
3845
3846static int CheckBigString(UsefulBufC BigString)
3847{
3848 if(BigString.len != 255) {
3849 return 1;
3850 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003851
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303852 for(uint8_t i = 0; i < 255; i++){
3853 if(((const uint8_t *)BigString.ptr)[i] != i) {
3854 return 1;
3855 }
3856 }
3857 return 0;
3858}
3859
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303860
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003861int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303862{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303863 QCBORDecodeContext DC;
3864 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303865 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003866 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003867
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303868 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003869 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303870 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003871
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303872 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303873 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303874 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003875
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303876 if(QCBORDecode_GetNext(&DC, &Item)) {
3877 return -2;
3878 }
3879 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3880 return -3;
3881 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003882
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303883 if(QCBORDecode_GetNext(&DC, &Item)) {
3884 return -4;
3885 }
3886 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3887 return -5;
3888 }
3889 if(QCBORDecode_Finish(&DC)) {
3890 return -6;
3891 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303892
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303893 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003894 QCBORDecode_Init(&DC,
3895 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3896 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003897
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303898 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3899 return -7;
3900 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003901
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303902 if(QCBORDecode_GetNext(&DC, &Item)) {
3903 return -8;
3904 }
3905 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3906 return -9;
3907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003908
Laurence Lundblade30816f22018-11-10 13:40:22 +07003909 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303910 return -10;
3911 }
3912
3913 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003914 QCBORDecode_Init(&DC,
3915 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3916 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003917
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303918 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3919 return -11;
3920 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003921
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303922 if(QCBORDecode_GetNext(&DC, &Item)) {
3923 return -12;
3924 }
3925 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3926 return -13;
3927 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003928
Laurence Lundblade30816f22018-11-10 13:40:22 +07003929 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303930 return -14;
3931 }
3932
3933 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003934 QCBORDecode_Init(&DC,
3935 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3936 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003937
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303938 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3939 return -15;
3940 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003941
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303942 if(QCBORDecode_GetNext(&DC, &Item)) {
3943 return -16;
3944 }
3945 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3946 return -17;
3947 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003948
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303949 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3950 return -18;
3951 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003952
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303953 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303954 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003955
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303956 QCBORDecode_GetNext(&DC, &Item);
3957 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303958 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003960
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303961 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303962 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303963 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003964
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303965 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003966 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303967
3968 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3969 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303970 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303971 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003972
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303973 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303974 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003975 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003976
Laurence Lundbladeee851742020-01-08 08:37:05 -08003977 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3978 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003979
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303980 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303981 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303982 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003984
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303985 QCBORDecode_GetNext(&DC, &Item);
3986 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303987 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303988 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003989 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303990 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303991 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003992
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303993 // ---- big bstr -----
3994 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003995
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303996 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3997 return -25;
3998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003999
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304000 if(QCBORDecode_GetNext(&DC, &Item)) {
4001 return -26;
4002 }
4003 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304004 return -26;
4005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004006
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304007 if(QCBORDecode_GetNext(&DC, &Item)) {
4008 return -27;
4009 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304010 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304011 return -28;
4012 }
4013 if(CheckBigString(Item.val.string)) {
4014 return -3;
4015 }
4016 if(QCBORDecode_Finish(&DC)) {
4017 return -29;
4018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004019
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304020 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004021 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004022
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304023 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4024 return -30;
4025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004026
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304027 QCBORDecode_GetNext(&DC, &Item);
4028 if(Item.uDataType != QCBOR_TYPE_MAP) {
4029 return -31;
4030 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004031
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304032 if(QCBORDecode_GetNext(&DC, &Item)){
4033 return -32;
4034 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004035 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4036 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304037 Item.uDataAlloc || !Item.uLabelAlloc ||
4038 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4039 return -33;
4040 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004041
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304042 if(QCBORDecode_Finish(&DC)) {
4043 return -34;
4044 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004045
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004046 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004047}
4048
4049
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004050int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304051{
4052 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004053 QCBORError nCBORError;
4054
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004055
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304056 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004057 QCBORDecode_Init(&DC,
4058 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4059 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004060
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004061 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004062
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004063 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4064 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304065 return -1;
4066 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004067
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004068 if(CheckCSRMaps(&DC)) {
4069 return -2;
4070 }
4071
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304072 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004073 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004074 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004075
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304076 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004077 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304078 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004079
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304080 QCBORItem Item1, Item2, Item3, Item4;
4081 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004082 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304083 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4084 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004085 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304086 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
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, &Item2)))
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, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004091 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304092 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004093 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004094
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304095 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004096
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304097 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304098 Item1.uDataType != QCBOR_TYPE_INT64 ||
4099 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004100 Item1.uDataAlloc != 0 ||
4101 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004102 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004103 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004104 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004105
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304106
4107 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004108 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304109 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004110 Item2.uDataAlloc != 0 ||
4111 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304112 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004113 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004114
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304115 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004116 Item3.uDataAlloc == 0 ||
4117 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004118 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004119 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004121
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304122 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004123 Item4.uDataAlloc == 0 ||
4124 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004125 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004126 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004127 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004128
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304129 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004130 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004131 QCBORDecode_Init(&DC,
4132 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4133 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304134 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4135 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004136 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304137 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004138 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004139 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004140 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304141 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4142 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4143 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4144 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4145 }
4146 }
4147 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004148 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004149 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304150 }
4151
4152 return 0;
4153}
4154
Laurence Lundbladef6531662018-12-04 10:42:22 +09004155
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304156
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004157int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004158{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004159 // Set up the decoder with a tiny bit of CBOR to parse because
4160 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004161 QCBORDecodeContext DC;
4162 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4163 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004164
Laurence Lundbladef6531662018-12-04 10:42:22 +09004165 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004166 // Then fish into the internals of the decode context
4167 // to get the allocator function so it can be called directly.
4168 // Also figure out how much pool is available for use
4169 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004170 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004171 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4172 if(nError) {
4173 return -9;
4174 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004175 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4176 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4177 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004178
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004179 // First test -- ask for one more byte than available and see failure
4180 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004181 if(!UsefulBuf_IsNULL(Allocated)) {
4182 return -1;
4183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004184
Laurence Lundbladef6531662018-12-04 10:42:22 +09004185 // Re do the set up for the next test that will do a successful alloc,
4186 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004187 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004188 pAlloc = DC.StringAllocator.pfAllocator;
4189 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4190 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004191
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004192 // Allocate one byte less than available and see success
4193 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004194 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4195 return -2;
4196 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004197 // Ask for some more and see failure
4198 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004199 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4200 return -3;
4201 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004202 // Free the first allocate, retry the second and see success
4203 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4204 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004205 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4206 return -4;
4207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004208
Laurence Lundbladef6531662018-12-04 10:42:22 +09004209 // Re do set up for next test that involves a successful alloc,
4210 // and a successful realloc and a failed realloc
4211 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004212 pAlloc = DC.StringAllocator.pfAllocator;
4213 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004214
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004215 // Allocate half the pool and see success
4216 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004217 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4218 return -5;
4219 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004220 // Reallocate to take up the whole pool and see success
4221 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004222 if(UsefulBuf_IsNULL(Allocated2)) {
4223 return -6;
4224 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004225 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004226 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4227 return -7;
4228 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004229 // Try to allocate more to be sure there is failure after a realloc
4230 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4231 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004232 return -8;
4233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004234
Laurence Lundbladef6531662018-12-04 10:42:22 +09004235 return 0;
4236}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004237
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004238
4239/* Just enough of an allocator to test configuration of one */
4240static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4241{
4242 (void)pOldMem; // unused variable
4243
4244 if(uNewSize) {
4245 // Assumes the context pointer is the buffer and
4246 // nothing too big will ever be asked for.
4247 // This is only good for this basic test!
4248 return (UsefulBuf) {pCtx, uNewSize};
4249 } else {
4250 return NULLUsefulBuf;
4251 }
4252}
4253
4254
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004255int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004256{
4257 // Set up the decoder with a tiny bit of CBOR to parse because
4258 // nothing can be done with it unless that is set up.
4259 QCBORDecodeContext DC;
4260 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4261 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4262
4263 uint8_t pAllocatorBuffer[50];
4264
4265 // This is really just to test that this call works.
4266 // The full functionality of string allocators is tested
4267 // elsewhere with the MemPool internal allocator.
4268 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4269
4270 QCBORItem Item;
4271 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4272 return -1;
4273 }
4274
4275 if(Item.uDataAlloc == 0 ||
4276 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4277 Item.val.string.ptr != pAllocatorBuffer) {
4278 return -2;
4279 }
4280
4281 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4282 return -3;
4283 }
4284
4285 return 0;
4286}
4287
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004288#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004289
Laurence Lundbladea826c502020-05-10 21:07:00 -07004290/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004291 [
4292 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004293 4([-20, 4759477275222530853136]),
4294 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004295 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004296 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004297 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004298 5([ 9223372036854775806, -4759477275222530853137])
4299 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004300 ]
4301 */
4302
4303static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004304 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004305 0xC4, 0x82, 0x20,
4306 0x03,
4307 0xC4, 0x82, 0x33,
4308 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4309 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4310 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4311 0xC5, 0x82, 0x19, 0x01, 0x2C,
4312 0x18, 0x64,
4313 0xC5, 0x82, 0x33,
4314 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4315 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4316 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004317 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4318 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004319 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4320 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4321};
4322
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004323
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004324int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004325{
4326 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004327 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004328 QCBORItem item;
4329
Laurence Lundblade17af4902020-01-07 19:11:55 -08004330 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4331 0x06, 0x07, 0x08, 0x09, 0x010};
4332 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004333
4334
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004335 QCBORDecode_Init(&DC,
4336 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4337 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004338
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004339 uErr = QCBORDecode_GetNext(&DC, &item);
4340 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004341 return 1;
4342 }
4343
4344 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4345 return 2;
4346 }
4347
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004348 uErr = QCBORDecode_GetNext(&DC, &item);
4349 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004350 return 3;
4351 }
4352
4353 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4354 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4355 item.val.expAndMantissa.nExponent != -1) {
4356 return 4;
4357 }
4358
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004359 uErr = QCBORDecode_GetNext(&DC, &item);
4360 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004361 return 5;
4362 }
4363
4364 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4365 item.val.expAndMantissa.nExponent != -20 ||
4366 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4367 return 6;
4368 }
4369
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004370 uErr = QCBORDecode_GetNext(&DC, &item);
4371 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004372 return 7;
4373 }
4374
4375 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4376 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4377 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4378 return 8;
4379 }
4380
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004381 uErr = QCBORDecode_GetNext(&DC, &item);
4382 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004383 return 9;
4384 }
4385
4386 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4387 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4388 item.val.expAndMantissa.nExponent != 300) {
4389 return 10;
4390 }
4391
Laurence Lundbladea826c502020-05-10 21:07:00 -07004392 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004393 uErr = QCBORDecode_GetNext(&DC, &item);
4394 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004395 return 11;
4396 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004397 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4398 item.val.expAndMantissa.nExponent != -20 ||
4399 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4400 return 12;
4401 }
4402
Laurence Lundbladea826c502020-05-10 21:07:00 -07004403 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004404 uErr = QCBORDecode_GetNext(&DC, &item);
4405 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004406 return 13;
4407 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004408 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4409 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4410 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4411 return 14;
4412 }
4413
Laurence Lundbladea826c502020-05-10 21:07:00 -07004414 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004415 uErr = QCBORDecode_GetNext(&DC, &item);
4416 if(uErr != QCBOR_SUCCESS) {
4417 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004418 }
4419 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4420 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4421 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004422 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004423 }
4424
Laurence Lundbladea826c502020-05-10 21:07:00 -07004425 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004426 uErr = QCBORDecode_GetNext(&DC, &item);
4427 if(uErr != QCBOR_SUCCESS) {
4428 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004429 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004430 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4431 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4432 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004433 return 18;
4434 }
4435
4436 uErr = QCBORDecode_Finish(&DC);
4437 if(uErr != QCBOR_SUCCESS) {
4438 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004439 }
4440
4441 /* Now encode some stuff and then decode it */
4442 uint8_t pBuf[40];
4443 QCBOREncodeContext EC;
4444 UsefulBufC Encoded;
4445
4446 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4447 QCBOREncode_OpenArray(&EC);
4448 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4449 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4450 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4451 QCBOREncode_CloseArray(&EC);
4452 QCBOREncode_Finish(&EC, &Encoded);
4453
4454
4455 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004456 uErr = QCBORDecode_GetNext(&DC, &item);
4457 if(uErr != QCBOR_SUCCESS) {
4458 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004459 }
4460
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004461 uErr = QCBORDecode_GetNext(&DC, &item);
4462 if(uErr != QCBOR_SUCCESS) {
4463 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004464 }
4465
4466 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4467 item.val.expAndMantissa.nExponent != 1000 ||
4468 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004469 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004470 }
4471
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004472 uErr = QCBORDecode_GetNext(&DC, &item);
4473 if(uErr != QCBOR_SUCCESS) {
4474 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004475 }
4476
4477 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4478 item.val.expAndMantissa.nExponent != INT32_MIN ||
4479 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004480 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004481 }
4482
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004483 uErr = QCBORDecode_GetNext(&DC, &item);
4484 if(uErr != QCBOR_SUCCESS) {
4485 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004486 }
4487
4488 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4489 item.val.expAndMantissa.nExponent != INT32_MAX ||
4490 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004491 return 106;
4492 }
4493
4494
4495 int64_t nExp, nMant;
4496 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4497 UsefulBufC Mant;
4498 bool bIsNeg;
4499
4500 QCBORDecode_Init(&DC,
4501 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4502 QCBOR_DECODE_MODE_NORMAL);
4503 QCBORDecode_EnterArray(&DC);
4504
4505 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004506 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004507
4508 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004509 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4510 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004511
4512 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004513 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4514 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004515
4516 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004517 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004518
4519 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004520 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4521 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004522
4523 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004524 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4525 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004526
4527 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004528 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4529 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004530
4531 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004532 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4533 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004534
4535 QCBORDecode_ExitArray(&DC);
4536
4537 uErr = QCBORDecode_Finish(&DC);
4538 if(uErr != QCBOR_SUCCESS) {
4539 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004540 }
4541
4542 return 0;
4543}
4544
4545
4546static struct FailInput ExponentAndMantissaFailures[] = {
4547 // Exponent > INT64_MAX
4548 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4549 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4550 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4551 // Mantissa > INT64_MAX
4552 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4553 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4554 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4555 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004556 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004557 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004558 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004559 // bad content for big num
4560 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4561 // bad content for big num
4562 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4563 // Bad integer for exponent
4564 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4565 // Bad integer for mantissa
4566 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4567 // 3 items in array
4568 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4569 // unterminated indefinite length array
4570 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4571 // Empty array
4572 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4573 // Second is not an integer
4574 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4575 // First is not an integer
4576 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4577 // Not an array
4578 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4579};
4580
4581
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004582int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004583{
4584 return ProcessFailures(ExponentAndMantissaFailures,
4585 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4586}
4587
4588#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004589
4590
4591
4592/*
4593 Some basic CBOR with map and array used in a lot of tests.
4594 The map labels are all strings
4595
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004596 {
4597 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004598 "an array of two strings": [
4599 "string1", "string2"
4600 ],
4601 "map in a map": {
4602 "bytes 1": h'78787878',
4603 "bytes 2": h'79797979',
4604 "another int": 98,
4605 "text 2": "lies, damn lies and statistics"
4606 }
4607 }
4608 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004609
Laurence Lundblade1341c592020-04-11 14:19:05 -07004610#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004611
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004612static char strbuf[10];
4613const char *PrintType(uint8_t type) {
4614 switch(type) {
4615 case QCBOR_TYPE_INT64: return "INT64";
4616 case QCBOR_TYPE_UINT64: return "UINT64";
4617 case QCBOR_TYPE_ARRAY: return "ARRAY";
4618 case QCBOR_TYPE_MAP: return "MAP";
4619 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
4620 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
4621 default:
4622 sprintf(strbuf, "%d", type);
4623 return strbuf;
4624 }
4625}
4626
4627
4628void PrintItem(QCBORItem Item)
4629{
Laurence Lundblade9b334962020-08-27 10:55:53 -07004630 printf("\nData: %s nest: %d,%d %s\n",
4631 PrintType(Item.uDataType),
4632 Item.uNestingLevel,
4633 Item.uNextNestLevel,
4634 Item.uDataAlloc ? "Allocated":"");
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004635 if(Item.uLabelType) {
4636 printf("Label: %s ", PrintType(Item.uLabelType));
4637 if(Item.uLabelType == QCBOR_TYPE_INT64) {
4638 printf("%lld\n", Item.label.int64);
4639 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07004640 // TODO: proper conversion to null-terminated string
4641 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004642 }
4643 }
4644}
4645
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004646
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004647int32_t EMap(UsefulBufC input)
4648{
4649 QCBORItem Item1, Item2, Item3;
4650 int64_t nDecodedInt1, nDecodedInt2;
4651 UsefulBufC B1, B2, S1, S2, S3;
4652
4653 QCBORDecodeContext DCtx;
4654 QCBORError nCBORError;
4655
4656 QCBORDecode_Init(&DCtx, input, 0);
4657
4658 QCBORDecode_EnterMap(&DCtx);
4659
4660 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4661
4662 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4663 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4664 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1);
4665 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2);
4666 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
4667 QCBORDecode_ExitMap(&DCtx);
4668
4669 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4670 QCBORDecode_GetNext(&DCtx, &Item1);
4671 QCBORDecode_GetNext(&DCtx, &Item2);
4672 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4673 return -400;
4674 }
4675 QCBORDecode_ExitArray(&DCtx);
4676
4677 // Parse the same array again using GetText() instead of GetItem()
4678 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4679 QCBORDecode_GetText(&DCtx, &S2);
4680 QCBORDecode_GetText(&DCtx, &S3);
4681 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4682 return 5000;
4683 }
4684 /* QCBORDecode_GetText(&DCtx, &S3);
4685 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4686 return 5001;
4687 } */
4688
4689 QCBORDecode_ExitArray(&DCtx);
4690
4691 QCBORDecode_ExitMap(&DCtx);
4692
4693 nCBORError = QCBORDecode_Finish(&DCtx);
4694
4695 if(nCBORError) {
4696 return (int32_t)nCBORError;
4697 }
4698
4699 if(nDecodedInt1 != 42) {
4700 return 1001;
4701 }
4702
4703 if(nDecodedInt2 != 98) {
4704 return 1002;
4705 }
4706
4707 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004708 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004709 return 1003;
4710 }
4711
4712 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004713 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004714 return 1004;
4715 }
4716
Laurence Lundblade9b334962020-08-27 10:55:53 -07004717 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004718 return 1005;
4719 }
4720
4721 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4722 return 1006;
4723 }
4724
4725 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4726 return 1007;
4727 }
4728
4729 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4730 return 1008;
4731 }
4732
4733 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4734 return 1009;
4735 }
4736
4737 return 0;
4738}
4739
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004740
4741/*
4742 [23,
4743 6000,
4744 h'67616C6163746963',
4745 h'686176656E20746F6B656E'
4746 ]
4747 */
4748static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004749 0x84,
4750 0x17,
4751 0x19, 0x17, 0x70,
4752 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4753 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004754
4755
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004756static const uint8_t spEmptyMap[] = {0xa0};
4757
4758static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004759
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004760static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4761
Laurence Lundbladef0499502020-08-01 11:55:57 -07004762/*
4763 {
4764 0: [],
4765 9: [
4766 [],
4767 []
4768 ],
4769 8: {
4770 1: [],
4771 2: {},
4772 3: []
4773 },
4774 4: {},
4775 5: [],
4776 6: [
4777 [],
4778 []
4779 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004780 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004781 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004782
4783
Laurence Lundbladef0499502020-08-01 11:55:57 -07004784static const uint8_t spMapOfEmpty[] = {
4785 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4786 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4787 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004788
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004789int32_t EnterMapTest()
4790{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004791 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004792 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004793 int32_t nReturn;
4794 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004795
4796
4797 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4798 QCBORDecode_EnterMap(&DCtx);
4799
Laurence Lundbladef0499502020-08-01 11:55:57 -07004800
4801 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004802 QCBORDecode_ExitArray(&DCtx);
4803
Laurence Lundbladef0499502020-08-01 11:55:57 -07004804 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004805 QCBORDecode_EnterArray(&DCtx);
4806 QCBORDecode_ExitArray(&DCtx);
4807 QCBORDecode_EnterArray(&DCtx);
4808 QCBORDecode_ExitArray(&DCtx);
4809 QCBORDecode_ExitArray(&DCtx);
4810
Laurence Lundbladef0499502020-08-01 11:55:57 -07004811 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004812 QCBORDecode_EnterArray(&DCtx);
4813 QCBORDecode_ExitArray(&DCtx);
4814 QCBORDecode_EnterMap(&DCtx);
4815 QCBORDecode_ExitMap(&DCtx);
4816 QCBORDecode_EnterArray(&DCtx);
4817 QCBORDecode_ExitArray(&DCtx);
4818 QCBORDecode_ExitMap(&DCtx);
4819
Laurence Lundbladef0499502020-08-01 11:55:57 -07004820 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004821 QCBORDecode_ExitMap(&DCtx);
4822
Laurence Lundbladef0499502020-08-01 11:55:57 -07004823 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004824 QCBORDecode_ExitArray(&DCtx);
4825
Laurence Lundbladef0499502020-08-01 11:55:57 -07004826 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004827 QCBORDecode_EnterArray(&DCtx);
4828 QCBORDecode_ExitArray(&DCtx);
4829 QCBORDecode_EnterArray(&DCtx);
4830 QCBORDecode_ExitArray(&DCtx);
4831 QCBORDecode_ExitArray(&DCtx);
4832
4833 QCBORDecode_ExitMap(&DCtx);
4834
4835 uErr = QCBORDecode_Finish(&DCtx);
4836 if(uErr != QCBOR_SUCCESS){
4837 return 3011;
4838 }
4839
4840
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004841 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004842 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004843 if(nReturn) {
4844 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004845 }
4846
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004847 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004848 if(nReturn) {
4849 return nReturn;
4850 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004851
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004852
Laurence Lundblade937ea812020-05-08 11:38:23 -07004853
4854 // These tests confirm the cursor is at the right place after entering a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004855 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004856
4857 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004858 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004859 QCBORDecode_EnterMap(&DCtx);
4860 QCBORDecode_GetNext(&DCtx, &Item1);
4861 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4862 return 2001;
4863 }
4864
4865
Laurence Lundblade9b334962020-08-27 10:55:53 -07004866 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004867 QCBORDecode_VGetNext(&DCtx, &Item1);
4868 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004869 QCBORDecode_EnterArray(&DCtx);
4870 QCBORDecode_GetNext(&DCtx, &Item1);
4871 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4872 return 2002;
4873 }
4874
Laurence Lundblade9b334962020-08-27 10:55:53 -07004875 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004876 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004877 QCBORDecode_GetNext(&DCtx, &Item1);
4878 QCBORDecode_GetNext(&DCtx, &Item1);
4879 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004880 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4881 QCBORDecode_GetNext(&DCtx, &Item1);
4882 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4883 return 2003;
4884 }
4885
Laurence Lundblade9b334962020-08-27 10:55:53 -07004886 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004887 QCBORDecode_EnterMap(&DCtx);
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_GetNext(&DCtx, &Item1);
4894 QCBORDecode_GetNext(&DCtx, &Item1);
4895 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4896 QCBORDecode_GetNext(&DCtx, &Item1);
4897 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004898 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004899 }
4900
Laurence Lundblade9b334962020-08-27 10:55:53 -07004901 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004902 QCBORDecode_EnterMap(&DCtx);
4903 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4904 QCBORDecode_ExitArray(&DCtx);
4905 QCBORDecode_GetNext(&DCtx, &Item1);
4906 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4907 return 2006;
4908 }
4909 QCBORDecode_ExitMap(&DCtx);
4910 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4911 return 2007;
4912 }
4913
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004914 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4915 QCBORDecode_EnterArray(&DCtx);
4916 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004917 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4918 uErr = QCBORDecode_GetAndResetError(&DCtx);
4919 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004920 return 2008;
4921 }
4922 UsefulBufC String;
4923 QCBORDecode_GetTextInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004924 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004925 return 2009;
4926 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004927
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004928
4929 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4930 QCBORDecode_EnterMap(&DCtx);
4931 // This will fail because the map is empty.
4932 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4933 uErr = QCBORDecode_GetAndResetError(&DCtx);
4934 if(uErr != QCBOR_ERR_NOT_FOUND){
4935 return 2010;
4936 }
4937 QCBORDecode_ExitMap(&DCtx);
4938 uErr = QCBORDecode_Finish(&DCtx);
4939 if(uErr != QCBOR_SUCCESS){
4940 return 2011;
4941 }
4942
4943
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004944 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4945 QCBORDecode_EnterMap(&DCtx);
4946 // This will fail because the map is empty.
4947 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4948 uErr = QCBORDecode_GetAndResetError(&DCtx);
4949 if(uErr != QCBOR_ERR_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004950 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004951 }
4952 QCBORDecode_ExitMap(&DCtx);
4953 uErr = QCBORDecode_Finish(&DCtx);
4954 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004955 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004956 }
4957
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004958
4959 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
4960 QCBORDecode_EnterArray(&DCtx);
4961 QCBORDecode_GetBytes(&DCtx, &String);
4962 QCBORDecode_EnterMap(&DCtx);
4963 QCBORDecode_ExitMap(&DCtx);
4964 QCBORDecode_EnterArray(&DCtx);
4965 QCBORDecode_ExitArray(&DCtx);
4966 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
4967 QCBORDecode_ExitArray(&DCtx);
4968 uErr = QCBORDecode_Finish(&DCtx);
4969 if(uErr != QCBOR_SUCCESS){
4970 return 2014;
4971 }
4972
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07004973 // TODO: more testing of entered mapps and arrays with problems
4974 // TODO: document error handling better (maybe improve error handling)
4975
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004976 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004977}
4978
4979
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004980struct NumberConversion {
4981 char *szDescription;
4982 UsefulBufC CBOR;
4983 int64_t nConvertedToInt64;
4984 QCBORError uErrorInt64;
4985 uint64_t uConvertToUInt64;
4986 QCBORError uErrorUint64;
4987 double dConvertToDouble;
4988 QCBORError uErrorDouble;
4989};
4990
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004991static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004992 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07004993 "too large to fit into int64_t",
4994 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
4995 0,
4996 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4997 0,
4998 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4999 ((double)INT64_MIN) + 1 ,
5000 QCBOR_SUCCESS
5001 },
5002 {
5003 "largest negative int that fits in int64_t",
5004 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5005 INT64_MIN,
5006 QCBOR_SUCCESS,
5007 0,
5008 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5009 (double)INT64_MIN,
5010 QCBOR_SUCCESS
5011 },
5012 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005013 "negative bignum -1",
5014 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5015 -1,
5016 QCBOR_SUCCESS,
5017 0,
5018 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5019 -1.0,
5020 QCBOR_SUCCESS
5021 },
5022 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005023 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005024 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5025 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005026#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005027 257000,
5028 QCBOR_SUCCESS,
5029 257000,
5030 QCBOR_SUCCESS,
5031 257000.0,
5032 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005033#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5034 0,
5035 QCBOR_ERR_UNEXPECTED_TYPE,
5036 0,
5037 QCBOR_ERR_UNEXPECTED_TYPE,
5038 0.0,
5039 QCBOR_ERR_UNEXPECTED_TYPE
5040#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005041 },
5042 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005043 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005044 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5045 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005046#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005047 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005048 QCBOR_SUCCESS,
5049 0,
5050 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005051 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005052 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005053#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5054 0,
5055 QCBOR_ERR_UNEXPECTED_TYPE,
5056 0,
5057 QCBOR_ERR_UNEXPECTED_TYPE,
5058 0.0,
5059 QCBOR_ERR_UNEXPECTED_TYPE
5060#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005061 },
5062 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005063 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005064 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5065 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005066#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005067 2056,
5068 QCBOR_SUCCESS,
5069 2056,
5070 QCBOR_SUCCESS,
5071 2056.0,
5072 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005073#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5074 0,
5075 QCBOR_ERR_UNEXPECTED_TYPE,
5076 0,
5077 QCBOR_ERR_UNEXPECTED_TYPE,
5078 0.0,
5079 QCBOR_ERR_UNEXPECTED_TYPE
5080#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005081 },
5082 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005083 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005084 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5085 0,
5086 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5087 0,
5088 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5089 -18446744073709551617.0,
5090 QCBOR_SUCCESS
5091 },
5092 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005093 "Positive bignum 0x01020304 indefinite length string",
5094 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5095 0x01020304,
5096 QCBOR_SUCCESS,
5097 0x01020304,
5098 QCBOR_SUCCESS,
5099 16909060.0,
5100 QCBOR_SUCCESS
5101 },
5102 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005103 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005104 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5105 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005106#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005107 0,
5108 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5109 0,
5110 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5111 -INFINITY,
5112 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005113#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5114 0,
5115 QCBOR_ERR_UNEXPECTED_TYPE,
5116 0,
5117 QCBOR_ERR_UNEXPECTED_TYPE,
5118 0.0,
5119 QCBOR_ERR_UNEXPECTED_TYPE
5120#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005121 },
5122 {
5123 "big float [9223372036854775806, 9223372036854775806]",
5124 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5125 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005126#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005127 0,
5128 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5129 0,
5130 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5131 INFINITY,
5132 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005133#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5134 0,
5135 QCBOR_ERR_UNEXPECTED_TYPE,
5136 0,
5137 QCBOR_ERR_UNEXPECTED_TYPE,
5138 0.0,
5139 QCBOR_ERR_UNEXPECTED_TYPE
5140#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005141 },
5142 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005143 "Big float 3 * 2^^2",
5144 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005145#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005146 12,
5147 QCBOR_SUCCESS,
5148 12,
5149 QCBOR_SUCCESS,
5150 12.0,
5151 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005152#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5153 0,
5154 QCBOR_ERR_UNEXPECTED_TYPE,
5155 0,
5156 QCBOR_ERR_UNEXPECTED_TYPE,
5157 0.0,
5158 QCBOR_ERR_UNEXPECTED_TYPE
5159#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade983500d2020-05-14 11:49:34 -07005160 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005161 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005162 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005163 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5164 0,
5165 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5166 18446744073709551615ULL,
5167 QCBOR_SUCCESS,
5168 18446744073709551615.0,
5169 QCBOR_SUCCESS
5170 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005171 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005172 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005173 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5174 65536-1,
5175 QCBOR_SUCCESS,
5176 0xffff,
5177 QCBOR_SUCCESS,
5178 65535.0,
5179 QCBOR_SUCCESS
5180 },
5181 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005182 "Postive integer 0",
5183 {(uint8_t[]){0x0}, 1},
5184 0LL,
5185 QCBOR_SUCCESS,
5186 0ULL,
5187 QCBOR_SUCCESS,
5188 0.0,
5189 QCBOR_SUCCESS
5190 },
5191 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005192 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005193 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5194 -9223372036854775807-1, // INT64_MIN
5195 QCBOR_SUCCESS,
5196 0ULL,
5197 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5198 -9223372036854775808.0,
5199 QCBOR_SUCCESS
5200 },
5201 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005202 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005203 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005204#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005205 100L,
5206 QCBOR_SUCCESS,
5207 100ULL,
5208 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005209#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5210 0,
5211 QCBOR_ERR_HW_FLOAT_DISABLED,
5212 0,
5213 QCBOR_ERR_HW_FLOAT_DISABLED,
5214#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005215 100.3,
5216 QCBOR_SUCCESS
5217 },
5218 {
5219 "Floating point value NaN 0xfa7fc00000",
5220 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005221#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005222 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005223 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005224 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005225 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005226#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5227 0,
5228 QCBOR_ERR_HW_FLOAT_DISABLED,
5229 0,
5230 QCBOR_ERR_HW_FLOAT_DISABLED,
5231#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005232 NAN,
5233 QCBOR_SUCCESS
5234 },
5235 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005236 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005237 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005238#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5239#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5240 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005241 -4,
5242 QCBOR_SUCCESS,
5243 0,
5244 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5245 -4.0,
5246 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005247#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5248 // Float HW disabled
5249 -4,
5250 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5251 0,
5252 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5253 -4.0,
5254 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5255#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5256#else
5257 // Half-precision disabled
5258 -4,
5259 QCBOR_ERR_HALF_PRECISION_DISABLED,
5260 0,
5261 QCBOR_ERR_HALF_PRECISION_DISABLED,
5262 -4.0,
5263 QCBOR_ERR_HALF_PRECISION_DISABLED
5264#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005265 },
5266 {
5267 "Decimal fraction 3/10",
5268 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005269#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005270 0,
5271 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5272 0,
5273 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5274 0.30000000000000004,
5275 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005276#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5277 0,
5278 QCBOR_ERR_UNEXPECTED_TYPE,
5279 0,
5280 QCBOR_ERR_UNEXPECTED_TYPE,
5281 0.0,
5282 QCBOR_ERR_UNEXPECTED_TYPE
5283#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005284 },
5285 {
5286 "+inifinity",
5287 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5288#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5289 0,
5290 QCBOR_ERR_FLOAT_EXCEPTION,
5291 0,
5292 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5293#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5294 0,
5295 QCBOR_ERR_HW_FLOAT_DISABLED,
5296 0,
5297 QCBOR_ERR_HW_FLOAT_DISABLED,
5298#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5299 INFINITY,
5300 QCBOR_SUCCESS
5301 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005302};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005303
5304
5305
Laurence Lundblade313b2862020-05-16 01:23:06 -07005306int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005307{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005308 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005309
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005310 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5311 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005312
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005313 // Set up the decoding context including a memory pool so that
5314 // indefinite length items can be checked
5315 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005316 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005317
5318 /* ----- test conversion to int64_t ------ */
5319 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005320 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5321 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005322 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005323 }
5324
5325 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005326 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005327 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005328 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005329 }
5330 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005331 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005332 }
5333
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005334 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005335 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5336 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5337 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005338 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005339 }
5340 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005341 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005342 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005343 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005344 }
5345 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005346 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005347 }
5348
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005349 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005350 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5351 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5352 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005353 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005354 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005355#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005356 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005357 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005358 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005359 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005360 }
5361 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5362 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005363 // NaN's can't be compared for equality. A NaN is
5364 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005365 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005366 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005367 }
5368 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005369 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005370 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005371 }
5372 }
5373 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005374#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005375 }
5376
5377 return 0;
5378}
5379
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005380
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005381
5382
Laurence Lundbladee3553422020-05-02 11:11:17 -07005383int32_t CBORSequenceDecodeTests(void)
5384{
5385 QCBORDecodeContext DCtx;
5386 QCBORItem Item;
5387 QCBORError uCBORError;
5388
5389 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005390
Laurence Lundbladee3553422020-05-02 11:11:17 -07005391 // The input for the date test happens to be a sequence so it
5392 // is reused. It is a sequence because it doesn't start as
5393 // an array or map.
5394 QCBORDecode_Init(&DCtx,
5395 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5396 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005397
Laurence Lundbladee3553422020-05-02 11:11:17 -07005398 // Get the first item
5399 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5400 if(uCBORError != QCBOR_SUCCESS) {
5401 return 1;
5402 }
5403 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5404 return 2;
5405 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005406
Laurence Lundbladee3553422020-05-02 11:11:17 -07005407 // Get a second item
5408 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005409 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5410 return 66;
5411 }
5412
5413 // Get a third item
5414 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005415 if(uCBORError != QCBOR_SUCCESS) {
5416 return 2;
5417 }
5418 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5419 return 3;
5420 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005421
Laurence Lundbladee3553422020-05-02 11:11:17 -07005422 // A sequence can have stuff at the end that may
5423 // or may not be valid CBOR. The protocol decoder knows
5424 // when to stop by definition of the protocol, not
5425 // when the top-level map or array is ended.
5426 // Finish still has to be called to know that
5427 // maps and arrays (if there were any) were closed
5428 // off correctly. When called like this it
5429 // must return the error QCBOR_ERR_EXTRA_BYTES.
5430 uCBORError = QCBORDecode_Finish(&DCtx);
5431 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5432 return 4;
5433 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005434
5435
Laurence Lundbladee3553422020-05-02 11:11:17 -07005436 // --- Test an empty input ----
5437 uint8_t empty[1];
5438 UsefulBufC Empty = {empty, 0};
5439 QCBORDecode_Init(&DCtx,
5440 Empty,
5441 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005442
Laurence Lundbladee3553422020-05-02 11:11:17 -07005443 uCBORError = QCBORDecode_Finish(&DCtx);
5444 if(uCBORError != QCBOR_SUCCESS) {
5445 return 5;
5446 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005447
5448
Laurence Lundbladee3553422020-05-02 11:11:17 -07005449 // --- Sequence with unclosed indefinite length array ---
5450 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005451
Laurence Lundbladee3553422020-05-02 11:11:17 -07005452 QCBORDecode_Init(&DCtx,
5453 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5454 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005455
Laurence Lundbladee3553422020-05-02 11:11:17 -07005456 // Get the first item
5457 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5458 if(uCBORError != QCBOR_SUCCESS) {
5459 return 7;
5460 }
5461 if(Item.uDataType != QCBOR_TYPE_INT64) {
5462 return 8;
5463 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005464
Laurence Lundbladee3553422020-05-02 11:11:17 -07005465 // Get a second item
5466 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5467 if(uCBORError != QCBOR_SUCCESS) {
5468 return 9;
5469 }
5470 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5471 return 10;
5472 }
5473
5474 // Try to finish before consuming all bytes to confirm
5475 // that the still-open error is returned.
5476 uCBORError = QCBORDecode_Finish(&DCtx);
5477 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
5478 return 11;
5479 }
5480
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005481
Laurence Lundbladee3553422020-05-02 11:11:17 -07005482 // --- Sequence with a closed indefinite length array ---
5483 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005484
Laurence Lundbladee3553422020-05-02 11:11:17 -07005485 QCBORDecode_Init(&DCtx,
5486 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5487 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005488
Laurence Lundbladee3553422020-05-02 11:11:17 -07005489 // Get the first item
5490 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5491 if(uCBORError != QCBOR_SUCCESS) {
5492 return 12;
5493 }
5494 if(Item.uDataType != QCBOR_TYPE_INT64) {
5495 return 13;
5496 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005497
Laurence Lundbladee3553422020-05-02 11:11:17 -07005498 // Get a second item
5499 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5500 if(uCBORError != QCBOR_SUCCESS) {
5501 return 14;
5502 }
5503 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5504 return 15;
5505 }
5506
5507 // Try to finish before consuming all bytes to confirm
5508 // that the still-open error is returned.
5509 uCBORError = QCBORDecode_Finish(&DCtx);
5510 if(uCBORError != QCBOR_SUCCESS) {
5511 return 16;
5512 }
5513
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005514
Laurence Lundbladee3553422020-05-02 11:11:17 -07005515 return 0;
5516}
5517
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005518
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005519
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005520int32_t IntToTests()
5521{
5522 int nErrCode;
5523 int32_t n32;
5524 int16_t n16;
5525 int8_t n8;
5526 uint32_t u32;
5527 uint16_t u16;
5528 uint8_t u8;
5529 uint64_t u64;
5530
5531 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5532 if(nErrCode == -1 || n32 != 1) {
5533 return 1;
5534 }
5535
5536 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5537 if(nErrCode == -1 || n32 != INT32_MAX) {
5538 return 2;
5539 }
5540
5541 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5542 if(nErrCode == -1 || n32 != INT32_MIN) {
5543 return 3;
5544 }
5545
5546 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5547 if(nErrCode != -1) {
5548 return 4;
5549 }
5550
5551 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5552 if(nErrCode != -1) {
5553 return 5;
5554 }
5555
5556
5557 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5558 if(nErrCode == -1 || n16 != INT16_MAX) {
5559 return 6;
5560 }
5561
5562 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5563 if(nErrCode == -1 || n16 != INT16_MIN) {
5564 return 7;
5565 }
5566
5567 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5568 if(nErrCode == -1 || n16 != 1) {
5569 return 8;
5570 }
5571
5572 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5573 if(nErrCode != -1) {
5574 return 9;
5575 }
5576
5577 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5578 if(nErrCode != -1) {
5579 return 10;
5580 }
5581
5582
5583 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5584 if(nErrCode == -1 || n8 != 1) {
5585 return 11;
5586 }
5587
5588 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5589 if(nErrCode == -1 || n8 != INT8_MAX) {
5590 return 12;
5591 }
5592
5593 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5594 if(nErrCode == -1 || n8 != INT8_MIN) {
5595 return 13;
5596 }
5597
5598 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5599 if(nErrCode != -1) {
5600 return 14;
5601 }
5602
5603 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5604 if(nErrCode != -1) {
5605 return 15;
5606 }
5607
5608
5609 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5610 if(nErrCode == -1 || u32 != 1) {
5611 return 16;
5612 }
5613
5614 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5615 if(nErrCode == -1 || u32 != UINT32_MAX) {
5616 return 17;
5617 }
5618
5619 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5620 if(nErrCode == -1 || u32 != 0) {
5621 return 18;
5622 }
5623
5624 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5625 if(nErrCode != -1) {
5626 return 19;
5627 }
5628
5629 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5630 if(nErrCode != -1) {
5631 return 20;
5632 }
5633
5634
5635 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5636 if(nErrCode == -1 || u16 != UINT16_MAX) {
5637 return 21;
5638 }
5639
5640 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5641 if(nErrCode == -1 || u16 != 0) {
5642 return 22;
5643 }
5644
5645 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5646 if(nErrCode == -1 || u16 != 1) {
5647 return 23;
5648 }
5649
5650 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5651 if(nErrCode != -1) {
5652 return 24;
5653 }
5654
5655 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5656 if(nErrCode != -1) {
5657 return 25;
5658 }
5659
5660
5661 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5662 if(nErrCode == -1 || u8 != UINT8_MAX) {
5663 return 26;
5664 }
5665
5666 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5667 if(nErrCode == -1 || u8 != 0) {
5668 return 27;
5669 }
5670
5671 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5672 if(nErrCode == -1 || u8 != 1) {
5673 return 28;
5674 }
5675
5676 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5677 if(nErrCode != -1) {
5678 return 29;
5679 }
5680
5681 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5682 if(nErrCode != -1) {
5683 return 30;
5684 }
5685
5686
5687 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5688 if(nErrCode == -1 || u64 != 1) {
5689 return 31;
5690 }
5691
5692 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
5693 if(nErrCode == -1 || u64 != INT64_MAX) {
5694 return 32;
5695 }
5696
5697 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
5698 if(nErrCode == -1 || u64 != 0) {
5699 return 33;
5700 }
5701
5702 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
5703 if(nErrCode != -1) {
5704 return 34;
5705 }
5706
5707 return 0;
5708}
5709
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005710
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005711
5712
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005713/*
5714A sequence with
5715 A wrapping bstr
5716 containing a map
5717 1
5718 2
5719 A wrapping bstr
5720 containing an array
5721 3
5722 wrapping bstr
5723 4
5724 5
5725 6
5726 array
5727 7
5728 8
5729
5730 */
5731
5732static UsefulBufC foo(UsefulBuf ffo)
5733{
5734 UsefulBufC Encoded;
5735 QCBOREncodeContext EC;
5736 QCBORError uErr;
5737
5738 QCBOREncode_Init(&EC, ffo);
5739
5740 QCBOREncode_BstrWrap(&EC);
5741 QCBOREncode_OpenMap(&EC);
5742 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
5743 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
5744 QCBOREncode_CloseMap(&EC);
5745 QCBOREncode_BstrWrap(&EC);
5746 QCBOREncode_OpenArray(&EC);
5747 QCBOREncode_AddInt64(&EC, 3);
5748 QCBOREncode_BstrWrap(&EC);
5749 QCBOREncode_AddInt64(&EC, 4);
5750 QCBOREncode_CloseBstrWrap(&EC, NULL);
5751 QCBOREncode_AddInt64(&EC, 5);
5752 QCBOREncode_CloseArray(&EC);
5753 QCBOREncode_CloseBstrWrap(&EC, NULL);
5754 QCBOREncode_AddInt64(&EC, 6);
5755 QCBOREncode_CloseBstrWrap(&EC, NULL);
5756 QCBOREncode_OpenArray(&EC);
5757 QCBOREncode_AddInt64(&EC, 7);
5758 QCBOREncode_AddInt64(&EC, 8);
5759 QCBOREncode_CloseArray(&EC);
5760
5761 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07005762 if(uErr) {
5763 Encoded = NULLUsefulBufC;
5764 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005765
5766 return Encoded;
5767}
5768
5769
5770int32_t EnterBstrTest()
5771{
5772 MakeUsefulBufOnStack(ffo, 100);
5773
5774 QCBORDecodeContext DC;
5775
5776 QCBORDecode_Init(&DC, foo(ffo), 0);
5777
5778 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
5779
5780
Laurence Lundblade9b334962020-08-27 10:55:53 -07005781 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005782 QCBORDecode_EnterMap(&DC);
5783 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
5784 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
5785 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005786 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005787 QCBORDecode_EnterArray(&DC);
5788 QCBORDecode_GetInt64(&DC, &i3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005789 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07005790 QCBORDecode_GetInt64(&DC, &i4);
5791 QCBORDecode_ExitBstrWrapped(&DC);
5792 QCBORDecode_GetInt64(&DC, &i5);
5793 QCBORDecode_ExitArray(&DC);
5794 QCBORDecode_ExitBstrWrapped(&DC);
5795 QCBORDecode_GetInt64(&DC, &i6);
5796 QCBORDecode_ExitBstrWrapped(&DC);
5797 QCBORDecode_EnterArray(&DC);
5798 QCBORDecode_GetInt64(&DC, &i7);
5799 QCBORDecode_GetInt64(&DC, &i8);
5800 QCBORDecode_ExitArray(&DC);
5801
5802 QCBORError uErr = QCBORDecode_Finish(&DC);
5803
5804 return (int32_t)uErr;
5805}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005806
5807
5808
5809
5810static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005811 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005812
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005813 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005814 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005815 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
5816 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
5817 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005818
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005819 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005820 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
5821 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
5822 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005823
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005824 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005825 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005826 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
5827 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005828
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005829 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005830 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
5831 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005832
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005833 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005834 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005835 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
5836 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005837
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005838 21,
5839 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
5840 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
5841
5842 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005843 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005844 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
5845 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005846
5847 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005848 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
5849 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005850
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005851 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005852 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005853 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
5854 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005855
5856 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005857 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
5858 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005859
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005860 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005861 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005862 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
5863 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005864
5865 0x18, 0x33,
5866 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
5867
5868 // MIME
5869 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005870 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
5871 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
5872 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005873
5874 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005875 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
5876 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005877
5878 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005879 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
5880 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
5881 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005882
5883 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005884 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
5885 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005886
5887 // UUID
5888 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005889 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
5890 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005891
5892 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005893 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
5894 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005895};
5896
5897int32_t DecodeTaggedTypeTests()
5898{
5899 QCBORDecodeContext DC;
5900 QCBORError uErr;
5901
5902 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
5903
5904 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005905 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005906
5907 QCBORDecode_EnterMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005908 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005909 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005910 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
5911 return 1;
5912 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005913 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005914 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5915 return 2;
5916 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005917 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005918 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5919 return 3;
5920 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005921 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005922 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005923 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5924 return 4;
5925 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005926 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
5927 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5928 return 5;
5929 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005930
Laurence Lundblade9b334962020-08-27 10:55:53 -07005931 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005932 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5933 bNeg != false) {
5934 return 10;
5935 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005936 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005937 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
5938 bNeg != true) {
5939 return 11;
5940 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005941 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005942 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
5943 return 12;
5944 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005945 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005946 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5947 return 13;
5948 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005949 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005950 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5951 return 14;
5952 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005953
Laurence Lundblade9b334962020-08-27 10:55:53 -07005954 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005955 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5956 return 20;
5957 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005958 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005959 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5960 return 21;
5961 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005962 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005963 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5964 return 22;
5965 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005966 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005967 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5968 return 23;
5969 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005970
Laurence Lundblade9b334962020-08-27 10:55:53 -07005971 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005972 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5973 return 30;
5974 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005975 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005976 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5977 return 31;
5978 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005979 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005980 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5981 return 32;
5982 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005983 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005984 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5985 return 33;
5986 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07005987
Laurence Lundblade9b334962020-08-27 10:55:53 -07005988 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005989 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5990 return 40;
5991 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005992 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005993 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
5994 return 41;
5995 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005996 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07005997 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
5998 return 42;
5999 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006000 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006001 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
6002 return 43;
6003 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006004
Laurence Lundblade9b334962020-08-27 10:55:53 -07006005 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006006 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6007 return 50;
6008 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006009 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006010 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6011 return 51;
6012 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006013 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006014 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
6015 return 52;
6016 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006017 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006018 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
6019 return 53;
6020 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006021
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006022 // MIME
6023 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006024 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006025 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6026 bIsNot7Bit == true) {
6027 return 60;
6028 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006029 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006030 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6031 bIsNot7Bit == true) {
6032 return 61;
6033 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006034 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006035 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6036 bIsNot7Bit == false) {
6037 return 62;
6038 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006039 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006040 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6041 bIsNot7Bit == false) {
6042 return 63;
6043 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006044 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006045 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
6046 return 64;
6047 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006048 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006049 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
6050 return 65;
6051 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006052
Laurence Lundblade9b334962020-08-27 10:55:53 -07006053 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006054 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6055 return 70;
6056 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006057 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006058 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6059 return 71;
6060 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006061 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006062 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
6063 return 72;
6064 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006065 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006066 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_NOT_FOUND) {
6067 return 73;
6068 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006069
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006070 // Improvement: add some more error test cases
6071
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006072 QCBORDecode_ExitMap(&DC);
6073
6074 uErr = QCBORDecode_Finish(&DC);
6075 if(uErr != QCBOR_SUCCESS) {
6076 return 100;
6077 }
6078
6079 return 0;
6080}