blob: 71bde052cee3766f5fd9d7a6c1078d6bc441986d [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070036#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080037#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080038#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070039#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080040
Laurence Lundblade9b334962020-08-27 10:55:53 -070041// Handy macro to compare a UsefulBuf to a C string
42#define UsefulBufCompareToSZ(x, y) \
43 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044
Laurence Lundbladea2e29072018-12-30 09:20:06 -080045#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080046#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080047
48static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080049{
50 if(szLabel) {
51 printf("%s ", szLabel);
52 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080053
Laurence Lundblade570fab52018-10-13 18:28:27 +080054 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080055 for(i = 0; i < Buf.len; i++) {
56 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 printf("%02x ", Z);
58 }
59 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080060
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 fflush(stdout);
62}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080063#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064
65
Laurence Lundblade9b334962020-08-27 10:55:53 -070066// TODO: error handling for well-formed CBOR that is invalid or hits an implementation limit
67
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070068static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080069 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
71 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
72 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
73 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
74 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
75 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
76 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
77 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
78 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
79 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
80 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
81 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
82 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
83 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
84 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
85 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
86 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
87 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
88 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
89 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
90 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
91 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
92 0xff, 0xff};
93
94
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080095// return CBOR error or -1 if type of value doesn't match
96
Laurence Lundbladec5fef682020-01-25 11:38:45 -080097static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080098{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070099 QCBORItem Item;
100 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800101
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700103 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800104 if(Item.uDataType != QCBOR_TYPE_ARRAY)
105 return -1;
106
107 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700108 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800109 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800110 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800111 return -1;
112
113 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700114 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800116 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800118
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700120 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800122 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800124
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700126 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800128 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800131 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700132 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800134 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800136
137
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700139 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800140 if(Item.uDataType != QCBOR_TYPE_INT64 ||
141 Item.val.int64 != -2147483648)
142 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800143
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700145 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800146 if(Item.uDataType != QCBOR_TYPE_INT64 ||
147 Item.val.int64 != -2147483647)
148 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800149
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700151 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800152 if(Item.uDataType != QCBOR_TYPE_INT64 ||
153 Item.val.int64 != -65538)
154 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800155
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700157 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800158 if(Item.uDataType != QCBOR_TYPE_INT64 ||
159 Item.val.int64 != -65537)
160 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800162 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700163 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164 if(Item.uDataType != QCBOR_TYPE_INT64 ||
165 Item.val.int64 != -65536)
166 return -1;
167
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700170 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171 if(Item.uDataType != QCBOR_TYPE_INT64 ||
172 Item.val.int64 != -65535)
173 return -1;
174
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800175
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700177 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800178 if(Item.uDataType != QCBOR_TYPE_INT64 ||
179 Item.val.int64 != -65534)
180 return -1;
181
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700184 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 if(Item.uDataType != QCBOR_TYPE_INT64 ||
186 Item.val.int64 != -257)
187 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800188
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700190 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 if(Item.uDataType != QCBOR_TYPE_INT64 ||
192 Item.val.int64 != -256)
193 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700196 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if(Item.uDataType != QCBOR_TYPE_INT64 ||
198 Item.val.int64 != -255)
199 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700202 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800203 if(Item.uDataType != QCBOR_TYPE_INT64 ||
204 Item.val.int64 != -254)
205 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700209 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800210 if(Item.uDataType != QCBOR_TYPE_INT64 ||
211 Item.val.int64 != -25)
212 return -1;
213
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800214
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700216 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800217 if(Item.uDataType != QCBOR_TYPE_INT64 ||
218 Item.val.int64 != -24)
219 return -1;
220
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700223 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 if(Item.uDataType != QCBOR_TYPE_INT64 ||
225 Item.val.int64 != -23)
226 return -1;
227
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800228
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800229 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700230 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800231 if(Item.uDataType != QCBOR_TYPE_INT64 ||
232 Item.val.int64 != -1)
233 return -1;
234
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700237 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238 if(Item.uDataType != QCBOR_TYPE_INT64 ||
239 Item.val.int64 != 0)
240 return -1;
241
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700244 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800245 if(Item.uDataType != QCBOR_TYPE_INT64 ||
246 Item.val.int64 != 0)
247 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800248
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700250 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(Item.uDataType != QCBOR_TYPE_INT64 ||
252 Item.val.int64 != 1)
253 return -1;
254
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700257 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 if(Item.uDataType != QCBOR_TYPE_INT64 ||
259 Item.val.int64 != 22)
260 return -1;
261
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700264 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 if(Item.uDataType != QCBOR_TYPE_INT64 ||
266 Item.val.int64 != 23)
267 return -1;
268
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800269
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800270 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700271 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800272 if(Item.uDataType != QCBOR_TYPE_INT64 ||
273 Item.val.int64 != 24)
274 return -1;
275
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800276
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800277 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700278 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800279 if(Item.uDataType != QCBOR_TYPE_INT64 ||
280 Item.val.int64 != 25)
281 return -1;
282
283 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700284 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800285 if(Item.uDataType != QCBOR_TYPE_INT64 ||
286 Item.val.int64 != 26)
287 return -1;
288
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800289
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700291 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800292 if(Item.uDataType != QCBOR_TYPE_INT64 ||
293 Item.val.int64 != 254)
294 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800295
296
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700298 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800299 if(Item.uDataType != QCBOR_TYPE_INT64 ||
300 Item.val.int64 != 255)
301 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800302
303
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700305 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306 if(Item.uDataType != QCBOR_TYPE_INT64 ||
307 Item.val.int64 != 256)
308 return -1;
309
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800310
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700312 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800313 if(Item.uDataType != QCBOR_TYPE_INT64 ||
314 Item.val.int64 != 257)
315 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700318 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800319 if(Item.uDataType != QCBOR_TYPE_INT64 ||
320 Item.val.int64 != 65534)
321 return -1;
322
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800323
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700325 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800326 if(Item.uDataType != QCBOR_TYPE_INT64 ||
327 Item.val.int64 != 65535)
328 return -1;
329
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800330
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700332 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800333 if(Item.uDataType != QCBOR_TYPE_INT64 ||
334 Item.val.int64 != 65536)
335 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700338 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800339 if(Item.uDataType != QCBOR_TYPE_INT64 ||
340 Item.val.int64 != 65537)
341 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700344 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800345 if(Item.uDataType != QCBOR_TYPE_INT64 ||
346 Item.val.int64 != 65538)
347 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800348
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700350 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
352 Item.val.int64 != 2147483647)
353 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700356 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800357 if(Item.uDataType != QCBOR_TYPE_INT64 ||
358 Item.val.int64 != 2147483647)
359 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700362 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800363 if(Item.uDataType != QCBOR_TYPE_INT64 ||
364 Item.val.int64 != 2147483648)
365 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700368 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800369 if(Item.uDataType != QCBOR_TYPE_INT64 ||
370 Item.val.int64 != 2147483649)
371 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700374 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800375 if(Item.uDataType != QCBOR_TYPE_INT64 ||
376 Item.val.int64 != 4294967294)
377 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800378
379
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700381 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800382 if(Item.uDataType != QCBOR_TYPE_INT64 ||
383 Item.val.int64 != 4294967295)
384 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800385
386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700388 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800389 if(Item.uDataType != QCBOR_TYPE_INT64 ||
390 Item.val.int64 != 4294967296)
391 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800392
393
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800394 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700395 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800396 if(Item.uDataType != QCBOR_TYPE_INT64 ||
397 Item.val.int64 != 4294967297)
398 return -1;
399
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800400
401
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800402 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700403 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800404 if(Item.uDataType != QCBOR_TYPE_INT64 ||
405 Item.val.int64 != 9223372036854775807LL)
406 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800407
408
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700410 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
412 Item.val.uint64 != 18446744073709551615ULL)
413 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800414
415
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800416 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
417 return -1;
418 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800419
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800420 return 0;
421}
422
423
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800424// One less than the smallest negative integer allowed in C. Decoding
425// this should fail.
426static const uint8_t spTooSmallNegative[] = {
427 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000428};
429
430
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800431/*
432 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800433 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800435int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800436{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000437 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800438 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800439
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000440 QCBORDecode_Init(&DCtx,
441 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
442 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800443
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000444 // The really big test of all successes
445 nReturn = IntegerValuesParseTestInternal(&DCtx);
446 if(nReturn) {
447 return nReturn;
448 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800449
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000450 // The one large negative integer that can be parsed
451 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800452 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000453 QCBOR_DECODE_MODE_NORMAL);
454
455 QCBORItem item;
456 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
457 nReturn = -4000;
458 }
459
460 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800461}
462
463
464/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800465 Creates a simple CBOR array and returns it in *pEncoded. The array is
466 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800469
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 */
471
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800472static uint8_t spSimpleArrayBuffer[50];
473
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800474static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475{
476 QCBOREncodeContext ECtx;
477 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800478
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800479 *pEncoded = NULL;
480 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800483 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800484 // and last with the buffer to do the actual encoding
485 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700486 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487 QCBOREncode_OpenArray(&ECtx);
488 QCBOREncode_AddInt64(&ECtx, nInt1);
489 QCBOREncode_AddInt64(&ECtx, nInt2);
490 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
491 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
492 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800493
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800494 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800495 goto Done;
496
497 if(*pEncoded != NULL) {
498 nReturn = 0;
499 goto Done;
500 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800501
502 // Use static buffer to avoid dependency on malloc()
503 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800504 goto Done;
505 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800506 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800508 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800509
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800510Done:
511 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800512}
513
514
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800515/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800516 Some basic CBOR with map and array used in a lot of tests.
517 The map labels are all strings
518
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800519 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900520 "an array of two strings": [
521 "string1", "string2"
522 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523 "map in a map": {
524 "bytes 1": h'78787878',
525 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900526 "another int": 98,
527 "text 2": "lies, damn lies and statistics"
528 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529 }
530 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800531static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700532 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
533 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
534 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
535 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
536 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
537 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
538 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
539 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
540 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
541 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
542 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
543 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
544 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
545 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
546 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700547 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800548
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700549// Same as above, but with indefinite lengths.
550static const uint8_t pValidMapIndefEncoded[] = {
5510xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
5520x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
5530x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
5540x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
5550x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
5560x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0xff, 0x6c, 0x6d,
5570x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
5580x70, 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
5590x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
5600x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
5610x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
5620x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
5630x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
5640x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
5650x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
5660x73, 0xff, 0xff};
567
568
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800569static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700570 size_t nLen,
571 int64_t *pInt1,
572 int64_t *pInt2,
573 const uint8_t **pBuf3,
574 size_t *pBuf3Len,
575 const uint8_t **pBuf4,
576 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800577{
578 QCBORDecodeContext DCtx;
579 QCBORItem Item;
580 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800581
Laurence Lundbladeee851742020-01-08 08:37:05 -0800582 QCBORDecode_Init(&DCtx,
583 (UsefulBufC){pEncoded, nLen},
584 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800586 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700587 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
588 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800591
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800592 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700593 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
594 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700596 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800598
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700600 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
601 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800602 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700603 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700607 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
608 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700610 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800611 *pBuf3 = Item.val.string.ptr;
612 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800613
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800614 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700615 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
616 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700618 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800619 *pBuf4 = Item.val.string.ptr;
620 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800621
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800622 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800624Done:
625 return(nReturn);
626}
627
628
629
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800630
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800631int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800632{
633 uint8_t *pEncoded;
634 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800635
Laurence Lundblade5e390822019-01-06 12:35:01 -0800636 int64_t i1=0, i2=0;
637 size_t i3=0, i4=0;
638 const uint8_t *s3= (uint8_t *)"";
639 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800640
641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800642 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
643 return(-1);
644 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800646 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800648 if(i1 != 23 ||
649 i2 != 6000 ||
650 i3 != 8 ||
651 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530652 memcmp("galactic", s3, 8) !=0 ||
653 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654 return(-1);
655 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800656
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800657 return(0);
658}
659
660
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700661/*
662 [
663 0,
664 [],
665 [
666 [],
667 [
668 0
669 ],
670 {},
671 {
672 1: {},
673 2: {},
674 3: []
675 }
676 ]
677 ]
678 */
679static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
680 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
681
Laurence Lundblade02625d42020-06-25 14:41:41 -0700682/* Same as above, but with indefinte lengths */
683static uint8_t sEmptiesIndef[] = {
6840x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700685 0x00,
686 0x9F,
687 0xFF,
688 0x9F,
689 0x9F,
690 0xFF,
691 0x9F,
692 0x00,
693 0xFF,
694 0xBF,
695 0xFF,
696 0xBF,
697 0x01,
698 0xBF,
699 0xFF,
700 0x02,
701 0xBF,
702 0xFF,
703 0x03,
704 0x9F,
705 0xFF,
706 0xFF,
707 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700708 0xFF};
709
710
711
712static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700713{
714 QCBORDecodeContext DCtx;
715 QCBORItem Item;
716
Laurence Lundbladeee851742020-01-08 08:37:05 -0800717 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700718 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800719 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700720
721 // Array with 3 items
722 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
723 Item.uDataType != QCBOR_TYPE_ARRAY ||
724 Item.uNestingLevel != 0 ||
725 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700726 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700727 return -1;
728 }
729
730 // An integer 0
731 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
732 Item.uDataType != QCBOR_TYPE_INT64 ||
733 Item.uNestingLevel != 1 ||
734 Item.uNextNestLevel != 1 ||
735 Item.val.uint64 != 0) {
736 return -2;
737 }
738
739 // An empty array
740 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
741 Item.uDataType != QCBOR_TYPE_ARRAY ||
742 Item.uNestingLevel != 1 ||
743 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700744 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700745 return -3;
746 }
747
748 // An array with 4 items
749 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
750 Item.uDataType != QCBOR_TYPE_ARRAY ||
751 Item.uNestingLevel != 1 ||
752 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700753 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700754 return -4;
755 }
756
757 // An empty array
758 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
759 Item.uDataType != QCBOR_TYPE_ARRAY ||
760 Item.uNestingLevel != 2 ||
761 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700762 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700763 return -5;
764 }
765
766 // An array with 1 item
767 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
768 Item.uDataType != QCBOR_TYPE_ARRAY ||
769 Item.uNestingLevel != 2 ||
770 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700771 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700772 return -6;
773 }
774
775 // An integer 0
776 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
777 Item.uDataType != QCBOR_TYPE_INT64 ||
778 Item.uNestingLevel != 3 ||
779 Item.uNextNestLevel != 2 ||
780 Item.val.uint64 != 0) {
781 return -7;
782 }
783
784 // An empty map
785 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
786 Item.uDataType != QCBOR_TYPE_MAP ||
787 Item.uNestingLevel != 2 ||
788 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700789 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700790 return -8;
791 }
792
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700793 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700794 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
795 Item.uDataType != QCBOR_TYPE_MAP ||
796 Item.uNestingLevel != 2 ||
797 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700798 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700799 return -9;
800 }
801
802 // An empty map
803 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
804 Item.uDataType != QCBOR_TYPE_MAP ||
805 Item.uNestingLevel != 3 ||
806 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700807 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700808 return -10;
809 }
810
811 // An empty map
812 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
813 Item.uDataType != QCBOR_TYPE_MAP ||
814 Item.uNestingLevel != 3 ||
815 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700816 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700817 return -11;
818 }
819
820 // An empty array
821 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
822 Item.uDataType != QCBOR_TYPE_ARRAY ||
823 Item.uNestingLevel != 3 ||
824 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700825 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700826 return -12;
827 }
828
829 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
830 return -13;
831 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700832 return 0;
833}
834
835
836int32_t EmptyMapsAndArraysTest()
837{
838 int nResult;
839 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
840 true);
841 if(nResult) {
842 return nResult;
843 }
844
845 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
846 false);
847
848 if(nResult) {
849 return nResult -100;
850 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700851
852 return 0;
853}
854
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800855
Laurence Lundbladeee851742020-01-08 08:37:05 -0800856static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
857 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800858
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800859int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800860{
861 QCBORDecodeContext DCtx;
862 int nReturn = 0;
863 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800864
Laurence Lundbladeee851742020-01-08 08:37:05 -0800865 QCBORDecode_Init(&DCtx,
866 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
867 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800868
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800869 for(i = 0; i < 10; i++) {
870 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800871
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800872 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
873 Item.uDataType != QCBOR_TYPE_ARRAY ||
874 Item.uNestingLevel != i) {
875 nReturn = -1;
876 break;
877 }
878 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800879
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800880 return(nReturn);
881}
882
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700883// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800884static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
885 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
886 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
887 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800888
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800889int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800890{
891 QCBORDecodeContext DCtx;
892 int nReturn = 0;
893 int i;
894 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
896
Laurence Lundbladeee851742020-01-08 08:37:05 -0800897 QCBORDecode_Init(&DCtx,
898 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
899 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800900
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700901 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800902
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800903 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
904 Item.uDataType != QCBOR_TYPE_ARRAY ||
905 Item.uNestingLevel != i) {
906 nReturn = -1;
907 break;
908 }
909 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800910
Laurence Lundbladea9489f82020-09-12 13:50:56 -0700911 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800912 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800913
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800914 return(nReturn);
915}
916
917
918
919
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800920int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800921{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700922 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800923
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800924 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700925 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800926
Laurence Lundbladeee851742020-01-08 08:37:05 -0800927 QCBORDecode_Init(&DCtx,
928 (UsefulBufC){spExpectedEncodedInts, nNum},
929 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800931 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800932
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700933 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800934 nResult = -1;
935 goto Done;
936 }
937 }
938Done:
939 return nResult;
940}
941
942
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800943
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800944int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800945{
946 uint8_t *pEncoded;
947 int nReturn;
948 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800949
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800950 int64_t i1, i2;
951 size_t i3, i4;
952 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800953
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800954 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800955
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800956 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
957 return(-1);
958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800959
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -0700961 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
962 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800963 if(nResult == 0) {
964 nReturn = -1;
965 }
966 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800967
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800968 return(nReturn);
969}
970
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530971/*
972 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800973 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
974 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530975 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800976static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977{
978 QCBORDecodeContext DCtx;
979 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700980 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800981
Laurence Lundbladeee851742020-01-08 08:37:05 -0800982 QCBORDecode_Init(&DCtx,
983 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
984 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800985
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700987 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900988 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800989 if(Item.uDataType != QCBOR_TYPE_MAP ||
990 Item.val.uCount != 3)
991 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800992
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900993 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700994 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900995 }
Laurence Lundblade9b334962020-08-27 10:55:53 -0700996
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800997 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800998 Item.uDataType != QCBOR_TYPE_INT64 ||
999 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301000 Item.uDataAlloc ||
1001 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001002 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001003 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001005
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001007 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001008 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001009 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301010 Item.uDataAlloc ||
1011 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001012 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001013 Item.uDataType != QCBOR_TYPE_ARRAY ||
1014 Item.val.uCount != 2)
1015 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001016
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001017 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001018 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001019 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001020 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301021 Item.uDataAlloc ||
1022 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001023 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001024 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001026
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001028 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001029 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001030 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301031 Item.uDataAlloc ||
1032 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001033 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001034 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001036
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001037 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001038 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001039 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001040 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301041 Item.uDataAlloc ||
1042 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001043 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001045 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001046 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001047 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001048
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001049 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001050 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001051 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001052 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001053 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001054 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301055 Item.uDataAlloc ||
1056 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001057 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001058 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001060
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001061 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001062 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001063 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001064 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001065 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001066 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301067 Item.uDataAlloc ||
1068 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001069 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001071 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001072
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001073 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001074 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001075 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001076 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301077 Item.uDataAlloc ||
1078 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001079 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001080 Item.uDataType != QCBOR_TYPE_INT64 ||
1081 Item.val.int64 != 98)
1082 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001083
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001084 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001085 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001086 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001087 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001088 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301090 Item.uDataAlloc ||
1091 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001092 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001095
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001096 return 0;
1097}
1098
1099
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001100/*
1101 Decode and thoroughly check a moderately complex
1102 set of maps
1103 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001104int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001105{
1106 QCBORDecodeContext DCtx;
1107 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001108 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001109
Laurence Lundbladeee851742020-01-08 08:37:05 -08001110 QCBORDecode_Init(&DCtx,
1111 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1112 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001113
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001115 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001116 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001117 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1118 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 return -1;
1120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001121
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001123 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001124 }
1125 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1126 Item.uDataAlloc ||
1127 Item.uLabelAlloc ||
1128 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001129 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 return -2;
1131 }
1132
1133 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001134 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001135 }
1136 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1137 Item.uDataType != QCBOR_TYPE_INT64 ||
1138 Item.val.int64 != 42 ||
1139 Item.uDataAlloc ||
1140 Item.uLabelAlloc) {
1141 return -3;
1142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001143
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001145 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001146 }
1147 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1148 Item.uDataAlloc ||
1149 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001150 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001151 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1152 return -4;
1153 }
1154
1155 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001156 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 }
1158 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1159 Item.uDataAlloc ||
1160 Item.uLabelAlloc ||
1161 Item.uDataType != QCBOR_TYPE_ARRAY ||
1162 Item.val.uCount != 2) {
1163 return -5;
1164 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001165
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001167 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001168 }
1169 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1170 Item.val.string.len != 7 ||
1171 Item.uDataAlloc ||
1172 Item.uLabelAlloc ||
1173 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1174 return -6;
1175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001176
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001178 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 }
1180 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1181 Item.uDataAlloc ||
1182 Item.uLabelAlloc ||
1183 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1184 return -7;
1185 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001186
1187
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001189 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001190 }
1191 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1192 Item.uDataAlloc ||
1193 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001194 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 return -8;
1196 }
1197
1198 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001199 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 }
1201 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1202 Item.uDataAlloc ||
1203 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001204 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1205 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001206 return -9;
1207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001208
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001210 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001211 }
1212 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001213 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1215 Item.uDataAlloc ||
1216 Item.uLabelAlloc) {
1217 return -10;
1218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001219
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001221 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001222 }
1223 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1224 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1225 Item.uDataAlloc ||
1226 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001227 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001228 return -11;
1229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001230
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001232 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001233 }
1234 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001235 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001236 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1237 Item.uDataAlloc ||
1238 Item.uLabelAlloc) {
1239 return -12;
1240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001241
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001242 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001243 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001244 }
1245 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1246 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1247 Item.uDataAlloc ||
1248 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001249 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001250 return -13;
1251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001252
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001253 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001254 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001255 }
1256 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1257 Item.uDataAlloc ||
1258 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001259 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1261 return -14;
1262 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001263
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001264 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001265 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001266 }
1267 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1268 Item.uDataAlloc ||
1269 Item.uLabelAlloc ||
1270 Item.uDataType != QCBOR_TYPE_INT64 ||
1271 Item.val.int64 != 98) {
1272 return -15;
1273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001274
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001275 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001276 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001277 }
1278 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001279 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001280 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1281 Item.uDataAlloc ||
1282 Item.uLabelAlloc) {
1283 return -16;
1284 }
1285
1286 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001287 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001288 }
1289 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1290 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1291 Item.uDataAlloc ||
1292 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001293 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001294 return -17;
1295 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001296
1297
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001298 /*
1299 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1300 map that when interpreted as an array will be too many. Test
1301 data just has the start of the map, not all the items in the map.
1302 */
1303 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001304
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001305 QCBORDecode_Init(&DCtx,
1306 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1307 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001308
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001309 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001310 return -50;
1311 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001312
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001313 return 0;
1314}
1315
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001316
1317/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301318 Fully or partially decode pValidMapEncoded. When
1319 partially decoding check for the right error code.
1320 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301322 The partial decodes test error conditions of
1323 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001324
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301325 This could be combined with the above test
1326 and made prettier and maybe a little more
1327 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001329static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001330{
1331 QCBORDecodeContext DCtx;
1332 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001333 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001334
Laurence Lundbladeee851742020-01-08 08:37:05 -08001335 QCBORDecode_Init(&DCtx,
1336 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1337 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001338
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001339 if(nLevel < 1) {
1340 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1341 return -1;
1342 } else {
1343 return 0;
1344 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001345 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301346
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001347
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001348 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001349 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001350 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001351 if(Item.uDataType != QCBOR_TYPE_MAP ||
1352 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001353 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001354
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001355 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001356 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1357 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001358 } else {
1359 return 0;
1360 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001362
1363
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001364 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001365 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001366 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 Item.uDataType != QCBOR_TYPE_INT64 ||
1369 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001370 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001371 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001373
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001374 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001375 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1376 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001377 } else {
1378 return 0;
1379 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001381
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001382 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001383 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001386 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001388 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001389 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
1392
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001393 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001394 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1395 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001396 } else {
1397 return 0;
1398 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001400
1401
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001402 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001403 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001406 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001407 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001409
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001410 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001411 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1412 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001413 } else {
1414 return 0;
1415 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001417
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001418 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001419 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001420 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001421 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001422 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001423 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001425
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001426 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001427 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1428 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001429 } else {
1430 return 0;
1431 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001432 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001433
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001434 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001435 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001436 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001438 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001439 Item.uDataType != QCBOR_TYPE_MAP ||
1440 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001441 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001442
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001443 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001444 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1445 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001446 } else {
1447 return 0;
1448 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001449 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001450
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001451 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001452 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001453 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001455 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001457 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001458 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001460
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001461 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001462 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1463 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001464 } else {
1465 return 0;
1466 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001469 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001470 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001471 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001472 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001473 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001474 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001475 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001476 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001479 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001480 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1481 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001482 } else {
1483 return 0;
1484 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001485 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001486
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001487 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001488 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001489 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001491 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492 Item.uDataType != QCBOR_TYPE_INT64 ||
1493 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001494 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001495
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001496 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001497 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1498 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001499 } else {
1500 return 0;
1501 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001502 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001503
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001504 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001505 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001506 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001507 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001508 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001509 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001510 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001511 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001512 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001513
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301514 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001515 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001517
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001518 return 0;
1519}
1520
1521
1522
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001523
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001524int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001525{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001526 // Parse a moderatly complex map structure very thoroughly
1527 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1528 if(nResult) {
1529 return nResult;
1530 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001531
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001532 // Again, but in strings-only mode. It should succeed since the input
1533 // map has only string labels.
1534 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1535 if(nResult) {
1536 return nResult;
1537 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001538
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001539 // Again, but try to finish the decoding before the end of the
1540 // input at 10 different place and see that the right error code
1541 // is returned.
1542 for(int i = 0; i < 10; i++) {
1543 nResult = ExtraBytesTest(i);
1544 if(nResult) {
1545 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001546 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001547 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001548
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001549 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001550}
1551
1552
Laurence Lundbladeee851742020-01-08 08:37:05 -08001553static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1554 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1555 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001556
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001557int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001558{
1559 QCBORDecodeContext DCtx;
1560 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001561 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001562
1563
Laurence Lundbladeee851742020-01-08 08:37:05 -08001564 QCBORDecode_Init(&DCtx,
1565 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1566 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001567
1568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001570 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001571 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1572 Item.val.uCount != 10)
1573 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001574
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001576 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001577 if(Item.uDataType != QCBOR_TYPE_FALSE)
1578 return -1;
1579
1580 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001581 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 if(Item.uDataType != QCBOR_TYPE_TRUE)
1583 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001586 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 if(Item.uDataType != QCBOR_TYPE_NULL)
1588 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001591 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001592 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1593 return -1;
1594
1595 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001596 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001597 return -1;
1598
1599 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001600 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001601 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1602 return -1;
1603
1604 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001605 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001606 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1607 return -1;
1608
Laurence Lundblade077475f2019-04-26 09:06:33 -07001609 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001610 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001611
Laurence Lundblade077475f2019-04-26 09:06:33 -07001612 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001613 return -1;
1614
Laurence Lundblade077475f2019-04-26 09:06:33 -07001615 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001616 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001617
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001618 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001619 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001620 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1621 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001622
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001623 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001624 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001625 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1626 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001627
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001628 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001629
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001630}
1631
1632
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001633int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001634{
1635 // Loop over all the not-well-formed instance of CBOR
1636 // that are test vectors in not_well_formed_cbor.h
1637 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1638 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1639 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1640 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1641
Laurence Lundbladeee851742020-01-08 08:37:05 -08001642 // Set up decoder context. String allocator needed for indefinite
1643 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001644 QCBORDecodeContext DCtx;
1645 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1646 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1647 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1648
1649 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001650 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001651 do {
1652 QCBORItem Item;
1653
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001654 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1655 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001656
1657 // Every test vector must fail with
1658 // a not-well-formed error. If not
1659 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001660 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001661 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001662 // Return index of failure in the error code
1663 return 2000 + nIterate;
1664 }
1665 }
1666 return 0;
1667}
1668
1669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001670struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001671 UsefulBufC Input;
1672 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001673};
1674
Laurence Lundblade59289e52019-12-30 13:44:37 -08001675
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001676static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001677{
1678 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1679 // Set up the decoding context including a memory pool so that
1680 // indefinite length items can be checked
1681 QCBORDecodeContext DCtx;
1682 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1683 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001684
Laurence Lundblade59289e52019-12-30 13:44:37 -08001685 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1686 if(nCBORError) {
1687 return -9;
1688 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001689
Laurence Lundblade59289e52019-12-30 13:44:37 -08001690 // Iterate until there is an error of some sort error
1691 QCBORItem Item;
1692 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001693 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001694 memset(&Item, 0x33, sizeof(Item));
1695
1696 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1697 } while(nCBORError == QCBOR_SUCCESS);
1698
1699 // Must get the expected error or the this test fails
1700 // The data and label type must also be QCBOR_TYPE_NONE
1701 if(nCBORError != pF->nError ||
1702 Item.uDataType != QCBOR_TYPE_NONE ||
1703 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001704 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001705 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001706 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001707 }
1708 }
1709
1710 return 0;
1711}
1712
1713
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001714struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001715 // Most of this is copied from not_well_formed.h. Here the error code
1716 // returned is also checked.
1717
1718 // Indefinite length strings must be closed off
1719 // An indefinite length byte string not closed off
1720 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1721 // An indefinite length text string not closed off
1722 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1723
1724
1725 // All the chunks in an indefinite length string must be of the type of indefinite length string
1726 // indefinite length byte string with text string chunk
1727 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1728 // indefinite length text string with a byte string chunk
1729 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1730 // indefinite length byte string with an positive integer chunk
1731 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1732 // indefinite length byte string with an negative integer chunk
1733 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1734 // indefinite length byte string with an array chunk
1735 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1736 // indefinite length byte string with an map chunk
1737 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1738 // indefinite length byte string with tagged integer chunk
1739 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1740 // indefinite length byte string with an simple type chunk
1741 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1742 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1743 // indefinite length text string with indefinite string inside
1744 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1745
1746
1747 // Definte length maps and arrays must be closed by having the right number of items
1748 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001749 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001750 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001751 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752 // A definte length array that is supposed to have 511 items, but has only 1
1753 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1754 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001755 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001756 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001757 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001758
1759
1760 // Indefinte length maps and arrays must be ended by a break
1761 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001762 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001764 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001765 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001766 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001767 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001768 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001769
1770
1771 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001772 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001773 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001774 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001775 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001776 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001777 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS }, // TODO: 23
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001778 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001779 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001780 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001781 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001782 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001783 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001784 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001785
1786
1787 // The "argument" for the data item is incomplete
1788 // Positive integer missing 1 byte argument
1789 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1790 // Positive integer missing 2 byte argument
1791 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1792 // Positive integer missing 4 byte argument
1793 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1794 // Positive integer missing 8 byte argument
1795 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1796 // Positive integer missing 1 byte of 2 byte argument
1797 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1798 // Positive integer missing 2 bytes of 4 byte argument
1799 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1800 // Positive integer missing 1 bytes of 7 byte argument
1801 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1802 // Negative integer missing 1 byte argument
1803 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1804 // Binary string missing 1 byte argument
1805 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1806 // Text string missing 1 byte argument
1807 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1808 // Array missing 1 byte argument
1809 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1810 // Map missing 1 byte argument
1811 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1812 // Tag missing 1 byte argument
1813 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1814 // Simple missing 1 byte argument
1815 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1816
1817
1818 // Breaks must not occur in definite length arrays and maps
1819 // Array of length 1 with sole member replaced by a break
1820 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1821 // Array of length 2 with 2nd member replaced by a break
1822 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1823 // Map of length 1 with sole member label replaced by a break
1824 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1825 // Map of length 1 with sole member label replaced by break
1826 // Alternate representation that some decoders handle difference
1827 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1828 // Array of length 1 with 2nd member value replaced by a break
1829 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1830 // Map of length 2 with 2nd member replaced by a break
1831 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1832
1833
1834 // Breaks must not occur on their own out of an indefinite length data item
1835 // A bare break is not well formed
1836 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1837 // A bare break after a zero length definite length array
1838 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1839 // A bare break after a zero length indefinite length map
1840 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1841
1842
1843 // Forbidden two byte encodings of simple types
1844 // Must use 0xe0 instead
1845 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1846 // Should use 0xe1 instead
1847 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1848 // Should use 0xe2 instead
1849 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1850 // Should use 0xe3 instead
1851 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1852 // Should use 0xe4 instead
1853 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1854 // Should use 0xe5 instead
1855 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1856 // Should use 0xe6 instead
1857 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1858 // Should use 0xe7 instead
1859 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1860 // Should use 0xe8 instead
1861 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1862 // Should use 0xe9 instead
1863 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1864 // Should use 0xea instead
1865 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1866 // Should use 0xeb instead
1867 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1868 // Should use 0xec instead
1869 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1870 // Should use 0xed instead
1871 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1872 // Should use 0xee instead
1873 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1874 // Should use 0xef instead
1875 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1876 // Should use 0xf0 instead
1877 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1878 // Should use 0xf1 instead
1879 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1880 // Should use 0xf2 instead
1881 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1882 // Must use 0xf3 instead
1883 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1884 // Must use 0xf4 instead
1885 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1886 // Must use 0xf5 instead
1887 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1888 // Must use 0xf6 instead
1889 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1890 // Must use 0xf7 instead
1891 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1892 // Must use 0xf8 instead
1893 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1894
1895
1896 // Integers with additional info indefinite length
1897 // Positive integer with additional info indefinite length
1898 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1899 // Negative integer with additional info indefinite length
1900 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1901 // CBOR tag with "argument" an indefinite length
1902 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1903 // CBOR tag with "argument" an indefinite length alternate vector
1904 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1905
1906
1907 // Missing bytes from a deterministic length string
1908 // A byte string is of length 1 without the 1 byte
1909 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1910 // A text string is of length 1 without the 1 byte
1911 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001912 // Byte string should have 2^32-15 bytes, but has one
1913 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1914 // Byte string should have 2^32-15 bytes, but has one
1915 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001916
1917
1918 // Use of unassigned additional information values
1919 // Major type positive integer with reserved value 28
1920 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1921 // Major type positive integer with reserved value 29
1922 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1923 // Major type positive integer with reserved value 30
1924 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1925 // Major type negative integer with reserved value 28
1926 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1927 // Major type negative integer with reserved value 29
1928 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1929 // Major type negative integer with reserved value 30
1930 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1931 // Major type byte string with reserved value 28 length
1932 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1933 // Major type byte string with reserved value 29 length
1934 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1935 // Major type byte string with reserved value 30 length
1936 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1937 // Major type text string with reserved value 28 length
1938 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1939 // Major type text string with reserved value 29 length
1940 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1941 // Major type text string with reserved value 30 length
1942 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1943 // Major type array with reserved value 28 length
1944 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1945 // Major type array with reserved value 29 length
1946 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1947 // Major type array with reserved value 30 length
1948 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1949 // Major type map with reserved value 28 length
1950 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1951 // Major type map with reserved value 29 length
1952 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1953 // Major type map with reserved value 30 length
1954 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1955 // Major type tag with reserved value 28 length
1956 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1957 // Major type tag with reserved value 29 length
1958 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1959 // Major type tag with reserved value 30 length
1960 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1961 // Major type simple with reserved value 28 length
1962 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1963 // Major type simple with reserved value 29 length
1964 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1965 // Major type simple with reserved value 30 length
1966 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1967
1968
1969 // Maps must have an even number of data items (key & value)
1970 // Map with 1 item when it should have 2
1971 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1972 // Map with 3 item when it should have 4
1973 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1974 // Map with 1 item when it should have 2
1975 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1976 // Map with 3 item when it should have 4
1977 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1978
1979
1980 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001981 // Text-based date, with an integer
1982 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1983 // Epoch date, with an byte string
1984 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1985 // tagged as both epoch and string dates
1986 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1987 // big num tagged an int, not a byte string
1988 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001989};
1990
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001991int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001993 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001994
Laurence Lundblade59289e52019-12-30 13:44:37 -08001995 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1996 if(nResult) {
1997 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001999
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002000 // Corrupt the UsefulInputBuf and see that
2001 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002002 QCBORDecodeContext DCtx;
2003 QCBORItem Item;
2004 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002005
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002006 QCBORDecode_Init(&DCtx,
2007 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2008 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002009
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002010 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2011 return (int32_t)uQCBORError;
2012 }
2013 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2014 // This wasn't supposed to happen
2015 return -1;
2016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002017
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002018 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002019
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002020 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2021 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2022 // Did not get back the error expected
2023 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002025
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002026/*
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002027 TODO: fix this
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002028 This test is disabled until QCBOREncode_EncodeHead() is brought in so
2029 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
2030
2031 This relies on the largest string allowed being SIZE_MAX -4 rather than
2032 SIZE_MAX. That way the test can be performed.
2033 {
2034 QCBORDecodeContext DCtx;
2035 QCBORItem Item;
2036
2037 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
2038 0xff, 0xff, 0xff, 0xff};
2039
2040 QCBORDecode_Init(&DCtx,
2041 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
2042 QCBOR_DECODE_MODE_NORMAL);
2043
2044 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2045 return -4;
2046 }
2047 }
2048*/
2049
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002050 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002051}
2052
2053
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002054/* Try all 256 values of the byte at nLen including recursing for
2055 each of the values to try values at nLen+1 ... up to nLenMax
2056 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002057static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002058{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002059 if(nLen >= nLenMax) {
2060 return;
2061 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002062
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002063 for(int inputByte = 0; inputByte < 256; inputByte++) {
2064 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002065 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002066 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002067
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002068 // Get ready to parse
2069 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002070 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002071
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002072 // Parse by getting the next item until an error occurs
2073 // Just about every possible decoder error can occur here
2074 // The goal of this test is not to check for the correct
2075 // error since that is not really possible. It is to
2076 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002077 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002078 QCBORItem Item;
2079 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002081 break;
2082 }
2083 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002084
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002085 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002086 }
2087}
2088
2089
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002090int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002092 // Size 2 tests 64K inputs and runs quickly
2093 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002094
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002095 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002096
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002097 return 0;
2098}
2099
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002100
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002101int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002102{
2103 // size 3 tests 16 million inputs and runs OK
2104 // in seconds on fast machines. Size 4 takes
2105 // 10+ minutes and 5 half a day on fast
2106 // machines. This test is kept separate from
2107 // the others so as to no slow down the use
2108 // of them as a very frequent regression.
2109 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002110
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002111 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002112
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002113 return 0;
2114}
2115
2116
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002117static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002118 0xc0, // tag for string date
2119 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002120
Laurence Lundbladec7114722020-08-13 05:11:40 -07002121 0xc0, // tag for string date
2122 0x00, // Wrong type for a string date
2123
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002124 0xc1, // tag for epoch date
2125 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2126
Laurence Lundbladec7114722020-08-13 05:11:40 -07002127 0xc1,
2128 0x62, 'h', 'i', // wrong type tagged
2129
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002130 // CBOR_TAG_B64
Laurence Lundblade9b334962020-08-27 10:55:53 -07002131 0xcf, 0xd8, 0x22, 0xc1, // 0xee, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002132 0x1a, 0x53, 0x72, 0x4E, 0x01,
2133
2134 0xc1, // tag for epoch date
2135 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002136
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002137 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002138 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002139
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002140 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002141 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002142
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002143 0xc1, // tag for epoch date
2144 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2145 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2146
2147 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002148 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2149
2150 0xc1, // tag for epoch date
2151 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2152
2153 0xc1,
2154 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2155
2156 0xc1, // tag for epoch date
2157 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158};
2159
2160
Laurence Lundbladec7114722020-08-13 05:11:40 -07002161
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002162// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002163#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002164static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002165
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002166 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002167
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002168 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002169
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002170 return diff > 0.0000001;
2171}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002172#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002173
2174
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002175int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002176{
2177 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002178 QCBORItem Item;
2179 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002180
Laurence Lundbladeee851742020-01-08 08:37:05 -08002181 QCBORDecode_Init(&DCtx,
2182 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2183 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002184
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002185 // String date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002186 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002187 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002188 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002189 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002190 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002191 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002192 }
2193
Laurence Lundbladec7114722020-08-13 05:11:40 -07002194 // Wrong type for a string date
2195 uError = QCBORDecode_GetNext(&DCtx, &Item);
2196 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002197 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002198 }
2199
2200 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2201 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2202 return -4;
2203 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002204 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2205 Item.val.epochDate.nSeconds != 1400000000 ||
2206 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002207 return -5;
2208 }
2209
2210 // Wrong type for an epoch date
2211 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2212 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002214
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002215 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2216 // but want to be sure extra tag doesn't cause a problem
Laurence Lundbladec7114722020-08-13 05:11:40 -07002217 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2218 return -7;
2219 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002220 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2221 Item.val.epochDate.nSeconds != 1400000001 ||
2222 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002223 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002224 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227 // Epoch date that is too large for our representation
2228 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002229 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundblade9682a532020-06-06 18:33:04 -07002232#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladec7114722020-08-13 05:11:40 -07002233 // Epoch date in float format with fractional seconds
2234 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2235 return -10;
2236 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002237 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2238 Item.val.epochDate.nSeconds != 1 ||
2239 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002240 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002243 // Epoch date float that is too large for our representation
2244 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002245 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002247
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002248 // Epoch date double that is just slightly too large
2249 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002250 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002251 }
2252
2253 // Largest double epoch date supported
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002254 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2255 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2256 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2257 Item.val.epochDate.nSeconds == 0) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002258 return -14;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002259 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002260
2261 // Nan
2262 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2263 return -15;
2264 }
2265
2266 // +Inifinity double-precision
2267 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2268 return -16;
2269 }
2270
2271#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2272 // -Inifinity half-precision
2273 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2274 return -17;
2275 }
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002276#else
Laurence Lundbladec7114722020-08-13 05:11:40 -07002277 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2278 return -18;
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002279 }
2280#endif
2281
Laurence Lundbladec7114722020-08-13 05:11:40 -07002282#else
2283 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2284 return -19;
2285 }
2286 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2287 return -20;
2288 }
2289 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2290 return -21;
2291 }
2292 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2293 return -22;
2294 }
2295 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2296 return -23;
2297 }
2298 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2299 return -24;
2300 }
2301#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2302 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2303 return -25;
2304 }
2305#else
2306 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_HALF_PRECISION_DISABLED) {
2307 return -26;
2308 }
2309#endif
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002310
Laurence Lundbladec7114722020-08-13 05:11:40 -07002311#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002312
2313 return 0;
2314}
2315
Laurence Lundblade4b270642020-08-14 12:53:07 -07002316/*
2317 Test cases covered here. Some items cover more than one of these.
2318 positive integer (zero counts as a positive integer)
2319 negative integer
2320 half-precision float
2321 single-precision float
2322 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002323
Laurence Lundblade4b270642020-08-14 12:53:07 -07002324 float Overflow error
2325 Wrong type error for epoch
2326 Wrong type error for date string
2327 float disabled error
2328 half-precision disabled error
2329 -Infinity
2330 Slightly too large integer
2331 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002332
Laurence Lundblade4b270642020-08-14 12:53:07 -07002333 Get epoch by int
2334 Get string by int
2335 Get epoch by string
2336 Get string by string
2337 Fail to get epoch by wrong int label
2338 Fail to get string by wrong string label
2339 Fail to get epoch by string because it is invalid
2340 Fail to get epoch by int because it is invalid
2341
2342 Untagged values
2343 */
2344static uint8_t spSpiffyDateTestInput[] = {
2345 0x86,
2346
2347 0xc1,
2348 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2349
Laurence Lundbladec7114722020-08-13 05:11:40 -07002350 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002351 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2352
2353 0xc1, // tag for epoch date
2354 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2355
2356 0xc1, // tag for epoch date
2357 0x9f, 0xff, // Erroneous empty array as content for date
2358
2359 0xc0, // tag for string date
2360 0xbf, 0xff, // Erroneous empty map as content for date
2361
2362 0xbf, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002363
2364 0x00,
2365 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002366 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002367
2368 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002369 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002370 0xc1, // tag for epoch date
2371 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2372
2373 // Untagged integer 0
2374 0x08,
2375 0x00,
2376
2377 // Utagged date string with string label y
2378 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002379 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002380
2381 // Untagged -1000 with label z
2382 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002383 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002384 0x39, 0x03, 0xe7,
2385
Laurence Lundbladec7114722020-08-13 05:11:40 -07002386 0x07,
2387 0xc1, // tag for epoch date
2388 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2389
Laurence Lundblade4b270642020-08-14 12:53:07 -07002390 0x05,
2391 0xc1,
2392 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2393
Laurence Lundbladec7114722020-08-13 05:11:40 -07002394 // Untagged single-precision float with value 3.14 with string label x
2395 0x61, 0x78,
2396 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2397
Laurence Lundbladec7114722020-08-13 05:11:40 -07002398 // Untagged half-precision float with value -2
2399 0x09,
2400 0xF9, 0xC0, 0x00,
Laurence Lundbladec7114722020-08-13 05:11:40 -07002401
2402 0xff,
2403};
2404
2405int32_t SpiffyDateDecodeTest()
2406{
2407 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002408 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002409 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2410 nEpochDate4, nEpochDate6, nEpochDateFail,
2411 nEpochDate1400000000;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002412 UsefulBufC StringDate1, StringDate2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002413 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002414
2415 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002416 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002417 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002418 QCBORDecode_EnterArray(&DC);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002419
Laurence Lundblade9b334962020-08-27 10:55:53 -07002420 // Too-negative float, -9.2233720368547748E+18
2421 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002422 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002423#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade4b270642020-08-14 12:53:07 -07002424 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2425 return 1111;
2426 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002427#else
2428 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2429 return 1112;
2430 }
2431#endif
Laurence Lundblade4b270642020-08-14 12:53:07 -07002432
2433 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002434 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002435 uError = QCBORDecode_GetAndResetError(&DC);
2436 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002437 return 1;
2438 }
2439
Laurence Lundblade4b270642020-08-14 12:53:07 -07002440 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002441 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002442 uError = QCBORDecode_GetAndResetError(&DC);
2443#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2444#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2445 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_DATE_OVERFLOW;
2446#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2447 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_FLOAT_DATE_DISABLED;
2448#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2449#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2450 const QCBORError uExpectedforHalfMinusInfinity = QCBOR_ERR_HALF_PRECISION_DISABLED;
2451#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2452 if(uError != uExpectedforHalfMinusInfinity) {
2453 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002454 }
2455
Laurence Lundblade4b270642020-08-14 12:53:07 -07002456 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002457 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002458 uError = QCBORDecode_GetAndResetError(&DC);
2459 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2460 return 3;
2461 }
2462
2463 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002464 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002465 uError = QCBORDecode_GetAndResetError(&DC);
2466 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2467 return 4;
2468 }
2469
2470 QCBORDecode_EnterMap(&DC);
2471
2472 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002473 QCBORDecode_GetEpochDateInMapN(&DC,
2474 5,
2475 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2476 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2477 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002478#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2479 if(nEpochDate2 != -9223372036854773760LL) {
2480 return 101;
2481 }
2482#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2483 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002484 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002485 return 102;
2486 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002487#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002488
Laurence Lundblade4b270642020-08-14 12:53:07 -07002489 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002490 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2491 &nEpochDate2);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002492#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2493 if(nEpochDate2 != 9223372036854773760ULL) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002494 return 111;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002495 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002496#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2497 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07002498 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002499 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002500 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002501#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2502
2503 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002504 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2505 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002506#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2507 if(nEpochDate5 != 3) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002508 return 103;
2509 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002510#else /* QCBOR_DISABLE_FLOAT_HW_USE */
2511 uError = QCBORDecode_GetAndResetError(&DC);
2512 if(uError != QCBOR_ERR_FLOAT_DATE_DISABLED) {
2513 return 104;
2514 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002515#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2516
Laurence Lundblade9b334962020-08-27 10:55:53 -07002517 // A half-precision date with value -2 FFF
2518 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2519 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002520#if !defined(QCBOR_DISABLE_FLOAT_HW_USE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
2521 if(nEpochDate4 != -2) {
2522 return 105;
2523 }
2524#else
2525 uError = QCBORDecode_GetAndResetError(&DC);
2526 if(uError == QCBOR_SUCCESS) {
2527 return 106;
2528 }
2529#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002530
Laurence Lundblade4b270642020-08-14 12:53:07 -07002531
2532 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002533 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2534 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2535 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002536 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002537 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002538 return 107;
2539 }
2540
2541 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002542 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2543 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002544 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002545 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002546 return 108;
2547 }
2548
2549 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002550 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2551 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2552 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002553 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002554 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002555 return 109;
2556 }
2557
2558 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002559 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2560 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002561 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002562 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002563 return 110;
2564 }
2565
2566 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002567
Laurence Lundblade4b270642020-08-14 12:53:07 -07002568 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002569 QCBORDecode_GetEpochDateInMapN(&DC,
2570 1,
2571 QCBOR_TAG_REQUIREMENT_TAG |
2572 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2573 &nEpochDate1400000000);
2574 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002575 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002576 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2577 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002578 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002579 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2580 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002581 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002582 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2583 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002584 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002585 QCBORDecode_GetEpochDateInMapSZ(&DC,
2586 "z",
2587 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2588 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2589 &nEpochDate6);
2590 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002591
2592 QCBORDecode_ExitMap(&DC);
2593 QCBORDecode_ExitArray(&DC);
2594 uError = QCBORDecode_Finish(&DC);
2595 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002596 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002597 }
2598
Laurence Lundblade9b334962020-08-27 10:55:53 -07002599 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002600 return 200;
2601 }
2602
Laurence Lundblade9b334962020-08-27 10:55:53 -07002603 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002604 return 201;
2605 }
2606
Laurence Lundblade9b334962020-08-27 10:55:53 -07002607 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002608 return 202;
2609 }
2610
Laurence Lundblade9b334962020-08-27 10:55:53 -07002611 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002612 return 203;
2613 }
2614
Laurence Lundblade9b334962020-08-27 10:55:53 -07002615 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002616 return 204;
2617 }
2618
Laurence Lundblade9b334962020-08-27 10:55:53 -07002619 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2620 return 205;
2621 }
2622
2623 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2624 return 206;
2625 }
2626
Laurence Lundbladec7114722020-08-13 05:11:40 -07002627 return 0;
2628}
2629
2630
2631
Laurence Lundblade9b334962020-08-27 10:55:53 -07002632// Input for one of the tagging tests
2633static uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002634 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002635 0x81, // Array of one
2636 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2637 0x82, // Array of two that is the faction 1/3
2638 0x01,
2639 0x03,
2640
2641 /*
2642 More than 4 tags on an item 225(226(227(228(229([])))))
2643 */
2644 0xd8, 0xe1,
2645 0xd8, 0xe2,
2646 0xd8, 0xe3,
2647 0xd8, 0xe4,
2648 0xd8, 0xe5,
2649 0x80,
2650
2651 /* tag 10489608748473423768(
2652 2442302356(
2653 21590(
2654 240(
2655 []))))
2656 */
2657 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2658 0xda, 0x91, 0x92, 0x93, 0x94,
2659 0xd9, 0x54, 0x56,
2660 0xd8, 0xf0,
2661 0x80,
2662
2663 /* tag 21590(
2664 10489608748473423768(
2665 2442302357(
2666 65534(
2667 []))))
2668 */
2669 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2670 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2671 0xda, 0x91, 0x92, 0x93, 0x95,
2672 0xd9, 0xff, 0xfe,
2673 0x80,
2674
2675 /* Make sure to blow past the limit of tags that must be mapped.
2676 works in conjuntion with entries above.
2677 269488144(269488145(269488146(269488147([]))))
2678 */
2679 0xda, 0x10, 0x10, 0x10, 0x10,
2680 0xda, 0x10, 0x10, 0x10, 0x11,
2681 0xda, 0x10, 0x10, 0x10, 0x12,
2682 0xda, 0x10, 0x10, 0x10, 0x13,
2683 0x80,
2684
2685 /* An invalid decimal fraction with an additional tag */
2686 0xd9, 0xff, 0xfa,
2687 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2688 0x00, // the integer 0; should be a byte string
2689};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002690
Laurence Lundblade59289e52019-12-30 13:44:37 -08002691/*
2692 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002693 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002694 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002695static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2696 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002697
Laurence Lundblade59289e52019-12-30 13:44:37 -08002698/*
2699DB 9192939495969798 # tag(10489608748473423768)
2700 D8 88 # tag(136)
2701 C6 # tag(6)
2702 C7 # tag(7)
2703 80 # array(0)
2704*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002705static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2706 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002707
2708/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002709 55799(55799(55799({
2710 6(7(-23)): 5859837686836516696(7({
2711 7(-20): 11({
2712 17(-18): 17(17(17("Organization"))),
2713 9(-17): 773("SSG"),
2714 -15: 16(17(6(7("Confusion")))),
2715 17(-16): 17("San Diego"),
2716 17(-14): 17("US")
2717 }),
2718 23(-19): 19({
2719 -11: 9({
2720 -9: -7
2721 }),
2722 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2723 })
2724 })),
2725 16(-22): 23({
2726 11(8(7(-5))): 8(-3)
2727 })
2728 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002729 */
2730static uint8_t spCSRWithTags[] = {
2731 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2732 0xc6, 0xc7, 0x36,
2733 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2734 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2735 0xcb, 0xa5,
2736 0xd1, 0x31,
2737 0xd1, 0xd1, 0xd1, 0x6c,
2738 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2739 0xc9, 0x30,
2740 0xd9, 0x03, 0x05, 0x63,
2741 0x53, 0x53, 0x47,
2742 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002743 0xd0, 0xd1, 0xc6, 0xc7,
2744 0x69,
2745 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002746 0xd1, 0x2f,
2747 0xd1, 0x69,
2748 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2749 0xd1, 0x2d,
2750 0xd1, 0x62,
2751 0x55, 0x53,
2752 0xd7, 0x32,
2753 0xd3, 0xa2,
2754 0x2a,
2755 0xc9, 0xa1,
2756 0x28,
2757 0x26,
2758 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2759 0xcc, 0x4a,
2760 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2761 0xd0, 0x35,
2762 0xd7, 0xa1,
2763 0xcb, 0xc8, 0xc7, 0x24,
2764 0xc8, 0x22};
2765
Laurence Lundblade9b334962020-08-27 10:55:53 -07002766
2767static uint8_t spSpiffyTagInput[] = {
2768 0x9f, // Open indefinite array
2769
2770 0xc0, // tag for string date
2771 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2772
2773 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2774
2775 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2776
2777 0xd8, 0x23, // tag for regex
2778 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
2779
2780 0xc0, // tag for string date
2781 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
2782
2783 0xff
2784};
2785
2786
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002787static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002788
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002789
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002790int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002791{
2792 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002793 QCBORItem Item;
2794 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002795
Laurence Lundbladeee851742020-01-08 08:37:05 -08002796 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002797 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08002798 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002799
Laurence Lundblade9b334962020-08-27 10:55:53 -07002800 /*
2801 This test matches the magic number tag and the fraction tag
2802 55799([...])
2803 */
2804 uError = QCBORDecode_GetNext(&DCtx, &Item);
2805 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002806 return -2;
2807 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002808 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002809 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2810 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002811 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002812
Laurence Lundblade9b334962020-08-27 10:55:53 -07002813 /*
2814 4([1,3])
2815 */
2816 uError = QCBORDecode_GetNext(&DCtx, &Item);
2817#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2818 if(uError != QCBOR_SUCCESS ||
2819 Item.uDataType != QCBOR_TYPE_ARRAY ||
2820 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
2821 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
2822 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2823 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2824 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2825 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
2826 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002827 return -4;
2828 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002829 // consume the items in the array
2830 uError = QCBORDecode_GetNext(&DCtx, &Item);
2831 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002832
Laurence Lundblade59289e52019-12-30 13:44:37 -08002833#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07002834 if(uError != QCBOR_SUCCESS ||
2835 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
2836 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
2837 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
2838 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
2839 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
2840 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
2841 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002842 }
2843#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002844
Laurence Lundblade9b334962020-08-27 10:55:53 -07002845 /*
2846 More than 4 tags on an item 225(226(227(228(229([])))))
2847 */
2848 uError = QCBORDecode_GetNext(&DCtx, &Item);
2849 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002850 return -6;
2851 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07002852
2853 /* tag 10489608748473423768(
2854 2442302356(
2855 21590(
2856 240(
2857 []))))
2858 */
2859 uError = QCBORDecode_GetNext(&DCtx, &Item);
2860 if(uError != QCBOR_SUCCESS ||
2861 Item.uDataType != QCBOR_TYPE_ARRAY ||
2862 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
2863 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
2864 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
2865 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002866 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002867 }
2868
2869 /* tag 21590(
2870 10489608748473423768(
2871 2442302357(
2872 21591(
2873 []))))
2874 */
2875 uError = QCBORDecode_GetNext(&DCtx, &Item);
2876 if(uError != QCBOR_SUCCESS ||
2877 Item.uDataType != QCBOR_TYPE_ARRAY ||
2878 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
2879 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
2880 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
2881 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
2882 return -8;
2883 }
2884
2885 /* Make sure to blow past the limit of tags that must be mapped.
2886 works in conjuntion with entries above.
2887 269488144(269488145(269488146(269488147([]))))
2888 */
2889 uError = QCBORDecode_GetNext(&DCtx, &Item);
2890 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
2891 return -9;
2892 }
2893
2894 uError = QCBORDecode_GetNext(&DCtx, &Item);
2895 if(uError == QCBOR_SUCCESS) {
2896 return -10;
2897 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002898
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002899 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002900 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07002901 // tage and then matches it. Caller-config lists are no longer
2902 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002903 QCBORDecode_Init(&DCtx,
2904 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2905 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002906 const uint64_t puList[] = {0x9192939495969798, 257};
2907 const QCBORTagListIn TL = {2, puList};
2908 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002909
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002910 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2911 return -8;
2912 }
2913 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2914 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2915 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2916 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2917 Item.val.uCount != 0) {
2918 return -9;
2919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002920
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002921 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002922 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07002923 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002924 const uint64_t puLongList[17] = {1,2,1};
2925 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002926 QCBORDecode_Init(&DCtx,
2927 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2928 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002929 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2930 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2931 return -11;
2932 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002933
Laurence Lundblade9b334962020-08-27 10:55:53 -07002934 uint64_t puTags[16];
2935 QCBORTagListOut Out = {0, 4, puTags};
2936
2937
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002938 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002939 QCBORDecode_Init(&DCtx,
2940 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2941 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002942 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2943 return -12;
2944 }
2945 if(puTags[0] != 0x9192939495969798 ||
2946 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002947 puTags[2] != 0x06 ||
2948 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002949 return -13;
2950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002951
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002952 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002953 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002954 QCBORDecode_Init(&DCtx,
2955 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2956 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002957 QCBORTagListOut OutSmall = {0, 3, puTags};
2958 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2959 return -14;
2960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002961
Laurence Lundblade9b334962020-08-27 10:55:53 -07002962
2963
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002964 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07002965 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
2966 // It is a bit of a messy test and maybe could be improved, but
2967 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002968 QCBORDecode_Init(&DCtx,
2969 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2970 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002971 int n = CheckCSRMaps(&DCtx);
2972 if(n) {
2973 return n-2000;
2974 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002975
Laurence Lundblade59289e52019-12-30 13:44:37 -08002976 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002977 QCBORDecode_Init(&DCtx,
2978 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2979 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002980
Laurence Lundblade9b334962020-08-27 10:55:53 -07002981 /* With the spiffy decode revision, this tag list is not used.
2982 It doesn't matter if a tag is in this list or not so some
2983 tests that couldn't process a tag because it isn't in this list
2984 now can process these unlisted tags. The tests have been
2985 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002986 const uint64_t puTagList[] = {773, 1, 90599561};
2987 const QCBORTagListIn TagList = {3, puTagList};
2988 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002989
2990
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002991 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2992 return -100;
2993 }
2994 if(Item.uDataType != QCBOR_TYPE_MAP ||
2995 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2996 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2997 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2998 Item.val.uCount != 2 ||
2999 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3000 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3001 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3002 Out.uNumUsed != 3) {
3003 return -101;
3004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003005
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003006 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3007 return -102;
3008 }
3009 if(Item.uDataType != QCBOR_TYPE_MAP ||
3010 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3011 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003012 !QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003013 Item.val.uCount != 2 ||
3014 puTags[0] != 5859837686836516696 ||
3015 puTags[1] != 7 ||
3016 Out.uNumUsed != 2) {
3017 return -103;
3018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003019
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003020 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3021 return -104;
3022 }
3023 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003024 //Item.uTagBits ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003025 Item.val.uCount != 5 ||
3026 puTags[0] != 0x0b ||
3027 Out.uNumUsed != 1) {
3028 return -105;
3029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003030
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003031 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3032 return -106;
3033 }
3034 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3035 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3036 Item.val.string.len != 12 ||
3037 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3038 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3039 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3040 Out.uNumUsed != 3) {
3041 return -105;
3042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003043
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003044 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3045 return -107;
3046 }
3047 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3048 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3049 Item.val.string.len != 3 ||
3050 puTags[0] != 773 ||
3051 Out.uNumUsed != 1) {
3052 return -108;
3053 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003054
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003055 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3056 return -109;
3057 }
3058 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003059 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003060 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003061 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003062 puTags[3] != 7 ||
3063 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003064 return -110;
3065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003066
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003067 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3068 return -111;
3069 }
3070 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3071 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3072 Item.val.string.len != 9 ||
3073 puTags[0] != 17 ||
3074 Out.uNumUsed != 1) {
3075 return -112;
3076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003077
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003078 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3079 return -111;
3080 }
3081 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3082 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3083 Item.val.string.len != 2 ||
3084 puTags[0] != 17 ||
3085 Out.uNumUsed != 1) {
3086 return -112;
3087 }
3088
3089 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3090 return -113;
3091 }
3092 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003093 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003094 Item.val.uCount != 2 ||
3095 puTags[0] != 19 ||
3096 Out.uNumUsed != 1) {
3097 return -114;
3098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003099
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003100 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3101 return -115;
3102 }
3103 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003104 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003105 Item.val.uCount != 1 ||
3106 puTags[0] != 9 ||
3107 Out.uNumUsed != 1) {
3108 return -116;
3109 }
3110
3111 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3112 return -116;
3113 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003114 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003115 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003116 Out.uNumUsed != 0) {
3117 return -117;
3118 }
3119
3120 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3121 return -118;
3122 }
3123 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3124 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003125 puTags[0] != 12 ||
3126 Out.uNumUsed != 1) {
3127 return -119;
3128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003129
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003130 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3131 return -120;
3132 }
3133 if(Item.uDataType != QCBOR_TYPE_MAP ||
3134 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3135 Item.val.uCount != 1 ||
3136 puTags[0] != 0x17 ||
3137 Out.uNumUsed != 1) {
3138 return -121;
3139 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003140
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003141 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3142 return -122;
3143 }
3144 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003145 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003146 Item.val.int64 != -3 ||
3147 puTags[0] != 8 ||
3148 Out.uNumUsed != 1) {
3149 return -123;
3150 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003151
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003152 if(QCBORDecode_Finish(&DCtx)) {
3153 return -124;
3154 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003155
3156 UsefulBufC DateString;
3157 QCBORDecode_Init(&DCtx,
3158 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3159 QCBOR_DECODE_MODE_NORMAL);
3160
3161 QCBORDecode_EnterArray(&DCtx);
3162 // tagged date string
3163 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3164 // untagged date string
3165 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3166 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3167 return 100;
3168 }
3169 // untagged byte string
3170 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3171 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3172 return 101;
3173 }
3174 // tagged regex
3175 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3176 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3177 return 102;
3178 }
3179 // tagged date string with a byte string
3180 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3181 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3182 return 103;
3183 }
3184 QCBORDecode_ExitArray(&DCtx);
3185 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3186 return 104;
3187 }
3188
3189
3190 QCBORDecode_Init(&DCtx,
3191 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3192 QCBOR_DECODE_MODE_NORMAL);
3193
3194 QCBORDecode_EnterArray(&DCtx);
3195 // tagged date string
3196 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3197 // untagged date string
3198 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3199 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3200 return 200;
3201 }
3202 // untagged byte string
3203 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3204 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3205 return 201;
3206 }
3207 // tagged regex
3208 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3209 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3210 return 202;
3211 }
3212 // tagged date string with a byte string
3213 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3214 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3215 return 203;
3216 }
3217 QCBORDecode_ExitArray(&DCtx);
3218 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3219 return 204;
3220 }
3221
3222 QCBORDecode_Init(&DCtx,
3223 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3224 QCBOR_DECODE_MODE_NORMAL);
3225
3226 QCBORDecode_EnterArray(&DCtx);
3227 // tagged date string
3228 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3229 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3230 return 300;
3231 }
3232 // untagged date string
3233 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3234 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3235 return 301;
3236 }
3237 // untagged byte string
3238 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3239 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3240 return 302;
3241 }
3242 // tagged regex
3243 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3244 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3245 return 303;
3246 }
3247 // tagged date string with a byte string
3248 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3249 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3250 return 304;
3251 }
3252 QCBORDecode_ExitArray(&DCtx);
3253 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3254 return 305;
3255 }
3256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003257 return 0;
3258}
3259
3260
3261
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003262
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003263static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003264 0x83,
3265 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3266 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3267 0xA4,
3268 0x63, 0x42, 0x4E, 0x2B,
3269 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3270 0x18, 0x40,
3271 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3272 0x63, 0x42, 0x4E, 0x2D,
3273 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3274 0x38, 0x3F,
3275 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3276
3277
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003278static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003279
3280
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003281int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003282{
3283 QCBORDecodeContext DCtx;
3284 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003285 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003286
Laurence Lundbladeee851742020-01-08 08:37:05 -08003287 QCBORDecode_Init(&DCtx,
3288 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3289 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003290
3291
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003292 //
3293 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3294 return -1;
3295 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003296 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003298
3299 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003300 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003301 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003302 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003303 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003304 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003305 }
3306
3307 //
3308 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003309 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003310 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003311 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003312 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003315 //
3316 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003317 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003318 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003319 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003322 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003323 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003324 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3325 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003326 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003327 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003328 }
3329
3330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003331 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003332 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3333 Item.uLabelType != QCBOR_TYPE_INT64 ||
3334 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003335 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003336 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003338
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003339 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003340 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003341 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3342 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003343 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003344 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003348 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003349 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3350 Item.uLabelType != QCBOR_TYPE_INT64 ||
3351 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003352 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003353 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003356 return 0;
3357}
3358
3359
3360
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003361static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003362 uint8_t uDataType,
3363 uint8_t uNestingLevel,
3364 uint8_t uNextNest,
3365 int64_t nLabel,
3366 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003367{
3368 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003369 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003370
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003371 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3372 if(Item.uDataType != uDataType) return -1;
3373 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003374 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3375 Item.uLabelType != QCBOR_TYPE_UINT64) {
3376 return -1;
3377 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003378 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3379 if(Item.label.int64 != nLabel) return -1;
3380 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003381 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003382 }
3383 }
3384 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303385 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003387 if(pItem) {
3388 *pItem = Item;
3389 }
3390 return 0;
3391}
3392
3393
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003394// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003395static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003396{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303397 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003398
Laurence Lundblade9b334962020-08-27 10:55:53 -07003399 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003400
Laurence Lundblade9b334962020-08-27 10:55:53 -07003401 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003402
Laurence Lundblade9b334962020-08-27 10:55:53 -07003403 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3404 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3405 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3406 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3407 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003408
Laurence Lundblade9b334962020-08-27 10:55:53 -07003409 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3410 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003411
Laurence Lundblade9b334962020-08-27 10:55:53 -07003412 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3413 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003414
Laurence Lundblade9b334962020-08-27 10:55:53 -07003415 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3416 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003417
Laurence Lundblade9b334962020-08-27 10:55:53 -07003418 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003419
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003420 return 0;
3421}
3422
3423
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003424/*
3425// cbor.me decoded output
3426{
3427 -23: {
3428 -20: {
3429 -18: "Organization",
3430 -17: "SSG",
3431 -15: "Confusion",
3432 -16: "San Diego",
3433 -14: "US"
3434 },
3435 -19: {
3436 -11: {
3437 -9: -7
3438 },
3439 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3440 }
3441 },
3442 -22: {
3443 -5: -3
3444 }
3445}
3446 */
3447
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003448
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003449static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003450 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3451 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3452 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3453 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3454 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3455 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3456 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3457 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3458 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3459
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003460int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003461{
3462 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003463
Laurence Lundbladeee851742020-01-08 08:37:05 -08003464 QCBORDecode_Init(&DCtx,
3465 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3466 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003467
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003468 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003469}
3470
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003471
3472
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003473int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003474{
3475 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003476
Laurence Lundbladeee851742020-01-08 08:37:05 -08003477 QCBORDecode_Init(&DCtx,
3478 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3479 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003480
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003481 QCBORItem Item;
3482 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003483
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003484 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3485 return -1;
3486 }
3487 if(Item.uDataType != QCBOR_TYPE_MAP) {
3488 return -2;
3489 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003490
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003491 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3492 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3493 return -3;
3494 }
3495
3496 return 0;
3497}
3498
3499
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003500// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003501static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003502 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3503 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3504 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3505 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3506 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3507 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003508 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3509 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3510 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3511 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003512
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003513int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003514{
3515 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003516
Laurence Lundbladeee851742020-01-08 08:37:05 -08003517 QCBORDecode_Init(&DCtx,
3518 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3519 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003520
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003521 return CheckCSRMaps(&DCtx);
3522}
3523
3524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003525
Laurence Lundblade17ede402018-10-13 11:43:07 +08003526static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3527{
3528 UsefulOutBuf UOB;
3529 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003530
Laurence Lundblade17ede402018-10-13 11:43:07 +08003531 int i;
3532 for(i = 0; i < n; i++) {
3533 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3534 }
3535
3536 for(i = 0; i < n; i++) {
3537 UsefulOutBuf_AppendByte(&UOB, 0xff);
3538 }
3539 return UsefulOutBuf_OutUBuf(&UOB);
3540}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003541
3542
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003543static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003544{
3545 QCBORDecodeContext DC;
3546 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003547
Laurence Lundblade17ede402018-10-13 11:43:07 +08003548 int j;
3549 for(j = 0; j < nNestLevel; j++) {
3550 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003551 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003552 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3553 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003554 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003555 return -4;
3556 } else {
3557 return 0; // Decoding doesn't recover after an error
3558 }
3559 } else {
3560 // Should be no error
3561 if(nReturn) {
3562 return -9; // Should not have got an error
3563 }
3564 }
3565 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3566 return -7;
3567 }
3568 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003569 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003570 if(nReturn) {
3571 return -3;
3572 }
3573 return 0;
3574}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003575
3576
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003577int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003578{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303579 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003580 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003581 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003582 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003583 int nReturn = parse_indeflen_nested(Nested, i);
3584 if(nReturn) {
3585 return nReturn;
3586 }
3587 }
3588 return 0;
3589}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003590
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003591
Laurence Lundbladeee851742020-01-08 08:37:05 -08003592// [1, [2, 3]]
3593static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3594// No closing break
3595static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3596// Not enough closing breaks
3597static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3598// Too many closing breaks
3599static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3600// Unclosed indeflen inside def len
3601static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3602// confused tag
3603static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003604
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003605int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003606{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003607 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003608 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003609 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003610
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003611 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303612 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003613 QCBORDecodeContext DC;
3614 QCBORItem Item;
3615 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003616
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003617 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003618
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003619 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303620
3621 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3622 Item.uNestingLevel != 0 ||
3623 Item.uNextNestLevel != 1) {
3624 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003625 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003626
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003627 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303628 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3629 Item.uNestingLevel != 1 ||
3630 Item.uNextNestLevel != 1) {
3631 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003632 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003633
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003634 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303635 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3636 Item.uNestingLevel != 1 ||
3637 Item.uNextNestLevel != 2) {
3638 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003639 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003640
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003641 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003642 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303643 Item.uNestingLevel != 2 ||
3644 Item.uNextNestLevel != 2) {
3645 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003646 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003647
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003648 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003649 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303650 Item.uNestingLevel != 2 ||
3651 Item.uNextNestLevel != 0) {
3652 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003654
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003655 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303656 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003657 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003658
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003659 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003660 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003661
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003662 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003663
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003664 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003665
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003666 nResult = QCBORDecode_GetNext(&DC, &Item);
3667 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303668 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003670
Laurence Lundblade570fab52018-10-13 18:28:27 +08003671 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003672 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303673 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003674 }
3675
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003676
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003677 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003678 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003679
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003680 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003681
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003682 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003683
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003684 nResult = QCBORDecode_GetNext(&DC, &Item);
3685 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303686 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003687 }
3688
3689 nResult = QCBORDecode_GetNext(&DC, &Item);
3690 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303691 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003692 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003693
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003694 nResult = QCBORDecode_GetNext(&DC, &Item);
3695 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303696 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003698
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003699 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003700 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303701 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003702 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003703
3704
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003705 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003706 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003707
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003708 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003709
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003710 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003711
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003712 nResult = QCBORDecode_GetNext(&DC, &Item);
3713 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303714 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003715 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003716
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003717 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003718 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303719 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003720 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303721
Laurence Lundblade642282a2020-06-23 12:00:33 -07003722 nResult = QCBORDecode_GetNext(&DC, &Item);
3723 if(nResult != QCBOR_ERR_BAD_BREAK) {
3724 return -140;
3725 }
3726
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003727
Laurence Lundblade570fab52018-10-13 18:28:27 +08003728 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003729 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003730
Laurence Lundblade570fab52018-10-13 18:28:27 +08003731 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003732
Laurence Lundblade570fab52018-10-13 18:28:27 +08003733 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003734
Laurence Lundblade570fab52018-10-13 18:28:27 +08003735 nResult = QCBORDecode_GetNext(&DC, &Item);
3736 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303737 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003738 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003739
Laurence Lundblade570fab52018-10-13 18:28:27 +08003740 nResult = QCBORDecode_GetNext(&DC, &Item);
3741 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303742 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003743 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003744
Laurence Lundblade570fab52018-10-13 18:28:27 +08003745 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003746 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303747 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003748 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003749
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303750 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003751 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003752
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303753 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003754
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303755 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003756
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303757 nResult = QCBORDecode_GetNext(&DC, &Item);
3758 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303759 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303760 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003761
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303762 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303763 if(nResult != QCBOR_ERR_BAD_BREAK) {
3764 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303765 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003766
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003767 return 0;
3768}
3769
Laurence Lundblade17ede402018-10-13 11:43:07 +08003770
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003771static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003772 0x81, // Array of length one
3773 0x7f, // text string marked with indefinite length
3774 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3775 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3776 0xff // ending break
3777};
3778
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003779static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303780 0x81, // Array of length one
3781 0x7f, // text string marked with indefinite length
3782 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3783 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3784 0xff // ending break
3785};
3786
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003787static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303788 0x81, // Array of length one
3789 0x7f, // text string marked with indefinite length
3790 0x01, 0x02, // Not a string
3791 0xff // ending break
3792};
3793
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003794static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303795 0x81, // Array of length one
3796 0x7f, // text string marked with indefinite length
3797 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3798 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3799 // missing end of string
3800};
3801
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003802static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303803 0xa1, // Array of length one
3804 0x7f, // text string marked with indefinite length
3805 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3806 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3807 0xff, // ending break
3808 0x01 // integer being labeled.
3809};
3810
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003811/**
3812 Make an indefinite length string
3813
3814 @param Storage Storage for string, must be 144 bytes in size
3815 @return The indefinite length string
3816
3817 This makes an array with one indefinite length string that has 7 chunks
3818 from size of 1 byte up to 64 bytes.
3819 */
3820static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303821{
3822 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003823
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303824 UsefulOutBuf_Init(&UOB, Storage);
3825 UsefulOutBuf_AppendByte(&UOB, 0x81);
3826 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003827
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003828 uint8_t uStringByte = 0;
3829 // Use of type int is intentional
3830 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3831 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303832 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003833 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3834 for(int j = 0; j < uChunkSize; j++) {
3835 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3836 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303837 }
3838 }
3839 UsefulOutBuf_AppendByte(&UOB, 0xff);
3840
3841 return UsefulOutBuf_OutUBuf(&UOB);
3842}
3843
3844static int CheckBigString(UsefulBufC BigString)
3845{
3846 if(BigString.len != 255) {
3847 return 1;
3848 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003849
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303850 for(uint8_t i = 0; i < 255; i++){
3851 if(((const uint8_t *)BigString.ptr)[i] != i) {
3852 return 1;
3853 }
3854 }
3855 return 0;
3856}
3857
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303858
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003859int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303860{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303861 QCBORDecodeContext DC;
3862 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303863 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003864 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003865
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303866 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003867 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303868 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003869
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303870 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303871 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303872 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003873
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303874 if(QCBORDecode_GetNext(&DC, &Item)) {
3875 return -2;
3876 }
3877 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3878 return -3;
3879 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003880
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303881 if(QCBORDecode_GetNext(&DC, &Item)) {
3882 return -4;
3883 }
3884 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3885 return -5;
3886 }
3887 if(QCBORDecode_Finish(&DC)) {
3888 return -6;
3889 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303890
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303891 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003892 QCBORDecode_Init(&DC,
3893 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3894 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003895
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303896 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3897 return -7;
3898 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003899
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303900 if(QCBORDecode_GetNext(&DC, &Item)) {
3901 return -8;
3902 }
3903 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3904 return -9;
3905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003906
Laurence Lundblade30816f22018-11-10 13:40:22 +07003907 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303908 return -10;
3909 }
3910
3911 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003912 QCBORDecode_Init(&DC,
3913 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3914 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003915
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303916 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3917 return -11;
3918 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003919
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303920 if(QCBORDecode_GetNext(&DC, &Item)) {
3921 return -12;
3922 }
3923 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3924 return -13;
3925 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003926
Laurence Lundblade30816f22018-11-10 13:40:22 +07003927 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303928 return -14;
3929 }
3930
3931 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003932 QCBORDecode_Init(&DC,
3933 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3934 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003935
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303936 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3937 return -15;
3938 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003939
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303940 if(QCBORDecode_GetNext(&DC, &Item)) {
3941 return -16;
3942 }
3943 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3944 return -17;
3945 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003946
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303947 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3948 return -18;
3949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003950
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303951 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303952 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003953
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303954 QCBORDecode_GetNext(&DC, &Item);
3955 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303956 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303957 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303959 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303960 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303961 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003962
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303963 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003964 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303965
3966 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3967 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303968 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003970
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303971 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303972 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003973 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003974
Laurence Lundbladeee851742020-01-08 08:37:05 -08003975 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3976 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003977
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303978 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303979 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303980 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003982
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303983 QCBORDecode_GetNext(&DC, &Item);
3984 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303985 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303986 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003987 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303988 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303989 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303991 // ---- big bstr -----
3992 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003993
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303994 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3995 return -25;
3996 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003997
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303998 if(QCBORDecode_GetNext(&DC, &Item)) {
3999 return -26;
4000 }
4001 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304002 return -26;
4003 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004004
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304005 if(QCBORDecode_GetNext(&DC, &Item)) {
4006 return -27;
4007 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304008 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304009 return -28;
4010 }
4011 if(CheckBigString(Item.val.string)) {
4012 return -3;
4013 }
4014 if(QCBORDecode_Finish(&DC)) {
4015 return -29;
4016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004017
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304018 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004019 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004020
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304021 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4022 return -30;
4023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004024
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304025 QCBORDecode_GetNext(&DC, &Item);
4026 if(Item.uDataType != QCBOR_TYPE_MAP) {
4027 return -31;
4028 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004029
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304030 if(QCBORDecode_GetNext(&DC, &Item)){
4031 return -32;
4032 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004033 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4034 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304035 Item.uDataAlloc || !Item.uLabelAlloc ||
4036 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4037 return -33;
4038 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004039
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304040 if(QCBORDecode_Finish(&DC)) {
4041 return -34;
4042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004043
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004044 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004045}
4046
4047
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004048int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304049{
4050 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004051 QCBORError nCBORError;
4052
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004053
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304054 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004055 QCBORDecode_Init(&DC,
4056 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4057 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004058
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004059 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004060
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004061 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4062 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304063 return -1;
4064 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004065
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004066 if(CheckCSRMaps(&DC)) {
4067 return -2;
4068 }
4069
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304070 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004071 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004072 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004073
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304074 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004075 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304076 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004077
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304078 QCBORItem Item1, Item2, Item3, Item4;
4079 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004080 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304081 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4082 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004083 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304084 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004085 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304086 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004087 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304088 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004089 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304090 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004091 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004092
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304093 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004094
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304095 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304096 Item1.uDataType != QCBOR_TYPE_INT64 ||
4097 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004098 Item1.uDataAlloc != 0 ||
4099 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004100 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004101 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004103
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304104
4105 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004106 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304107 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004108 Item2.uDataAlloc != 0 ||
4109 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304110 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004111 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004112
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304113 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004114 Item3.uDataAlloc == 0 ||
4115 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004116 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004117 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004119
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304120 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004121 Item4.uDataAlloc == 0 ||
4122 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004123 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004124 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004126
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304127 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004128 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004129 QCBORDecode_Init(&DC,
4130 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4131 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304132 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4133 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004134 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304135 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004136 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004137 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004138 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304139 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4140 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4141 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4142 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4143 }
4144 }
4145 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004146 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004147 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304148 }
4149
4150 return 0;
4151}
4152
Laurence Lundbladef6531662018-12-04 10:42:22 +09004153
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304154
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004155int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004156{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004157 // Set up the decoder with a tiny bit of CBOR to parse because
4158 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004159 QCBORDecodeContext DC;
4160 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4161 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004162
Laurence Lundbladef6531662018-12-04 10:42:22 +09004163 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004164 // Then fish into the internals of the decode context
4165 // to get the allocator function so it can be called directly.
4166 // Also figure out how much pool is available for use
4167 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004168 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004169 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4170 if(nError) {
4171 return -9;
4172 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004173 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4174 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4175 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004176
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004177 // First test -- ask for one more byte than available and see failure
4178 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004179 if(!UsefulBuf_IsNULL(Allocated)) {
4180 return -1;
4181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004182
Laurence Lundbladef6531662018-12-04 10:42:22 +09004183 // Re do the set up for the next test that will do a successful alloc,
4184 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004185 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004186 pAlloc = DC.StringAllocator.pfAllocator;
4187 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4188 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004189
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004190 // Allocate one byte less than available and see success
4191 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004192 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4193 return -2;
4194 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004195 // Ask for some more and see failure
4196 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004197 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4198 return -3;
4199 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004200 // Free the first allocate, retry the second and see success
4201 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4202 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004203 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4204 return -4;
4205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004206
Laurence Lundbladef6531662018-12-04 10:42:22 +09004207 // Re do set up for next test that involves a successful alloc,
4208 // and a successful realloc and a failed realloc
4209 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004210 pAlloc = DC.StringAllocator.pfAllocator;
4211 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004212
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004213 // Allocate half the pool and see success
4214 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004215 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4216 return -5;
4217 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004218 // Reallocate to take up the whole pool and see success
4219 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004220 if(UsefulBuf_IsNULL(Allocated2)) {
4221 return -6;
4222 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004223 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004224 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4225 return -7;
4226 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004227 // Try to allocate more to be sure there is failure after a realloc
4228 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4229 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004230 return -8;
4231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004232
Laurence Lundbladef6531662018-12-04 10:42:22 +09004233 return 0;
4234}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004235
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004236
4237/* Just enough of an allocator to test configuration of one */
4238static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4239{
4240 (void)pOldMem; // unused variable
4241
4242 if(uNewSize) {
4243 // Assumes the context pointer is the buffer and
4244 // nothing too big will ever be asked for.
4245 // This is only good for this basic test!
4246 return (UsefulBuf) {pCtx, uNewSize};
4247 } else {
4248 return NULLUsefulBuf;
4249 }
4250}
4251
4252
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004253int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004254{
4255 // Set up the decoder with a tiny bit of CBOR to parse because
4256 // nothing can be done with it unless that is set up.
4257 QCBORDecodeContext DC;
4258 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4259 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4260
4261 uint8_t pAllocatorBuffer[50];
4262
4263 // This is really just to test that this call works.
4264 // The full functionality of string allocators is tested
4265 // elsewhere with the MemPool internal allocator.
4266 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4267
4268 QCBORItem Item;
4269 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4270 return -1;
4271 }
4272
4273 if(Item.uDataAlloc == 0 ||
4274 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4275 Item.val.string.ptr != pAllocatorBuffer) {
4276 return -2;
4277 }
4278
4279 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4280 return -3;
4281 }
4282
4283 return 0;
4284}
4285
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07004286#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004287
Laurence Lundbladea826c502020-05-10 21:07:00 -07004288/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004289 [
4290 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004291 4([-20, 4759477275222530853136]),
4292 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004293 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004294 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004295 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004296 5([ 9223372036854775806, -4759477275222530853137])
4297 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004298 ]
4299 */
4300
4301static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004302 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004303 0xC4, 0x82, 0x20,
4304 0x03,
4305 0xC4, 0x82, 0x33,
4306 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4307 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4308 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4309 0xC5, 0x82, 0x19, 0x01, 0x2C,
4310 0x18, 0x64,
4311 0xC5, 0x82, 0x33,
4312 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4313 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4314 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004315 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4316 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004317 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4318 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4319};
4320
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004321
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004322int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004323{
4324 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004325 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004326 QCBORItem item;
4327
Laurence Lundblade17af4902020-01-07 19:11:55 -08004328 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4329 0x06, 0x07, 0x08, 0x09, 0x010};
4330 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004331
4332
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004333 QCBORDecode_Init(&DC,
4334 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4335 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004336
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004337 uErr = QCBORDecode_GetNext(&DC, &item);
4338 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004339 return 1;
4340 }
4341
4342 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4343 return 2;
4344 }
4345
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004346 uErr = QCBORDecode_GetNext(&DC, &item);
4347 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004348 return 3;
4349 }
4350
4351 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4352 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4353 item.val.expAndMantissa.nExponent != -1) {
4354 return 4;
4355 }
4356
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004357 uErr = QCBORDecode_GetNext(&DC, &item);
4358 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004359 return 5;
4360 }
4361
4362 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4363 item.val.expAndMantissa.nExponent != -20 ||
4364 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4365 return 6;
4366 }
4367
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004368 uErr = QCBORDecode_GetNext(&DC, &item);
4369 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004370 return 7;
4371 }
4372
4373 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4374 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4375 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4376 return 8;
4377 }
4378
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004379 uErr = QCBORDecode_GetNext(&DC, &item);
4380 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004381 return 9;
4382 }
4383
4384 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4385 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4386 item.val.expAndMantissa.nExponent != 300) {
4387 return 10;
4388 }
4389
Laurence Lundbladea826c502020-05-10 21:07:00 -07004390 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004391 uErr = QCBORDecode_GetNext(&DC, &item);
4392 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004393 return 11;
4394 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004395 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4396 item.val.expAndMantissa.nExponent != -20 ||
4397 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4398 return 12;
4399 }
4400
Laurence Lundbladea826c502020-05-10 21:07:00 -07004401 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004402 uErr = QCBORDecode_GetNext(&DC, &item);
4403 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004404 return 13;
4405 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004406 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4407 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4408 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4409 return 14;
4410 }
4411
Laurence Lundbladea826c502020-05-10 21:07:00 -07004412 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004413 uErr = QCBORDecode_GetNext(&DC, &item);
4414 if(uErr != QCBOR_SUCCESS) {
4415 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004416 }
4417 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4418 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4419 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004420 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004421 }
4422
Laurence Lundbladea826c502020-05-10 21:07:00 -07004423 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004424 uErr = QCBORDecode_GetNext(&DC, &item);
4425 if(uErr != QCBOR_SUCCESS) {
4426 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004427 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004428 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4429 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4430 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004431 return 18;
4432 }
4433
4434 uErr = QCBORDecode_Finish(&DC);
4435 if(uErr != QCBOR_SUCCESS) {
4436 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004437 }
4438
4439 /* Now encode some stuff and then decode it */
4440 uint8_t pBuf[40];
4441 QCBOREncodeContext EC;
4442 UsefulBufC Encoded;
4443
4444 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4445 QCBOREncode_OpenArray(&EC);
4446 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4447 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4448 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4449 QCBOREncode_CloseArray(&EC);
4450 QCBOREncode_Finish(&EC, &Encoded);
4451
4452
4453 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004454 uErr = QCBORDecode_GetNext(&DC, &item);
4455 if(uErr != QCBOR_SUCCESS) {
4456 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004457 }
4458
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004459 uErr = QCBORDecode_GetNext(&DC, &item);
4460 if(uErr != QCBOR_SUCCESS) {
4461 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004462 }
4463
4464 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4465 item.val.expAndMantissa.nExponent != 1000 ||
4466 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004467 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004468 }
4469
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004470 uErr = QCBORDecode_GetNext(&DC, &item);
4471 if(uErr != QCBOR_SUCCESS) {
4472 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004473 }
4474
4475 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4476 item.val.expAndMantissa.nExponent != INT32_MIN ||
4477 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004478 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004479 }
4480
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004481 uErr = QCBORDecode_GetNext(&DC, &item);
4482 if(uErr != QCBOR_SUCCESS) {
4483 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004484 }
4485
4486 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4487 item.val.expAndMantissa.nExponent != INT32_MAX ||
4488 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004489 return 106;
4490 }
4491
4492
4493 int64_t nExp, nMant;
4494 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4495 UsefulBufC Mant;
4496 bool bIsNeg;
4497
4498 QCBORDecode_Init(&DC,
4499 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4500 QCBOR_DECODE_MODE_NORMAL);
4501 QCBORDecode_EnterArray(&DC);
4502
4503 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004504 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004505
4506 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004507 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4508 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004509
4510 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004511 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4512 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004513
4514 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004515 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004516
4517 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004518 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4519 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004520
4521 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004522 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4523 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004524
4525 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004526 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4527 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004528
4529 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004530 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4531 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004532
4533 QCBORDecode_ExitArray(&DC);
4534
4535 uErr = QCBORDecode_Finish(&DC);
4536 if(uErr != QCBOR_SUCCESS) {
4537 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004538 }
4539
4540 return 0;
4541}
4542
4543
4544static struct FailInput ExponentAndMantissaFailures[] = {
4545 // Exponent > INT64_MAX
4546 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4547 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4548 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4549 // Mantissa > INT64_MAX
4550 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4551 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4552 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4553 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004554 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004555 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004556 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004557 // bad content for big num
4558 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4559 // bad content for big num
4560 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4561 // Bad integer for exponent
4562 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4563 // Bad integer for mantissa
4564 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4565 // 3 items in array
4566 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4567 // unterminated indefinite length array
4568 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4569 // Empty array
4570 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4571 // Second is not an integer
4572 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4573 // First is not an integer
4574 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4575 // Not an array
4576 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4577};
4578
4579
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004580int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004581{
4582 return ProcessFailures(ExponentAndMantissaFailures,
4583 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
4584}
4585
4586#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004587
4588
4589
4590/*
4591 Some basic CBOR with map and array used in a lot of tests.
4592 The map labels are all strings
4593
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004594 {
4595 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004596 "an array of two strings": [
4597 "string1", "string2"
4598 ],
4599 "map in a map": {
4600 "bytes 1": h'78787878',
4601 "bytes 2": h'79797979',
4602 "another int": 98,
4603 "text 2": "lies, damn lies and statistics"
4604 }
4605 }
4606 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004607
Laurence Lundblade1341c592020-04-11 14:19:05 -07004608#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004609
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004610static char strbuf[10];
4611const char *PrintType(uint8_t type) {
4612 switch(type) {
4613 case QCBOR_TYPE_INT64: return "INT64";
4614 case QCBOR_TYPE_UINT64: return "UINT64";
4615 case QCBOR_TYPE_ARRAY: return "ARRAY";
4616 case QCBOR_TYPE_MAP: return "MAP";
4617 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
4618 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
4619 default:
4620 sprintf(strbuf, "%d", type);
4621 return strbuf;
4622 }
4623}
4624
4625
4626void PrintItem(QCBORItem Item)
4627{
Laurence Lundblade9b334962020-08-27 10:55:53 -07004628 printf("\nData: %s nest: %d,%d %s\n",
4629 PrintType(Item.uDataType),
4630 Item.uNestingLevel,
4631 Item.uNextNestLevel,
4632 Item.uDataAlloc ? "Allocated":"");
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004633 if(Item.uLabelType) {
4634 printf("Label: %s ", PrintType(Item.uLabelType));
4635 if(Item.uLabelType == QCBOR_TYPE_INT64) {
4636 printf("%lld\n", Item.label.int64);
4637 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade40a04322020-06-27 22:52:52 -07004638 // TODO: proper conversion to null-terminated string
4639 printf("\"%4.4s\"\n", (const char *)Item.label.string.ptr);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004640 }
4641 }
4642}
4643
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004644
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004645int32_t EMap(UsefulBufC input)
4646{
4647 QCBORItem Item1, Item2, Item3;
4648 int64_t nDecodedInt1, nDecodedInt2;
4649 UsefulBufC B1, B2, S1, S2, S3;
4650
4651 QCBORDecodeContext DCtx;
4652 QCBORError nCBORError;
4653
4654 QCBORDecode_Init(&DCtx, input, 0);
4655
4656 QCBORDecode_EnterMap(&DCtx);
4657
4658 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4659
4660 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4661 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004662 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4663 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4664 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004665 QCBORDecode_ExitMap(&DCtx);
4666
4667 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4668 QCBORDecode_GetNext(&DCtx, &Item1);
4669 QCBORDecode_GetNext(&DCtx, &Item2);
4670 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4671 return -400;
4672 }
4673 QCBORDecode_ExitArray(&DCtx);
4674
4675 // Parse the same array again using GetText() instead of GetItem()
4676 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004677 QCBORDecode_GetTextString(&DCtx, &S2);
4678 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004679 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4680 return 5000;
4681 }
4682 /* QCBORDecode_GetText(&DCtx, &S3);
4683 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4684 return 5001;
4685 } */
4686
4687 QCBORDecode_ExitArray(&DCtx);
4688
4689 QCBORDecode_ExitMap(&DCtx);
4690
4691 nCBORError = QCBORDecode_Finish(&DCtx);
4692
4693 if(nCBORError) {
4694 return (int32_t)nCBORError;
4695 }
4696
4697 if(nDecodedInt1 != 42) {
4698 return 1001;
4699 }
4700
4701 if(nDecodedInt2 != 98) {
4702 return 1002;
4703 }
4704
4705 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004706 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004707 return 1003;
4708 }
4709
4710 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004711 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004712 return 1004;
4713 }
4714
Laurence Lundblade9b334962020-08-27 10:55:53 -07004715 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004716 return 1005;
4717 }
4718
4719 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4720 return 1006;
4721 }
4722
4723 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4724 return 1007;
4725 }
4726
4727 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4728 return 1008;
4729 }
4730
4731 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4732 return 1009;
4733 }
4734
4735 return 0;
4736}
4737
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004738
4739/*
4740 [23,
4741 6000,
4742 h'67616C6163746963',
4743 h'686176656E20746F6B656E'
4744 ]
4745 */
4746static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004747 0x84,
4748 0x17,
4749 0x19, 0x17, 0x70,
4750 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
4751 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004752
4753
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004754static const uint8_t spEmptyMap[] = {0xa0};
4755
4756static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004757
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07004758static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
4759
Laurence Lundbladef0499502020-08-01 11:55:57 -07004760/*
4761 {
4762 0: [],
4763 9: [
4764 [],
4765 []
4766 ],
4767 8: {
4768 1: [],
4769 2: {},
4770 3: []
4771 },
4772 4: {},
4773 5: [],
4774 6: [
4775 [],
4776 []
4777 ]
Laurence Lundblade44080632020-08-06 21:43:50 -07004778 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07004779 */
Laurence Lundblade44080632020-08-06 21:43:50 -07004780
Laurence Lundbladef0499502020-08-01 11:55:57 -07004781static const uint8_t spMapOfEmpty[] = {
4782 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08, 0xa3, 0x01,
4783 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04, 0xa0, 0x05, 0x9f, 0xff,
4784 0x06, 0x9f, 0x80, 0x9f, 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004785
Laurence Lundblade93d3f532020-09-28 21:09:12 -07004786/*
4787 Too many tags
4788 Invalid tag content
4789 Duplicate label
4790 Integer overflow
4791 Date overflow
4792
4793 {1: 224(225(226(227(4(0))))),
4794 2: 1(h''),
4795 3: -18446744073709551616,
4796 4: 1(1.0e+300),
4797 5: 0, 8: 8}
4798 */
4799static const uint8_t spRecoverableMapErrors[] = {
4800 0xbf,
4801 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
4802 0x02, 0xc1, 0x40,
4803 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4804 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
4805 0x05, 0x00,
4806 0x05, 0x00,
4807 0x08, 0x08,
4808 0xff
4809};
4810
4811// Bad break
4812static const uint8_t spUnRecoverableMapError1[] = {
4813 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
4814};
4815
4816// No more items
4817static const uint8_t spUnRecoverableMapError2[] = {
4818 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
4819};
4820
4821// Hit end because string is too long
4822static const uint8_t spUnRecoverableMapError3[] = {
4823 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
4824};
4825
4826// Hit end because string is too long
4827static const uint8_t spUnRecoverableMapError4[] = {
4828 0xbf,
4829 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4830 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
4831 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4832 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4833 0xff
4834};
4835
4836
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004837int32_t EnterMapTest()
4838{
Laurence Lundbladef0499502020-08-01 11:55:57 -07004839 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004840 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07004841 int32_t nReturn;
4842 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004843
4844
4845 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
4846 QCBORDecode_EnterMap(&DCtx);
4847
Laurence Lundbladef0499502020-08-01 11:55:57 -07004848
4849 QCBORDecode_EnterArray(&DCtx); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004850 QCBORDecode_ExitArray(&DCtx);
4851
Laurence Lundbladef0499502020-08-01 11:55:57 -07004852 QCBORDecode_EnterArray(&DCtx); // Label 9
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004853 QCBORDecode_EnterArray(&DCtx);
4854 QCBORDecode_ExitArray(&DCtx);
4855 QCBORDecode_EnterArray(&DCtx);
4856 QCBORDecode_ExitArray(&DCtx);
4857 QCBORDecode_ExitArray(&DCtx);
4858
Laurence Lundbladef0499502020-08-01 11:55:57 -07004859 QCBORDecode_EnterMap(&DCtx); // Label 8
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004860 QCBORDecode_EnterArray(&DCtx);
4861 QCBORDecode_ExitArray(&DCtx);
4862 QCBORDecode_EnterMap(&DCtx);
4863 QCBORDecode_ExitMap(&DCtx);
4864 QCBORDecode_EnterArray(&DCtx);
4865 QCBORDecode_ExitArray(&DCtx);
4866 QCBORDecode_ExitMap(&DCtx);
4867
Laurence Lundbladef0499502020-08-01 11:55:57 -07004868 QCBORDecode_EnterMap(&DCtx); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004869 QCBORDecode_ExitMap(&DCtx);
4870
Laurence Lundbladef0499502020-08-01 11:55:57 -07004871 QCBORDecode_EnterArray(&DCtx); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004872 QCBORDecode_ExitArray(&DCtx);
4873
Laurence Lundbladef0499502020-08-01 11:55:57 -07004874 QCBORDecode_EnterArray(&DCtx); // Label 6
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004875 QCBORDecode_EnterArray(&DCtx);
4876 QCBORDecode_ExitArray(&DCtx);
4877 QCBORDecode_EnterArray(&DCtx);
4878 QCBORDecode_ExitArray(&DCtx);
4879 QCBORDecode_ExitArray(&DCtx);
4880
4881 QCBORDecode_ExitMap(&DCtx);
4882
4883 uErr = QCBORDecode_Finish(&DCtx);
4884 if(uErr != QCBOR_SUCCESS){
4885 return 3011;
4886 }
4887
4888
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004889 (void)pValidMapIndefEncoded;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004890 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004891 if(nReturn) {
4892 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004893 }
4894
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004895 nReturn = EMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004896 if(nReturn) {
4897 return nReturn;
4898 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004899
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004900
Laurence Lundblade937ea812020-05-08 11:38:23 -07004901
4902 // These tests confirm the cursor is at the right place after entering a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004903 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004904
4905 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07004906 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004907 QCBORDecode_EnterMap(&DCtx);
4908 QCBORDecode_GetNext(&DCtx, &Item1);
4909 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4910 return 2001;
4911 }
4912
4913
Laurence Lundblade9b334962020-08-27 10:55:53 -07004914 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07004915 QCBORDecode_VGetNext(&DCtx, &Item1);
4916 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004917 QCBORDecode_EnterArray(&DCtx);
4918 QCBORDecode_GetNext(&DCtx, &Item1);
4919 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4920 return 2002;
4921 }
4922
Laurence Lundblade9b334962020-08-27 10:55:53 -07004923 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004924 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004925 QCBORDecode_GetNext(&DCtx, &Item1);
4926 QCBORDecode_GetNext(&DCtx, &Item1);
4927 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004928 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4929 QCBORDecode_GetNext(&DCtx, &Item1);
4930 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4931 return 2003;
4932 }
4933
Laurence Lundblade9b334962020-08-27 10:55:53 -07004934 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004935 QCBORDecode_EnterMap(&DCtx);
4936 QCBORDecode_GetNext(&DCtx, &Item1);
4937 QCBORDecode_GetNext(&DCtx, &Item1);
4938 QCBORDecode_GetNext(&DCtx, &Item1);
4939 QCBORDecode_GetNext(&DCtx, &Item1);
4940 QCBORDecode_GetNext(&DCtx, &Item1);
4941 QCBORDecode_GetNext(&DCtx, &Item1);
4942 QCBORDecode_GetNext(&DCtx, &Item1);
4943 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4944 QCBORDecode_GetNext(&DCtx, &Item1);
4945 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004946 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004947 }
4948
Laurence Lundblade9b334962020-08-27 10:55:53 -07004949 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004950 QCBORDecode_EnterMap(&DCtx);
4951 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4952 QCBORDecode_ExitArray(&DCtx);
4953 QCBORDecode_GetNext(&DCtx, &Item1);
4954 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4955 return 2006;
4956 }
4957 QCBORDecode_ExitMap(&DCtx);
4958 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4959 return 2007;
4960 }
4961
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004962 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
4963 QCBORDecode_EnterArray(&DCtx);
4964 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004965 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4966 uErr = QCBORDecode_GetAndResetError(&DCtx);
4967 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004968 return 2008;
4969 }
4970 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004971 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07004972 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07004973 return 2009;
4974 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004975
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004976
4977 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
4978 QCBORDecode_EnterMap(&DCtx);
4979 // This will fail because the map is empty.
4980 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4981 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004982 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004983 return 2010;
4984 }
4985 QCBORDecode_ExitMap(&DCtx);
4986 uErr = QCBORDecode_Finish(&DCtx);
4987 if(uErr != QCBOR_SUCCESS){
4988 return 2011;
4989 }
4990
4991
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004992 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
4993 QCBORDecode_EnterMap(&DCtx);
4994 // This will fail because the map is empty.
4995 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
4996 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004997 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07004998 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004999 }
5000 QCBORDecode_ExitMap(&DCtx);
5001 uErr = QCBORDecode_Finish(&DCtx);
5002 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005003 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005004 }
5005
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005006
5007 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
5008 QCBORDecode_EnterArray(&DCtx);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005009 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005010 QCBORDecode_EnterMap(&DCtx);
5011 QCBORDecode_ExitMap(&DCtx);
5012 QCBORDecode_EnterArray(&DCtx);
5013 QCBORDecode_ExitArray(&DCtx);
5014 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5015 QCBORDecode_ExitArray(&DCtx);
5016 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005017 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005018 return 2014;
5019 }
5020
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005021 int64_t nInt;
5022 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
5023 QCBORDecode_EnterMap(&DCtx);
5024 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5025 uErr = QCBORDecode_GetAndResetError(&DCtx);
5026 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5027 return 2021;
5028 }
5029
5030 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5031 uErr = QCBORDecode_GetAndResetError(&DCtx);
5032 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5033 return 2022;
5034 }
5035
5036 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5037 uErr = QCBORDecode_GetAndResetError(&DCtx);
5038 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5039 return 2023;
5040 }
5041
5042 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5043 uErr = QCBORDecode_GetAndResetError(&DCtx);
5044#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5045 if(uErr != QCBOR_ERR_DATE_OVERFLOW) {
5046 return 2024;
5047 }
5048#else
5049 if(uErr != QCBOR_ERR_FLOAT_DATE_DISABLED) {
5050 return 2027;
5051 }
5052#endif
5053
5054 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5055 uErr = QCBORDecode_GetAndResetError(&DCtx);
5056 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5057 return 2025;
5058 }
5059
5060 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5061
5062 QCBORDecode_ExitMap(&DCtx);
5063 uErr = QCBORDecode_Finish(&DCtx);
5064 if(uErr != QCBOR_SUCCESS) {
5065 return 2026;
5066 }
5067
5068 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
5069 QCBORDecode_EnterMap(&DCtx);
5070 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5071 uErr = QCBORDecode_GetAndResetError(&DCtx);
5072 if(uErr != QCBOR_ERR_BAD_BREAK) {
5073 return 2030;
5074 }
5075
5076 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
5077 QCBORDecode_EnterMap(&DCtx);
5078 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5079 uErr = QCBORDecode_GetAndResetError(&DCtx);
5080 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5081 return 2031;
5082 }
5083
5084 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
5085 QCBORDecode_EnterMap(&DCtx);
5086 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5087 uErr = QCBORDecode_GetAndResetError(&DCtx);
5088 if(uErr != QCBOR_ERR_HIT_END) {
5089 return 2032;
5090 }
5091
5092 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
5093 QCBORDecode_EnterMap(&DCtx);
5094 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5095 uErr = QCBORDecode_GetAndResetError(&DCtx);
5096 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5097 return 2033;
5098 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005099
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005100 return 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005101}
5102
5103
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005104struct NumberConversion {
5105 char *szDescription;
5106 UsefulBufC CBOR;
5107 int64_t nConvertedToInt64;
5108 QCBORError uErrorInt64;
5109 uint64_t uConvertToUInt64;
5110 QCBORError uErrorUint64;
5111 double dConvertToDouble;
5112 QCBORError uErrorDouble;
5113};
5114
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005115static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005116 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005117 "too large to fit into int64_t",
5118 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5119 0,
5120 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5121 0,
5122 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5123 ((double)INT64_MIN) + 1 ,
5124 QCBOR_SUCCESS
5125 },
5126 {
5127 "largest negative int that fits in int64_t",
5128 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5129 INT64_MIN,
5130 QCBOR_SUCCESS,
5131 0,
5132 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5133 (double)INT64_MIN,
5134 QCBOR_SUCCESS
5135 },
5136 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005137 "negative bignum -1",
5138 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5139 -1,
5140 QCBOR_SUCCESS,
5141 0,
5142 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5143 -1.0,
5144 QCBOR_SUCCESS
5145 },
5146 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005147 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005148 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5149 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005150#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005151 257000,
5152 QCBOR_SUCCESS,
5153 257000,
5154 QCBOR_SUCCESS,
5155 257000.0,
5156 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005157#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5158 0,
5159 QCBOR_ERR_UNEXPECTED_TYPE,
5160 0,
5161 QCBOR_ERR_UNEXPECTED_TYPE,
5162 0.0,
5163 QCBOR_ERR_UNEXPECTED_TYPE
5164#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005165 },
5166 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005167 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005168 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5169 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005170#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladeda095972020-06-06 18:35:33 -07005171 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07005172 QCBOR_SUCCESS,
5173 0,
5174 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07005175 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07005176 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005177#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5178 0,
5179 QCBOR_ERR_UNEXPECTED_TYPE,
5180 0,
5181 QCBOR_ERR_UNEXPECTED_TYPE,
5182 0.0,
5183 QCBOR_ERR_UNEXPECTED_TYPE
5184#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005185 },
5186 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005187 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005188 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5189 0xC2, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005190#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade887add82020-05-17 05:50:34 -07005191 2056,
5192 QCBOR_SUCCESS,
5193 2056,
5194 QCBOR_SUCCESS,
5195 2056.0,
5196 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005197#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5198 0,
5199 QCBOR_ERR_UNEXPECTED_TYPE,
5200 0,
5201 QCBOR_ERR_UNEXPECTED_TYPE,
5202 0.0,
5203 QCBOR_ERR_UNEXPECTED_TYPE
5204#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
Laurence Lundblade887add82020-05-17 05:50:34 -07005205 },
5206 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005207 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005208 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5209 0,
5210 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5211 0,
5212 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5213 -18446744073709551617.0,
5214 QCBOR_SUCCESS
5215 },
5216 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005217 "Positive bignum 0x01020304 indefinite length string",
5218 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5219 0x01020304,
5220 QCBOR_SUCCESS,
5221 0x01020304,
5222 QCBOR_SUCCESS,
5223 16909060.0,
5224 QCBOR_SUCCESS
5225 },
5226 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005227 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005228 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5229 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005230#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005231 0,
5232 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5233 0,
5234 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5235 -INFINITY,
5236 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005237#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5238 0,
5239 QCBOR_ERR_UNEXPECTED_TYPE,
5240 0,
5241 QCBOR_ERR_UNEXPECTED_TYPE,
5242 0.0,
5243 QCBOR_ERR_UNEXPECTED_TYPE
5244#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005245 },
5246 {
5247 "big float [9223372036854775806, 9223372036854775806]",
5248 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5249 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005250#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade313b2862020-05-16 01:23:06 -07005251 0,
5252 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5253 0,
5254 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5255 INFINITY,
5256 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005257#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5258 0,
5259 QCBOR_ERR_UNEXPECTED_TYPE,
5260 0,
5261 QCBOR_ERR_UNEXPECTED_TYPE,
5262 0.0,
5263 QCBOR_ERR_UNEXPECTED_TYPE
5264#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005265 },
5266 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005267 "Big float 3 * 2^^2",
5268 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005269#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade983500d2020-05-14 11:49:34 -07005270 12,
5271 QCBOR_SUCCESS,
5272 12,
5273 QCBOR_SUCCESS,
5274 12.0,
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 Lundblade983500d2020-05-14 11:49:34 -07005284 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005285 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005286 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005287 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5288 0,
5289 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5290 18446744073709551615ULL,
5291 QCBOR_SUCCESS,
5292 18446744073709551615.0,
5293 QCBOR_SUCCESS
5294 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005295 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005296 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005297 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5298 65536-1,
5299 QCBOR_SUCCESS,
5300 0xffff,
5301 QCBOR_SUCCESS,
5302 65535.0,
5303 QCBOR_SUCCESS
5304 },
5305 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005306 "Postive integer 0",
5307 {(uint8_t[]){0x0}, 1},
5308 0LL,
5309 QCBOR_SUCCESS,
5310 0ULL,
5311 QCBOR_SUCCESS,
5312 0.0,
5313 QCBOR_SUCCESS
5314 },
5315 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005316 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005317 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5318 -9223372036854775807-1, // INT64_MIN
5319 QCBOR_SUCCESS,
5320 0ULL,
5321 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5322 -9223372036854775808.0,
5323 QCBOR_SUCCESS
5324 },
5325 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005326 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005327 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005328#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005329 100L,
5330 QCBOR_SUCCESS,
5331 100ULL,
5332 QCBOR_SUCCESS,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005333#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5334 0,
5335 QCBOR_ERR_HW_FLOAT_DISABLED,
5336 0,
5337 QCBOR_ERR_HW_FLOAT_DISABLED,
5338#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005339 100.3,
5340 QCBOR_SUCCESS
5341 },
5342 {
5343 "Floating point value NaN 0xfa7fc00000",
5344 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005345#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005346 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005347 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005348 0,
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005349 QCBOR_ERR_FLOAT_EXCEPTION,
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005350#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5351 0,
5352 QCBOR_ERR_HW_FLOAT_DISABLED,
5353 0,
5354 QCBOR_ERR_HW_FLOAT_DISABLED,
5355#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005356 NAN,
5357 QCBOR_SUCCESS
5358 },
5359 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005360 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005361 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005362#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
5363#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5364 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005365 -4,
5366 QCBOR_SUCCESS,
5367 0,
5368 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5369 -4.0,
5370 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005371#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5372 // Float HW disabled
5373 -4,
5374 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5375 0,
5376 QCBOR_ERR_HW_FLOAT_DISABLED, // Can't convert to integer
5377 -4.0,
5378 QCBOR_SUCCESS // Uses ieee754.h to conver, not HW
5379#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5380#else
5381 // Half-precision disabled
5382 -4,
5383 QCBOR_ERR_HALF_PRECISION_DISABLED,
5384 0,
5385 QCBOR_ERR_HALF_PRECISION_DISABLED,
5386 -4.0,
5387 QCBOR_ERR_HALF_PRECISION_DISABLED
5388#endif
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005389 },
5390 {
5391 "Decimal fraction 3/10",
5392 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005393#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005394 0,
5395 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5396 0,
5397 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5398 0.30000000000000004,
5399 QCBOR_SUCCESS
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005400#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5401 0,
5402 QCBOR_ERR_UNEXPECTED_TYPE,
5403 0,
5404 QCBOR_ERR_UNEXPECTED_TYPE,
5405 0.0,
5406 QCBOR_ERR_UNEXPECTED_TYPE
5407#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005408 },
5409 {
5410 "+inifinity",
5411 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
5412#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5413 0,
5414 QCBOR_ERR_FLOAT_EXCEPTION,
5415 0,
5416 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5417#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5418 0,
5419 QCBOR_ERR_HW_FLOAT_DISABLED,
5420 0,
5421 QCBOR_ERR_HW_FLOAT_DISABLED,
5422#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5423 INFINITY,
5424 QCBOR_SUCCESS
5425 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005426
5427 {
5428 "extreme pos bignum",
5429 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5430 // 50 rows of 8 is 400 digits.
5431 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5432 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5433 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5434 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5435 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5436 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5437 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5438 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5439 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5440 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5441 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5442 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5443 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5444 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5445 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5446 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5447 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5448 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5449 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5450 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5451 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5452 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5453 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5454 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5455 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5456 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5457 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5458 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5481 404},
5482 0,
5483 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5484 0,
5485 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5486#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5487 INFINITY,
5488 QCBOR_SUCCESS
5489#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5490 0,
5491 QCBOR_ERR_HW_FLOAT_DISABLED,
5492#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5493 },
5494
5495 {
5496 "extreme neg bignum",
5497 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5498 // 50 rows of 8 is 400 digits.
5499 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5500 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5501 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5502 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5503 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5504 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5505 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5506 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5507 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5508 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5509 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5510 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5511 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5512 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5513 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5514 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5515 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5516 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5517 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5518 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5519 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5520 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5521 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5522 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5523 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5524 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5525 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5526 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5527 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5528 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5529 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5530 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5531 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5532 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5533 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5534 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5535 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5536 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5537 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5538 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5539 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5540 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5541 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5542 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5543 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5544 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5545 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5546 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5547 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5548 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5549 404},
5550 0,
5551 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5552 0,
5553 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5554#ifndef QCBOR_DISABLE_FLOAT_HW_USE
5555 -INFINITY,
5556 QCBOR_SUCCESS
5557#else /* QCBOR_DISABLE_FLOAT_HW_USE */
5558 0,
5559 QCBOR_ERR_HW_FLOAT_DISABLED,
5560#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
5561 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005562
5563 {
5564 "big float underflow [9223372036854775806, -9223372036854775806]",
5565 {(uint8_t[]){
5566 0xC5, 0x82,
5567 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5568 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5569#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5570 0,
5571 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5572 0,
5573 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5574 0,
5575 QCBOR_SUCCESS
5576#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5577 0,
5578 QCBOR_ERR_UNEXPECTED_TYPE,
5579 0,
5580 QCBOR_ERR_UNEXPECTED_TYPE,
5581 0.0,
5582 QCBOR_ERR_UNEXPECTED_TYPE
5583#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5584 },
5585
5586 {
5587 "bigfloat that evaluates to -INFINITY",
5588 {(uint8_t[]){
5589 0xC5, 0x82,
5590 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5591 0xC3, 0x42, 0x01, 0x01}, 15},
5592#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
5593 0,
5594 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5595 0,
5596 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5597 -INFINITY,
5598 QCBOR_SUCCESS
5599#else /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
5600 0,
5601 QCBOR_ERR_UNEXPECTED_TYPE,
5602 0,
5603 QCBOR_ERR_UNEXPECTED_TYPE,
5604 0.0,
5605 QCBOR_ERR_UNEXPECTED_TYPE
5606#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA*/
5607 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005608};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005609
5610
5611
Laurence Lundblade313b2862020-05-16 01:23:06 -07005612int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005613{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005614 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005615
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005616 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5617 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005618
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005619 // Set up the decoding context including a memory pool so that
5620 // indefinite length items can be checked
5621 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005622 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005623
5624 /* ----- test conversion to int64_t ------ */
5625 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005626 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5627 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005628 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005629 }
5630
5631 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005632 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005633 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005634 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005635 }
5636 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005637 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005638 }
5639
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005640 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005641 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5642 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5643 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005644 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005645 }
5646 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005647 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005648 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005649 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005650 }
5651 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005652 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005653 }
5654
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005655 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005656 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
5657 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
5658 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005659 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005660 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005661#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005662 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005663 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07005664 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005665 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005666 }
5667 if(pF->uErrorDouble == QCBOR_SUCCESS) {
5668 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005669 // NaN's can't be compared for equality. A NaN is
5670 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005671 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005672 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005673 }
5674 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005675 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005676 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005677 }
5678 }
5679 }
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005680#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005681 }
5682
5683 return 0;
5684}
5685
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005686
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07005687
5688
Laurence Lundbladee3553422020-05-02 11:11:17 -07005689int32_t CBORSequenceDecodeTests(void)
5690{
5691 QCBORDecodeContext DCtx;
5692 QCBORItem Item;
5693 QCBORError uCBORError;
5694
5695 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07005696
Laurence Lundbladee3553422020-05-02 11:11:17 -07005697 // The input for the date test happens to be a sequence so it
5698 // is reused. It is a sequence because it doesn't start as
5699 // an array or map.
5700 QCBORDecode_Init(&DCtx,
5701 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
5702 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005703
Laurence Lundbladee3553422020-05-02 11:11:17 -07005704 // Get the first item
5705 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5706 if(uCBORError != QCBOR_SUCCESS) {
5707 return 1;
5708 }
5709 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
5710 return 2;
5711 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07005712
Laurence Lundbladee3553422020-05-02 11:11:17 -07005713 // Get a second item
5714 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07005715 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
5716 return 66;
5717 }
5718
5719 // Get a third item
5720 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07005721 if(uCBORError != QCBOR_SUCCESS) {
5722 return 2;
5723 }
5724 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
5725 return 3;
5726 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005727
Laurence Lundbladee3553422020-05-02 11:11:17 -07005728 // A sequence can have stuff at the end that may
5729 // or may not be valid CBOR. The protocol decoder knows
5730 // when to stop by definition of the protocol, not
5731 // when the top-level map or array is ended.
5732 // Finish still has to be called to know that
5733 // maps and arrays (if there were any) were closed
5734 // off correctly. When called like this it
5735 // must return the error QCBOR_ERR_EXTRA_BYTES.
5736 uCBORError = QCBORDecode_Finish(&DCtx);
5737 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
5738 return 4;
5739 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07005740
5741
Laurence Lundbladee3553422020-05-02 11:11:17 -07005742 // --- Test an empty input ----
5743 uint8_t empty[1];
5744 UsefulBufC Empty = {empty, 0};
5745 QCBORDecode_Init(&DCtx,
5746 Empty,
5747 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07005748
Laurence Lundbladee3553422020-05-02 11:11:17 -07005749 uCBORError = QCBORDecode_Finish(&DCtx);
5750 if(uCBORError != QCBOR_SUCCESS) {
5751 return 5;
5752 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005753
5754
Laurence Lundbladee3553422020-05-02 11:11:17 -07005755 // --- Sequence with unclosed indefinite length array ---
5756 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005757
Laurence Lundbladee3553422020-05-02 11:11:17 -07005758 QCBORDecode_Init(&DCtx,
5759 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
5760 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005761
Laurence Lundbladee3553422020-05-02 11:11:17 -07005762 // Get the first item
5763 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5764 if(uCBORError != QCBOR_SUCCESS) {
5765 return 7;
5766 }
5767 if(Item.uDataType != QCBOR_TYPE_INT64) {
5768 return 8;
5769 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005770
Laurence Lundbladee3553422020-05-02 11:11:17 -07005771 // Get a second item
5772 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5773 if(uCBORError != QCBOR_SUCCESS) {
5774 return 9;
5775 }
5776 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5777 return 10;
5778 }
5779
5780 // Try to finish before consuming all bytes to confirm
5781 // that the still-open error is returned.
5782 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005783 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07005784 return 11;
5785 }
5786
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005787
Laurence Lundbladee3553422020-05-02 11:11:17 -07005788 // --- Sequence with a closed indefinite length array ---
5789 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005790
Laurence Lundbladee3553422020-05-02 11:11:17 -07005791 QCBORDecode_Init(&DCtx,
5792 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
5793 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005794
Laurence Lundbladee3553422020-05-02 11:11:17 -07005795 // Get the first item
5796 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5797 if(uCBORError != QCBOR_SUCCESS) {
5798 return 12;
5799 }
5800 if(Item.uDataType != QCBOR_TYPE_INT64) {
5801 return 13;
5802 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005803
Laurence Lundbladee3553422020-05-02 11:11:17 -07005804 // Get a second item
5805 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
5806 if(uCBORError != QCBOR_SUCCESS) {
5807 return 14;
5808 }
5809 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5810 return 15;
5811 }
5812
5813 // Try to finish before consuming all bytes to confirm
5814 // that the still-open error is returned.
5815 uCBORError = QCBORDecode_Finish(&DCtx);
5816 if(uCBORError != QCBOR_SUCCESS) {
5817 return 16;
5818 }
5819
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07005820
Laurence Lundbladee3553422020-05-02 11:11:17 -07005821 return 0;
5822}
5823
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005824
Laurence Lundblade70ecead2020-06-15 19:40:06 -07005825
Laurence Lundbladee15326f2020-06-15 15:50:23 -07005826int32_t IntToTests()
5827{
5828 int nErrCode;
5829 int32_t n32;
5830 int16_t n16;
5831 int8_t n8;
5832 uint32_t u32;
5833 uint16_t u16;
5834 uint8_t u8;
5835 uint64_t u64;
5836
5837 nErrCode = QCBOR_Int64ToInt32(1, &n32);
5838 if(nErrCode == -1 || n32 != 1) {
5839 return 1;
5840 }
5841
5842 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
5843 if(nErrCode == -1 || n32 != INT32_MAX) {
5844 return 2;
5845 }
5846
5847 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
5848 if(nErrCode == -1 || n32 != INT32_MIN) {
5849 return 3;
5850 }
5851
5852 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
5853 if(nErrCode != -1) {
5854 return 4;
5855 }
5856
5857 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
5858 if(nErrCode != -1) {
5859 return 5;
5860 }
5861
5862
5863 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
5864 if(nErrCode == -1 || n16 != INT16_MAX) {
5865 return 6;
5866 }
5867
5868 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
5869 if(nErrCode == -1 || n16 != INT16_MIN) {
5870 return 7;
5871 }
5872
5873 nErrCode = QCBOR_Int64ToInt16(1, &n16);
5874 if(nErrCode == -1 || n16 != 1) {
5875 return 8;
5876 }
5877
5878 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
5879 if(nErrCode != -1) {
5880 return 9;
5881 }
5882
5883 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
5884 if(nErrCode != -1) {
5885 return 10;
5886 }
5887
5888
5889 nErrCode = QCBOR_Int64ToInt8(1, &n8);
5890 if(nErrCode == -1 || n8 != 1) {
5891 return 11;
5892 }
5893
5894 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
5895 if(nErrCode == -1 || n8 != INT8_MAX) {
5896 return 12;
5897 }
5898
5899 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
5900 if(nErrCode == -1 || n8 != INT8_MIN) {
5901 return 13;
5902 }
5903
5904 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
5905 if(nErrCode != -1) {
5906 return 14;
5907 }
5908
5909 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
5910 if(nErrCode != -1) {
5911 return 15;
5912 }
5913
5914
5915 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
5916 if(nErrCode == -1 || u32 != 1) {
5917 return 16;
5918 }
5919
5920 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
5921 if(nErrCode == -1 || u32 != UINT32_MAX) {
5922 return 17;
5923 }
5924
5925 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
5926 if(nErrCode == -1 || u32 != 0) {
5927 return 18;
5928 }
5929
5930 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
5931 if(nErrCode != -1) {
5932 return 19;
5933 }
5934
5935 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
5936 if(nErrCode != -1) {
5937 return 20;
5938 }
5939
5940
5941 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
5942 if(nErrCode == -1 || u16 != UINT16_MAX) {
5943 return 21;
5944 }
5945
5946 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
5947 if(nErrCode == -1 || u16 != 0) {
5948 return 22;
5949 }
5950
5951 nErrCode = QCBOR_Int64UToInt16(1, &u16);
5952 if(nErrCode == -1 || u16 != 1) {
5953 return 23;
5954 }
5955
5956 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
5957 if(nErrCode != -1) {
5958 return 24;
5959 }
5960
5961 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
5962 if(nErrCode != -1) {
5963 return 25;
5964 }
5965
5966
5967 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
5968 if(nErrCode == -1 || u8 != UINT8_MAX) {
5969 return 26;
5970 }
5971
5972 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
5973 if(nErrCode == -1 || u8 != 0) {
5974 return 27;
5975 }
5976
5977 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
5978 if(nErrCode == -1 || u8 != 1) {
5979 return 28;
5980 }
5981
5982 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
5983 if(nErrCode != -1) {
5984 return 29;
5985 }
5986
5987 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
5988 if(nErrCode != -1) {
5989 return 30;
5990 }
5991
5992
5993 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
5994 if(nErrCode == -1 || u64 != 1) {
5995 return 31;
5996 }
5997
5998 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
5999 if(nErrCode == -1 || u64 != INT64_MAX) {
6000 return 32;
6001 }
6002
6003 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6004 if(nErrCode == -1 || u64 != 0) {
6005 return 33;
6006 }
6007
6008 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6009 if(nErrCode != -1) {
6010 return 34;
6011 }
6012
6013 return 0;
6014}
6015
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006016
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006017
6018
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006019/*
6020A sequence with
6021 A wrapping bstr
6022 containing a map
6023 1
6024 2
6025 A wrapping bstr
6026 containing an array
6027 3
6028 wrapping bstr
6029 4
6030 5
6031 6
6032 array
6033 7
6034 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006035 */
6036
Laurence Lundblade55013642020-09-23 05:39:22 -07006037static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006038{
Laurence Lundblade55013642020-09-23 05:39:22 -07006039 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006040 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006041 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006042
Laurence Lundblade55013642020-09-23 05:39:22 -07006043 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006044
6045 QCBOREncode_BstrWrap(&EC);
6046 QCBOREncode_OpenMap(&EC);
6047 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6048 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6049 QCBOREncode_CloseMap(&EC);
6050 QCBOREncode_BstrWrap(&EC);
6051 QCBOREncode_OpenArray(&EC);
6052 QCBOREncode_AddInt64(&EC, 3);
6053 QCBOREncode_BstrWrap(&EC);
6054 QCBOREncode_AddInt64(&EC, 4);
6055 QCBOREncode_CloseBstrWrap(&EC, NULL);
6056 QCBOREncode_AddInt64(&EC, 5);
6057 QCBOREncode_CloseArray(&EC);
6058 QCBOREncode_CloseBstrWrap(&EC, NULL);
6059 QCBOREncode_AddInt64(&EC, 6);
6060 QCBOREncode_CloseBstrWrap(&EC, NULL);
6061 QCBOREncode_OpenArray(&EC);
6062 QCBOREncode_AddInt64(&EC, 7);
6063 QCBOREncode_AddInt64(&EC, 8);
6064 QCBOREncode_CloseArray(&EC);
6065
6066 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006067 if(uErr) {
6068 Encoded = NULLUsefulBufC;
6069 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006070
6071 return Encoded;
6072}
6073
6074
6075int32_t EnterBstrTest()
6076{
Laurence Lundblade55013642020-09-23 05:39:22 -07006077 MakeUsefulBufOnStack(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006078
6079 QCBORDecodeContext DC;
6080
Laurence Lundblade55013642020-09-23 05:39:22 -07006081 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006082
Laurence Lundblade55013642020-09-23 05:39:22 -07006083 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006084
6085
Laurence Lundblade9b334962020-08-27 10:55:53 -07006086 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006087 QCBORDecode_EnterMap(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006088 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6089 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006090 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006091 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006092 QCBORDecode_EnterArray(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006093 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006094 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006095 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006096 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006097 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006098 QCBORDecode_ExitArray(&DC);
6099 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006100 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006101 QCBORDecode_ExitBstrWrapped(&DC);
6102 QCBORDecode_EnterArray(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006103 QCBORDecode_GetInt64(&DC, &n7);
6104 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006105 QCBORDecode_ExitArray(&DC);
6106
6107 QCBORError uErr = QCBORDecode_Finish(&DC);
6108
6109 return (int32_t)uErr;
6110}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006111
6112
6113
6114
6115static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006116 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006117
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006118 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006119 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006120 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6121 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6122 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006123
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006124 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006125 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6126 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6127 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006128
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006129 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006130 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006131 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6132 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006133
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006134 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006135 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6136 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006137
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006138 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006139 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006140 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6141 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006142
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006143 21,
6144 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6145 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6146
6147 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006148 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006149 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6150 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006151
6152 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006153 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6154 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006155
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006156 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006157 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006158 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6159 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006160
6161 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006162 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6163 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006164
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006165 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006166 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006167 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6168 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006169
6170 0x18, 0x33,
6171 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6172
6173 // MIME
6174 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006175 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6176 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6177 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006178
6179 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006180 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6181 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006182
6183 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006184 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6185 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6186 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006187
6188 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006189 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6190 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006191
6192 // UUID
6193 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006194 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6195 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006196
6197 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006198 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6199 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006200};
6201
6202int32_t DecodeTaggedTypeTests()
6203{
6204 QCBORDecodeContext DC;
6205 QCBORError uErr;
6206
6207 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6208
6209 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006210 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006211
6212 QCBORDecode_EnterMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006213 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006214 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006215 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6216 return 1;
6217 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006218 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006219 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6220 return 2;
6221 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006222 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006223 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6224 return 3;
6225 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006226 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006227 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006228 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6229 return 4;
6230 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006231 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006232 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006233 return 5;
6234 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006235
Laurence Lundblade9b334962020-08-27 10:55:53 -07006236 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006237 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6238 bNeg != false) {
6239 return 10;
6240 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006241 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006242 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6243 bNeg != true) {
6244 return 11;
6245 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006246 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006247 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6248 return 12;
6249 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006250 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006251 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006252 return 13;
6253 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006254 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006255 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006256 return 14;
6257 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006258
Laurence Lundblade9b334962020-08-27 10:55:53 -07006259 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006260 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6261 return 20;
6262 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006263 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006264 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6265 return 21;
6266 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006267 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006268 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006269 return 22;
6270 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006271 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006272 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006273 return 23;
6274 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006275
Laurence Lundblade9b334962020-08-27 10:55:53 -07006276 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006277 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6278 return 30;
6279 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006280 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006281 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6282 return 31;
6283 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006284 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006285 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006286 return 32;
6287 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006288 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006289 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006290 return 33;
6291 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006292
Laurence Lundblade9b334962020-08-27 10:55:53 -07006293 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006294 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6295 return 40;
6296 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006297 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006298 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6299 return 41;
6300 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006301 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006302 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006303 return 42;
6304 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006305 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006306 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006307 return 43;
6308 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006309
Laurence Lundblade9b334962020-08-27 10:55:53 -07006310 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006311 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6312 return 50;
6313 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006314 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006315 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6316 return 51;
6317 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006318 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006319 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006320 return 52;
6321 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006322 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006323 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006324 return 53;
6325 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006326
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006327 // MIME
6328 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006329 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006330 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6331 bIsNot7Bit == true) {
6332 return 60;
6333 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006334 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006335 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6336 bIsNot7Bit == true) {
6337 return 61;
6338 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006339 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006340 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6341 bIsNot7Bit == false) {
6342 return 62;
6343 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006344 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006345 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6346 bIsNot7Bit == false) {
6347 return 63;
6348 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006349 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006350 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006351 return 64;
6352 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006353 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006354 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006355 return 65;
6356 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006357
Laurence Lundblade9b334962020-08-27 10:55:53 -07006358 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006359 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6360 return 70;
6361 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006362 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006363 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6364 return 71;
6365 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006366 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006367 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006368 return 72;
6369 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006370 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006371 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006372 return 73;
6373 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006374
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006375 // Improvement: add some more error test cases
6376
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006377 QCBORDecode_ExitMap(&DC);
6378
6379 uErr = QCBORDecode_Finish(&DC);
6380 if(uErr != QCBOR_SUCCESS) {
6381 return 100;
6382 }
6383
6384 return 0;
6385}