blob: be5548c3a710db1705cfcbcc503d0e4ebcdea591 [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 Lundblade8e36f812024-01-26 10:59:29 -07003 Copyright (c) 2018-2024, Laurence Lundblade.
adam280946eefce2022-08-13 21:48:07 +02004 Copyright (c) 2022, Arm Limited. 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 Lundbladedc6e28e2018-10-11 19:19:27 +053032
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080033#include "qcbor/qcbor_encode.h"
34#include "qcbor/qcbor_decode.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035#include "qcbor_encode_tests.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080036
37
Laurence Lundblade369b90a2018-10-22 02:04:37 +053038/*
39 This is the test set for CBOR encoding.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade369b90a2018-10-22 02:04:37 +053041 This is largely complete for the implemented.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade369b90a2018-10-22 02:04:37 +053043 A few more things to do include:
44 - Add a test for counting the top level items and adding it back in with AddRaw()
45 - Run on some different CPUs like 32-bit and maybe even 16-bit
46 - Test the large array count limit
47 - Add the CBOR diagnostic output for every expected
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080048
Laurence Lundblade369b90a2018-10-22 02:04:37 +053049 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050
Laurence Lundbladeee851742020-01-08 08:37:05 -080051//#define PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052
Laurence Lundbladeee851742020-01-08 08:37:05 -080053#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053054#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055
Laurence Lundbladeee851742020-01-08 08:37:05 -080056#if 0
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057// ifdef these out to not have compiler warnings
58static void printencoded(const uint8_t *pEncoded, size_t nLen)
59{
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070060 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 for(i = 0; i < nLen; i++) {
62 uint8_t Z = pEncoded[i];
63 printf("%02x ", Z);
64 }
65 printf("\n");
66
67 fflush(stdout);
68}
Laurence Lundbladeee851742020-01-08 08:37:05 -080069#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080071
Laurence Lundblade369b90a2018-10-22 02:04:37 +053072// Do the comparison and print out where it fails
73static int UsefulBuf_Compare_Print(UsefulBufC U1, UsefulBufC U2) {
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070074 size_t i;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 for(i = 0; i < U1.len; i++) {
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070076 if(((const uint8_t *)U1.ptr)[i] != ((const uint8_t *)U2.ptr)[i]) {
escherstairb5563422023-02-13 18:36:45 +010077 printf("Position: %u Actual: 0x%x Expected: 0x%x\n",
Laurence Lundbladeee851742020-01-08 08:37:05 -080078 (uint32_t)i,
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070079 ((const uint8_t *)U1.ptr)[i],
80 ((const uint8_t *)U2.ptr)[i]);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053081 return 1;
82 }
83 }
84 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080085
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053086}
87
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070088#define CheckResults(Enc, Expected) \
Laurence Lundblade369b90a2018-10-22 02:04:37 +053089 UsefulBuf_Compare_Print(Enc, (UsefulBufC){Expected, sizeof(Expected)})
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053090
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070091#else
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092
93#define CheckResults(Enc, Expected) \
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +080094 UsefulBuf_Compare(Enc, (UsefulBufC){Expected, sizeof(Expected)})
95
Laurence Lundbladecafcfe12018-10-31 21:59:50 +070096#endif
97
98
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -070099#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -0800100/*
101 Returns 0 if UsefulBufs are equal
102 Returns 1000000 + offeset if they are not equal.
103
104
105
106*/
107struct UBCompareDiagnostic {
108 uint8_t uActual;
109 uint8_t uExpected;
110 size_t uOffset;
111};
112
Laurence Lundbladeee851742020-01-08 08:37:05 -0800113static int32_t
114UsefulBuf_CompareWithDiagnostic(UsefulBufC Actual,
115 UsefulBufC Expected,
116 struct UBCompareDiagnostic *pDiag) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800117 size_t i;
118 for(i = 0; i < Actual.len; i++) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800119 if(((const uint8_t *)Actual.ptr)[i] != ((const uint8_t *)Expected.ptr)[i]) {
Laurence Lundblade59289e52019-12-30 13:44:37 -0800120 if(pDiag) {
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800121 pDiag->uActual = ((const uint8_t *)Actual.ptr)[i];
122 pDiag->uExpected = ((const uint8_t *)Expected.ptr)[i];
Laurence Lundblade59289e52019-12-30 13:44:37 -0800123 pDiag->uOffset = i;
124 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800125 // Cast to int is OK as this is only a diagnostic and the sizes
126 // here are never over a few KB.
127 return (int32_t)i + 1000000;
Laurence Lundblade59289e52019-12-30 13:44:37 -0800128 }
129 }
130 return 0;
131
132}
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -0700133#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade59289e52019-12-30 13:44:37 -0800134
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700135
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530136// One big buffer that is used by all the tests to encode into
137// Putting it in uninitialized data is better than using a lot
138// of stack. The tests should run on small devices too.
139static uint8_t spBigBuf[2200];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800140
141
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800142
143/*
144 Some very minimal tests.
145 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300146int32_t BasicEncodeTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800147{
148 // Very simple CBOR, a map with one boolean that is true in it
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800149 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800150
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530151 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530152
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800153 QCBOREncode_OpenMap(&EC);
154 QCBOREncode_AddBoolToMapN(&EC, 66, true);
155 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800157 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700158 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530159 return -1;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
162
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800163 // Decode it and see that is right
164 QCBORDecodeContext DC;
165 QCBORItem Item;
166 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800167
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800168 QCBORDecode_GetNext(&DC, &Item);
169 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530170 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800173 QCBORDecode_GetNext(&DC, &Item);
174 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530175 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800177
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800178 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530179 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800181
182
Laurence Lundbladeee851742020-01-08 08:37:05 -0800183 // Make another encoded message with the CBOR from the previous
184 // put into this one
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530185 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800186 QCBOREncode_Init(&EC, MemoryForEncoded2);
187 QCBOREncode_OpenArray(&EC);
188 QCBOREncode_AddUInt64(&EC, 451);
189 QCBOREncode_AddEncoded(&EC, Encoded);
190 QCBOREncode_OpenMap(&EC);
191 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
192 QCBOREncode_CloseMap(&EC);
193 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800195 UsefulBufC Encoded2;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700196 if(QCBOREncode_Finish(&EC, &Encoded2)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530197 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800198 }
199 /*
200 [ // 0 1:3
201 451, // 1 1:2
202 { // 1 1:2 2:1
203 66: true // 2 1:1
204 },
205 { // 1 1:1 2:1
206 -70000: { // 2 1:1 2:1 3:1
207 66: true // 3 XXXXXX
208 }
209 }
210 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
212
213
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800214 83 # array(3)
215 19 01C3 # unsigned(451)
216 A1 # map(1)
217 18 42 # unsigned(66)
218 F5 # primitive(21)
219 A1 # map(1)
220 3A 0001116F # negative(69999)
221 A1 # map(1)
222 18 42 # unsigned(66)
223 F5 # primitive(21)
224 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800226 // Decode it and see if it is OK
227 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800228
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800229 // 0 1:3
230 QCBORDecode_GetNext(&DC, &Item);
231 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530232 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800234
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800235 // 1 1:2
236 QCBORDecode_GetNext(&DC, &Item);
237 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530238 return -7;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800241 // 1 1:2 2:1
242 QCBORDecode_GetNext(&DC, &Item);
243 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530244 return -8;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800247 // 2 1:1
248 QCBORDecode_GetNext(&DC, &Item);
249 if(Item.uDataType != QCBOR_TYPE_TRUE) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530250 return -9;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800252
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800253 // 1 1:1 2:1
254 QCBORDecode_GetNext(&DC, &Item);
255 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530256 return -10;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800258
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800259 // 2 1:1 2:1 3:1
260 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800261 if(Item.uDataType != QCBOR_TYPE_MAP ||
262 Item.val.uCount != 1 ||
263 Item.uLabelType != QCBOR_TYPE_INT64 ||
264 Item.label.int64 != -70000) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530265 return -11;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800268 // 3 XXXXXX
269 QCBORDecode_GetNext(&DC, &Item);
270 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530271 return -12;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800272 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800273
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800274 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530275 return -13;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800277
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800278 return 0;
279}
280
281
282
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530283static const uint8_t spExpectedEncodedAll[] = {
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700284 0x98, 0x23, 0x66, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x32, 0xd8,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530285 0x64, 0x1a, 0x05, 0x5d, 0x23, 0x15, 0x65, 0x49, 0x4e, 0x54,
286 0x36, 0x34, 0xd8, 0x4c, 0x1b, 0x00, 0x00, 0x00, 0x12, 0x16,
287 0xaf, 0x2b, 0x15, 0x00, 0x38, 0x2b, 0xa4, 0x63, 0x4c, 0x42,
288 0x4c, 0x18, 0x4d, 0x23, 0x18, 0x58, 0x78, 0x1a, 0x4e, 0x45,
289 0x47, 0x4c, 0x42, 0x4c, 0x54, 0x48, 0x41, 0x54, 0x20, 0x49,
290 0x53, 0x20, 0x4b, 0x49, 0x4e, 0x44, 0x20, 0x4f, 0x46, 0x20,
291 0x4c, 0x4f, 0x4e, 0x47, 0x3b, 0x00, 0x00, 0x02, 0x2d, 0x9a,
292 0xc6, 0x94, 0x55, 0x3a, 0x05, 0xf5, 0xe0, 0xff, 0x3a, 0x2f,
Laurence Lundblade3df8c7e2018-11-02 13:12:41 +0700293 0xaf, 0x07, 0xff, 0xc1, 0x1a, 0x8e, 0x15, 0x1c, 0x8a,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530294 0xa3, 0x74, 0x4c, 0x6f, 0x6e, 0x67, 0x4c, 0x69, 0x76, 0x65,
295 0x44, 0x65, 0x6e, 0x69, 0x73, 0x52, 0x69, 0x74, 0x63, 0x68,
296 0x69, 0x65, 0xc1, 0x1a, 0x53, 0x72, 0x4e, 0x00, 0x66, 0x74,
297 0x69, 0x6d, 0x65, 0x28, 0x29, 0xc1, 0x1a, 0x58, 0x0d, 0x41,
298 0x72, 0x39, 0x07, 0xb0, 0xc1, 0x1a, 0x58, 0x0d, 0x3f, 0x76,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700299 0x42, 0xff, 0x00, 0xa4, 0x66, 0x62, 0x69, 0x6e, 0x62, 0x69,
300 0x6e, 0xda, 0x00, 0x01, 0x86, 0xa0, 0x41, 0x00,
301 0x65, 0x65, 0x6D, 0x70, 0x74, 0x79, 0x40,
302 0x66, 0x62,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530303 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x43, 0x01, 0x02, 0x03, 0x00,
304 0x44, 0x04, 0x02, 0x03, 0xfe, 0x6f, 0x62, 0x61, 0x72, 0x20,
305 0x62, 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61,
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700306 0x72, 0x64, 0x6f, 0x6f, 0x66, 0x0a, 0x60, 0xd8, 0x20, 0x78, 0x6b,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530307 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x61,
308 0x63, 0x6b, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
309 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x71, 0x75, 0x65, 0x73, 0x74,
310 0x69, 0x6f, 0x6e, 0x73, 0x2f, 0x32, 0x38, 0x30, 0x35, 0x39,
311 0x36, 0x39, 0x37, 0x2f, 0x68, 0x6f, 0x77, 0x2d, 0x64, 0x6f,
312 0x2d, 0x69, 0x2d, 0x74, 0x6f, 0x67, 0x67, 0x6c, 0x65, 0x2d,
313 0x62, 0x65, 0x74, 0x77, 0x65, 0x65, 0x6e, 0x2d, 0x64, 0x65,
314 0x62, 0x75, 0x67, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x72, 0x65,
315 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2d, 0x62, 0x75, 0x69, 0x6c,
316 0x64, 0x73, 0x2d, 0x69, 0x6e, 0x2d, 0x78, 0x63, 0x6f, 0x64,
317 0x65, 0x2d, 0x36, 0x2d, 0x37, 0x2d, 0x38, 0xd8, 0x22, 0x78,
318 0x1c, 0x59, 0x57, 0x35, 0x35, 0x49, 0x47, 0x4e, 0x68, 0x63,
319 0x6d, 0x35, 0x68, 0x62, 0x43, 0x42, 0x77, 0x62, 0x47, 0x56,
320 0x68, 0x63, 0x33, 0x56, 0x79, 0x5a, 0x51, 0x3d, 0x3d, 0xd8,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700321 0x23, 0x67, 0x5b, 0x5e, 0x61, 0x62, 0x63, 0x5d, 0x2b, 0xd9,
322 0x01, 0x01, 0x59, 0x01, 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530323 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e,
324 0x30, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
325 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74,
326 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65,
327 0x64, 0x3b, 0x0a, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
328 0x79, 0x3d, 0x22, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75,
329 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74,
330 0x22, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
331 0x20, 0x61, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
332 0x72, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
333 0x20, 0x69, 0x6e, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66,
334 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d,
335 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
336 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f,
337 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
338 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61,
339 0x69, 0x6e, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
340 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79,
341 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58,
342 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
343 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e,
344 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a,
345 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69,
346 0x6e, 0x3b, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
347 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69,
348 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68,
349 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65,
350 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74,
351 0x2e, 0x74, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69,
352 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61,
353 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20,
354 0x74, 0x65, 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58,
355 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79,
356 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x2d, 0xae, 0x65, 0x23,
357 0x23, 0x23, 0x23, 0x23, 0x6f, 0x66, 0x6f, 0x6f, 0x20, 0x62,
358 0x61, 0x72, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f,
359 0x64, 0x5f, 0x5f, 0x5f, 0x5f, 0x67, 0x66, 0x6f, 0x6f, 0x20,
360 0x62, 0x61, 0x72, 0x66, 0x28, 0x29, 0x28, 0x29, 0x28, 0x29,
361 0xd9, 0x03, 0xe8, 0x6b, 0x72, 0x61, 0x62, 0x20, 0x72, 0x61,
362 0x62, 0x20, 0x6f, 0x6f, 0x66, 0x16, 0x6f, 0x66, 0x6f, 0x6f,
363 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66, 0x6f, 0x6f, 0x20, 0x66,
364 0x6f, 0x6f, 0x62, 0x5e, 0x5e, 0x69, 0x6f, 0x6f, 0x6f, 0x6f,
365 0x6f, 0x6f, 0x6f, 0x6f, 0x66, 0x18, 0x63, 0x6d, 0x66, 0x66,
366 0x66, 0x66, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f, 0x6f,
367 0x66, 0x63, 0x52, 0x46, 0x43, 0xd8, 0x20, 0x78, 0x31, 0x68,
368 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x74, 0x6f, 0x6f,
369 0x6c, 0x73, 0x2e, 0x69, 0x65, 0x74, 0x66, 0x2e, 0x6f, 0x72,
370 0x67, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2f, 0x72, 0x66, 0x63,
371 0x37, 0x30, 0x34, 0x39, 0x23, 0x73, 0x65, 0x63, 0x74, 0x69,
372 0x6f, 0x6e, 0x2d, 0x32, 0x2e, 0x34, 0x2e, 0x35, 0x18, 0x89,
373 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f,
374 0x63, 0x62, 0x6f, 0x72, 0x2e, 0x6d, 0x65, 0x2f, 0x68, 0x77,
375 0x68, 0x65, 0x6e, 0x69, 0x6d, 0x36, 0x34, 0xd8, 0x22, 0x6c,
376 0x63, 0x47, 0x78, 0x6c, 0x59, 0x58, 0x4e, 0x31, 0x63, 0x6d,
377 0x55, 0x75, 0x18, 0x40, 0xd8, 0x22, 0x68, 0x63, 0x33, 0x56,
378 0x79, 0x5a, 0x53, 0x34, 0x3d, 0x64, 0x70, 0x6f, 0x70, 0x6f,
379 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5c, 0x73, 0x2a, 0x6d,
380 0x6b, 0x38, 0x32, 0xd8, 0x23, 0x66, 0x70, 0x65, 0x72, 0x6c,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700381 0x5c, 0x42, 0x63, 0x4e, 0x65, 0x64, 0xd9, 0x01, 0x01, 0x59, 0x01,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530382 0x57, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73,
383 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43,
384 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
385 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61,
386 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a,
387 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22,
388 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
389 0x72, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a,
390 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
391 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20,
392 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e,
393 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d,
394 0x61, 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58,
395 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20,
396 0x74, 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65,
397 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
398 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a,
399 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
400 0x68, 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65,
401 0x78, 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
402 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
403 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
404 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
405 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a,
406 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69,
407 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
408 0x20, 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e,
409 0x74, 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d,
410 0x65, 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
411 0x74, 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69,
412 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61,
413 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78,
414 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
415 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
Laurence Lundblade4982f412020-09-18 23:02:18 -0700416 0x78, 0x74, 0x2d, 0x2d, 0x0a, 0xd9, 0x01, 0x01, 0x59, 0x01, 0x57,
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530417 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69,
418 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a, 0x43, 0x6f,
419 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
420 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72,
421 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x62,
422 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x22, 0x58,
423 0x58, 0x58, 0x58, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72,
424 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x22, 0x0a, 0x0a, 0x54,
425 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6d,
426 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6d,
427 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x69, 0x6e, 0x20,
428 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61,
429 0x74, 0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58,
430 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74,
431 0x65, 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
432 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65,
433 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0a, 0x0a,
434 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
435 0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x65, 0x78,
436 0x74, 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62,
437 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65,
438 0x78, 0x74, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
439 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78,
440 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x3b, 0x0a, 0x43,
441 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73,
442 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
443 0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74,
444 0x3b, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65,
445 0x3d, 0x22, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
446 0x22, 0x0a, 0x0a, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
447 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x74, 0x74, 0x61, 0x63,
448 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74,
449 0x0a, 0x0a, 0x2d, 0x2d, 0x58, 0x58, 0x58, 0x58, 0x62, 0x6f,
450 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x20, 0x74, 0x65, 0x78,
451 0x74, 0x2d, 0x2d, 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2d,
452 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33,
453 0x30, 0x3a, 0x30, 0x32, 0x5a, 0xa2, 0x68, 0x42, 0x65, 0x64,
454 0x20, 0x74, 0x69, 0x6d, 0x65, 0xc0, 0x78, 0x1c, 0x32, 0x30,
455 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x33, 0x54, 0x31,
456 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32, 0x2e, 0x32, 0x35,
457 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0x18, 0x58, 0xc0, 0x78,
458 0x1c, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x31, 0x32, 0x2d, 0x31,
459 0x33, 0x54, 0x31, 0x38, 0x3a, 0x33, 0x30, 0x3a, 0x30, 0x32,
460 0x2e, 0x32, 0x35, 0x2b, 0x30, 0x31, 0x3a, 0x30, 0x30, 0xf7,
461 0xa3, 0x64, 0x64, 0x61, 0x72, 0x65, 0xd8, 0x42, 0xf5, 0x62,
462 0x75, 0x75, 0xf4, 0x1a, 0x00, 0x0b, 0x41, 0x62, 0xf6, 0x80,
463 0xa3, 0x78, 0x1c, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x20, 0x61,
464 0x6e, 0x64, 0x20, 0x74, 0x61, 0x67, 0x67, 0x65, 0x64, 0x20,
465 0x65, 0x6d, 0x70, 0x74, 0x79, 0x20, 0x61, 0x72, 0x72, 0x61,
466 0x79, 0xd9, 0x04, 0x45, 0x80, 0x65, 0x61, 0x6c, 0x61, 0x62,
467 0x6c, 0x80, 0x18, 0x2a, 0x80, 0xa1, 0x68, 0x69, 0x6e, 0x20,
468 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa1, 0x19, 0x15, 0xb4, 0xa1,
469 0x6e, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x69, 0x6e, 0x20, 0x61,
470 0x20, 0x69, 0x6e, 0x20, 0x61, 0xd9, 0x23, 0x7f, 0xa0, 0xa5,
471 0x62, 0x73, 0x31, 0xd8, 0x58, 0xf8, 0xff, 0x62, 0x73, 0x32,
472 0xe0, 0x62, 0x73, 0x33, 0xd8, 0x58, 0xf8, 0x21, 0x1a, 0x05,
473 0x44, 0x8c, 0x06, 0xd8, 0x58, 0xf8, 0xff, 0x18, 0x59, 0xd8,
474 0x58, 0xf3, 0xd8, 0x25, 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54,
475 0x43, 0x53, 0x4c, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41,
476 0x32, 0xa2, 0x64, 0x55, 0x55, 0x55, 0x55, 0xd8, 0x25, 0x50,
477 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54, 0x54,
478 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0x18, 0x63, 0xd8, 0x25,
479 0x50, 0x53, 0x4d, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4c, 0x54,
480 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32, 0xf5, 0xf4, 0xa2,
481 0x71, 0x47, 0x65, 0x6f, 0x72, 0x67, 0x65, 0x20, 0x69, 0x73,
482 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x61, 0x6e, 0xf5, 0x19,
483 0x10, 0x41, 0xf5, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
484 0x00, 0x00, 0x00, 0x00, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00,
485 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x63, 0x42, 0x4E, 0x2B,
486 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
487 0x00, 0x18, 0x40, 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00,
488 0x00, 0x00, 0x00, 0x00, 0x63, 0x42, 0x4E, 0x2D, 0xC3, 0x49,
489 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
490 0x3F, 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
491 0x00, 0x00
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800492};
493
494
495static const char *szMIME = "\
496MIME-Version: 1.0\n\
497Content-Type: multipart/mixed;\n\
498boundary=\"XXXXboundary text\"\n\
499\n\
500This is a multipart message in MIME format.\n\
501\n\
502--XXXXboundary text\n\
503Content-Type: text/plain\n\
504\n\
505this is the body text\n\
506\n\
507--XXXXboundary text\n\
508Content-Type: text/plain;\n\
509Content-Disposition: attachment;\n\
510filename=\"test.txt\"\n\
511\n\
512this is the attachment text\n\
513\n\
514--XXXXboundary text--";
515
516
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700517static void AddAll(QCBOREncodeContext *pECtx)
518{
519 QCBOREncode_OpenArray(pECtx);
520
521 /* Some ints that are tagged and have strings preceeding them
522 * (not labels becase it is not a map) */
523 QCBOREncode_AddSZString(pECtx, "UINT62");
524 QCBOREncode_AddTag(pECtx, 100);
525 QCBOREncode_AddUInt64(pECtx, 89989909);
526 QCBOREncode_AddSZString(pECtx, "INT64");
527 QCBOREncode_AddTag(pECtx, 76);
528 QCBOREncode_AddInt64(pECtx, 77689989909);
529 QCBOREncode_AddUInt64(pECtx,0);
530 QCBOREncode_AddInt64(pECtx, -44);
531
532 /* ints that go in maps */
533 QCBOREncode_OpenMap(pECtx);
534 QCBOREncode_AddUInt64ToMap(pECtx, "LBL", 77);
535 QCBOREncode_AddUInt64ToMapN(pECtx, -4, 88);
536 QCBOREncode_AddInt64ToMap(pECtx, "NEGLBLTHAT IS KIND OF LONG", -2394893489238);
537 QCBOREncode_AddInt64ToMapN(pECtx, -100000000, -800000000);
538 QCBOREncode_CloseMap(pECtx);
539
540 /* Epoch Date */
541 QCBOREncode_AddDateEpoch(pECtx, 2383748234);
542
543 /* Epoch date with labels */
544 QCBOREncode_OpenMap(pECtx);
545 QCBOREncode_AddDateEpochToMap(pECtx, "LongLiveDenisRitchie", 1400000000);
546 QCBOREncode_AddDateEpochToMap(pECtx, "time()", 1477263730);
547 QCBOREncode_AddDateEpochToMapN(pECtx, -1969, 1477263222);
548 QCBOREncode_CloseMap(pECtx);
549
550 /* Binary blobs */
551 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0xff, 0x00}, 2}));
552
553 /* binary blobs in maps */
554 QCBOREncode_OpenMap(pECtx);
555 QCBOREncode_AddSZString(pECtx, "binbin");
556 QCBOREncode_AddTag(pECtx, 100000);
557 QCBOREncode_AddBytes(pECtx, ((UsefulBufC) {(uint8_t []){0x00}, 1}));
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700558 QCBOREncode_AddBytesToMap(pECtx, "empty", NULLUsefulBufC); // Empty string
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700559 QCBOREncode_AddBytesToMap(pECtx, "blabel", ((UsefulBufC) {(uint8_t []){0x01, 0x02, 0x03}, 3}));
560 QCBOREncode_AddBytesToMapN(pECtx, 0, ((UsefulBufC){(uint8_t []){0x04, 0x02, 0x03, 0xfe}, 4}));
561 QCBOREncode_CloseMap(pECtx);
562
563 /* text blobs */
564 QCBOREncode_AddText(pECtx, UsefulBuf_FROM_SZ_LITERAL("bar bar foo bar"));
565 QCBOREncode_AddSZString(pECtx, "oof\n");
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700566 QCBOREncode_AddText(pECtx, NULLUsefulBufC); // Empty string
567
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700568 const char *szURL =
569 "http://stackoverflow.com/questions/28059697/how-do-i-toggle-between-debug-and-release-builds-in-xcode-6-7-8";
570 QCBOREncode_AddURI(pECtx, UsefulBuf_FromSZ(szURL));
571 QCBOREncode_AddB64Text(pECtx, UsefulBuf_FROM_SZ_LITERAL("YW55IGNhcm5hbCBwbGVhc3VyZQ=="));
572 QCBOREncode_AddRegex(pECtx, UsefulBuf_FROM_SZ_LITERAL("[^abc]+"));
573 QCBOREncode_AddMIMEData(pECtx, UsefulBuf_FromSZ(szMIME));
574
575 /* text blobs in maps */
576 QCBOREncode_OpenMap(pECtx);
577 QCBOREncode_AddTextToMap(pECtx, "#####", UsefulBuf_FROM_SZ_LITERAL("foo bar foo foo"));
578 QCBOREncode_AddTextToMap(pECtx, "____", UsefulBuf_FROM_SZ_LITERAL("foo bar"));
579 QCBOREncode_AddSZString(pECtx, "()()()");
580 QCBOREncode_AddTag(pECtx, 1000);
581 QCBOREncode_AddSZString(pECtx, "rab rab oof");
582 QCBOREncode_AddTextToMapN(pECtx,22, UsefulBuf_FROM_SZ_LITERAL("foo foo foo foo"));
583 QCBOREncode_AddSZStringToMap(pECtx, "^^", "oooooooof");
584 QCBOREncode_AddSZStringToMapN(pECtx, 99, "ffffoooooooof");
585 QCBOREncode_AddURIToMap(pECtx,
586 "RFC",
587 UsefulBuf_FROM_SZ_LITERAL("https://tools.ietf.org/html/rfc7049#section-2.4.5"));
588 QCBOREncode_AddURIToMapN(pECtx, 0x89, UsefulBuf_FROM_SZ_LITERAL("http://cbor.me/"));
589 QCBOREncode_AddB64TextToMap(pECtx, "whenim64", UsefulBuf_FROM_SZ_LITERAL("cGxlYXN1cmUu"));
590 QCBOREncode_AddB64TextToMapN(pECtx, 64, UsefulBuf_FROM_SZ_LITERAL("c3VyZS4="));
591 QCBOREncode_AddRegexToMap(pECtx, "popo", UsefulBuf_FROM_SZ_LITERAL("100\\s*mk")); // x code string literal bug
592 QCBOREncode_AddRegexToMapN(pECtx, -51, UsefulBuf_FROM_SZ_LITERAL("perl\\B")); // x code string literal bug
593 QCBOREncode_AddMIMEDataToMap(pECtx, "Ned", UsefulBuf_FromSZ(szMIME));
594 QCBOREncode_AddMIMEDataToMapN(pECtx, 10, UsefulBuf_FromSZ(szMIME));
595 QCBOREncode_CloseMap(pECtx);
596
597 /* Date strings */
598 QCBOREncode_AddDateString(pECtx, "2003-12-13T18:30:02Z");
599 QCBOREncode_OpenMap(pECtx);
600 QCBOREncode_AddDateStringToMap(pECtx, "Bed time", "2003-12-13T18:30:02.25+01:00");
601 QCBOREncode_AddDateStringToMapN(pECtx, 88, "2003-12-13T18:30:02.25+01:00");
602 QCBOREncode_CloseMap(pECtx);
603
604 /* true / false ... */
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700605 QCBOREncode_AddUndef(pECtx);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700606 QCBOREncode_OpenMap(pECtx);
607 QCBOREncode_AddSZString(pECtx, "dare");
608 QCBOREncode_AddTag(pECtx, 66);
609 QCBOREncode_AddBool(pECtx, true);
610 QCBOREncode_AddBoolToMap(pECtx, "uu", false);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700611 QCBOREncode_AddNULLToMapN(pECtx, 737634);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700612 QCBOREncode_CloseMap(pECtx);
613
614 /* opening an array */
615 QCBOREncode_OpenArray(pECtx);
616 QCBOREncode_CloseArray(pECtx);
617
618 /* opening arrays in a map */
619 QCBOREncode_OpenMap(pECtx);
620 QCBOREncode_AddSZString(pECtx, "label and tagged empty array");
621 QCBOREncode_AddTag(pECtx, 1093);
622 QCBOREncode_OpenArray(pECtx);
623 QCBOREncode_CloseArray(pECtx);
624 QCBOREncode_OpenArrayInMap(pECtx, "alabl");
625 QCBOREncode_CloseArray(pECtx);
626 QCBOREncode_OpenArrayInMapN(pECtx, 42);
627 QCBOREncode_CloseArray(pECtx);
628 QCBOREncode_CloseMap(pECtx);
629
630 /* opening maps with labels and tagging */
631 QCBOREncode_OpenMap(pECtx);
632 QCBOREncode_OpenMapInMap(pECtx, "in a map");
633 QCBOREncode_OpenMapInMapN(pECtx, 5556);
634 QCBOREncode_AddSZString(pECtx, "in a in a in a");
635 QCBOREncode_AddTag(pECtx, 9087);
636 QCBOREncode_OpenMap(pECtx);
637 QCBOREncode_CloseMap(pECtx);
638 QCBOREncode_CloseMap(pECtx);
639 QCBOREncode_CloseMap(pECtx);
640 QCBOREncode_CloseMap(pECtx);
641
642 /* Extended simple values (these are not standard...) */
643 QCBOREncode_OpenMap(pECtx);
644 QCBOREncode_AddSZString(pECtx, "s1");
645 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700646 QCBOREncode_AddSimple(pECtx, 255);
647 QCBOREncode_AddSimpleToMap(pECtx, "s2", 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700648 QCBOREncode_AddSZString(pECtx, "s3");
649 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700650 QCBOREncode_AddSimple(pECtx, 33);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700651 QCBOREncode_AddInt64(pECtx, 88378374); // label before tag
652 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700653 QCBOREncode_AddSimple(pECtx, 255);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700654 QCBOREncode_AddInt64(pECtx, 89); // label before tag
655 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700656 QCBOREncode_AddSimple(pECtx, 19);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700657 QCBOREncode_CloseMap(pECtx);
658
659 /* UUIDs */
660 static const uint8_t ppppUUID[] = {0x53, 0x4D, 0x41, 0x52, 0x54, 0x43,
661 0x53, 0x4C, 0x54, 0x54, 0x43, 0x46,
662 0x49, 0x43, 0x41, 0x32};
663 const UsefulBufC XXUUID = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(ppppUUID);
664 QCBOREncode_AddBinaryUUID(pECtx, XXUUID);
665 QCBOREncode_OpenMap(pECtx);
666 QCBOREncode_AddBinaryUUIDToMap(pECtx, "UUUU", XXUUID);
667 QCBOREncode_AddBinaryUUIDToMapN(pECtx, 99, XXUUID);
668 QCBOREncode_CloseMap(pECtx);
669
670 /* Bool */
671 QCBOREncode_AddBool(pECtx, true);
672 QCBOREncode_AddBool(pECtx, false);
673 QCBOREncode_OpenMap(pECtx);
674 QCBOREncode_AddBoolToMap(pECtx, "George is the man", true);
675 QCBOREncode_AddBoolToMapN(pECtx, 010101, true);
676 QCBOREncode_CloseMap(pECtx);
677
678 /* Big numbers */
679 static const uint8_t pBignum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
680 const UsefulBufC BIGNUM = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBignum);
681 QCBOREncode_AddPositiveBignum(pECtx, BIGNUM);
682 QCBOREncode_AddNegativeBignum(pECtx, BIGNUM);
683 QCBOREncode_OpenMap(pECtx);
684 QCBOREncode_AddPositiveBignumToMap(pECtx, "BN+", BIGNUM);
685 QCBOREncode_AddPositiveBignumToMapN(pECtx, 64, BIGNUM);
686 QCBOREncode_AddNegativeBignumToMap(pECtx, "BN-", BIGNUM);
687 QCBOREncode_AddNegativeBignumToMapN(pECtx, -64, BIGNUM);
688 QCBOREncode_CloseMap(pECtx);
689
690 QCBOREncode_CloseArray(pECtx);
691}
692
693
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300694int32_t AllAddMethodsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800695{
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700696 /* Improvement: this test should be broken down into several so it is more
697 * managable. Tags and labels could be more sensible */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800698 QCBOREncodeContext ECtx;
699 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800700
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530701 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800702
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700703 AddAll (&ECtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530705 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700706 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800707 nReturn = -1;
708 goto Done;
709 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800710
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700711 if(CheckResults(Enc, spExpectedEncodedAll)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530712 nReturn = -2;
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700713 }
714
715
716 /* Also test size calculation */
717 QCBOREncode_Init(&ECtx, SizeCalculateUsefulBuf);
718
719 AddAll (&ECtx);
720
721 size_t size;
722 if(QCBOREncode_FinishGetSize(&ECtx, &size)) {
723 nReturn = -10;
724 goto Done;
725 }
726
727 if(size != sizeof(spExpectedEncodedAll)) {
728 nReturn = -11;
729 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800730
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800731Done:
732 return nReturn;
733}
734
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700735
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530736/*
Jan Jongboom5d827882019-08-07 12:51:15 +0200737 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530738 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
739 3B 0000000100000000 # negative(4294967296)
740 3A FFFFFFFF # negative(4294967295)
741 3A FFFFFFFE # negative(4294967294)
742 3A FFFFFFFD # negative(4294967293)
743 3A 7FFFFFFF # negative(2147483647)
744 3A 7FFFFFFE # negative(2147483646)
745 3A 00010001 # negative(65537)
746 3A 00010000 # negative(65536)
747 39 FFFF # negative(65535)
748 39 FFFE # negative(65534)
749 39 FFFD # negative(65533)
750 39 0100 # negative(256)
751 38 FF # negative(255)
752 38 FE # negative(254)
753 38 FD # negative(253)
754 38 18 # negative(24)
755 37 # negative(23)
756 36 # negative(22)
757 20 # negative(0)
758 00 # unsigned(0)
759 00 # unsigned(0)
760 01 # unsigned(1)
761 16 # unsigned(22)
762 17 # unsigned(23)
763 18 18 # unsigned(24)
764 18 19 # unsigned(25)
765 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +0200766 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530767 18 FE # unsigned(254)
768 18 FF # unsigned(255)
769 19 0100 # unsigned(256)
770 19 0101 # unsigned(257)
771 19 FFFE # unsigned(65534)
772 19 FFFF # unsigned(65535)
773 1A 00010000 # unsigned(65536)
774 1A 00010001 # unsigned(65537)
775 1A 00010002 # unsigned(65538)
776 1A 7FFFFFFF # unsigned(2147483647)
777 1A 7FFFFFFF # unsigned(2147483647)
778 1A 80000000 # unsigned(2147483648)
779 1A 80000001 # unsigned(2147483649)
780 1A FFFFFFFE # unsigned(4294967294)
781 1A FFFFFFFF # unsigned(4294967295)
782 1B 0000000100000000 # unsigned(4294967296)
783 1B 0000000100000001 # unsigned(4294967297)
784 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
785 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
786 */
787static const uint8_t spExpectedEncodedInts[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +0200788 0x98, 0x30, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800789 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
790 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
791 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
792 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
793 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
794 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
795 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
796 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
797 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
798 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +0200799 0x1a, 0x18, 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19,
800 0x01, 0x00, 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe,
801 0x19, 0xff, 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00,
802 0x1a, 0x00, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01,
803 0x00, 0x02, 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a,
804 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00,
805 0x00, 0x1a, 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff,
806 0xff, 0xff, 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff,
807 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
808 0x00, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
809 0x00, 0x01, 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff,
810 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff,
811 0xff, 0xff, 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800812
813/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800814
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800815 Test the generation of integers. This also ends up testing
816 encoding of all the different lengths. It encodes integers
817 of many lengths and values, especially around the boundaries
818 for different types of integers. It compares the output
819 to expected values generated from http://cbor.me.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800820
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800821 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300822int32_t IntegerValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800823{
824 QCBOREncodeContext ECtx;
825 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800826
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530827 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800828 QCBOREncode_OpenArray(&ECtx);
829
830 QCBOREncode_AddInt64(&ECtx, -9223372036854775807LL - 1);
831 QCBOREncode_AddInt64(&ECtx, -4294967297);
832 QCBOREncode_AddInt64(&ECtx, -4294967296);
833 QCBOREncode_AddInt64(&ECtx, -4294967295);
834 QCBOREncode_AddInt64(&ECtx, -4294967294);
835 QCBOREncode_AddInt64(&ECtx, -2147483648);
836 QCBOREncode_AddInt64(&ECtx, -2147483647);
837 QCBOREncode_AddInt64(&ECtx, -65538);
838 QCBOREncode_AddInt64(&ECtx, -65537);
839 QCBOREncode_AddInt64(&ECtx, -65536);
840 QCBOREncode_AddInt64(&ECtx, -65535);
841 QCBOREncode_AddInt64(&ECtx, -65534);
842 QCBOREncode_AddInt64(&ECtx, -257);
843 QCBOREncode_AddInt64(&ECtx, -256);
844 QCBOREncode_AddInt64(&ECtx, -255);
845 QCBOREncode_AddInt64(&ECtx, -254);
846 QCBOREncode_AddInt64(&ECtx, -25);
847 QCBOREncode_AddInt64(&ECtx, -24);
848 QCBOREncode_AddInt64(&ECtx, -23);
849 QCBOREncode_AddInt64(&ECtx, -1);
850 QCBOREncode_AddInt64(&ECtx, 0);
851 QCBOREncode_AddUInt64(&ECtx, 0ULL);
852 QCBOREncode_AddInt64(&ECtx, 1);
853 QCBOREncode_AddInt64(&ECtx, 22);
854 QCBOREncode_AddInt64(&ECtx, 23);
855 QCBOREncode_AddInt64(&ECtx, 24);
856 QCBOREncode_AddInt64(&ECtx, 25);
857 QCBOREncode_AddInt64(&ECtx, 26);
Jan Jongboom5d827882019-08-07 12:51:15 +0200858 QCBOREncode_AddInt64(&ECtx, 31);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800859 QCBOREncode_AddInt64(&ECtx, 254);
860 QCBOREncode_AddInt64(&ECtx, 255);
861 QCBOREncode_AddInt64(&ECtx, 256);
862 QCBOREncode_AddInt64(&ECtx, 257);
863 QCBOREncode_AddInt64(&ECtx, 65534);
864 QCBOREncode_AddInt64(&ECtx, 65535);
865 QCBOREncode_AddInt64(&ECtx, 65536);
866 QCBOREncode_AddInt64(&ECtx, 65537);
867 QCBOREncode_AddInt64(&ECtx, 65538);
868 QCBOREncode_AddInt64(&ECtx, 2147483647);
869 QCBOREncode_AddInt64(&ECtx, 2147483647);
870 QCBOREncode_AddInt64(&ECtx, 2147483648);
871 QCBOREncode_AddInt64(&ECtx, 2147483649);
872 QCBOREncode_AddInt64(&ECtx, 4294967294);
873 QCBOREncode_AddInt64(&ECtx, 4294967295);
874 QCBOREncode_AddInt64(&ECtx, 4294967296);
875 QCBOREncode_AddInt64(&ECtx, 4294967297);
876 QCBOREncode_AddInt64(&ECtx, 9223372036854775807LL);
877 QCBOREncode_AddUInt64(&ECtx, 18446744073709551615ULL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800878
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800879 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530881 UsefulBufC Enc;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700882 if(QCBOREncode_Finish(&ECtx, &Enc)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800883 nReturn = -1;
884 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800885
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530886 if(CheckResults(Enc, spExpectedEncodedInts))
887 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800888
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800889 return(nReturn);
890}
891
892
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530893/*
894 85 # array(5)
895 F5 # primitive(21)
896 F4 # primitive(20)
897 F6 # primitive(22)
898 F7 # primitive(23)
899 A1 # map(1)
900 65 # text(5)
901 554E446566 # "UNDef"
902 F7 # primitive(23)
903 */
904static const uint8_t spExpectedEncodedSimple[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800905 0x85, 0xf5, 0xf4, 0xf6, 0xf7, 0xa1, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7};
906
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300907int32_t SimpleValuesTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800908{
909 QCBOREncodeContext ECtx;
910 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800911
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530912 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800913 QCBOREncode_OpenArray(&ECtx);
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +0900914
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700915 QCBOREncode_AddBool(&ECtx, true);
916 QCBOREncode_AddBool(&ECtx, false);
917 QCBOREncode_AddNULL(&ECtx);
918 QCBOREncode_AddUndef(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800919
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800920 QCBOREncode_OpenMap(&ECtx);
921
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700922 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800923 QCBOREncode_CloseMap(&ECtx);
924
925 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800926
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530927 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +0700928 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800929 nReturn = -1;
930 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800931
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530932 if(CheckResults(ECBOR, spExpectedEncodedSimple))
933 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800934
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800935 return(nReturn);
936}
937
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700938#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Jan Jongboom47d86c52019-07-25 08:54:16 +0200939/*
940 9F # array(5)
941 F5 # primitive(21)
942 F4 # primitive(20)
943 F6 # primitive(22)
944 F7 # primitive(23)
945 BF # map(1)
946 65 # text(5)
947 554E446566 # "UNDef"
948 F7 # primitive(23)
949 FF # break
950 FF # break
951 */
952static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
953 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
954
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300955int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +0200956{
957 QCBOREncodeContext ECtx;
958 int nReturn = 0;
959
960 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
961 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
962
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700963 QCBOREncode_AddBool(&ECtx, true);
964 QCBOREncode_AddBool(&ECtx, false);
965 QCBOREncode_AddNULL(&ECtx);
966 QCBOREncode_AddUndef(&ECtx);
Jan Jongboom47d86c52019-07-25 08:54:16 +0200967
968 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
969
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700970 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Jan Jongboom47d86c52019-07-25 08:54:16 +0200971 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
972
973 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
974
975 UsefulBufC ECBOR;
976 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
977 nReturn = -1;
978 }
979
980 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
981 return -2;
982
983 return(nReturn);
984}
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700985#endif
Jan Jongboom47d86c52019-07-25 08:54:16 +0200986
Jan Jongboom5d827882019-08-07 12:51:15 +0200987/*
988A5 # map(5)
989 63 # text(3)
990 617272 # "arr"
991 98 1F # array(31)
992 00 # unsigned(0)
993 01 # unsigned(1)
994 02 # unsigned(2)
995 03 # unsigned(3)
996 04 # unsigned(4)
997 05 # unsigned(5)
998 06 # unsigned(6)
999 07 # unsigned(7)
1000 08 # unsigned(8)
1001 09 # unsigned(9)
1002 0A # unsigned(10)
1003 0B # unsigned(11)
1004 0C # unsigned(12)
1005 0D # unsigned(13)
1006 0E # unsigned(14)
1007 0F # unsigned(15)
1008 10 # unsigned(16)
1009 11 # unsigned(17)
1010 12 # unsigned(18)
1011 13 # unsigned(19)
1012 14 # unsigned(20)
1013 15 # unsigned(21)
1014 16 # unsigned(22)
1015 17 # unsigned(23)
1016 18 18 # unsigned(24)
1017 18 19 # unsigned(25)
1018 18 1A # unsigned(26)
1019 18 1B # unsigned(27)
1020 18 1C # unsigned(28)
1021 18 1D # unsigned(29)
1022 18 1E # unsigned(30)
1023 63 # text(3)
1024 6D6170 # "map"
1025 B8 1F # map(31)
1026 61 # text(1)
1027 61 # "a"
1028 00 # unsigned(0)
1029 61 # text(1)
1030 62 # "b"
1031 01 # unsigned(1)
1032 61 # text(1)
1033 63 # "c"
1034 02 # unsigned(2)
1035 61 # text(1)
1036 64 # "d"
1037 03 # unsigned(3)
1038 61 # text(1)
1039 65 # "e"
1040 04 # unsigned(4)
1041 61 # text(1)
1042 66 # "f"
1043 05 # unsigned(5)
1044 61 # text(1)
1045 67 # "g"
1046 06 # unsigned(6)
1047 61 # text(1)
1048 68 # "h"
1049 07 # unsigned(7)
1050 61 # text(1)
1051 69 # "i"
1052 08 # unsigned(8)
1053 61 # text(1)
1054 6A # "j"
1055 09 # unsigned(9)
1056 61 # text(1)
1057 6B # "k"
1058 0A # unsigned(10)
1059 61 # text(1)
1060 6C # "l"
1061 0B # unsigned(11)
1062 61 # text(1)
1063 6D # "m"
1064 0C # unsigned(12)
1065 61 # text(1)
1066 6E # "n"
1067 0D # unsigned(13)
1068 61 # text(1)
1069 6F # "o"
1070 0E # unsigned(14)
1071 61 # text(1)
1072 70 # "p"
1073 0F # unsigned(15)
1074 61 # text(1)
1075 71 # "q"
1076 10 # unsigned(16)
1077 61 # text(1)
1078 72 # "r"
1079 11 # unsigned(17)
1080 61 # text(1)
1081 73 # "s"
1082 12 # unsigned(18)
1083 61 # text(1)
1084 74 # "t"
1085 13 # unsigned(19)
1086 61 # text(1)
1087 75 # "u"
1088 14 # unsigned(20)
1089 61 # text(1)
1090 76 # "v"
1091 15 # unsigned(21)
1092 61 # text(1)
1093 77 # "w"
1094 16 # unsigned(22)
1095 61 # text(1)
1096 78 # "x"
1097 17 # unsigned(23)
1098 61 # text(1)
1099 79 # "y"
1100 18 18 # unsigned(24)
1101 61 # text(1)
1102 7A # "z"
1103 18 19 # unsigned(25)
1104 61 # text(1)
1105 41 # "A"
1106 18 1A # unsigned(26)
1107 61 # text(1)
1108 42 # "B"
1109 18 1B # unsigned(27)
1110 61 # text(1)
1111 43 # "C"
1112 18 1C # unsigned(28)
1113 61 # text(1)
1114 44 # "D"
1115 18 1D # unsigned(29)
1116 61 # text(1)
1117 45 # "E"
1118 18 1E # unsigned(30)
1119 65 # text(5)
1120 6D696E3331 # "min31"
1121 38 1E # negative(30)
1122 66 # text(6)
1123 706C75733331 # "plus31"
1124 18 1F # unsigned(31)
1125 63 # text(3)
1126 737472 # "str"
1127 78 1F # text(31)
1128 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1129 */
1130static const uint8_t EncodeLengthThirtyone[] = {
1131 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1132 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1133 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1134 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1135 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1136 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1137 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1138 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1139 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1140 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1141 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1142 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1143 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1144 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1145 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1146 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1147 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1148 0x31
1149};
1150
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001151int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001152{
1153 QCBOREncodeContext ECtx;
1154 int nReturn = 0;
1155
1156 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1157 QCBOREncode_OpenMap(&ECtx);
1158
1159 // add array with 31 items
1160 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1161 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001162 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001163 }
1164 QCBOREncode_CloseArray(&ECtx);
1165
1166 // add map with 31 items
1167 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001168 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001169 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001170 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001171 if (ix < 26) c = c + ix;
1172 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001173 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001174 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1175 }
1176 QCBOREncode_CloseMap(&ECtx);
1177
1178 // add -31 and +31
1179 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1180 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1181
1182 // add string with length 31
1183 const char *str = "testtesttesttesttesttestqcbor11";
1184 UsefulBufC str_b = { str, 31 };
1185 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1186
1187 QCBOREncode_CloseMap(&ECtx);
1188
1189 UsefulBufC ECBOR;
1190 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1191 nReturn = -1;
1192 }
1193
1194 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1195 return -2;
1196
1197 return(nReturn);
1198}
1199
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301200
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301201/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001202 * [ "2013-03-21T20:04:00Z",
1203 * 0("2013-03-21T20:04:00Z"),
1204 * 1363896240,
1205 * 1(1363896240),
1206 * 100(-10676),
1207 * 3994,
1208 * 1004("1940-10-09"),
1209 * "1980-12-08",
1210 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1211 * "SD": 1(999),
1212 * "Sample Date from RFC 8943": "1985-04-12",
1213 * 42: 1004("1985-04-12T23:20:50.52Z"),
1214 * "SY": 100(-10676),
1215 * 45: 3994
1216 * }
1217 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301218 */
1219static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001220 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1221 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1222 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1223 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1224 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1225 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1226 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1227 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1228 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1229 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1230 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1231 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1232 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1233 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1234 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1235 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1236 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1237 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1238 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1239 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1240 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1241 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1242 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001243
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001244int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001245{
1246 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001247
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301248 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001250 QCBOREncode_OpenArray(&ECtx);
1251
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001252 /* The values are taken from the CBOR RFCs */
1253 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1254 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1255 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1256 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1257 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1258 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1259 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1260 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001261
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001262 QCBOREncode_OpenMap(&ECtx);
1263
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001264 QCBOREncode_AddDateStringToMap(&ECtx,
1265 "Sample Date from RFC 3339",
1266 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001267 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001268 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1269 "Sample Date from RFC 8943",
1270 QCBOR_ENCODE_AS_BORROWED,
1271 "1985-04-12");
1272 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1273 42,
1274 QCBOR_ENCODE_AS_TAG,
1275 "1985-04-12T23:20:50.52Z");
1276 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1277 "SY",
1278 QCBOR_ENCODE_AS_TAG,
1279 -10676);
1280 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1281 45,
1282 QCBOR_ENCODE_AS_BORROWED,
1283 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001284
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001285 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001286
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001287 QCBOREncode_CloseArray(&ECtx);
1288
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301289 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001290 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001291 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001292 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001293
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301294 if(CheckResults(ECBOR, spExpectedEncodedDates))
1295 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001296
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001297 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001298}
1299
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301300
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001301int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001302{
1303 QCBOREncodeContext ECtx;
1304 int i;
1305 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001306
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301307 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001308 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1309 QCBOREncode_OpenArray(&ECtx);
1310 }
1311 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1312 QCBOREncode_CloseArray(&ECtx);
1313 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001314 UsefulBufC Encoded;
1315 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001316 nReturn = -1;
1317 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001318
1319 return(nReturn);
1320}
1321
1322
1323
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001324int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001325{
1326 QCBOREncodeContext ECtx;
1327 int i;
1328 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001329
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301330 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001331 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1332 QCBOREncode_OpenArray(&ECtx);
1333 }
1334 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1335 QCBOREncode_CloseArray(&ECtx);
1336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001337
Laurence Lundblade0595e932018-11-02 22:22:47 +07001338 UsefulBufC Encoded;
1339 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001340 nReturn = -1;
1341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001343 return(nReturn);
1344}
1345
1346
1347
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001348int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349{
1350 QCBOREncodeContext ECtx;
1351 int i;
1352 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001353
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301354 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001355 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1356 QCBOREncode_OpenArray(&ECtx);
1357 }
1358 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1359 QCBOREncode_CloseArray(&ECtx);
1360 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001361 UsefulBufC Encoded;
1362 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363 nReturn = -1;
1364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001365
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001366 return(nReturn);
1367}
1368
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301370/*
1371 81 # array(1)
1372 81 # array(1)
1373 81 # array(1)
1374 81 # array(1)
1375 80 # array(0)
1376*/
1377static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001378
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001379// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301380/*
1381 82 # array(2)
1382 81 # array(1)
1383 81 # array(1)
1384 81 # array(1)
1385 81 # array(1)
1386 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001387 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301388 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1389 3B 0000000100000000 # negative(4294967296)
1390 3A FFFFFFFF # negative(4294967295)
1391 3A FFFFFFFE # negative(4294967294)
1392 3A FFFFFFFD # negative(4294967293)
1393 3A 7FFFFFFF # negative(2147483647)
1394 3A 7FFFFFFE # negative(2147483646)
1395 3A 00010001 # negative(65537)
1396 3A 00010000 # negative(65536)
1397 39 FFFF # negative(65535)
1398 39 FFFE # negative(65534)
1399 39 FFFD # negative(65533)
1400 39 0100 # negative(256)
1401 38 FF # negative(255)
1402 38 FE # negative(254)
1403 38 FD # negative(253)
1404 38 18 # negative(24)
1405 37 # negative(23)
1406 36 # negative(22)
1407 20 # negative(0)
1408 00 # unsigned(0)
1409 00 # unsigned(0)
1410 01 # unsigned(1)
1411 16 # unsigned(22)
1412 17 # unsigned(23)
1413 18 18 # unsigned(24)
1414 18 19 # unsigned(25)
1415 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001416 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301417 18 FE # unsigned(254)
1418 18 FF # unsigned(255)
1419 19 0100 # unsigned(256)
1420 19 0101 # unsigned(257)
1421 19 FFFE # unsigned(65534)
1422 19 FFFF # unsigned(65535)
1423 1A 00010000 # unsigned(65536)
1424 1A 00010001 # unsigned(65537)
1425 1A 00010002 # unsigned(65538)
1426 1A 7FFFFFFF # unsigned(2147483647)
1427 1A 7FFFFFFF # unsigned(2147483647)
1428 1A 80000000 # unsigned(2147483648)
1429 1A 80000001 # unsigned(2147483649)
1430 1A FFFFFFFE # unsigned(4294967294)
1431 1A FFFFFFFF # unsigned(4294967295)
1432 1B 0000000100000000 # unsigned(4294967296)
1433 1B 0000000100000001 # unsigned(4294967297)
1434 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1435 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1436 */
1437static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001438 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001439 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1440 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1441 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1442 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1443 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1444 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1445 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1446 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1447 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1448 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1449 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001450 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1451 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1452 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1453 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1454 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1455 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1456 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1457 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1458 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1459 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1460 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1461 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1462 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463
1464
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001465int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001466{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001467 QCBOREncodeContext ECtx;
1468
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301469 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001470 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301471 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1472 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001473 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001474
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001475 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001476
Laurence Lundblade0595e932018-11-02 22:22:47 +07001477 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001478 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001479 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001480
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301481 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001482 return -5;
1483 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001484
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001485 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001486}
1487
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301488/*
1489 This returns a pointer to spBigBuf
1490 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001491static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492{
1493 QCBOREncodeContext ECtx;
1494 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001495
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001496 *pEncoded = NULL;
1497 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301498 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001499
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001500 // loop runs CBOR encoding twice. First with no buffer to
1501 // calucate the length so buffer can be allocated correctly,
1502 // and last with the buffer to do the actual encoding
1503 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301504 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001505 QCBOREncode_OpenMap(&ECtx);
1506 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1507 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1508 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1509 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1510 QCBOREncode_CloseArray(&ECtx);
1511 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1512 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1513 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1514 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1515 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1516 QCBOREncode_CloseMap(&ECtx);
1517 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001518
Laurence Lundblade0595e932018-11-02 22:22:47 +07001519 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001520 goto Done;
1521 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301522 if(uFirstSizeEstimate != *pEncodedLen) {
1523 nReturn = 1;
1524 } else {
1525 nReturn = 0;
1526 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001527 goto Done;
1528 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301529 *pEncoded = spBigBuf;
1530 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001531
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001532 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001534 Done:
1535 return(nReturn);
1536}
1537
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301538/*
1539 A3 # map(3)
1540 6D # text(13)
1541 666972737420696E7465676572 # "first integer"
1542 18 2A # unsigned(42)
1543 77 # text(23)
1544 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1545 82 # array(2)
1546 67 # text(7)
1547 737472696E6731 # "string1"
1548 67 # text(7)
1549 737472696E6732 # "string2"
1550 6C # text(12)
1551 6D617020696E2061206D6170 # "map in a map"
1552 A4 # map(4)
1553 67 # text(7)
1554 62797465732031 # "bytes 1"
1555 44 # bytes(4)
1556 78787878 # "xxxx"
1557 67 # text(7)
1558 62797465732032 # "bytes 2"
1559 44 # bytes(4)
1560 79797979 # "yyyy"
1561 6B # text(11)
1562 616E6F7468657220696E74 # "another int"
1563 18 62 # unsigned(98)
1564 66 # text(6)
1565 746578742032 # "text 2"
1566 78 1E # text(30)
1567 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1568 */
1569static const uint8_t spValidMapEncoded[] = {
1570 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1571 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1572 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1573 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1574 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1575 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1576 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1577 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1578 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1579 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1580 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1581 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1582 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1583 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1584 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1585 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001586
1587
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001588int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001589{
1590 uint8_t *pEncodedMaps;
1591 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001592
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001593 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301594 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001595 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001596
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001597 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301598 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1599 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001601 return(nReturn);
1602}
1603
1604
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001605/*
1606 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001607
Laurence Lundbladeee851742020-01-08 08:37:05 -08001608 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1609 CBOR_SIMPLEV_NULL
1610 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001611 @param[in] szAlexString Diagnostic code.
1612 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001613 @param[in/out] pnLen Size of pOut buffer when called; length of data
1614 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001615
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001616 @return
1617 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001618
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001619 The size of pOut should be 30 bytes plus the length of pnLen. If you make it too
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001620 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001621 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001622
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001623 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1624 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001625
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001626 */
1627
Laurence Lundbladeee851742020-01-08 08:37:05 -08001628static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001629FormatRTICResults(uint8_t uRResult,
1630 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001631 const char *szType,
1632 const char *szAlexString,
1633 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001634{
1635 // Buffer that the result will be written in to
1636 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001637 // QCBOREncode will never write off the end of this buffer. If it won't
1638 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001639
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001640 // Context for the encoder
1641 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301642 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001644 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1645 // Contents are label / value pairs
1646 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001648 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001649
Laurence Lundbladeee851742020-01-08 08:37:05 -08001650 // The result: 0 if scan happened and found nothing; 1 if it happened and
1651 // found something wrong; 2 if it didn't happen
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07001652 QCBOREncode_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001653
1654 // Add the diagnostic code
1655 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1656
1657 // Add a time stamp
1658 if(time) {
1659 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1660 }
1661
1662 // Add the diagnostic code
1663 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1664
1665 // Open a subordinate map for telemtry data
1666 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1667
1668 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001670 // Add a few fake integers and buffers for now.
1671 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1672
1673 // Add a few fake integers and buffers for now.
1674 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1675
1676 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301677 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001678 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001679
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001680 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1681 }
1682 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001683
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001684 // Close the telemetry map
1685 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001686
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001687 // Close the map
1688 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001689
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301690 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001691
Laurence Lundblade0595e932018-11-02 22:22:47 +07001692 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001693
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301694 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001695}
1696
1697
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301698/*
1699 A5 # map(5)
1700 69 # text(9)
1701 696E74656772697479 # "integrity"
1702 F4 # primitive(20)
1703 64 # text(4)
1704 74797065 # "type"
1705 66 # text(6)
1706 726563656E74 # "recent"
1707 64 # text(4)
1708 74696D65 # "time"
1709 C1 # tag(1)
1710 1A 580D4172 # unsigned(1477263730)
1711 64 # text(4)
1712 64696167 # "diag"
1713 6A # text(10)
1714 30784131654335303031 # "0xA1eC5001"
1715 69 # text(9)
1716 74656C656D65747279 # "telemetry"
1717 A3 # map(3)
1718 69 # text(9)
1719 53686F652053697A65 # "Shoe Size"
1720 0C # unsigned(12)
1721 62 # text(2)
1722 4951 # "IQ"
1723 1A FFFFFFFF # unsigned(4294967295)
1724 77 # text(23)
1725 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1726 48 # bytes(8)
1727 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1728 */
1729static const uint8_t spExpectedRTIC[] = {
1730 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1731 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1732 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1733 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1734 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1735 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1736 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1737 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1738 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1739 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1740 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1741 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001742
1743
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001744int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001745{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001746 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301747 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301748 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301749 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001750 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301751 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001752
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301753 if(CheckResults(Encoded, spExpectedRTIC)) {
1754 return -2;
1755 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001756
1757 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001758}
1759
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301760
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301761/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001762 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001763
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301764 82 # array(2)
1765 19 01C3 # unsigned(451)
1766 43 # bytes(3)
1767 1901D2 # "\x19\x01\xD2"
1768*/
1769static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301770
Laurence Lundblade684aec22018-10-12 19:33:53 +08001771/*
Laurence Lundbladeda532272019-04-07 11:40:17 -07001772 81 #array(1)
1773 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes")
1774 */
1775static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25};
1776
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001777static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1778
Laurence Lundbladeda532272019-04-07 11:40:17 -07001779/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001780 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001781 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001782int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001783{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001784 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001785
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001786 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301787 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001788
Laurence Lundblade684aec22018-10-12 19:33:53 +08001789 QCBOREncode_OpenArray(&EC);
1790 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001791
Laurence Lundblade684aec22018-10-12 19:33:53 +08001792 QCBOREncode_BstrWrap(&EC);
1793 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001794
Laurence Lundblade684aec22018-10-12 19:33:53 +08001795 UsefulBufC Wrapped;
1796 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001797
Laurence Lundblade684aec22018-10-12 19:33:53 +08001798 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001799
Laurence Lundblade684aec22018-10-12 19:33:53 +08001800 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001801 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001802 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001803 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001804
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301805 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001806 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001807 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001808
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001809 // Second test - see if the length of the wrapped
1810 // bstr is correct. Also tests bstr wrapping
1811 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001812 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1813 QCBOREncode_OpenArray(&EC);
1814 QCBOREncode_BstrWrap(&EC);
1815 QCBOREncode_OpenArray(&EC);
1816 QCBOREncode_AddNULL(&EC);
1817 QCBOREncode_CloseArray(&EC);
1818 UsefulBufC BStr;
1819 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001820 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1821 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001822 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001823 return -5;
1824 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001825
Laurence Lundbladeda532272019-04-07 11:40:17 -07001826 // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the
1827 // bstr wrapping use cases.
1828 UsefulBuf_MAKE_STACK_UB(StuffBuf, 50);
1829 QCBOREncode_Init(&EC, StuffBuf);
1830 QCBOREncode_OpenArray(&EC);
1831 QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes"));
1832 QCBOREncode_CloseArray(&EC);
1833 if(QCBOREncode_Finish(&EC, &Encoded)) {
1834 return -6;
1835 }
1836 if(CheckResults(Encoded, spExpectedTypeAndLen)) {
1837 return -7;
1838 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001839
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001840 // Fourth test, cancelling a byte string
1841 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1842
1843 QCBOREncode_OpenArray(&EC);
1844 QCBOREncode_AddUInt64(&EC, 451);
1845
1846 QCBOREncode_BstrWrap(&EC);
1847 QCBOREncode_CancelBstrWrap(&EC);
1848
1849
1850 QCBOREncode_AddUInt64(&EC, 42);
1851 QCBOREncode_CloseArray(&EC);
1852 if(QCBOREncode_Finish(&EC, &Encoded)) {
1853 return -8;
1854 }
1855 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1856 return -9;
1857 }
1858
1859 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001860 // Fifth test, failed cancelling
1861 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1862
1863 QCBOREncode_OpenArray(&EC);
1864 QCBOREncode_AddUInt64(&EC, 451);
1865
1866 QCBOREncode_BstrWrap(&EC);
1867 QCBOREncode_AddUInt64(&EC, 99);
1868 QCBOREncode_CancelBstrWrap(&EC);
1869
1870 QCBOREncode_AddUInt64(&EC, 42);
1871 QCBOREncode_CloseArray(&EC);
1872 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001873#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001874 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1875 return -10;
1876 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001877#else
1878 if(uErr != QCBOR_SUCCESS) {
1879 return -110;
1880 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001881#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1882
1883 // Sixth test, another cancel, but the error is not caught
1884 // This use will produce unintended CBOR. The error
1885 // is not caught because it would require tracking state
1886 // for QCBOREncode_BstrWrapInMapN.
1887 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1888
1889 QCBOREncode_OpenMap(&EC);
1890 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1891
1892 QCBOREncode_BstrWrapInMapN(&EC, 55);
1893 QCBOREncode_CancelBstrWrap(&EC);
1894
1895 QCBOREncode_CloseMap(&EC);
1896 uErr = QCBOREncode_Finish(&EC, &Encoded);
1897 if(uErr != QCBOR_SUCCESS) {
1898 return -11;
1899 }
1900
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001901 return 0;
1902}
1903
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001904
1905
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001906int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001907{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001908 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001909 UsefulBufC Wrapped;
1910 UsefulBufC Encoded2;
1911 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001912
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001913 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001914
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301915 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001916
Laurence Lundblade684aec22018-10-12 19:33:53 +08001917 QCBOREncode_OpenArray(&EC);
1918 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001919
Laurence Lundblade684aec22018-10-12 19:33:53 +08001920 QCBOREncode_BstrWrap(&EC);
1921 QCBOREncode_AddUInt64(&EC, 466);
1922 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001923
Laurence Lundblade684aec22018-10-12 19:33:53 +08001924 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001925
Laurence Lundblade684aec22018-10-12 19:33:53 +08001926 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001927
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001928 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001929#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001930 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001931 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001932 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001933#else
1934 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1935 * is set and not to be sure to excerice all the relavant code in
1936 * both conditions. When the guards are disabled, there is no
1937 * error returned, but the code path is still covered.
1938 */
1939 if(uError != QCBOR_SUCCESS) {
1940 return (int32_t)(600 + uError);
1941 }
1942#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001943
Laurence Lundbladeee851742020-01-08 08:37:05 -08001944 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301945 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001946 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001947 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001948#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001949 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001950 return (int32_t)(700 + uError);
1951 }
1952#else
1953 if(uError != QCBOR_SUCCESS) {
1954 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001955 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001956#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001957
Laurence Lundblade684aec22018-10-12 19:33:53 +08001958 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301959 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001960 for(int i = 1; i < 18; i++) {
1961 QCBOREncode_BstrWrap(&EC);
1962 }
1963 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001964
Laurence Lundblade684aec22018-10-12 19:33:53 +08001965 for(int i = 1; i < 18; i++) {
1966 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1967 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001968
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001969 uError = QCBOREncode_Finish(&EC, &Encoded2);
1970 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001971 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001972 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001973
Laurence Lundblade684aec22018-10-12 19:33:53 +08001974 return 0;
1975}
1976
1977
Laurence Lundblade684aec22018-10-12 19:33:53 +08001978/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001979 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001980
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001981 [
1982 h'82004E82014B8202488203458204428105',
1983 {
1984 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
1985 8426568656C6C6F18234BA2131318436568656C6C6F'
1986 }
1987 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001988
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001989 Unwrapping the first byte string in the above gives
1990 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301991
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001992 Unwrapping again, the byte string immediately above gives
1993 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301994
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001995 ...
1996
1997 Unrapping the second byte string in the top-level CBOR above gives
1998 {16: 16,
1999 64: "hello",
2000 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
2001 }
2002
2003 Unwrapping again, the byte string immediately above gives
2004 {17: 17,
2005 65: "hello",
2006 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2007 }
2008
2009 ...
2010
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302011 */
2012static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002013{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002014 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2015 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2016 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2017 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2018 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2019 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2020 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2021 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2022 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2023 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002024};
2025
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002026
2027/*
2028 Get an int64 out of the decoder or fail.
2029 */
2030static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002031{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002032 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002033 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002034
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002035 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002036 if(nReturn) {
2037 return nReturn;
2038 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002039 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002040 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002041 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002042
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002043 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002044 return 0;
2045}
2046
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002047/*
2048 Get an array out of the decoder or fail.
2049 */
2050static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002051{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002052 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002053 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002054
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002055 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002056 if(nReturn) {
2057 return nReturn;
2058 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002059 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2060 return -1;
2061 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002062
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002063 *pInt = Item.val.uCount;
2064 return 0;
2065}
2066
2067/*
2068 Get a map out of the decoder or fail.
2069 */
2070static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2071{
2072 QCBORItem Item;
2073 int32_t nReturn;
2074
2075 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002076 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002077 return nReturn;
2078 }
2079 if(Item.uDataType != QCBOR_TYPE_MAP) {
2080 return -1;
2081 }
2082
2083 *pInt = Item.val.uCount;
2084 return 0;
2085}
2086
2087/*
2088 Get a byte string out of the decoder or fail.
2089 */
2090static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2091{
2092 QCBORItem Item;
2093 int32_t nReturn;
2094
2095 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2096 if(nReturn) {
2097 return nReturn;
2098 }
2099 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002100 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002101 }
2102
2103 *pBstr = Item.val.string;
2104 return 0;
2105}
2106
2107/*
2108 Get a byte string out of the decoder or fail.
2109 */
2110static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2111{
2112 QCBORItem Item;
2113 int nReturn;
2114
2115 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2116 if(nReturn) {
2117 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002118 }
2119 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002120 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002121 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002122
2123 *pTstr = Item.val.string;
2124 return 0;
2125}
2126
2127
2128/*
2129 Recursively decode array containing a little CBOR and a bstr wrapped array
2130 with a little CBOR and a bstr wrapped array...
2131
2132 Part of bstr_wrap_nest_test.
2133 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2134{
2135 int64_t nInt;
2136 UsefulBufC Bstr;
2137 uint16_t nArrayCount;
2138 QCBORDecodeContext DC;
2139 int32_t nResult;
2140
2141 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2142
2143 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2144 return -10;
2145 }
2146
2147 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002148 return -11;
2149 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002150
2151 nResult = GetByteString(&DC, &Bstr);
2152 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2153 if(nArrayCount != 1) {
2154 return -12;
2155 } else {
2156 // successful exit
2157 return 0;
2158 }
2159 }
2160 if(nResult) {
2161 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002162 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002163
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002164 // tail recursion; good compilers will reuse the stack frame
2165 return DecodeNextNested(Bstr);
2166}
2167
2168
2169/*
2170 Recursively decode map containing a little CBOR and a bstr wrapped map
2171 with a little CBOR and a bstr wrapped map...
2172
2173 Part of bstr_wrap_nest_test.
2174 */
2175static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2176{
2177 int32_t nResult;
2178 uint16_t nMapCount;
2179 int64_t nInt;
2180 UsefulBufC Bstr;
2181 QCBORDecodeContext DC;
2182
2183 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2184
2185 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2186 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002188
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002189 if(GetInt64(&DC, &nInt)) {
2190 return -21;
2191 }
2192
2193 // The "hello"
2194 if(GetTextString(&DC, &Bstr)) {
2195 return -22;
2196 }
2197
2198 nResult = GetByteString(&DC, &Bstr);
2199 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2200 if(nMapCount == 2) {
2201 // successful exit
2202 return 0;
2203 } else {
2204 return -23;
2205 }
2206 }
2207
2208 if(nResult) {
2209 return -24;
2210 }
2211
2212 // tail recursion; good compilers will reuse the stack frame
2213 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002214}
2215
2216
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002217int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002218{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002219 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302220 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002221
Laurence Lundblade684aec22018-10-12 19:33:53 +08002222 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002223 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002224
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002225 QCBOREncode_OpenArray(&EC);
2226
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002227 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002228 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002229 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002230 QCBOREncode_AddInt64(&EC, i);
2231 }
2232 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2233 QCBOREncode_CloseArray(&EC);
2234 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002236
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002237 QCBOREncode_OpenMap(&EC);
2238 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2239 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2240 QCBOREncode_OpenMap(&EC);
2241 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002242 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002243 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002244
2245 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2246 QCBOREncode_CloseMap(&EC);
2247 QCBOREncode_CloseBstrWrap(&EC, NULL);
2248 }
2249 QCBOREncode_CloseMap(&EC);
2250
Laurence Lundblade684aec22018-10-12 19:33:53 +08002251 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002252
Laurence Lundblade684aec22018-10-12 19:33:53 +08002253 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002254 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002255 return -1;
2256 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002257
Laurence Lundblade684aec22018-10-12 19:33:53 +08002258 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302259 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002260 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002262
Laurence Lundblade684aec22018-10-12 19:33:53 +08002263 // ---- Decode it and see if it is OK ------
2264 QCBORDecodeContext DC;
2265 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002266
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002267 UsefulBufC Bstr;
2268 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002269
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002270 // Array surrounding the the whole thing
2271 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002272 return -3;
2273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002274
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002275 // Get the byte string wrapping some array stuff
2276 if(GetByteString(&DC, &Bstr)) {
2277 return -4;
2278 }
2279
2280 // Decode the wrapped nested structure
2281 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002282 if(nReturn) {
2283 return nReturn;
2284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002285
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002286 // A map enclosing some map-oriented bstr wraps
2287 if(GetMap(&DC, &nArrayCount)) {
2288 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002289 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002290
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002291 // Get the byte string wrapping some array stuff
2292 if(GetByteString(&DC, &Bstr)) {
2293 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002296 // Decode the wrapped nested structure
2297 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002298 if(nReturn) {
2299 return nReturn;
2300 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002301
Laurence Lundblade684aec22018-10-12 19:33:53 +08002302 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002303 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002304 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002305
Laurence Lundblade684aec22018-10-12 19:33:53 +08002306 return 0;
2307}
2308
2309
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002310static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302311 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2312 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2313 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2314 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2315 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2316 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2317 0x45, 0xca, 0xcb, 0x36};
2318
2319/*
2320 D2 # tag(18)
2321 84 # array(4)
2322 43 # bytes(3)
2323 A10126 # "\xA1\x01&"
2324 A1 # map(1)
2325 04 # unsigned(4)
2326 42 # bytes(2)
2327 3131 # "11"
2328 54 # bytes(20)
2329 546869732069732074686520636F6E74656E742E # "This is the content."
2330 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002331 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2332 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2333 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2334 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2335 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2336 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2337 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2338 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302339 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002340static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302341 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2342 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2343 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2344 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2345 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2346 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2347 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2348 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2349 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2350 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2351
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002352static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2353
2354
Laurence Lundblade684aec22018-10-12 19:33:53 +08002355/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002356 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002357 C.2.1. This doesn't actually verify the signature (however
2358 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002359 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002360int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002361{
2362 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002363 const char *szKid = "11";
2364 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2365 const char *szPayload = "This is the content.";
2366 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2367 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2368 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundblade684aec22018-10-12 19:33:53 +08002370 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002371
2372 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302373 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002374
Laurence Lundblade684aec22018-10-12 19:33:53 +08002375 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002376 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002377 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002378
Laurence Lundblade684aec22018-10-12 19:33:53 +08002379 // Add protected headers
2380 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002381
Laurence Lundblade684aec22018-10-12 19:33:53 +08002382 // Empty map with unprotected headers
2383 QCBOREncode_OpenMap(&EC);
2384 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2385 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002386
Laurence Lundblade684aec22018-10-12 19:33:53 +08002387 // The payload
2388 UsefulBufC WrappedPayload;
2389 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002390 // Payload is not actually CBOR in example C.2.1 like it would be
2391 // for a CWT or EAT. It is just a text string.
2392 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002393 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
Laurence Lundblade684aec22018-10-12 19:33:53 +08002395 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002396 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2397 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002398 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002399 return -1;
2400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002401
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002402/* if(UsefulBuf_Compare(WrappedPayload,
2403 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2404 return -1;
2405 } */
2406
Laurence Lundblade684aec22018-10-12 19:33:53 +08002407 // The signature
2408 QCBOREncode_AddBytes(&EC, Signature);
2409 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002410
Laurence Lundblade684aec22018-10-12 19:33:53 +08002411 // Finish and check the results
2412 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002413 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002414 return -2;
2415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002416
Laurence Lundblade684aec22018-10-12 19:33:53 +08002417 // 98 is the size from RFC 8152 C.2.1
2418 if(COSE_Sign1.len != 98) {
2419 return -3;
2420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002421
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002422 // It would be good to compare this to the output from a COSE
2423 // implementation like COSE-C. This has been checked against the
2424 // CBOR playground.
2425 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002426 return -4;
2427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002428
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002429
2430 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2431 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2432
2433 // top level array for cose sign1, 18 is the tag for COSE sign
2434 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2435 QCBOREncode_OpenArray(&EC);
2436
2437 // Add protected headers
2438 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2439
2440 // Empty map with unprotected headers
2441 QCBOREncode_OpenMap(&EC);
2442 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2443 QCBOREncode_CloseMap(&EC);
2444
2445 // The payload
2446 QCBOREncode_BstrWrap(&EC);
2447 // Payload is not actually CBOR in example C.2.1 like it would be
2448 // for a CWT or EAT. It is just a text string.
2449 QCBOREncode_AddEncoded(&EC, Payload);
2450 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2451
2452 // Check we got back the actual payload expected
2453 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2454 if(UsefulBuf_Compare(WrappedPayload,
2455 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2456 return -11;
2457 }
2458
2459 // The signature
2460 QCBOREncode_AddBytes(&EC, Signature);
2461 QCBOREncode_CloseArray(&EC);
2462
2463 // Finish and check the results
2464 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2465 return -12;
2466 }
2467
2468 // 98 is the size from RFC 8152 C.2.1
2469 if(COSE_Sign1.len != 98) {
2470 return -13;
2471 }
2472
2473 // It would be good to compare this to the output from a COSE
2474 // implementation like COSE-C. This has been checked against the
2475 // CBOR playground.
2476 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2477 return -14;
2478 }
2479
Laurence Lundblade684aec22018-10-12 19:33:53 +08002480 return 0;
2481}
2482
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002483
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002484int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002485{
2486 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002487 QCBORError uErr;
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07002488 UsefulBufC EncodedResult;
2489 MakeUsefulBufOnStack(SmallBuffer, 4);
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002490
2491
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002492 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002493 // Do all of these tests with NULL buffers so no actual
2494 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002495 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002496
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002497 // First verify no error from a big buffer
2498 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002499 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002500 // 6 is the CBOR overhead for opening the array and encodng the length
2501 // This exactly fills the buffer.
2502 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002503 QCBOREncode_CloseArray(&EC);
2504 size_t xx;
2505 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2506 return -1;
2507 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002508
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002509 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002510 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002511 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002512 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002513 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002514 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002515 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2516 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002517 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002518 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002519 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002520 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2521 return -2;
2522 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002523
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002524 // Third, fit an array in exactly at max position allowed
2525 QCBOREncode_Init(&EC, Buffer);
2526 QCBOREncode_OpenArray(&EC);
2527 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2528 QCBOREncode_OpenArray(&EC);
2529 QCBOREncode_CloseArray(&EC);
2530 QCBOREncode_CloseArray(&EC);
2531 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2532 return -10;
2533 }
2534
2535
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002536 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2537 // Work close to the 4GB size limit for a better test
2538 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002539 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002540
2541 QCBOREncode_Init(&EC, Large);
2542 QCBOREncode_OpenArray(&EC);
2543 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2544 QCBOREncode_CloseArray(&EC);
2545 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2546 // Making sure it succeeds when it should first
2547 return -3;
2548 }
2549
2550 QCBOREncode_Init(&EC, Large);
2551 QCBOREncode_OpenArray(&EC);
2552 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2553 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2554 QCBOREncode_CloseArray(&EC);
2555 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2556 // Now just 1 byte over, see that it fails
2557 return -4;
2558 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002559
2560
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002561 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2562 QCBOREncode_Init(&EC, Large);
2563 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2564 QCBOREncode_OpenArray(&EC);
2565 }
2566 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2567 QCBOREncode_CloseArray(&EC);
2568 }
2569 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2570 // Making sure it succeeds when it should first
2571 return -5;
2572 }
2573
2574 QCBOREncode_Init(&EC, Large);
2575 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2576 QCBOREncode_OpenArray(&EC);
2577 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002578 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002579 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2580 QCBOREncode_CloseArray(&EC);
2581 }
2582 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002583 return -6;
2584 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002585
2586
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002587 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002588 QCBOREncode_Init(&EC, Large);
2589 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2590 QCBOREncode_OpenArray(&EC);
2591 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002592 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002593 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2594 QCBOREncode_CloseArray(&EC);
2595 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002596 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2597#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2598 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002599 return -7;
2600 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002601#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2602 if(uErr != QCBOR_SUCCESS) {
2603 return -107;
2604 }
2605#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002606
2607
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002608 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002609 QCBOREncode_Init(&EC, Large);
2610 QCBOREncode_OpenArray(&EC);
2611 UsefulBufC Wrap;
2612 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002613 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2614#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2615 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002616 return -8;
2617 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002618#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2619 if(uErr != QCBOR_SUCCESS) {
2620 return -108;
2621 }
2622#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002623
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002624 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002625 QCBOREncode_Init(&EC, Large);
2626 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2627 QCBOREncode_OpenArray(&EC);
2628 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002629 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002630 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2631 QCBOREncode_CloseArray(&EC);
2632 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002633
2634 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2635#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2636 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002637 return -9;
2638 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002639#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2640 if(uErr != QCBOR_SUCCESS) {
2641 return -109;
2642 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002643#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002644
Laurence Lundblade241705e2018-12-30 18:56:14 -08002645 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2646 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002647
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002648
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002649 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002650 QCBOREncode_Init(&EC, Buffer);
2651 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2652 return -11;
2653 }
2654
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002655 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002656 QCBOREncode_Init(&EC, Large);
2657 QCBOREncode_OpenArray(&EC);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002658 QCBOREncode_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002659 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2660#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2661 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002662 return -12;
2663 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002664#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2665 if(uErr != QCBOR_SUCCESS) {
2666 return -112;
2667 }
2668#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2669
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002670
2671 QCBOREncode_Init(&EC, Large);
2672 QCBOREncode_OpenArray(&EC);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002673 QCBOREncode_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002674 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2675#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2676 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002677 return -13;
2678 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002679#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2680 if(uErr != QCBOR_SUCCESS) {
2681 return -113;
2682 }
2683#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002684
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07002685 /* Test that still-open error sticks */
2686 QCBOREncode_Init(&EC, Large);
2687 QCBOREncode_OpenArray(&EC);
2688 QCBOREncode_Finish(&EC, &EncodedResult);
2689#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2690 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2691 return -120;
2692 }
2693#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2694 if(QCBOREncode_GetErrorState(&EC) != QCBOR_SUCCESS) {
2695 return -122;
2696 }
2697#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2698
2699 /* Test that too-small error is sticky */
2700 QCBOREncode_Init(&EC, SmallBuffer);
2701 QCBOREncode_OpenArray(&EC);
2702 QCBOREncode_AddInt64(&EC, INT64_MAX);
2703 QCBOREncode_AddInt64(&EC, INT64_MAX);
2704 QCBOREncode_AddInt64(&EC, INT64_MAX);
2705 QCBOREncode_CloseArray(&EC);
2706 QCBOREncode_Finish(&EC, &EncodedResult);
2707 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2708 return -130;
2709 }
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002710
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002711 return 0;
2712}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002713
2714
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002715#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002716/*
2717 [
2718 4([-1, 3]),
2719 4([-20, 4759477275222530853136]),
2720 4([9223372036854775807, -4759477275222530853137]),
2721 5([300, 100]),
2722 5([-20, 4759477275222530853136]),
2723 5([-9223372036854775808, -4759477275222530853137])
2724 ]
2725 */
2726static const uint8_t spExpectedExponentAndMantissaArray[] = {
2727 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2728 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2729 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2730 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2731 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2732 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2733 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2734 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2735 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2736 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2737 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2738
2739
2740/*
2741 {
2742 "decimal fraction": 4([-1, 3]),
2743 300: 4([-1, 3]),
2744 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2745 400: 4([2147483647, 4759477275222530853136]),
2746 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2747 500: 4([9223372036854775807, -4759477275222530853137]),
2748 "big float": 5([300, 100]),
2749 600: 5([300, 100]),
2750 "big float bignum positive": 5([-20, 4759477275222530853136]),
2751 700: 5([-20, 4759477275222530853136]),
2752 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2753 800: 5([-9223372036854775808, -4759477275222530853137])
2754 }
2755 */
2756static const uint8_t spExpectedExponentAndMantissaMap[] = {
2757 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2758 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2759 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2760 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2761 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2762 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2763 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2764 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2765 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2766 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2767 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2768 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2769 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2770 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2771 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2772 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2773 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2774 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2775 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2776 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2777 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2778 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2779 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2780 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2781 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2782 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2783 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2784 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2785 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2786 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2787 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2788 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2789 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2790 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2791 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2792 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2793 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2794 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2795 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2796 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2797 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2798 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2799 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2800 0x09, 0x10
2801};
2802
2803
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002804int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002805{
2806 QCBOREncodeContext EC;
2807 UsefulBufC EncodedExponentAndMantissa;
2808
2809 // Constant for the big number used in all the tests.
2810 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2811 0x06, 0x07, 0x08, 0x09, 0x010};
2812 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2813
2814 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2815 QCBOREncode_OpenArray(&EC);
2816 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2817 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2818 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2819 QCBOREncode_AddBigFloat(&EC, 100, 300);
2820 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2821 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2822 QCBOREncode_CloseArray(&EC);
2823
2824 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2825 return -2;
2826 }
2827
2828 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2829 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2830 NULL);
2831 if(nReturn) {
2832 return nReturn;
2833 }
2834
2835 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2836 QCBOREncode_OpenMap(&EC);
2837
2838 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2839
2840 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2841
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002842 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002843 "decimal fraction bignum postive",
2844 BigNum,
2845 false,
2846 -200);
2847
2848 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2849 400,
2850 BigNum,
2851 false,
2852 INT32_MAX);
2853
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002854 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002855 "decimal fraction bignum negative",
2856 BigNum,
2857 true,
2858 INT64_MAX);
2859
2860 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2861 500,
2862 BigNum,
2863 true,
2864 INT64_MAX);
2865
2866 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2867
2868 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2869
2870 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2871 "big float bignum positive",
2872 BigNum,
2873 false,
2874 -20);
2875
2876 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2877 700,
2878 BigNum,
2879 false,
2880 -20);
2881
2882 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2883 "big float bignum negative",
2884 BigNum,
2885 true,
2886 INT64_MIN);
2887
2888 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2889 800,
2890 BigNum,
2891 true,
2892 INT64_MIN);
2893
2894 QCBOREncode_CloseMap(&EC);
2895
2896 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2897 return -3;
2898 }
2899
2900
2901 struct UBCompareDiagnostic Diag;
2902
2903 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2904 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2905 &Diag);
2906 if(nReturn) {
2907 return nReturn + 1000000; // +1000000 to distinguish from first test above
2908 }
2909
2910 return 0;
2911}
2912
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002913#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002914
2915
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002916int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002917{
2918 /* This test doesn't have to be extensive, because just about every
2919 * other test exercises QCBOREncode_EncodeHead().
2920 */
2921 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002922 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002923
2924 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2925 CBOR_MAJOR_TYPE_POSITIVE_INT,
2926 0,
2927 0);
2928
2929 static const uint8_t expectedZero[] = {0x00};
2930
2931 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2932 return -1;
2933 }
2934
2935 // ---- Encode a zero padded out to an 8 byte integer ----
2936 encoded = QCBOREncode_EncodeHead(RightSize,
2937 CBOR_MAJOR_TYPE_POSITIVE_INT,
2938 8, // uMinSize is 8 bytes
2939 0);
2940
2941 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2942 0x00, 0x00, 0x00, 0x00};
2943
2944 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2945 return -2;
2946 }
2947
2948
2949 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002950 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002951
2952 encoded = QCBOREncode_EncodeHead(TooSmall,
2953 CBOR_MAJOR_TYPE_POSITIVE_INT,
2954 0,
2955 0);
2956
2957 if(!UsefulBuf_IsNULLC(encoded)) {
2958 return -3;
2959 }
2960
2961 return 0;
2962}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002963
2964
2965static const uint8_t spExpectedForOpenBytes[] = {
2966 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2967 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2968 0x78
2969};
2970
2971static const uint8_t spExpectedForOpenBytes2[] = {
2972 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
2973 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
2974 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
2975};
2976
2977int32_t
2978OpenCloseBytesTest(void)
2979{
2980 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
2981 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
2982 QCBOREncodeContext EC;
2983 UsefulBuf Place;
2984 UsefulBufC Encoded;
2985 QCBORError uErr;
2986
2987 /* Normal use case -- add a byte string that fits */
2988 QCBOREncode_Init(&EC, TestBuf);
2989 QCBOREncode_OpenBytes(&EC, &Place);
2990 if(Place.ptr != TestBuf.ptr ||
2991 Place.len != TestBuf.len) {
2992 return 1;
2993 }
2994 Place.len -= 4;
2995 UsefulBuf_Set(Place, 'x');
2996 QCBOREncode_CloseBytes(&EC, Place.len);
2997 QCBOREncode_Finish(&EC, &Encoded);
2998 if(UsefulBuf_Compare(Encoded,
2999 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
3000 return 2;
3001 }
3002
Paul Liétar79789772022-07-26 20:33:18 +01003003 /* Run the same test but with a NULL buffer */
3004 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
3005 QCBOREncode_OpenBytes(&EC, &Place);
3006 if(!UsefulBuf_IsNULL(Place)) {
3007 return 3;
3008 }
3009 Place.len -= 4;
3010 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
3011 QCBOREncode_CloseBytes(&EC, Place.len);
3012 uErr = QCBOREncode_Finish(&EC, &Encoded);
3013 if(uErr != QCBOR_SUCCESS ||
3014 Encoded.len != sizeof(spExpectedForOpenBytes)) {
3015 return 4;
3016 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003017
3018 /* Open a byte string with no room left */
3019 QCBOREncode_Init(&EC, TestBuf);
3020 QCBOREncode_AddSZString(&EC, "0123456789012345678");
3021 QCBOREncode_OpenBytes(&EC, &Place);
3022 if(Place.ptr != NULL ||
3023 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01003024 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003025 }
3026
3027 /* Try to extend byte string past end of encoding output buffer */
3028 QCBOREncode_Init(&EC, TestBuf);
3029 QCBOREncode_AddSZString(&EC, "012345678901234567");
3030 QCBOREncode_OpenBytes(&EC, &Place);
3031 /* Don't bother to write any bytes*/
3032 QCBOREncode_CloseBytes(&EC, Place.len+1);
3033 uErr = QCBOREncode_GetErrorState(&EC);
3034 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003035 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003036 }
3037
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003038 /* Close a byte string without opening one. */
3039 QCBOREncode_Init(&EC, TestBuf);
3040 QCBOREncode_AddSZString(&EC, "012345678");
3041 QCBOREncode_CloseBytes(&EC, 1);
3042 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003043#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003044 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003045 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003046 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003047#else
3048 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003049 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003050 }
3051#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003052
3053 /* Forget to close a byte string */
3054 QCBOREncode_Init(&EC, TestBuf);
3055 QCBOREncode_AddSZString(&EC, "012345678");
3056 QCBOREncode_OpenBytes(&EC, &Place);
3057 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003058#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003059 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003060 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003061 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003062#else
3063 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003064 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003065 }
3066#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003067
3068 /* Try to open a byte string in a byte string */
3069 QCBOREncode_Init(&EC, TestBuf);
3070 QCBOREncode_AddSZString(&EC, "012345678");
3071 QCBOREncode_OpenBytes(&EC, &Place);
3072 QCBOREncode_OpenBytes(&EC, &Place);
3073 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003074#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003075 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003076 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003077 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003078#else
3079 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003080 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003081 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003082#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3083
3084 /* A successful case with a little complexity */
3085 QCBOREncode_Init(&EC, TestBuf2);
3086 QCBOREncode_OpenMap(&EC);
3087 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3088 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3089 Place.len = 2;
3090 UsefulBuf_Set(Place, 'x');
3091 QCBOREncode_CloseBytes(&EC, 2);
3092 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3093 Place.len = 3;
3094 UsefulBuf_Set(Place, 'y');
3095 QCBOREncode_CloseBytes(&EC, 3);
3096 QCBOREncode_OpenArrayInMapN(&EC, 40);
3097 QCBOREncode_OpenBytes(&EC, &Place);
3098 QCBOREncode_CloseBytes(&EC, 0);
3099 QCBOREncode_CloseArray(&EC);
3100 QCBOREncode_CloseMap(&EC);
3101 uErr = QCBOREncode_Finish(&EC, &Encoded);
3102 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003103 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003104 }
3105 if(UsefulBuf_Compare(Encoded,
3106 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003107 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003108 }
3109
3110 return 0;
3111}