blob: 267b439bc2050a2d71347fdc4aa9b92a6775efc4 [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 Lundblade8e36f812024-01-26 10:59:29 -0700646 QCBOREncode_Private_AddSimple(pECtx, 255);
647 QCBOREncode_Private_AddSimpleToMap(pECtx, "s2", 0);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700648 QCBOREncode_AddSZString(pECtx, "s3");
649 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700650 QCBOREncode_Private_AddSimple(pECtx, 33);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700651 QCBOREncode_AddInt64(pECtx, 88378374); // label before tag
652 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700653 QCBOREncode_Private_AddSimple(pECtx, 255);
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700654 QCBOREncode_AddInt64(pECtx, 89); // label before tag
655 QCBOREncode_AddTag(pECtx, 88);
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700656 QCBOREncode_Private_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
Jan Jongboom47d86c52019-07-25 08:54:16 +0200938/*
939 9F # array(5)
940 F5 # primitive(21)
941 F4 # primitive(20)
942 F6 # primitive(22)
943 F7 # primitive(23)
944 BF # map(1)
945 65 # text(5)
946 554E446566 # "UNDef"
947 F7 # primitive(23)
948 FF # break
949 FF # break
950 */
951static const uint8_t spExpectedEncodedSimpleIndefiniteLength[] = {
952 0x9f, 0xf5, 0xf4, 0xf6, 0xf7, 0xbf, 0x65, 0x55, 0x4e, 0x44, 0x65, 0x66, 0xf7, 0xff, 0xff};
953
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300954int32_t SimpleValuesIndefiniteLengthTest1(void)
Jan Jongboom47d86c52019-07-25 08:54:16 +0200955{
956 QCBOREncodeContext ECtx;
957 int nReturn = 0;
958
959 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
960 QCBOREncode_OpenArrayIndefiniteLength(&ECtx);
961
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700962 QCBOREncode_AddBool(&ECtx, true);
963 QCBOREncode_AddBool(&ECtx, false);
964 QCBOREncode_AddNULL(&ECtx);
965 QCBOREncode_AddUndef(&ECtx);
Jan Jongboom47d86c52019-07-25 08:54:16 +0200966
967 QCBOREncode_OpenMapIndefiniteLength(&ECtx);
968
Laurence Lundblade8e36f812024-01-26 10:59:29 -0700969 QCBOREncode_AddUndefToMap(&ECtx, "UNDef");
Jan Jongboom47d86c52019-07-25 08:54:16 +0200970 QCBOREncode_CloseMapIndefiniteLength(&ECtx);
971
972 QCBOREncode_CloseArrayIndefiniteLength(&ECtx);
973
974 UsefulBufC ECBOR;
975 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
976 nReturn = -1;
977 }
978
979 if(CheckResults(ECBOR, spExpectedEncodedSimpleIndefiniteLength))
980 return -2;
981
982 return(nReturn);
983}
984
Jan Jongboom5d827882019-08-07 12:51:15 +0200985/*
986A5 # map(5)
987 63 # text(3)
988 617272 # "arr"
989 98 1F # array(31)
990 00 # unsigned(0)
991 01 # unsigned(1)
992 02 # unsigned(2)
993 03 # unsigned(3)
994 04 # unsigned(4)
995 05 # unsigned(5)
996 06 # unsigned(6)
997 07 # unsigned(7)
998 08 # unsigned(8)
999 09 # unsigned(9)
1000 0A # unsigned(10)
1001 0B # unsigned(11)
1002 0C # unsigned(12)
1003 0D # unsigned(13)
1004 0E # unsigned(14)
1005 0F # unsigned(15)
1006 10 # unsigned(16)
1007 11 # unsigned(17)
1008 12 # unsigned(18)
1009 13 # unsigned(19)
1010 14 # unsigned(20)
1011 15 # unsigned(21)
1012 16 # unsigned(22)
1013 17 # unsigned(23)
1014 18 18 # unsigned(24)
1015 18 19 # unsigned(25)
1016 18 1A # unsigned(26)
1017 18 1B # unsigned(27)
1018 18 1C # unsigned(28)
1019 18 1D # unsigned(29)
1020 18 1E # unsigned(30)
1021 63 # text(3)
1022 6D6170 # "map"
1023 B8 1F # map(31)
1024 61 # text(1)
1025 61 # "a"
1026 00 # unsigned(0)
1027 61 # text(1)
1028 62 # "b"
1029 01 # unsigned(1)
1030 61 # text(1)
1031 63 # "c"
1032 02 # unsigned(2)
1033 61 # text(1)
1034 64 # "d"
1035 03 # unsigned(3)
1036 61 # text(1)
1037 65 # "e"
1038 04 # unsigned(4)
1039 61 # text(1)
1040 66 # "f"
1041 05 # unsigned(5)
1042 61 # text(1)
1043 67 # "g"
1044 06 # unsigned(6)
1045 61 # text(1)
1046 68 # "h"
1047 07 # unsigned(7)
1048 61 # text(1)
1049 69 # "i"
1050 08 # unsigned(8)
1051 61 # text(1)
1052 6A # "j"
1053 09 # unsigned(9)
1054 61 # text(1)
1055 6B # "k"
1056 0A # unsigned(10)
1057 61 # text(1)
1058 6C # "l"
1059 0B # unsigned(11)
1060 61 # text(1)
1061 6D # "m"
1062 0C # unsigned(12)
1063 61 # text(1)
1064 6E # "n"
1065 0D # unsigned(13)
1066 61 # text(1)
1067 6F # "o"
1068 0E # unsigned(14)
1069 61 # text(1)
1070 70 # "p"
1071 0F # unsigned(15)
1072 61 # text(1)
1073 71 # "q"
1074 10 # unsigned(16)
1075 61 # text(1)
1076 72 # "r"
1077 11 # unsigned(17)
1078 61 # text(1)
1079 73 # "s"
1080 12 # unsigned(18)
1081 61 # text(1)
1082 74 # "t"
1083 13 # unsigned(19)
1084 61 # text(1)
1085 75 # "u"
1086 14 # unsigned(20)
1087 61 # text(1)
1088 76 # "v"
1089 15 # unsigned(21)
1090 61 # text(1)
1091 77 # "w"
1092 16 # unsigned(22)
1093 61 # text(1)
1094 78 # "x"
1095 17 # unsigned(23)
1096 61 # text(1)
1097 79 # "y"
1098 18 18 # unsigned(24)
1099 61 # text(1)
1100 7A # "z"
1101 18 19 # unsigned(25)
1102 61 # text(1)
1103 41 # "A"
1104 18 1A # unsigned(26)
1105 61 # text(1)
1106 42 # "B"
1107 18 1B # unsigned(27)
1108 61 # text(1)
1109 43 # "C"
1110 18 1C # unsigned(28)
1111 61 # text(1)
1112 44 # "D"
1113 18 1D # unsigned(29)
1114 61 # text(1)
1115 45 # "E"
1116 18 1E # unsigned(30)
1117 65 # text(5)
1118 6D696E3331 # "min31"
1119 38 1E # negative(30)
1120 66 # text(6)
1121 706C75733331 # "plus31"
1122 18 1F # unsigned(31)
1123 63 # text(3)
1124 737472 # "str"
1125 78 1F # text(31)
1126 7465737474657374746573747465737474657374746573747163626F723131 # "testtesttesttesttesttestqcbor11"
1127 */
1128static const uint8_t EncodeLengthThirtyone[] = {
1129 0xa5, 0x63, 0x61, 0x72, 0x72, 0x98, 0x1f, 0x00, 0x01, 0x02, 0x03, 0x04,
1130 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
1131 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
1132 0x1a, 0x18, 0x1b, 0x18, 0x1c, 0x18, 0x1d, 0x18, 0x1e, 0x63, 0x6d, 0x61,
1133 0x70, 0xb8, 0x1f, 0x61, 0x61, 0x00, 0x61, 0x62, 0x01, 0x61, 0x63, 0x02,
1134 0x61, 0x64, 0x03, 0x61, 0x65, 0x04, 0x61, 0x66, 0x05, 0x61, 0x67, 0x06,
1135 0x61, 0x68, 0x07, 0x61, 0x69, 0x08, 0x61, 0x6a, 0x09, 0x61, 0x6b, 0x0a,
1136 0x61, 0x6c, 0x0b, 0x61, 0x6d, 0x0c, 0x61, 0x6e, 0x0d, 0x61, 0x6f, 0x0e,
1137 0x61, 0x70, 0x0f, 0x61, 0x71, 0x10, 0x61, 0x72, 0x11, 0x61, 0x73, 0x12,
1138 0x61, 0x74, 0x13, 0x61, 0x75, 0x14, 0x61, 0x76, 0x15, 0x61, 0x77, 0x16,
1139 0x61, 0x78, 0x17, 0x61, 0x79, 0x18, 0x18, 0x61, 0x7a, 0x18, 0x19, 0x61,
1140 0x41, 0x18, 0x1a, 0x61, 0x42, 0x18, 0x1b, 0x61, 0x43, 0x18, 0x1c, 0x61,
1141 0x44, 0x18, 0x1d, 0x61, 0x45, 0x18, 0x1e, 0x65, 0x6d, 0x69, 0x6e, 0x33,
1142 0x31, 0x38, 0x1e, 0x66, 0x70, 0x6c, 0x75, 0x73, 0x33, 0x31, 0x18, 0x1f,
1143 0x63, 0x73, 0x74, 0x72, 0x78, 0x1f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1144 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65,
1145 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x71, 0x63, 0x62, 0x6f, 0x72, 0x31,
1146 0x31
1147};
1148
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001149int32_t EncodeLengthThirtyoneTest(void)
Jan Jongboom5d827882019-08-07 12:51:15 +02001150{
1151 QCBOREncodeContext ECtx;
1152 int nReturn = 0;
1153
1154 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1155 QCBOREncode_OpenMap(&ECtx);
1156
1157 // add array with 31 items
1158 QCBOREncode_OpenArrayInMap(&ECtx, "arr");
1159 for (size_t ix = 0; ix < 31; ix++) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001160 QCBOREncode_AddInt64(&ECtx, (int64_t)ix);
Jan Jongboom5d827882019-08-07 12:51:15 +02001161 }
1162 QCBOREncode_CloseArray(&ECtx);
1163
1164 // add map with 31 items
1165 QCBOREncode_OpenMapInMap(&ECtx, "map");
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001166 for (int ix = 0; ix < 31; ix++) {
Jan Jongboom5d827882019-08-07 12:51:15 +02001167 // make sure we have unique keys in the map (a-z then follow by A-Z)
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001168 int c = 'a';
Jan Jongboom5d827882019-08-07 12:51:15 +02001169 if (ix < 26) c = c + ix;
1170 else c = 'A' + (ix - 26);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001171 char buffer[2] = { (char)c, 0 };
Jan Jongboom5d827882019-08-07 12:51:15 +02001172 QCBOREncode_AddInt64ToMap(&ECtx, buffer, ix);
1173 }
1174 QCBOREncode_CloseMap(&ECtx);
1175
1176 // add -31 and +31
1177 QCBOREncode_AddInt64ToMap(&ECtx, "min31", -31);
1178 QCBOREncode_AddInt64ToMap(&ECtx, "plus31", 31);
1179
1180 // add string with length 31
1181 const char *str = "testtesttesttesttesttestqcbor11";
1182 UsefulBufC str_b = { str, 31 };
1183 QCBOREncode_AddTextToMap(&ECtx, "str", str_b);
1184
1185 QCBOREncode_CloseMap(&ECtx);
1186
1187 UsefulBufC ECBOR;
1188 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
1189 nReturn = -1;
1190 }
1191
1192 if(CheckResults(ECBOR, EncodeLengthThirtyone))
1193 return -2;
1194
1195 return(nReturn);
1196}
1197
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301198
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301199/*
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001200 * [ "2013-03-21T20:04:00Z",
1201 * 0("2013-03-21T20:04:00Z"),
1202 * 1363896240,
1203 * 1(1363896240),
1204 * 100(-10676),
1205 * 3994,
1206 * 1004("1940-10-09"),
1207 * "1980-12-08",
1208 * { "Sample Date from RFC 3339": 0("1985-04-12T23:20:50.52Z"),
1209 * "SD": 1(999),
1210 * "Sample Date from RFC 8943": "1985-04-12",
1211 * 42: 1004("1985-04-12T23:20:50.52Z"),
1212 * "SY": 100(-10676),
1213 * 45: 3994
1214 * }
1215 * ]
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301216 */
1217static const uint8_t spExpectedEncodedDates[] = {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001218 0x89, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30, 0x33, 0x2D,
1219 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34, 0x3A, 0x30,
1220 0x30, 0x5A, 0xC0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2D, 0x30,
1221 0x33, 0x2D, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3A, 0x30, 0x34,
1222 0x3A, 0x30, 0x30, 0x5A, 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xC1,
1223 0x1A, 0x51, 0x4B, 0x67, 0xB0, 0xD8, 0x64, 0x39, 0x29, 0xB3,
1224 0x19, 0x0F, 0x9A, 0xD9, 0x03, 0xEC, 0x6A, 0x31, 0x39, 0x34,
1225 0x30, 0x2D, 0x31, 0x30, 0x2D, 0x30, 0x39, 0x6A, 0x31, 0x39,
1226 0x38, 0x30, 0x2D, 0x31, 0x32, 0x2D, 0x30, 0x38, 0xA6, 0x78,
1227 0x19, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61,
1228 0x74, 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46,
1229 0x43, 0x20, 0x33, 0x33, 0x33, 0x39, 0xC0, 0x77, 0x31, 0x39,
1230 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x54, 0x32,
1231 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35, 0x30, 0x2E, 0x35, 0x32,
1232 0x5A, 0x62, 0x53, 0x44, 0xC1, 0x19, 0x03, 0xE7, 0x78, 0x19,
1233 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x44, 0x61, 0x74,
1234 0x65, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x52, 0x46, 0x43,
1235 0x20, 0x38, 0x39, 0x34, 0x33, 0x6A, 0x31, 0x39, 0x38, 0x35,
1236 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, 0x18, 0x2A, 0xD9, 0x03,
1237 0xEC, 0x77, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D,
1238 0x31, 0x32, 0x54, 0x32, 0x33, 0x3A, 0x32, 0x30, 0x3A, 0x35,
1239 0x30, 0x2E, 0x35, 0x32, 0x5A, 0x62, 0x53, 0x59, 0xD8, 0x64,
1240 0x39, 0x29, 0xB3, 0x18, 0x2D, 0x19, 0x0F, 0x9A};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001242int32_t EncodeDateTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001243{
1244 QCBOREncodeContext ECtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001245
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301246 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001247
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001248 QCBOREncode_OpenArray(&ECtx);
1249
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001250 /* The values are taken from the CBOR RFCs */
1251 QCBOREncode_AddTDateString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "2013-03-21T20:04:00Z");
1252 QCBOREncode_AddDateString(&ECtx, "2013-03-21T20:04:00Z");
1253 QCBOREncode_AddTDateEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 1363896240);
1254 QCBOREncode_AddDateEpoch(&ECtx, 1363896240);
1255 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_TAG, -10676);
1256 QCBOREncode_AddTDaysEpoch(&ECtx, QCBOR_ENCODE_AS_BORROWED, 3994);
1257 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_TAG, "1940-10-09");
1258 QCBOREncode_AddTDaysString(&ECtx, QCBOR_ENCODE_AS_BORROWED, "1980-12-08");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001259
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001260 QCBOREncode_OpenMap(&ECtx);
1261
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001262 QCBOREncode_AddDateStringToMap(&ECtx,
1263 "Sample Date from RFC 3339",
1264 "1985-04-12T23:20:50.52Z");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001265 QCBOREncode_AddDateEpochToMap(&ECtx, "SD", 999);
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001266 QCBOREncode_AddTDaysStringToMapSZ(&ECtx,
1267 "Sample Date from RFC 8943",
1268 QCBOR_ENCODE_AS_BORROWED,
1269 "1985-04-12");
1270 QCBOREncode_AddTDaysStringToMapN(&ECtx,
1271 42,
1272 QCBOR_ENCODE_AS_TAG,
1273 "1985-04-12T23:20:50.52Z");
1274 QCBOREncode_AddTDaysEpochToMapSZ(&ECtx,
1275 "SY",
1276 QCBOR_ENCODE_AS_TAG,
1277 -10676);
1278 QCBOREncode_AddTDaysEpochToMapN(&ECtx,
1279 45,
1280 QCBOR_ENCODE_AS_BORROWED,
1281 3994);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001283 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001284
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001285 QCBOREncode_CloseArray(&ECtx);
1286
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301287 UsefulBufC ECBOR;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001288 if(QCBOREncode_Finish(&ECtx, &ECBOR)) {
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001289 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001291
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301292 if(CheckResults(ECBOR, spExpectedEncodedDates))
1293 return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001294
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001295 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001296}
1297
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301298
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001299int32_t ArrayNestingTest1(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001300{
1301 QCBOREncodeContext ECtx;
1302 int i;
1303 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301305 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001306 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1307 QCBOREncode_OpenArray(&ECtx);
1308 }
1309 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1310 QCBOREncode_CloseArray(&ECtx);
1311 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001312 UsefulBufC Encoded;
1313 if(QCBOREncode_Finish(&ECtx, &Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001314 nReturn = -1;
1315 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001316
1317 return(nReturn);
1318}
1319
1320
1321
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001322int32_t ArrayNestingTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001323{
1324 QCBOREncodeContext ECtx;
1325 int i;
1326 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001327
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301328 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 for(i = QCBOR_MAX_ARRAY_NESTING+1; i; i--) {
1330 QCBOREncode_OpenArray(&ECtx);
1331 }
1332 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1333 QCBOREncode_CloseArray(&ECtx);
1334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001335
Laurence Lundblade0595e932018-11-02 22:22:47 +07001336 UsefulBufC Encoded;
1337 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001338 nReturn = -1;
1339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001341 return(nReturn);
1342}
1343
1344
1345
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001346int32_t ArrayNestingTest3(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001347{
1348 QCBOREncodeContext ECtx;
1349 int i;
1350 int nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001351
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301352 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001353 for(i = QCBOR_MAX_ARRAY_NESTING; i; i--) {
1354 QCBOREncode_OpenArray(&ECtx);
1355 }
1356 for(i = QCBOR_MAX_ARRAY_NESTING+1 ; i; i--) {
1357 QCBOREncode_CloseArray(&ECtx);
1358 }
Laurence Lundblade0595e932018-11-02 22:22:47 +07001359 UsefulBufC Encoded;
1360 if(QCBOREncode_Finish(&ECtx, &Encoded) != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361 nReturn = -1;
1362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001364 return(nReturn);
1365}
1366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301368/*
1369 81 # array(1)
1370 81 # array(1)
1371 81 # array(1)
1372 81 # array(1)
1373 80 # array(0)
1374*/
1375static const uint8_t spFiveArrarys[] = {0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001377// Validated at http://cbor.me and by manually examining its output
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301378/*
1379 82 # array(2)
1380 81 # array(1)
1381 81 # array(1)
1382 81 # array(1)
1383 81 # array(1)
1384 80 # array(0)
Jan Jongboom5d827882019-08-07 12:51:15 +02001385 98 30 # array(48)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301386 3B 7FFFFFFFFFFFFFFF # negative(9223372036854775807)
1387 3B 0000000100000000 # negative(4294967296)
1388 3A FFFFFFFF # negative(4294967295)
1389 3A FFFFFFFE # negative(4294967294)
1390 3A FFFFFFFD # negative(4294967293)
1391 3A 7FFFFFFF # negative(2147483647)
1392 3A 7FFFFFFE # negative(2147483646)
1393 3A 00010001 # negative(65537)
1394 3A 00010000 # negative(65536)
1395 39 FFFF # negative(65535)
1396 39 FFFE # negative(65534)
1397 39 FFFD # negative(65533)
1398 39 0100 # negative(256)
1399 38 FF # negative(255)
1400 38 FE # negative(254)
1401 38 FD # negative(253)
1402 38 18 # negative(24)
1403 37 # negative(23)
1404 36 # negative(22)
1405 20 # negative(0)
1406 00 # unsigned(0)
1407 00 # unsigned(0)
1408 01 # unsigned(1)
1409 16 # unsigned(22)
1410 17 # unsigned(23)
1411 18 18 # unsigned(24)
1412 18 19 # unsigned(25)
1413 18 1A # unsigned(26)
Jan Jongboom5d827882019-08-07 12:51:15 +02001414 18 1F # unsigned(31)
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301415 18 FE # unsigned(254)
1416 18 FF # unsigned(255)
1417 19 0100 # unsigned(256)
1418 19 0101 # unsigned(257)
1419 19 FFFE # unsigned(65534)
1420 19 FFFF # unsigned(65535)
1421 1A 00010000 # unsigned(65536)
1422 1A 00010001 # unsigned(65537)
1423 1A 00010002 # unsigned(65538)
1424 1A 7FFFFFFF # unsigned(2147483647)
1425 1A 7FFFFFFF # unsigned(2147483647)
1426 1A 80000000 # unsigned(2147483648)
1427 1A 80000001 # unsigned(2147483649)
1428 1A FFFFFFFE # unsigned(4294967294)
1429 1A FFFFFFFF # unsigned(4294967295)
1430 1B 0000000100000000 # unsigned(4294967296)
1431 1B 0000000100000001 # unsigned(4294967297)
1432 1B 7FFFFFFFFFFFFFFF # unsigned(9223372036854775807)
1433 1B FFFFFFFFFFFFFFFF # unsigned(18446744073709551615)
1434 */
1435static const uint8_t spEncodeRawExpected[] = {
Jan Jongboom5d827882019-08-07 12:51:15 +02001436 0x82, 0x81, 0x81, 0x81, 0x81, 0x80, 0x98, 0x30,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1438 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
1439 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff, 0xff, 0x3a,
1440 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff, 0xff, 0xff,
1441 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff, 0x3a, 0x7f,
1442 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01, 0x00, 0x01,
1443 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39, 0xff, 0xff,
1444 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd, 0x39, 0x01,
1445 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38, 0xfd, 0x38,
1446 0x18, 0x37, 0x36, 0x20, 0x00, 0x00, 0x01, 0x16,
1447 0x17, 0x18, 0x18, 0x18, 0x19, 0x18, 0x1a, 0x18,
Jan Jongboom5d827882019-08-07 12:51:15 +02001448 0x1f, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
1449 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
1450 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
1451 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
1452 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
1453 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
1454 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
1455 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
1456 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
1457 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
1458 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1459 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1460 0xff, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001461
1462
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001463int32_t EncodeRawTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001464{
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001465 QCBOREncodeContext ECtx;
1466
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301467 QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001468 QCBOREncode_OpenArray(&ECtx);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301469 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spFiveArrarys));
1470 QCBOREncode_AddEncoded(&ECtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts));
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001471 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001472
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001473 UsefulBufC EncodedRawTest;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001474
Laurence Lundblade0595e932018-11-02 22:22:47 +07001475 if(QCBOREncode_Finish(&ECtx, &EncodedRawTest)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001476 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301479 if(CheckResults(EncodedRawTest, spEncodeRawExpected)) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001480 return -5;
1481 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001482
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001483 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001484}
1485
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301486/*
1487 This returns a pointer to spBigBuf
1488 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001489static int32_t CreateMap(uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490{
1491 QCBOREncodeContext ECtx;
1492 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001493
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001494 *pEncoded = NULL;
1495 *pEncodedLen = INT32_MAX;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301496 size_t uFirstSizeEstimate = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001497
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 // loop runs CBOR encoding twice. First with no buffer to
1499 // calucate the length so buffer can be allocated correctly,
1500 // and last with the buffer to do the actual encoding
1501 do {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301502 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503 QCBOREncode_OpenMap(&ECtx);
1504 QCBOREncode_AddInt64ToMap(&ECtx, "first integer", 42);
1505 QCBOREncode_OpenArrayInMap(&ECtx, "an array of two strings");
1506 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string1", 7}));
1507 QCBOREncode_AddText(&ECtx, ((UsefulBufC) {"string2", 7}));
1508 QCBOREncode_CloseArray(&ECtx);
1509 QCBOREncode_OpenMapInMap(&ECtx, "map in a map");
1510 QCBOREncode_AddBytesToMap(&ECtx,"bytes 1", ((UsefulBufC) { "xxxx", 4}));
1511 QCBOREncode_AddBytesToMap(&ECtx, "bytes 2",((UsefulBufC) { "yyyy", 4}));
1512 QCBOREncode_AddInt64ToMap(&ECtx, "another int", 98);
1513 QCBOREncode_AddTextToMap(&ECtx, "text 2", ((UsefulBufC) {"lies, damn lies and statistics", 30}));
1514 QCBOREncode_CloseMap(&ECtx);
1515 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001516
Laurence Lundblade0595e932018-11-02 22:22:47 +07001517 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001518 goto Done;
1519 if(*pEncoded != NULL) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301520 if(uFirstSizeEstimate != *pEncodedLen) {
1521 nReturn = 1;
1522 } else {
1523 nReturn = 0;
1524 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001525 goto Done;
1526 }
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301527 *pEncoded = spBigBuf;
1528 uFirstSizeEstimate = *pEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001531
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001532 Done:
1533 return(nReturn);
1534}
1535
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301536/*
1537 A3 # map(3)
1538 6D # text(13)
1539 666972737420696E7465676572 # "first integer"
1540 18 2A # unsigned(42)
1541 77 # text(23)
1542 616E206172726179206F662074776F20737472696E6773 # "an array of two strings"
1543 82 # array(2)
1544 67 # text(7)
1545 737472696E6731 # "string1"
1546 67 # text(7)
1547 737472696E6732 # "string2"
1548 6C # text(12)
1549 6D617020696E2061206D6170 # "map in a map"
1550 A4 # map(4)
1551 67 # text(7)
1552 62797465732031 # "bytes 1"
1553 44 # bytes(4)
1554 78787878 # "xxxx"
1555 67 # text(7)
1556 62797465732032 # "bytes 2"
1557 44 # bytes(4)
1558 79797979 # "yyyy"
1559 6B # text(11)
1560 616E6F7468657220696E74 # "another int"
1561 18 62 # unsigned(98)
1562 66 # text(6)
1563 746578742032 # "text 2"
1564 78 1E # text(30)
1565 6C6965732C2064616D6E206C69657320616E642073746174697374696373 # "lies, damn lies and statistics"
1566 */
1567static const uint8_t spValidMapEncoded[] = {
1568 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
1569 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
1570 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
1571 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
1572 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
1573 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
1574 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
1575 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
1576 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
1577 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
1578 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
1579 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
1580 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
1581 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
1582 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
1583 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001584
1585
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001586int32_t MapEncodeTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587{
1588 uint8_t *pEncodedMaps;
1589 size_t nEncodedMapLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001591 if(CreateMap(&pEncodedMaps, &nEncodedMapLen)) {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301592 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001594
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001595 int nReturn = 0;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301596 if(memcmp(spValidMapEncoded, pEncodedMaps, sizeof(spValidMapEncoded)))
1597 nReturn = 2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001598
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001599 return(nReturn);
1600}
1601
1602
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001603/*
1604 @brief Encode the RTIC results
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001605
Laurence Lundbladeee851742020-01-08 08:37:05 -08001606 @param[in] nRResult CBOR_SIMPLEV_TRUE, CBOR_SIMPLEV_FALSE or
1607 CBOR_SIMPLEV_NULL
1608 @param[in] time Time stamp in UNIX epoch time or 0 for none
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001609 @param[in] szAlexString Diagnostic code.
1610 @param[in[ pOut Buffer to put the result in
Laurence Lundbladeee851742020-01-08 08:37:05 -08001611 @param[in/out] pnLen Size of pOut buffer when called; length of data
1612 output in buffer on return
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001613
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001614 @return
1615 One of the CBOR encoder errors. QCBOR_SUCCESS, which is has value 0, if no error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001616
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001617 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 -08001618 short an error will be returned. This function will never write off the end
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001619 of the buffer passed to it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001620
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001621 If the result is 0, then the correct encoded CBOR is in pOut and *pnLen is the
1622 length of the encoded CBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001624 */
1625
Laurence Lundbladeee851742020-01-08 08:37:05 -08001626static UsefulBufC
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001627FormatRTICResults(uint8_t uRResult,
1628 int64_t time,
Laurence Lundbladeee851742020-01-08 08:37:05 -08001629 const char *szType,
1630 const char *szAlexString,
1631 UsefulBuf Storage)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001632{
1633 // Buffer that the result will be written in to
1634 // It is fixed size and small that a stack variable will be fine
Laurence Lundbladeee851742020-01-08 08:37:05 -08001635 // QCBOREncode will never write off the end of this buffer. If it won't
1636 // fit QCBOREncode_Finish will return an error.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001637
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001638 // Context for the encoder
1639 QCBOREncodeContext ECtx;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301640 QCBOREncode_Init(&ECtx, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001642 // All the RTIC results are grouped in a CBOR Map which will get turned into a JSON Object
1643 // Contents are label / value pairs
1644 QCBOREncode_OpenMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001646 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001647
Laurence Lundbladeee851742020-01-08 08:37:05 -08001648 // The result: 0 if scan happened and found nothing; 1 if it happened and
1649 // found something wrong; 2 if it didn't happen
Laurence Lundblade8e36f812024-01-26 10:59:29 -07001650 QCBOREncode_Private_AddSimpleToMap(&ECtx, "integrity", uRResult);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001651
1652 // Add the diagnostic code
1653 QCBOREncode_AddSZStringToMap(&ECtx, "type", szType);
1654
1655 // Add a time stamp
1656 if(time) {
1657 QCBOREncode_AddDateEpochToMap(&ECtx, "time", time);
1658 }
1659
1660 // Add the diagnostic code
1661 QCBOREncode_AddSZStringToMap(&ECtx, "diag", szAlexString);
1662
1663 // Open a subordinate map for telemtry data
1664 QCBOREncode_OpenMapInMap(&ECtx, "telemetry");
1665
1666 { // Brace / indention just to show CBOR encoding nesting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001667
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001668 // Add a few fake integers and buffers for now.
1669 QCBOREncode_AddInt64ToMap(&ECtx, "Shoe Size", 12);
1670
1671 // Add a few fake integers and buffers for now.
1672 QCBOREncode_AddInt64ToMap(&ECtx, "IQ", 0xffffffff);
1673
1674 // Add a few fake integers and buffers for now.
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301675 static const uint8_t pPV[] = {0x66, 0x67, 0x00, 0x56, 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001676 const UsefulBufC WSPV = {pPV, sizeof(pPV)};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001677
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001678 QCBOREncode_AddBytesToMap(&ECtx, "WhaleSharkPatternVector", WSPV);
1679 }
1680 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001681
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001682 // Close the telemetry map
1683 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001684
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001685 // Close the map
1686 QCBOREncode_CloseMap(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001687
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301688 UsefulBufC Result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001689
Laurence Lundblade0595e932018-11-02 22:22:47 +07001690 QCBOREncode_Finish(&ECtx, &Result);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001691
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301692 return Result;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001693}
1694
1695
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301696/*
1697 A5 # map(5)
1698 69 # text(9)
1699 696E74656772697479 # "integrity"
1700 F4 # primitive(20)
1701 64 # text(4)
1702 74797065 # "type"
1703 66 # text(6)
1704 726563656E74 # "recent"
1705 64 # text(4)
1706 74696D65 # "time"
1707 C1 # tag(1)
1708 1A 580D4172 # unsigned(1477263730)
1709 64 # text(4)
1710 64696167 # "diag"
1711 6A # text(10)
1712 30784131654335303031 # "0xA1eC5001"
1713 69 # text(9)
1714 74656C656D65747279 # "telemetry"
1715 A3 # map(3)
1716 69 # text(9)
1717 53686F652053697A65 # "Shoe Size"
1718 0C # unsigned(12)
1719 62 # text(2)
1720 4951 # "IQ"
1721 1A FFFFFFFF # unsigned(4294967295)
1722 77 # text(23)
1723 5768616C65536861726B5061747465726E566563746F72 # "WhaleSharkPatternVector"
1724 48 # bytes(8)
1725 66670056AABB0101 # "fg\x00V\xAA\xBB\x01\x01"
1726 */
1727static const uint8_t spExpectedRTIC[] = {
1728 0xa5, 0x69, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x72, 0x69, 0x74,
1729 0x79, 0xf4, 0x64, 0x74, 0x79, 0x70, 0x65, 0x66, 0x72, 0x65,
1730 0x63, 0x65, 0x6e, 0x74, 0x64, 0x74, 0x69, 0x6d, 0x65, 0xc1,
1731 0x1a, 0x58, 0x0d, 0x41, 0x72, 0x64, 0x64, 0x69, 0x61, 0x67,
1732 0x6a, 0x30, 0x78, 0x41, 0x31, 0x65, 0x43, 0x35, 0x30, 0x30,
1733 0x31, 0x69, 0x74, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72,
1734 0x79, 0xa3, 0x69, 0x53, 0x68, 0x6f, 0x65, 0x20, 0x53, 0x69,
1735 0x7a, 0x65, 0x0c, 0x62, 0x49, 0x51, 0x1a, 0xff, 0xff, 0xff,
1736 0xff, 0x77, 0x57, 0x68, 0x61, 0x6c, 0x65, 0x53, 0x68, 0x61,
1737 0x72, 0x6b, 0x50, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x56,
1738 0x65, 0x63, 0x74, 0x6f, 0x72, 0x48, 0x66, 0x67, 0x00, 0x56,
1739 0xaa, 0xbb, 0x01, 0x01};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001740
1741
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001742int32_t RTICResultsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001743{
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001744 const UsefulBufC Encoded = FormatRTICResults(CBOR_SIMPLEV_FALSE, 1477263730,
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301745 "recent", "0xA1eC5001",
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301746 UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301747 if(UsefulBuf_IsNULLC(Encoded)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001748 return -1;
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301749 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001750
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301751 if(CheckResults(Encoded, spExpectedRTIC)) {
1752 return -2;
1753 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001754
1755 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001756}
1757
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301758
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301759/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001760 The expected encoding for first test in BstrWrapTest()
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001761
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301762 82 # array(2)
1763 19 01C3 # unsigned(451)
1764 43 # bytes(3)
1765 1901D2 # "\x19\x01\xD2"
1766*/
1767static const uint8_t spExpectedBstrWrap[] = {0x82, 0x19, 0x01, 0xC3, 0x43, 0x19, 0x01, 0xD2};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +05301768
Laurence Lundblade684aec22018-10-12 19:33:53 +08001769/*
Laurence Lundbladeda532272019-04-07 11:40:17 -07001770 81 #array(1)
1771 0x58 0x25 # string of length 37 (length of "This is longer than twenty four bytes")
1772 */
1773static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25};
1774
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001775static const uint8_t spExpectedForBstrWrapCancel[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
1776
Laurence Lundbladeda532272019-04-07 11:40:17 -07001777/*
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001778 * bstr wrapping test
Laurence Lundblade684aec22018-10-12 19:33:53 +08001779 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001780int32_t BstrWrapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001781{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001782 QCBOREncodeContext EC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001783
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001784 // First test - make some wrapped CBOR and see that it is as expected
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301785 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001786
Laurence Lundblade684aec22018-10-12 19:33:53 +08001787 QCBOREncode_OpenArray(&EC);
1788 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001789
Laurence Lundblade684aec22018-10-12 19:33:53 +08001790 QCBOREncode_BstrWrap(&EC);
1791 QCBOREncode_AddUInt64(&EC, 466);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001792
Laurence Lundblade684aec22018-10-12 19:33:53 +08001793 UsefulBufC Wrapped;
1794 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001795
Laurence Lundblade684aec22018-10-12 19:33:53 +08001796 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001797
Laurence Lundblade684aec22018-10-12 19:33:53 +08001798 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07001799 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001800 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001801 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001802
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301803 if(CheckResults(Encoded, spExpectedBstrWrap)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08001804 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001805 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08001806
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001807 // Second test - see if the length of the wrapped
1808 // bstr is correct. Also tests bstr wrapping
1809 // in length calculation only mode.
Laurence Lundblade7412f812019-01-01 18:49:36 -08001810 QCBOREncode_Init(&EC, (UsefulBuf){NULL, INT32_MAX});
1811 QCBOREncode_OpenArray(&EC);
1812 QCBOREncode_BstrWrap(&EC);
1813 QCBOREncode_OpenArray(&EC);
1814 QCBOREncode_AddNULL(&EC);
1815 QCBOREncode_CloseArray(&EC);
1816 UsefulBufC BStr;
1817 QCBOREncode_CloseBstrWrap(&EC, &BStr);
Laurence Lundbladeee851742020-01-08 08:37:05 -08001818 // 3 is one byte for the wrapping bstr, 1 for an array of length 1,
1819 // and 1 byte for a NULL
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07001820 if(BStr.ptr != NULL || BStr.len != 3) {
Laurence Lundblade7412f812019-01-01 18:49:36 -08001821 return -5;
1822 }
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03001823
Laurence Lundbladeda532272019-04-07 11:40:17 -07001824 // Third, test QCBOREncode_AddBytesLenOnly() here as it is part of the
1825 // bstr wrapping use cases.
1826 UsefulBuf_MAKE_STACK_UB(StuffBuf, 50);
1827 QCBOREncode_Init(&EC, StuffBuf);
1828 QCBOREncode_OpenArray(&EC);
1829 QCBOREncode_AddBytesLenOnly(&EC, UsefulBuf_FROM_SZ_LITERAL("This is longer than twenty four bytes"));
1830 QCBOREncode_CloseArray(&EC);
1831 if(QCBOREncode_Finish(&EC, &Encoded)) {
1832 return -6;
1833 }
1834 if(CheckResults(Encoded, spExpectedTypeAndLen)) {
1835 return -7;
1836 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001837
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001838 // Fourth test, cancelling a byte string
1839 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1840
1841 QCBOREncode_OpenArray(&EC);
1842 QCBOREncode_AddUInt64(&EC, 451);
1843
1844 QCBOREncode_BstrWrap(&EC);
1845 QCBOREncode_CancelBstrWrap(&EC);
1846
1847
1848 QCBOREncode_AddUInt64(&EC, 42);
1849 QCBOREncode_CloseArray(&EC);
1850 if(QCBOREncode_Finish(&EC, &Encoded)) {
1851 return -8;
1852 }
1853 if(CheckResults(Encoded, spExpectedForBstrWrapCancel)) {
1854 return -9;
1855 }
1856
1857 QCBORError uErr;
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001858 // Fifth test, failed cancelling
1859 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1860
1861 QCBOREncode_OpenArray(&EC);
1862 QCBOREncode_AddUInt64(&EC, 451);
1863
1864 QCBOREncode_BstrWrap(&EC);
1865 QCBOREncode_AddUInt64(&EC, 99);
1866 QCBOREncode_CancelBstrWrap(&EC);
1867
1868 QCBOREncode_AddUInt64(&EC, 42);
1869 QCBOREncode_CloseArray(&EC);
1870 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001871#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001872 if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
1873 return -10;
1874 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001875#else
1876 if(uErr != QCBOR_SUCCESS) {
1877 return -110;
1878 }
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001879#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
1880
1881 // Sixth test, another cancel, but the error is not caught
1882 // This use will produce unintended CBOR. The error
1883 // is not caught because it would require tracking state
1884 // for QCBOREncode_BstrWrapInMapN.
1885 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
1886
1887 QCBOREncode_OpenMap(&EC);
1888 QCBOREncode_AddUInt64ToMapN(&EC, 451, 88);
1889
1890 QCBOREncode_BstrWrapInMapN(&EC, 55);
1891 QCBOREncode_CancelBstrWrap(&EC);
1892
1893 QCBOREncode_CloseMap(&EC);
1894 uErr = QCBOREncode_Finish(&EC, &Encoded);
1895 if(uErr != QCBOR_SUCCESS) {
1896 return -11;
1897 }
1898
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001899 return 0;
1900}
1901
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001902
1903
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001904int32_t BstrWrapErrorTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08001905{
Laurence Lundblade684aec22018-10-12 19:33:53 +08001906 QCBOREncodeContext EC;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001907 UsefulBufC Wrapped;
1908 UsefulBufC Encoded2;
1909 QCBORError uError;
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001910
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001911 // ---- Test closing a bstrwrap when it is an array that is open ---------
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001912
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301913 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001914
Laurence Lundblade684aec22018-10-12 19:33:53 +08001915 QCBOREncode_OpenArray(&EC);
1916 QCBOREncode_AddUInt64(&EC, 451);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001917
Laurence Lundblade684aec22018-10-12 19:33:53 +08001918 QCBOREncode_BstrWrap(&EC);
1919 QCBOREncode_AddUInt64(&EC, 466);
1920 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001921
Laurence Lundblade684aec22018-10-12 19:33:53 +08001922 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001923
Laurence Lundblade684aec22018-10-12 19:33:53 +08001924 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001925
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001926 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001927#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001928 if(uError != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001929 return (int32_t)(100 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001930 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001931#else
1932 /* The above test is run both when QCBOR_DISABLE_ENCODE_USAGE_GUARDS
1933 * is set and not to be sure to excerice all the relavant code in
1934 * both conditions. When the guards are disabled, there is no
1935 * error returned, but the code path is still covered.
1936 */
1937 if(uError != QCBOR_SUCCESS) {
1938 return (int32_t)(600 + uError);
1939 }
1940#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001941
Laurence Lundbladeee851742020-01-08 08:37:05 -08001942 // -------- test closing a bstrwrap when nothing is open ----------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301943 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001944 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001945 uError = QCBOREncode_Finish(&EC, &Encoded2);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001946#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001947 if(uError != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundblade274ddef2022-05-17 09:12:23 -07001948 return (int32_t)(700 + uError);
1949 }
1950#else
1951 if(uError != QCBOR_SUCCESS) {
1952 return (int32_t)(800 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001953 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08001954#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001955
Laurence Lundblade684aec22018-10-12 19:33:53 +08001956 // --------------- test nesting too deep ----------------------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301957 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade684aec22018-10-12 19:33:53 +08001958 for(int i = 1; i < 18; i++) {
1959 QCBOREncode_BstrWrap(&EC);
1960 }
1961 QCBOREncode_AddBool(&EC, true);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001962
Laurence Lundblade684aec22018-10-12 19:33:53 +08001963 for(int i = 1; i < 18; i++) {
1964 QCBOREncode_CloseBstrWrap(&EC, &Wrapped);
1965 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001966
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001967 uError = QCBOREncode_Finish(&EC, &Encoded2);
1968 if(uError != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladeb19ad282020-12-11 16:40:19 -08001969 return (int32_t)(300 + uError);
Laurence Lundblade684aec22018-10-12 19:33:53 +08001970 }
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001971
Laurence Lundblade684aec22018-10-12 19:33:53 +08001972 return 0;
1973}
1974
1975
Laurence Lundblade684aec22018-10-12 19:33:53 +08001976/*
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001977 This is bstr wrapped CBOR in 6 levels.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001978
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001979 [
1980 h'82004E82014B8202488203458204428105',
1981 {
1982 32:h'A3101018406568656C6C6F18215828A3111118416568656C6C6F18225819A312121
1983 8426568656C6C6F18234BA2131318436568656C6C6F'
1984 }
1985 ]
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001986
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001987 Unwrapping the first byte string in the above gives
1988 [0, h'82014B8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301989
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001990 Unwrapping again, the byte string immediately above gives
1991 [1, h'8202488203458204428105']
Laurence Lundblade369b90a2018-10-22 02:04:37 +05301992
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00001993 ...
1994
1995 Unrapping the second byte string in the top-level CBOR above gives
1996 {16: 16,
1997 64: "hello",
1998 33: h'A3111118416568656C6C6F18225819A3121218426568656C6C6F18234BA2....
1999 }
2000
2001 Unwrapping again, the byte string immediately above gives
2002 {17: 17,
2003 65: "hello",
2004 34: h'A3121218426568656C6C6F18234BA2131318436568656C6C6F'
2005 }
2006
2007 ...
2008
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302009 */
2010static const uint8_t spExpectedDeepBstr[] =
Laurence Lundblade684aec22018-10-12 19:33:53 +08002011{
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002012 0x82, 0x51, 0x82, 0x00, 0x4E, 0x82, 0x01, 0x4B,
2013 0x82, 0x02, 0x48, 0x82, 0x03, 0x45, 0x82, 0x04,
2014 0x42, 0x81, 0x05, 0xA1, 0x18, 0x20, 0x58, 0x37,
2015 0xA3, 0x10, 0x10, 0x18, 0x40, 0x65, 0x68, 0x65,
2016 0x6C, 0x6C, 0x6F, 0x18, 0x21, 0x58, 0x28, 0xA3,
2017 0x11, 0x11, 0x18, 0x41, 0x65, 0x68, 0x65, 0x6C,
2018 0x6C, 0x6F, 0x18, 0x22, 0x58, 0x19, 0xA3, 0x12,
2019 0x12, 0x18, 0x42, 0x65, 0x68, 0x65, 0x6C, 0x6C,
2020 0x6F, 0x18, 0x23, 0x4B, 0xA2, 0x13, 0x13, 0x18,
2021 0x43, 0x65, 0x68, 0x65, 0x6C, 0x6C, 0x6F
Laurence Lundblade684aec22018-10-12 19:33:53 +08002022};
2023
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002024
2025/*
2026 Get an int64 out of the decoder or fail.
2027 */
2028static int32_t GetInt64(QCBORDecodeContext *pDC, int64_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002029{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002030 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002031 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002032
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002033 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002034 if(nReturn) {
2035 return nReturn;
2036 }
Laurence Lundblade684aec22018-10-12 19:33:53 +08002037 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002038 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002039 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002040
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002041 *pInt = Item.val.int64;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002042 return 0;
2043}
2044
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002045/*
2046 Get an array out of the decoder or fail.
2047 */
2048static int32_t GetArray(QCBORDecodeContext *pDC, uint16_t *pInt)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002049{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002050 QCBORItem Item;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002051 int32_t nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002052
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002053 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002054 if(nReturn) {
2055 return nReturn;
2056 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002057 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2058 return -1;
2059 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002060
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002061 *pInt = Item.val.uCount;
2062 return 0;
2063}
2064
2065/*
2066 Get a map out of the decoder or fail.
2067 */
2068static int32_t GetMap(QCBORDecodeContext *pDC, uint16_t *pInt)
2069{
2070 QCBORItem Item;
2071 int32_t nReturn;
2072
2073 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002074 if(nReturn) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002075 return nReturn;
2076 }
2077 if(Item.uDataType != QCBOR_TYPE_MAP) {
2078 return -1;
2079 }
2080
2081 *pInt = Item.val.uCount;
2082 return 0;
2083}
2084
2085/*
2086 Get a byte string out of the decoder or fail.
2087 */
2088static int32_t GetByteString(QCBORDecodeContext *pDC, UsefulBufC *pBstr)
2089{
2090 QCBORItem Item;
2091 int32_t nReturn;
2092
2093 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2094 if(nReturn) {
2095 return nReturn;
2096 }
2097 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade323f8a92020-09-06 19:43:09 -07002098 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002099 }
2100
2101 *pBstr = Item.val.string;
2102 return 0;
2103}
2104
2105/*
2106 Get a byte string out of the decoder or fail.
2107 */
2108static int32_t GetTextString(QCBORDecodeContext *pDC, UsefulBufC *pTstr)
2109{
2110 QCBORItem Item;
2111 int nReturn;
2112
2113 nReturn = (int32_t)QCBORDecode_GetNext(pDC, &Item);
2114 if(nReturn) {
2115 return nReturn;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002116 }
2117 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002118 return -1;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002119 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002120
2121 *pTstr = Item.val.string;
2122 return 0;
2123}
2124
2125
2126/*
2127 Recursively decode array containing a little CBOR and a bstr wrapped array
2128 with a little CBOR and a bstr wrapped array...
2129
2130 Part of bstr_wrap_nest_test.
2131 */static int32_t DecodeNextNested(UsefulBufC Wrapped)
2132{
2133 int64_t nInt;
2134 UsefulBufC Bstr;
2135 uint16_t nArrayCount;
2136 QCBORDecodeContext DC;
2137 int32_t nResult;
2138
2139 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2140
2141 if(GetArray(&DC, &nArrayCount) || nArrayCount < 1 || nArrayCount > 2) {
2142 return -10;
2143 }
2144
2145 if(GetInt64(&DC, &nInt)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002146 return -11;
2147 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002148
2149 nResult = GetByteString(&DC, &Bstr);
2150 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2151 if(nArrayCount != 1) {
2152 return -12;
2153 } else {
2154 // successful exit
2155 return 0;
2156 }
2157 }
2158 if(nResult) {
2159 return -13;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002161
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002162 // tail recursion; good compilers will reuse the stack frame
2163 return DecodeNextNested(Bstr);
2164}
2165
2166
2167/*
2168 Recursively decode map containing a little CBOR and a bstr wrapped map
2169 with a little CBOR and a bstr wrapped map...
2170
2171 Part of bstr_wrap_nest_test.
2172 */
2173static int32_t DecodeNextNested2(UsefulBufC Wrapped)
2174{
2175 int32_t nResult;
2176 uint16_t nMapCount;
2177 int64_t nInt;
2178 UsefulBufC Bstr;
2179 QCBORDecodeContext DC;
2180
2181 QCBORDecode_Init(&DC, Wrapped, QCBOR_DECODE_MODE_NORMAL);
2182
2183 if(GetMap(&DC, &nMapCount) || nMapCount < 2 || nMapCount > 3) {
2184 return -20;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002185 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002186
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002187 if(GetInt64(&DC, &nInt)) {
2188 return -21;
2189 }
2190
2191 // The "hello"
2192 if(GetTextString(&DC, &Bstr)) {
2193 return -22;
2194 }
2195
2196 nResult = GetByteString(&DC, &Bstr);
2197 if(nResult == QCBOR_ERR_HIT_END || nResult == QCBOR_ERR_NO_MORE_ITEMS) {
2198 if(nMapCount == 2) {
2199 // successful exit
2200 return 0;
2201 } else {
2202 return -23;
2203 }
2204 }
2205
2206 if(nResult) {
2207 return -24;
2208 }
2209
2210 // tail recursion; good compilers will reuse the stack frame
2211 return DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002212}
2213
2214
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002215int32_t BstrWrapNestTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002216{
Laurence Lundblade684aec22018-10-12 19:33:53 +08002217 QCBOREncodeContext EC;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302218 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002219
Laurence Lundblade684aec22018-10-12 19:33:53 +08002220 // ---- Make a complicated nested CBOR structure ---
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002221 #define BSTR_TEST_DEPTH 6
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002222
Laurence Lundblade972e59c2018-11-11 15:57:23 +07002223 QCBOREncode_OpenArray(&EC);
2224
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002225 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002226 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002227 QCBOREncode_OpenArray(&EC);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002228 QCBOREncode_AddInt64(&EC, i);
2229 }
2230 for(int i = 0; i < BSTR_TEST_DEPTH; i++) {
2231 QCBOREncode_CloseArray(&EC);
2232 QCBOREncode_CloseBstrWrap(&EC, NULL);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002234
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002235 QCBOREncode_OpenMap(&EC);
2236 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2237 QCBOREncode_BstrWrapInMapN(&EC, i+0x20);
2238 QCBOREncode_OpenMap(&EC);
2239 QCBOREncode_AddInt64ToMapN(&EC, i+0x10, i+0x10);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002240 QCBOREncode_AddSZStringToMapN(&EC, i+0x40, "hello");
Laurence Lundblade684aec22018-10-12 19:33:53 +08002241 }
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002242
2243 for(int i = 0; i < (BSTR_TEST_DEPTH-2); i++) {
2244 QCBOREncode_CloseMap(&EC);
2245 QCBOREncode_CloseBstrWrap(&EC, NULL);
2246 }
2247 QCBOREncode_CloseMap(&EC);
2248
Laurence Lundblade684aec22018-10-12 19:33:53 +08002249 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002250
Laurence Lundblade684aec22018-10-12 19:33:53 +08002251 UsefulBufC Encoded;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002252 if(QCBOREncode_Finish(&EC, &Encoded)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002253 return -1;
2254 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002255
Laurence Lundblade684aec22018-10-12 19:33:53 +08002256 // ---Compare it to expected. Expected was hand checked with use of CBOR playground ----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302257 if(UsefulBuf_Compare(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedDeepBstr), Encoded)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002258 return -2;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundblade684aec22018-10-12 19:33:53 +08002261 // ---- Decode it and see if it is OK ------
2262 QCBORDecodeContext DC;
2263 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002264
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002265 UsefulBufC Bstr;
2266 uint16_t nArrayCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002267
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002268 // Array surrounding the the whole thing
2269 if(GetArray(&DC, &nArrayCount) || nArrayCount != 2) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002270 return -3;
2271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002272
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002273 // Get the byte string wrapping some array stuff
2274 if(GetByteString(&DC, &Bstr)) {
2275 return -4;
2276 }
2277
2278 // Decode the wrapped nested structure
2279 int nReturn = DecodeNextNested(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002280 if(nReturn) {
2281 return nReturn;
2282 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002283
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002284 // A map enclosing some map-oriented bstr wraps
2285 if(GetMap(&DC, &nArrayCount)) {
2286 return -5;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002287 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002288
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002289 // Get the byte string wrapping some array stuff
2290 if(GetByteString(&DC, &Bstr)) {
2291 return -6;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002292 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002293
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002294 // Decode the wrapped nested structure
2295 nReturn = DecodeNextNested2(Bstr);
Laurence Lundblade684aec22018-10-12 19:33:53 +08002296 if(nReturn) {
2297 return nReturn;
2298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002299
Laurence Lundblade684aec22018-10-12 19:33:53 +08002300 if(QCBORDecode_Finish(&DC)) {
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002301 return -7;
Laurence Lundblade684aec22018-10-12 19:33:53 +08002302 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002303
Laurence Lundblade684aec22018-10-12 19:33:53 +08002304 return 0;
2305}
2306
2307
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002308static const uint8_t spCoseSign1Signature[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302309 0x8e, 0xb3, 0x3e, 0x4c, 0xa3, 0x1d, 0x1c, 0x46, 0x5a, 0xb0,
2310 0x5a, 0xac, 0x34, 0xcc, 0x6b, 0x23, 0xd5, 0x8f, 0xef, 0x5c,
2311 0x08, 0x31, 0x06, 0xc4, 0xd2, 0x5a, 0x91, 0xae, 0xf0, 0xb0,
2312 0x11, 0x7e, 0x2a, 0xf9, 0xa2, 0x91, 0xaa, 0x32, 0xe1, 0x4a,
2313 0xb8, 0x34, 0xdc, 0x56, 0xed, 0x2a, 0x22, 0x34, 0x44, 0x54,
2314 0x7e, 0x01, 0xf1, 0x1d, 0x3b, 0x09, 0x16, 0xe5, 0xa4, 0xc3,
2315 0x45, 0xca, 0xcb, 0x36};
2316
2317/*
2318 D2 # tag(18)
2319 84 # array(4)
2320 43 # bytes(3)
2321 A10126 # "\xA1\x01&"
2322 A1 # map(1)
2323 04 # unsigned(4)
2324 42 # bytes(2)
2325 3131 # "11"
2326 54 # bytes(20)
2327 546869732069732074686520636F6E74656E742E # "This is the content."
2328 58 40 # bytes(64)
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002329 8EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25
2330 A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E
2331 01F11D3B0916E5A4C345CACB36 # "\x8E\xB3>L\xA3\x1D\x1CFZ\xB0Z\xAC4
2332 \xCCk#\xD5\x8F\xEF\b1\x06\xC4\xD2Z
2333 \x91\xAE\xF0\xB0\x11~*\xF9\xA2\x91
2334 \xAA2\xE1J\xB84\xDCV\xED*\"4DT~\x01
2335 \xF1\x1D;\t\x16\xE5\xA4\xC3E\xCA
2336 \xCB6"
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302337 */
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002338static const uint8_t spCoseSign1TBSExpected[] = {
Laurence Lundblade369b90a2018-10-22 02:04:37 +05302339 0xD2, 0x84, 0x43, 0xA1, 0x01, 0x26, 0xA1, 0x04, 0x42, 0x31,
2340 0x31, 0x54, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
2341 0x74, 0x68, 0x65, 0x20, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E,
2342 0x74, 0x2E, 0x58, 0x40, 0x8E, 0xB3, 0x3E, 0x4C, 0xA3, 0x1D,
2343 0x1C, 0x46, 0x5A, 0xB0, 0x5A, 0xAC, 0x34, 0xCC, 0x6B, 0x23,
2344 0xD5, 0x8F, 0xEF, 0x5C, 0x08, 0x31, 0x06, 0xC4, 0xD2, 0x5A,
2345 0x91, 0xAE, 0xF0, 0xB0, 0x11, 0x7E, 0x2A, 0xF9, 0xA2, 0x91,
2346 0xAA, 0x32, 0xE1, 0x4A, 0xB8, 0x34, 0xDC, 0x56, 0xED, 0x2A,
2347 0x22, 0x34, 0x44, 0x54, 0x7E, 0x01, 0xF1, 0x1D, 0x3B, 0x09,
2348 0x16, 0xE5, 0xA4, 0xC3, 0x45, 0xCA, 0xCB, 0x36};
2349
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002350static const uint8_t pProtectedHeaders[] = {0xa1, 0x01, 0x26};
2351
2352
Laurence Lundblade684aec22018-10-12 19:33:53 +08002353/*
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002354 This corresponds exactly to the example in RFC 8152 section
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002355 C.2.1. This doesn't actually verify the signature (however
2356 the t_cose implementation does).
Laurence Lundblade684aec22018-10-12 19:33:53 +08002357 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002358int32_t CoseSign1TBSTest(void)
Laurence Lundblade684aec22018-10-12 19:33:53 +08002359{
2360 // All of this is from RFC 8152 C.2.1
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002361 const char *szKid = "11";
2362 const UsefulBufC Kid = UsefulBuf_FromSZ(szKid);
2363 const char *szPayload = "This is the content.";
2364 const UsefulBufC Payload = UsefulBuf_FromSZ(szPayload);
2365 const UsefulBufC ProtectedHeaders = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pProtectedHeaders);
2366 const UsefulBufC Signature = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCoseSign1Signature);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002367
Laurence Lundblade684aec22018-10-12 19:33:53 +08002368 QCBOREncodeContext EC;
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002369
2370 // --------QCBOREncode_CloseBstrWrap2(&EC, **false** ----------------------
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302371 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002372
Laurence Lundblade684aec22018-10-12 19:33:53 +08002373 // top level array for cose sign1, 18 is the tag for COSE sign
Laurence Lundbladec6ec7ef2018-12-04 10:45:06 +09002374 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
Laurence Lundblade067035b2018-11-28 17:35:25 -08002375 QCBOREncode_OpenArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002376
Laurence Lundblade684aec22018-10-12 19:33:53 +08002377 // Add protected headers
2378 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002379
Laurence Lundblade684aec22018-10-12 19:33:53 +08002380 // Empty map with unprotected headers
2381 QCBOREncode_OpenMap(&EC);
2382 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2383 QCBOREncode_CloseMap(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002384
Laurence Lundblade684aec22018-10-12 19:33:53 +08002385 // The payload
2386 UsefulBufC WrappedPayload;
2387 QCBOREncode_BstrWrap(&EC);
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002388 // Payload is not actually CBOR in example C.2.1 like it would be
2389 // for a CWT or EAT. It is just a text string.
2390 QCBOREncode_AddEncoded(&EC, Payload);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002391 QCBOREncode_CloseBstrWrap2(&EC, false, &WrappedPayload);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002392
Laurence Lundblade684aec22018-10-12 19:33:53 +08002393 // Check we got back the actual payload expected
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002394 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2395 if(UsefulBuf_Compare(WrappedPayload,
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002396 UsefulBuf_FROM_SZ_LITERAL("This is the content."))) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002397 return -1;
2398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002399
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002400/* if(UsefulBuf_Compare(WrappedPayload,
2401 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2402 return -1;
2403 } */
2404
Laurence Lundblade684aec22018-10-12 19:33:53 +08002405 // The signature
2406 QCBOREncode_AddBytes(&EC, Signature);
2407 QCBOREncode_CloseArray(&EC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002408
Laurence Lundblade684aec22018-10-12 19:33:53 +08002409 // Finish and check the results
2410 UsefulBufC COSE_Sign1;
Laurence Lundblade0595e932018-11-02 22:22:47 +07002411 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002412 return -2;
2413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002414
Laurence Lundblade684aec22018-10-12 19:33:53 +08002415 // 98 is the size from RFC 8152 C.2.1
2416 if(COSE_Sign1.len != 98) {
2417 return -3;
2418 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002419
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -07002420 // It would be good to compare this to the output from a COSE
2421 // implementation like COSE-C. This has been checked against the
2422 // CBOR playground.
2423 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
Laurence Lundblade684aec22018-10-12 19:33:53 +08002424 return -4;
2425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002426
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002427
2428 // --------QCBOREncode_CloseBstrWrap2(&EC, **true** ------------------------
2429 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2430
2431 // top level array for cose sign1, 18 is the tag for COSE sign
2432 QCBOREncode_AddTag(&EC, CBOR_TAG_COSE_SIGN1);
2433 QCBOREncode_OpenArray(&EC);
2434
2435 // Add protected headers
2436 QCBOREncode_AddBytes(&EC, ProtectedHeaders);
2437
2438 // Empty map with unprotected headers
2439 QCBOREncode_OpenMap(&EC);
2440 QCBOREncode_AddBytesToMapN(&EC, 4, Kid);
2441 QCBOREncode_CloseMap(&EC);
2442
2443 // The payload
2444 QCBOREncode_BstrWrap(&EC);
2445 // Payload is not actually CBOR in example C.2.1 like it would be
2446 // for a CWT or EAT. It is just a text string.
2447 QCBOREncode_AddEncoded(&EC, Payload);
2448 QCBOREncode_CloseBstrWrap2(&EC, true, &WrappedPayload);
2449
2450 // Check we got back the actual payload expected
2451 // The extra "T" is 0x54, which is the initial byte a bstr of length 20.
2452 if(UsefulBuf_Compare(WrappedPayload,
2453 UsefulBuf_FROM_SZ_LITERAL("TThis is the content."))) {
2454 return -11;
2455 }
2456
2457 // The signature
2458 QCBOREncode_AddBytes(&EC, Signature);
2459 QCBOREncode_CloseArray(&EC);
2460
2461 // Finish and check the results
2462 if(QCBOREncode_Finish(&EC, &COSE_Sign1)) {
2463 return -12;
2464 }
2465
2466 // 98 is the size from RFC 8152 C.2.1
2467 if(COSE_Sign1.len != 98) {
2468 return -13;
2469 }
2470
2471 // It would be good to compare this to the output from a COSE
2472 // implementation like COSE-C. This has been checked against the
2473 // CBOR playground.
2474 if(CheckResults(COSE_Sign1, spCoseSign1TBSExpected)) {
2475 return -14;
2476 }
2477
Laurence Lundblade684aec22018-10-12 19:33:53 +08002478 return 0;
2479}
2480
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002481
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002482int32_t EncodeErrorTests(void)
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002483{
2484 QCBOREncodeContext EC;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002485 QCBORError uErr;
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002486
2487
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002488 // ------ Test for QCBOR_ERR_BUFFER_TOO_LARGE ------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002489 // Do all of these tests with NULL buffers so no actual
2490 // large allocations are neccesary
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002491 const UsefulBuf Buffer = (UsefulBuf){NULL, UINT32_MAX};
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002492
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002493 // First verify no error from a big buffer
2494 QCBOREncode_Init(&EC, Buffer);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002495 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002496 // 6 is the CBOR overhead for opening the array and encodng the length
2497 // This exactly fills the buffer.
2498 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-6});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002499 QCBOREncode_CloseArray(&EC);
2500 size_t xx;
2501 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2502 return -1;
2503 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002504
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002505 // Second verify error from an array in encoded output too large
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002506 // Also test fetching the error code before finish
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002507 QCBOREncode_Init(&EC, (UsefulBuf){NULL, UINT32_MAX});
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002508 QCBOREncode_OpenArray(&EC);
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002509 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, UINT32_MAX-10});
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002510 QCBOREncode_OpenArray(&EC); // Where QCBOR internally encounters and records error
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002511 if(QCBOREncode_GetErrorState(&EC) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2512 // Error fetch failed.
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002513 return -122;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002514 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002515 QCBOREncode_CloseArray(&EC);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002516 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_LARGE) {
2517 return -2;
2518 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002519
Laurence Lundbladed39cd392019-01-11 18:17:38 -08002520 // Third, fit an array in exactly at max position allowed
2521 QCBOREncode_Init(&EC, Buffer);
2522 QCBOREncode_OpenArray(&EC);
2523 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, QCBOR_MAX_ARRAY_OFFSET-6});
2524 QCBOREncode_OpenArray(&EC);
2525 QCBOREncode_CloseArray(&EC);
2526 QCBOREncode_CloseArray(&EC);
2527 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2528 return -10;
2529 }
2530
2531
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002532 // ----- QCBOR_ERR_BUFFER_TOO_SMALL --------------
2533 // Work close to the 4GB size limit for a better test
2534 const uint32_t uLargeSize = UINT32_MAX - 1024;
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002535 const UsefulBuf Large = (UsefulBuf){NULL,uLargeSize};
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002536
2537 QCBOREncode_Init(&EC, Large);
2538 QCBOREncode_OpenArray(&EC);
2539 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2540 QCBOREncode_CloseArray(&EC);
2541 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2542 // Making sure it succeeds when it should first
2543 return -3;
2544 }
2545
2546 QCBOREncode_Init(&EC, Large);
2547 QCBOREncode_OpenArray(&EC);
2548 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2 + 1});
2549 QCBOREncode_AddBytes(&EC, (UsefulBufC){NULL, uLargeSize/2});
2550 QCBOREncode_CloseArray(&EC);
2551 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_BUFFER_TOO_SMALL) {
2552 // Now just 1 byte over, see that it fails
2553 return -4;
2554 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002555
2556
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002557 // ----- QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -------
2558 QCBOREncode_Init(&EC, Large);
2559 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2560 QCBOREncode_OpenArray(&EC);
2561 }
2562 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2563 QCBOREncode_CloseArray(&EC);
2564 }
2565 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_SUCCESS) {
2566 // Making sure it succeeds when it should first
2567 return -5;
2568 }
2569
2570 QCBOREncode_Init(&EC, Large);
2571 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2572 QCBOREncode_OpenArray(&EC);
2573 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002574 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002575 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2576 QCBOREncode_CloseArray(&EC);
2577 }
2578 if(QCBOREncode_FinishGetSize(&EC, &xx) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002579 return -6;
2580 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002581
2582
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002583 /* ------ QCBOR_ERR_TOO_MANY_CLOSES -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002584 QCBOREncode_Init(&EC, Large);
2585 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2586 QCBOREncode_OpenArray(&EC);
2587 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002588 /* +1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002589 for(int i = QCBOR_MAX_ARRAY_NESTING+1; i > 0; i--) {
2590 QCBOREncode_CloseArray(&EC);
2591 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002592 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2593#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2594 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002595 return -7;
2596 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002597#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2598 if(uErr != QCBOR_SUCCESS) {
2599 return -107;
2600 }
2601#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002602
2603
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002604 /* ------ QCBOR_ERR_CLOSE_MISMATCH -------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002605 QCBOREncode_Init(&EC, Large);
2606 QCBOREncode_OpenArray(&EC);
2607 UsefulBufC Wrap;
2608 QCBOREncode_CloseBstrWrap(&EC, &Wrap);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002609 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2610#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2611 if(uErr != QCBOR_ERR_CLOSE_MISMATCH) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002612 return -8;
2613 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002614#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2615 if(uErr != QCBOR_SUCCESS) {
2616 return -108;
2617 }
2618#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002619
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002620 /* ------ QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN --------- */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002621 QCBOREncode_Init(&EC, Large);
2622 for(int i = QCBOR_MAX_ARRAY_NESTING; i > 0; i--) {
2623 QCBOREncode_OpenArray(&EC);
2624 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002625 /* -1 level to cause error */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002626 for(int i = QCBOR_MAX_ARRAY_NESTING-1; i > 0; i--) {
2627 QCBOREncode_CloseArray(&EC);
2628 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002629
2630 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2631#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2632 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002633 return -9;
2634 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002635#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2636 if(uErr != QCBOR_SUCCESS) {
2637 return -109;
2638 }
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002639#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -08002640
Laurence Lundblade241705e2018-12-30 18:56:14 -08002641 /* QCBOR_ERR_ARRAY_TOO_LONG is not tested here as
2642 it would require a 64KB of RAM to test */
Laurence Lundbladef2a58f62018-12-16 00:43:34 -08002643
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002644
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002645 /* ----- Test the check for NULL buffer ------ */
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +03002646 QCBOREncode_Init(&EC, Buffer);
2647 if(QCBOREncode_IsBufferNULL(&EC) == 0) {
2648 return -11;
2649 }
2650
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002651 /* ------ QCBOR_ERR_UNSUPPORTED -------- */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002652 QCBOREncode_Init(&EC, Large);
2653 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002654 QCBOREncode_Private_AddSimple(&EC, 24); /* CBOR_SIMPLEV_RESERVED_START */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002655 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2656#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2657 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002658 return -12;
2659 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002660#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2661 if(uErr != QCBOR_SUCCESS) {
2662 return -112;
2663 }
2664#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2665
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002666
2667 QCBOREncode_Init(&EC, Large);
2668 QCBOREncode_OpenArray(&EC);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002669 QCBOREncode_Private_AddSimple(&EC, 31); /* CBOR_SIMPLEV_RESERVED_END */
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002670 uErr = QCBOREncode_FinishGetSize(&EC, &xx);
2671#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2672 if(uErr != QCBOR_ERR_ENCODE_UNSUPPORTED) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002673 return -13;
2674 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07002675#else /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2676 if(uErr != QCBOR_SUCCESS) {
2677 return -113;
2678 }
2679#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladed8e1c512020-11-04 23:03:44 -08002680
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002681
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -08002682 return 0;
2683}
Laurence Lundblade59289e52019-12-30 13:44:37 -08002684
2685
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002686#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08002687/*
2688 [
2689 4([-1, 3]),
2690 4([-20, 4759477275222530853136]),
2691 4([9223372036854775807, -4759477275222530853137]),
2692 5([300, 100]),
2693 5([-20, 4759477275222530853136]),
2694 5([-9223372036854775808, -4759477275222530853137])
2695 ]
2696 */
2697static const uint8_t spExpectedExponentAndMantissaArray[] = {
2698 0x86, 0xC4, 0x82, 0x20, 0x03, 0xC4, 0x82, 0x33,
2699 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2700 0x07, 0x08, 0x09, 0x10, 0xC4, 0x82, 0x1B, 0x7F,
2701 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3,
2702 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2703 0x08, 0x09, 0x10, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2704 0x18, 0x64, 0xC5, 0x82, 0x33, 0xC2, 0x4A, 0x01,
2705 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
2706 0x10, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2707 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2708 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
2709
2710
2711/*
2712 {
2713 "decimal fraction": 4([-1, 3]),
2714 300: 4([-1, 3]),
2715 "decimal fraction bignum postive": 4([-200, 4759477275222530853136]),
2716 400: 4([2147483647, 4759477275222530853136]),
2717 "decimal fraction bignum negative": 4([9223372036854775807, -4759477275222530853137]),
2718 500: 4([9223372036854775807, -4759477275222530853137]),
2719 "big float": 5([300, 100]),
2720 600: 5([300, 100]),
2721 "big float bignum positive": 5([-20, 4759477275222530853136]),
2722 700: 5([-20, 4759477275222530853136]),
2723 "big float bignum negative": 5([-9223372036854775808, -4759477275222530853137]),
2724 800: 5([-9223372036854775808, -4759477275222530853137])
2725 }
2726 */
2727static const uint8_t spExpectedExponentAndMantissaMap[] = {
2728 0xAC, 0x70, 0x64, 0x65, 0x63, 0x69, 0x6D, 0x61,
2729 0x6C, 0x20, 0x66, 0x72, 0x61, 0x63, 0x74, 0x69,
2730 0x6F, 0x6E, 0xC4, 0x82, 0x20, 0x03, 0x19, 0x01,
2731 0x2C, 0xC4, 0x82, 0x20, 0x03, 0x78, 0x1F, 0x64,
2732 0x65, 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66,
2733 0x72, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
2734 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70,
2735 0x6F, 0x73, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2736 0x38, 0xC7, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2737 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01,
2738 0x90, 0xC4, 0x82, 0x1A, 0x7F, 0xFF, 0xFF, 0xFF,
2739 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2740 0x07, 0x08, 0x09, 0x10, 0x78, 0x20, 0x64, 0x65,
2741 0x63, 0x69, 0x6D, 0x61, 0x6C, 0x20, 0x66, 0x72,
2742 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x62,
2743 0x69, 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x6E, 0x65,
2744 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0xC4, 0x82,
2745 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2746 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
2747 0x06, 0x07, 0x08, 0x09, 0x10, 0x19, 0x01, 0xF4,
2748 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,
2749 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03,
2750 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x69,
2751 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2752 0x74, 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64,
2753 0x19, 0x02, 0x58, 0xC5, 0x82, 0x19, 0x01, 0x2C,
2754 0x18, 0x64, 0x78, 0x19, 0x62, 0x69, 0x67, 0x20,
2755 0x66, 0x6C, 0x6F, 0x61, 0x74, 0x20, 0x62, 0x69,
2756 0x67, 0x6E, 0x75, 0x6D, 0x20, 0x70, 0x6F, 0x73,
2757 0x69, 0x74, 0x69, 0x76, 0x65, 0xC5, 0x82, 0x33,
2758 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2759 0x07, 0x08, 0x09, 0x10, 0x19, 0x02, 0xBC, 0xC5,
2760 0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04,
2761 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x78, 0x19,
2762 0x62, 0x69, 0x67, 0x20, 0x66, 0x6C, 0x6F, 0x61,
2763 0x74, 0x20, 0x62, 0x69, 0x67, 0x6E, 0x75, 0x6D,
2764 0x20, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76,
2765 0x65, 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF,
2766 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02,
2767 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
2768 0x19, 0x03, 0x20, 0xC5, 0x82, 0x3B, 0x7F, 0xFF,
2769 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0x4A,
2770 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2771 0x09, 0x10
2772};
2773
2774
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002775int32_t ExponentAndMantissaEncodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002776{
2777 QCBOREncodeContext EC;
2778 UsefulBufC EncodedExponentAndMantissa;
2779
2780 // Constant for the big number used in all the tests.
2781 static const uint8_t spBigNum[] = {0x01, 0x02, 0x03, 0x04, 0x05,
2782 0x06, 0x07, 0x08, 0x09, 0x010};
2783 const UsefulBufC BigNum = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum);
2784
2785 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2786 QCBOREncode_OpenArray(&EC);
2787 QCBOREncode_AddDecimalFraction(&EC, 3, -1); // 3 * (10 ^ -1)
2788 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum , false, -20);
2789 QCBOREncode_AddDecimalFractionBigNum(&EC, BigNum, true, INT64_MAX);
2790 QCBOREncode_AddBigFloat(&EC, 100, 300);
2791 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, false, -20);
2792 QCBOREncode_AddBigFloatBigNum(&EC, BigNum, true, INT64_MIN);
2793 QCBOREncode_CloseArray(&EC);
2794
2795 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2796 return -2;
2797 }
2798
2799 int nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2800 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaArray),
2801 NULL);
2802 if(nReturn) {
2803 return nReturn;
2804 }
2805
2806 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
2807 QCBOREncode_OpenMap(&EC);
2808
2809 QCBOREncode_AddDecimalFractionToMap(&EC, "decimal fraction", 3, -1);
2810
2811 QCBOREncode_AddDecimalFractionToMapN(&EC, 300, 3, -1);
2812
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002813 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002814 "decimal fraction bignum postive",
2815 BigNum,
2816 false,
2817 -200);
2818
2819 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2820 400,
2821 BigNum,
2822 false,
2823 INT32_MAX);
2824
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002825 QCBOREncode_AddDecimalFractionBigNumToMapSZ(&EC,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002826 "decimal fraction bignum negative",
2827 BigNum,
2828 true,
2829 INT64_MAX);
2830
2831 QCBOREncode_AddDecimalFractionBigNumToMapN(&EC,
2832 500,
2833 BigNum,
2834 true,
2835 INT64_MAX);
2836
2837 QCBOREncode_AddBigFloatToMap(&EC, "big float", 100, 300);
2838
2839 QCBOREncode_AddBigFloatToMapN(&EC, 600, 100, 300);
2840
2841 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2842 "big float bignum positive",
2843 BigNum,
2844 false,
2845 -20);
2846
2847 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2848 700,
2849 BigNum,
2850 false,
2851 -20);
2852
2853 QCBOREncode_AddBigFloatBigNumToMap(&EC,
2854 "big float bignum negative",
2855 BigNum,
2856 true,
2857 INT64_MIN);
2858
2859 QCBOREncode_AddBigFloatBigNumToMapN(&EC,
2860 800,
2861 BigNum,
2862 true,
2863 INT64_MIN);
2864
2865 QCBOREncode_CloseMap(&EC);
2866
2867 if(QCBOREncode_Finish(&EC, &EncodedExponentAndMantissa)) {
2868 return -3;
2869 }
2870
2871
2872 struct UBCompareDiagnostic Diag;
2873
2874 nReturn = UsefulBuf_CompareWithDiagnostic(EncodedExponentAndMantissa,
2875 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentAndMantissaMap),
2876 &Diag);
2877 if(nReturn) {
2878 return nReturn + 1000000; // +1000000 to distinguish from first test above
2879 }
2880
2881 return 0;
2882}
2883
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002884#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002885
2886
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002887int32_t QCBORHeadTest(void)
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002888{
2889 /* This test doesn't have to be extensive, because just about every
2890 * other test exercises QCBOREncode_EncodeHead().
2891 */
2892 // ---- basic test to encode a zero ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002893 UsefulBuf_MAKE_STACK_UB(RightSize, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002894
2895 UsefulBufC encoded = QCBOREncode_EncodeHead(RightSize,
2896 CBOR_MAJOR_TYPE_POSITIVE_INT,
2897 0,
2898 0);
2899
2900 static const uint8_t expectedZero[] = {0x00};
2901
2902 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expectedZero))) {
2903 return -1;
2904 }
2905
2906 // ---- Encode a zero padded out to an 8 byte integer ----
2907 encoded = QCBOREncode_EncodeHead(RightSize,
2908 CBOR_MAJOR_TYPE_POSITIVE_INT,
2909 8, // uMinSize is 8 bytes
2910 0);
2911
2912 static const uint8_t expected9bytes[] = {0x1b, 0x00, 0x00, 0x00, 0x00,
2913 0x00, 0x00, 0x00, 0x00};
2914
2915 if(UsefulBuf_Compare(encoded, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected9bytes))) {
2916 return -2;
2917 }
2918
2919
2920 // ---- Try to encode into too-small a buffer ----
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002921 UsefulBuf_MAKE_STACK_UB(TooSmall, QCBOR_HEAD_BUFFER_SIZE-1);
Laurence Lundbladec9f0fbc2020-02-07 10:48:33 +00002922
2923 encoded = QCBOREncode_EncodeHead(TooSmall,
2924 CBOR_MAJOR_TYPE_POSITIVE_INT,
2925 0,
2926 0);
2927
2928 if(!UsefulBuf_IsNULLC(encoded)) {
2929 return -3;
2930 }
2931
2932 return 0;
2933}
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002934
2935
2936static const uint8_t spExpectedForOpenBytes[] = {
2937 0x50, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2938 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
2939 0x78
2940};
2941
2942static const uint8_t spExpectedForOpenBytes2[] = {
2943 0xA4, 0x0A, 0x16, 0x14, 0x42, 0x78, 0x78, 0x66,
2944 0x74, 0x68, 0x69, 0x72, 0x74, 0x79, 0x43, 0x79,
2945 0x79, 0x79, 0x18, 0x28, 0x81, 0x40
2946};
2947
2948int32_t
2949OpenCloseBytesTest(void)
2950{
2951 UsefulBuf_MAKE_STACK_UB( TestBuf, 20);
2952 UsefulBuf_MAKE_STACK_UB( TestBuf2, 30);
2953 QCBOREncodeContext EC;
2954 UsefulBuf Place;
2955 UsefulBufC Encoded;
2956 QCBORError uErr;
2957
2958 /* Normal use case -- add a byte string that fits */
2959 QCBOREncode_Init(&EC, TestBuf);
2960 QCBOREncode_OpenBytes(&EC, &Place);
2961 if(Place.ptr != TestBuf.ptr ||
2962 Place.len != TestBuf.len) {
2963 return 1;
2964 }
2965 Place.len -= 4;
2966 UsefulBuf_Set(Place, 'x');
2967 QCBOREncode_CloseBytes(&EC, Place.len);
2968 QCBOREncode_Finish(&EC, &Encoded);
2969 if(UsefulBuf_Compare(Encoded,
2970 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes))) {
2971 return 2;
2972 }
2973
Paul Liétar79789772022-07-26 20:33:18 +01002974 /* Run the same test but with a NULL buffer */
2975 QCBOREncode_Init(&EC, (UsefulBuf){NULL, 20});
2976 QCBOREncode_OpenBytes(&EC, &Place);
2977 if(!UsefulBuf_IsNULL(Place)) {
2978 return 3;
2979 }
2980 Place.len -= 4;
2981 /* We don't actually write anything since the pointer is NULL, but advance nevertheless. */
2982 QCBOREncode_CloseBytes(&EC, Place.len);
2983 uErr = QCBOREncode_Finish(&EC, &Encoded);
2984 if(uErr != QCBOR_SUCCESS ||
2985 Encoded.len != sizeof(spExpectedForOpenBytes)) {
2986 return 4;
2987 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002988
2989 /* Open a byte string with no room left */
2990 QCBOREncode_Init(&EC, TestBuf);
2991 QCBOREncode_AddSZString(&EC, "0123456789012345678");
2992 QCBOREncode_OpenBytes(&EC, &Place);
2993 if(Place.ptr != NULL ||
2994 Place.len != 0) {
Paul Liétar79789772022-07-26 20:33:18 +01002995 return 5;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002996 }
2997
2998 /* Try to extend byte string past end of encoding output buffer */
2999 QCBOREncode_Init(&EC, TestBuf);
3000 QCBOREncode_AddSZString(&EC, "012345678901234567");
3001 QCBOREncode_OpenBytes(&EC, &Place);
3002 /* Don't bother to write any bytes*/
3003 QCBOREncode_CloseBytes(&EC, Place.len+1);
3004 uErr = QCBOREncode_GetErrorState(&EC);
3005 if(uErr != QCBOR_ERR_BUFFER_TOO_SMALL) {
Paul Liétar79789772022-07-26 20:33:18 +01003006 return 6;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003007 }
3008
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003009 /* Close a byte string without opening one. */
3010 QCBOREncode_Init(&EC, TestBuf);
3011 QCBOREncode_AddSZString(&EC, "012345678");
3012 QCBOREncode_CloseBytes(&EC, 1);
3013 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003014#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003015 if(uErr != QCBOR_ERR_TOO_MANY_CLOSES) {
Paul Liétar79789772022-07-26 20:33:18 +01003016 return 7;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003017 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003018#else
3019 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003020 return 107;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003021 }
3022#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003023
3024 /* Forget to close a byte string */
3025 QCBOREncode_Init(&EC, TestBuf);
3026 QCBOREncode_AddSZString(&EC, "012345678");
3027 QCBOREncode_OpenBytes(&EC, &Place);
3028 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003029#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003030 if(uErr != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
Paul Liétar79789772022-07-26 20:33:18 +01003031 return 8;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003032 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003033#else
3034 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003035 return 108;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003036 }
3037#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003038
3039 /* Try to open a byte string in a byte string */
3040 QCBOREncode_Init(&EC, TestBuf);
3041 QCBOREncode_AddSZString(&EC, "012345678");
3042 QCBOREncode_OpenBytes(&EC, &Place);
3043 QCBOREncode_OpenBytes(&EC, &Place);
3044 uErr = QCBOREncode_GetErrorState(&EC);
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003045#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003046 if(uErr != QCBOR_ERR_OPEN_BYTE_STRING) {
Paul Liétar79789772022-07-26 20:33:18 +01003047 return 9;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003048 }
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003049#else
3050 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003051 return 109;
Laurence Lundblade274ddef2022-05-17 09:12:23 -07003052 }
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003053#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3054
3055 /* A successful case with a little complexity */
3056 QCBOREncode_Init(&EC, TestBuf2);
3057 QCBOREncode_OpenMap(&EC);
3058 QCBOREncode_AddInt64ToMapN(&EC, 10, 22);
3059 QCBOREncode_OpenBytesInMapN(&EC, 20, &Place);
3060 Place.len = 2;
3061 UsefulBuf_Set(Place, 'x');
3062 QCBOREncode_CloseBytes(&EC, 2);
3063 QCBOREncode_OpenBytesInMapSZ(&EC, "thirty", &Place);
3064 Place.len = 3;
3065 UsefulBuf_Set(Place, 'y');
3066 QCBOREncode_CloseBytes(&EC, 3);
3067 QCBOREncode_OpenArrayInMapN(&EC, 40);
3068 QCBOREncode_OpenBytes(&EC, &Place);
3069 QCBOREncode_CloseBytes(&EC, 0);
3070 QCBOREncode_CloseArray(&EC);
3071 QCBOREncode_CloseMap(&EC);
3072 uErr = QCBOREncode_Finish(&EC, &Encoded);
3073 if(uErr != QCBOR_SUCCESS) {
Paul Liétar79789772022-07-26 20:33:18 +01003074 return 10;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003075 }
3076 if(UsefulBuf_Compare(Encoded,
3077 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedForOpenBytes2))) {
Paul Liétar79789772022-07-26 20:33:18 +01003078 return 11;
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003079 }
3080
3081 return 0;
3082}